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"
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp *content = NULL;
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp return err;
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
436 6d9d28c3 2018-03-11 stsp return err;
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
542 024e9686 2019-05-14 stsp free(path_got);
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
572 507dc3bb 2018-12-29 stsp return err;
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
675 68c76935 2019-02-19 stsp return err;
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
700 68c76935 2019-02-19 stsp if (size1 != size2) {
702 68c76935 2019-02-19 stsp return NULL;
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
722 6353ad76 2019-02-08 stsp return err;
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
746 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
748 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
749 af54ae4a 2019-02-19 stsp if (parent == NULL)
750 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
752 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
759 af54ae4a 2019-02-19 stsp free(base_path);
760 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
762 af54ae4a 2019-02-19 stsp base_path = NULL;
766 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 46b6ee73 2019-06-04 stsp if (blob_orig) {
770 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 46b6ee73 2019-06-04 stsp blob_orig);
776 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
777 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
778 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
782 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 f69721c3 2019-10-21 stsp blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
788 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
800 68c76935 2019-02-19 stsp merged_path);
805 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
806 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
812 230a42bd 2019-05-11 jcs ondisk_path);
817 fdcb7daf 2019-12-15 stsp if (merged_path)
818 fdcb7daf 2019-12-15 stsp unlink(merged_path);
820 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
822 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
827 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
828 46b6ee73 2019-06-04 stsp free(blob_orig_path);
830 14c901f1 2019-08-08 stsp return err;
834 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
836 14c901f1 2019-08-08 stsp * acts as the second derived version.
838 14c901f1 2019-08-08 stsp static const struct got_error *
839 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
841 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
842 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
843 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
846 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
847 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
848 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
851 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
853 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
854 14c901f1 2019-08-08 stsp if (parent == NULL)
855 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
857 14c901f1 2019-08-08 stsp free(base_path);
858 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
860 14c901f1 2019-08-08 stsp base_path = NULL;
864 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
867 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 14c901f1 2019-08-08 stsp blob_deriv);
872 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
875 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
876 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
881 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
885 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
887 14c901f1 2019-08-08 stsp free(base_path);
888 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
889 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
890 14c901f1 2019-08-08 stsp free(blob_deriv_path);
892 6353ad76 2019-02-08 stsp free(id_str);
893 818c7501 2019-07-11 stsp free(label_deriv);
894 4a1ddfc2 2019-01-12 stsp return err;
897 4a1ddfc2 2019-01-12 stsp static const struct got_error *
898 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
899 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
900 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
901 13d9040b 2019-03-27 stsp int update_timestamps)
903 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
905 13d9040b 2019-03-27 stsp if (ie == NULL)
906 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
908 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
909 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
910 13d9040b 2019-03-27 stsp update_timestamps);
912 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
913 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&new_ie, path);
915 3969253a 2020-03-07 stsp return err;
916 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
917 3969253a 2020-03-07 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
919 3969253a 2020-03-07 stsp got_fileindex_entry_free(new_ie);
920 3969253a 2020-03-07 stsp return err;
922 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, new_ie);
924 3969253a 2020-03-07 stsp got_fileindex_entry_free(new_ie);
926 13d9040b 2019-03-27 stsp return err;
929 1ebedb77 2019-10-19 stsp static mode_t
930 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
932 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
934 1ebedb77 2019-10-19 stsp if (executable) {
935 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
936 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
937 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
938 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
939 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
940 1ebedb77 2019-10-19 stsp return st_mode | xbits;
943 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
946 13d9040b 2019-03-27 stsp static const struct got_error *
947 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
948 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
949 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
950 4b55f459 2019-09-08 stsp int reverting_versioned_file, struct got_repository *repo,
951 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
953 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
954 507dc3bb 2018-12-29 stsp int fd = -1;
955 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
956 507dc3bb 2018-12-29 stsp int update = 0;
957 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
959 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
960 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
961 9d31a1d8 2018-03-11 stsp if (fd == -1) {
962 21908da4 2019-01-13 stsp if (errno == ENOENT) {
963 21908da4 2019-01-13 stsp char *parent = dirname(path);
964 21908da4 2019-01-13 stsp if (parent == NULL)
965 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
966 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
968 21908da4 2019-01-13 stsp return err;
969 21908da4 2019-01-13 stsp fd = open(ondisk_path,
970 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
971 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
972 21908da4 2019-01-13 stsp if (fd == -1)
973 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
974 230a42bd 2019-05-11 jcs ondisk_path);
975 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
976 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
977 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
978 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
981 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
982 507dc3bb 2018-12-29 stsp ondisk_path);
985 507dc3bb 2018-12-29 stsp update = 1;
988 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
991 a378724f 2019-02-10 stsp if (restoring_missing_file)
992 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
993 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
994 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
996 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
997 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1001 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1003 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1004 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1007 9d31a1d8 2018-03-11 stsp if (len > 0) {
1008 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1009 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1010 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1011 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1012 b87c6f83 2018-12-24 stsp goto done;
1013 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1014 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1015 b87c6f83 2018-12-24 stsp goto done;
1017 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1019 9d31a1d8 2018-03-11 stsp } while (len != 0);
1021 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1022 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1023 816dc654 2019-02-16 stsp goto done;
1026 507dc3bb 2018-12-29 stsp if (update) {
1027 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1028 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1029 230a42bd 2019-05-11 jcs ondisk_path);
1030 2a57020b 2019-02-20 stsp unlink(tmppath);
1031 68ed9ba5 2019-02-10 stsp goto done;
1035 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1036 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1037 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1038 1ebedb77 2019-10-19 stsp goto done;
1042 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1043 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1044 507dc3bb 2018-12-29 stsp free(tmppath);
1045 6353ad76 2019-02-08 stsp return err;
1048 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1049 6353ad76 2019-02-08 stsp static const struct got_error *
1050 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1052 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1053 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1054 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1055 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1056 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1058 7154f6ce 2019-03-27 stsp int i = 0;
1059 7154f6ce 2019-03-27 stsp char *line;
1060 7154f6ce 2019-03-27 stsp size_t len;
1061 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1063 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1064 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1065 7154f6ce 2019-03-27 stsp if (line == NULL) {
1066 7154f6ce 2019-03-27 stsp if (feof(f))
1068 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1072 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1073 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1075 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1081 7154f6ce 2019-03-27 stsp return err;
1084 e2b1e152 2019-07-13 stsp static int
1085 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1087 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1088 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1091 1ebedb77 2019-10-19 stsp static int
1092 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1094 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1095 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1096 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1097 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1098 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1099 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1102 c363b2c1 2019-08-03 stsp static unsigned char
1103 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1105 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1106 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1107 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1108 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1109 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1110 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1111 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1113 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1117 7154f6ce 2019-03-27 stsp static const struct got_error *
1118 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1119 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1120 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1122 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1123 6353ad76 2019-02-08 stsp struct got_object_id id;
1124 6353ad76 2019-02-08 stsp size_t hdrlen;
1125 1338848f 2019-12-13 stsp int fd = -1;
1126 6353ad76 2019-02-08 stsp FILE *f = NULL;
1127 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1128 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1129 6353ad76 2019-02-08 stsp size_t flen, blen;
1130 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1132 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1135 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1136 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1137 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1139 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1140 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1141 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1142 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1143 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1145 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1146 882ef1b9 2019-12-13 stsp goto done;
1148 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1149 3d35a492 2019-12-13 stsp goto done;
1152 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1153 7f91a133 2019-12-13 stsp if (fd == -1 && errno != ENOENT)
1154 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1155 3d35a492 2019-12-13 stsp if (fd == -1 || fstat(fd, sb) == -1) {
1156 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1157 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1158 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1160 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1161 3d35a492 2019-12-13 stsp goto done;
1163 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1164 1338848f 2019-12-13 stsp goto done;
1168 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1169 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1170 1338848f 2019-12-13 stsp goto done;
1173 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1174 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1175 1338848f 2019-12-13 stsp goto done;
1176 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1177 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1178 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1179 1338848f 2019-12-13 stsp goto done;
1182 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1183 1338848f 2019-12-13 stsp goto done;
1185 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1186 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1187 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1189 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1191 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1193 1338848f 2019-12-13 stsp goto done;
1195 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1196 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1197 ab0d4361 2019-12-13 stsp if (fd == -1) {
1198 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1199 ab0d4361 2019-12-13 stsp goto done;
1203 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1204 6353ad76 2019-02-08 stsp if (f == NULL) {
1205 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1206 6353ad76 2019-02-08 stsp goto done;
1209 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1211 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1212 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1214 7154f6ce 2019-03-27 stsp goto done;
1215 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1216 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1217 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1218 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1219 7154f6ce 2019-03-27 stsp goto done;
1221 6353ad76 2019-02-08 stsp if (blen == 0) {
1222 6353ad76 2019-02-08 stsp if (flen != 0)
1223 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1225 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1226 6353ad76 2019-02-08 stsp if (blen != 0)
1227 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1229 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1230 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1231 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1232 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1236 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1239 6353ad76 2019-02-08 stsp hdrlen = 0;
1242 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1243 7154f6ce 2019-03-27 stsp rewind(f);
1244 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1245 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1246 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1249 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1250 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1251 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1252 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1253 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1254 9d31a1d8 2018-03-11 stsp return err;
1258 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1259 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1261 e2b1e152 2019-07-13 stsp static const struct got_error *
1262 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1263 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1265 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1266 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1267 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1269 e2b1e152 2019-07-13 stsp return NULL;
1272 9d31a1d8 2018-03-11 stsp static const struct got_error *
1273 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1274 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1275 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1276 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1277 0584f854 2019-04-06 stsp void *progress_arg)
1279 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1280 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1281 6353ad76 2019-02-08 stsp char *ondisk_path;
1282 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1283 b8f41171 2019-02-10 stsp struct stat sb;
1285 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1286 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1289 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1290 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1291 a76c42e6 2019-08-03 stsp goto done;
1293 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1296 abb4604f 2019-07-27 stsp goto done;
1297 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1298 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1300 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1302 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1303 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1304 b8f41171 2019-02-10 stsp goto done;
1306 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1307 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1309 5036ab18 2020-04-18 stsp goto done;
1312 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1313 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1314 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1315 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1316 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1317 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1319 e2b1e152 2019-07-13 stsp goto done;
1320 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1322 6353ad76 2019-02-08 stsp goto done;
1324 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1325 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1326 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1327 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1328 b8f41171 2019-02-10 stsp goto done;
1332 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1334 6353ad76 2019-02-08 stsp goto done;
1336 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1337 234035bc 2019-06-01 stsp int update_timestamps;
1338 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1339 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1340 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1341 234035bc 2019-06-01 stsp struct got_object_id id2;
1342 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1343 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1345 f69721c3 2019-10-21 stsp goto done;
1347 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1348 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1349 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1350 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1351 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1352 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1353 f69721c3 2019-10-21 stsp goto done;
1355 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1356 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1357 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1358 234035bc 2019-06-01 stsp goto done;
1361 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1362 f69721c3 2019-10-21 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1363 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1364 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1365 f69721c3 2019-10-21 stsp free(label_orig);
1366 234035bc 2019-06-01 stsp if (blob2)
1367 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1369 909d120e 2019-09-22 stsp goto done;
1371 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1372 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1373 234035bc 2019-06-01 stsp * unmodified files again.
1375 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1376 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1377 234035bc 2019-06-01 stsp update_timestamps);
1378 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1379 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1380 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1381 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1382 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1384 1ee397ad 2019-07-12 stsp goto done;
1385 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1386 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1388 13d9040b 2019-03-27 stsp goto done;
1390 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1391 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1392 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1394 13d9040b 2019-03-27 stsp goto done;
1395 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1396 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1398 13d9040b 2019-03-27 stsp goto done;
1400 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1402 6353ad76 2019-02-08 stsp free(ondisk_path);
1403 8da9e5f4 2019-01-12 stsp return err;
1406 90285c3b 2019-01-08 stsp static const struct got_error *
1407 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1409 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1410 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1412 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1413 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1415 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1416 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1417 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1419 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1420 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1421 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1422 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1423 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1427 90285c3b 2019-01-08 stsp parent = dirname(parent);
1430 90285c3b 2019-01-08 stsp free(ondisk_path);
1431 90285c3b 2019-01-08 stsp return err;
1434 708d8e67 2019-03-27 stsp static const struct got_error *
1435 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1436 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1437 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1439 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1440 708d8e67 2019-03-27 stsp unsigned char status;
1441 708d8e67 2019-03-27 stsp struct stat sb;
1442 708d8e67 2019-03-27 stsp char *ondisk_path;
1444 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1445 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1447 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1449 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1451 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1453 708d8e67 2019-03-27 stsp return err;
1455 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1456 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1457 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1459 1ee397ad 2019-07-12 stsp return err;
1461 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1462 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1464 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1467 708d8e67 2019-03-27 stsp return err;
1469 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1471 1ee397ad 2019-07-12 stsp return err;
1472 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1473 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1475 fc6346c4 2019-03-27 stsp return err;
1477 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1480 fc6346c4 2019-03-27 stsp return err;
1483 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1484 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1485 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1486 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1487 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1488 8da9e5f4 2019-01-12 stsp void *progress_arg;
1489 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
1490 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1493 512f0d0e 2019-01-02 stsp static const struct got_error *
1494 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1495 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1497 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1499 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1500 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1502 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1503 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1506 8da9e5f4 2019-01-12 stsp static const struct got_error *
1507 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1509 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1511 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1512 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1514 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1515 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1518 9d31a1d8 2018-03-11 stsp static const struct got_error *
1519 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1521 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1522 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1523 8da9e5f4 2019-01-12 stsp char *path;
1525 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1526 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1528 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
1529 63c5ca5d 2019-08-24 stsp return NULL;
1531 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1532 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1534 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1536 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1537 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1539 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1540 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1542 8da9e5f4 2019-01-12 stsp free(path);
1543 0cd1c46a 2019-03-11 stsp return err;
1546 818c7501 2019-07-11 stsp static const struct got_error *
1547 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1549 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1550 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1551 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1553 517bab73 2019-03-11 stsp *refname = NULL;
1555 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1556 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1557 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
1559 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1561 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1562 0647c563 2019-03-11 stsp *refname = NULL;
1564 517bab73 2019-03-11 stsp free(uuidstr);
1565 517bab73 2019-03-11 stsp return err;
1568 818c7501 2019-07-11 stsp const struct got_error *
1569 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1571 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1574 818c7501 2019-07-11 stsp static const struct got_error *
1575 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1577 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1578 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1581 818c7501 2019-07-11 stsp static const struct got_error *
1582 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1584 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1587 818c7501 2019-07-11 stsp static const struct got_error *
1588 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1590 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1591 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1594 818c7501 2019-07-11 stsp static const struct got_error *
1595 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1597 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1598 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1601 0ebf8283 2019-07-24 stsp static const struct got_error *
1602 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1604 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1605 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1608 0ebf8283 2019-07-24 stsp static const struct got_error *
1609 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1611 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1612 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1615 0ebf8283 2019-07-24 stsp static const struct got_error *
1616 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1618 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1619 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1622 0ebf8283 2019-07-24 stsp static const struct got_error *
1623 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1625 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1626 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1629 0ebf8283 2019-07-24 stsp const struct got_error *
1630 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1631 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1633 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1634 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1635 0ebf8283 2019-07-24 stsp *path = NULL;
1636 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1638 0ebf8283 2019-07-24 stsp return NULL;
1642 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1643 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1645 517bab73 2019-03-11 stsp static const struct got_error *
1646 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1648 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1649 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1650 517bab73 2019-03-11 stsp char *refname;
1652 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1654 517bab73 2019-03-11 stsp return err;
1656 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1658 0cd1c46a 2019-03-11 stsp goto done;
1660 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1662 0cd1c46a 2019-03-11 stsp free(refname);
1664 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1665 3e3a69f1 2019-07-25 stsp return err;
1668 3e3a69f1 2019-07-25 stsp static const struct got_error *
1669 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1671 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1673 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1674 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1675 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1676 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1678 9d31a1d8 2018-03-11 stsp return err;
1682 ebf99748 2019-05-09 stsp static const struct got_error *
1683 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1684 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
1686 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1687 ebf99748 2019-05-09 stsp FILE *index = NULL;
1689 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1690 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1691 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1692 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1694 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1696 ebf99748 2019-05-09 stsp goto done;
1698 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1699 ebf99748 2019-05-09 stsp if (index == NULL) {
1700 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1701 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1703 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1704 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1705 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1708 ebf99748 2019-05-09 stsp if (err) {
1709 ebf99748 2019-05-09 stsp free(*fileindex_path);
1710 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1711 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1712 ebf99748 2019-05-09 stsp *fileindex = NULL;
1714 ebf99748 2019-05-09 stsp return err;
1717 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1718 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1719 c932eeeb 2019-05-22 stsp const char *path;
1720 c932eeeb 2019-05-22 stsp size_t path_len;
1721 c932eeeb 2019-05-22 stsp const char *entry_name;
1722 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1723 a484d721 2019-06-10 stsp void *progress_arg;
1726 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1727 c932eeeb 2019-05-22 stsp static const struct got_error *
1728 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1730 1ee397ad 2019-07-12 stsp const struct got_error *err;
1731 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1733 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1734 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1735 c932eeeb 2019-05-22 stsp return NULL;
1736 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1737 102ff934 2019-06-11 stsp return NULL;
1739 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1740 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1741 c932eeeb 2019-05-22 stsp return NULL;
1743 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1744 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1745 edd02c5e 2019-07-11 stsp ie->path);
1747 1ee397ad 2019-07-12 stsp return err;
1749 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1750 c932eeeb 2019-05-22 stsp return NULL;
1753 9c6338c4 2019-06-02 stsp static const struct got_error *
1754 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1756 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1757 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1758 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1759 867630bb 2020-01-17 stsp struct timespec timeout;
1761 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1762 9c6338c4 2019-06-02 stsp fileindex_path);
1764 9c6338c4 2019-06-02 stsp goto done;
1766 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1768 9c6338c4 2019-06-02 stsp goto done;
1770 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1771 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1772 9c6338c4 2019-06-02 stsp fileindex_path);
1773 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1777 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
1778 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
1779 867630bb 2020-01-17 stsp * was recorded in the file index.
1781 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
1782 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
1783 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
1785 9c6338c4 2019-06-02 stsp if (new_index)
1786 9c6338c4 2019-06-02 stsp fclose(new_index);
1787 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1788 9c6338c4 2019-06-02 stsp return err;
1791 6ced4176 2019-07-12 stsp static const struct got_error *
1792 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1793 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1794 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1796 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1797 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1798 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1799 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1801 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1802 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1803 6ced4176 2019-07-12 stsp *tree_id = NULL;
1805 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1806 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1807 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1808 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1809 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1810 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1811 6ced4176 2019-07-12 stsp goto done;
1813 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1814 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1816 6ced4176 2019-07-12 stsp goto done;
1817 6ced4176 2019-07-12 stsp return NULL;
1820 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1822 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1823 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1824 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1825 6ced4176 2019-07-12 stsp goto done;
1828 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1829 6ced4176 2019-07-12 stsp in_repo_path);
1831 6ced4176 2019-07-12 stsp goto done;
1833 6ced4176 2019-07-12 stsp free(in_repo_path);
1834 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1836 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1838 6ced4176 2019-07-12 stsp goto done;
1840 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1841 6ced4176 2019-07-12 stsp /* Check out a single file. */
1842 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1843 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1844 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1845 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1846 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1847 6ced4176 2019-07-12 stsp goto done;
1849 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1850 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1851 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1852 6ced4176 2019-07-12 stsp goto done;
1855 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1856 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1858 6ced4176 2019-07-12 stsp return err;
1859 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1860 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1861 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1862 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1863 6ced4176 2019-07-12 stsp goto done;
1866 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1867 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1869 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1870 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1871 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1872 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1873 6ced4176 2019-07-12 stsp goto done;
1875 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1876 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1877 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1878 6ced4176 2019-07-12 stsp goto done;
1883 6ced4176 2019-07-12 stsp free(in_repo_path);
1884 6ced4176 2019-07-12 stsp if (err) {
1885 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1886 6ced4176 2019-07-12 stsp free(*tree_relpath);
1887 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1888 6ced4176 2019-07-12 stsp free(*tree_id);
1889 6ced4176 2019-07-12 stsp *tree_id = NULL;
1891 07ed472d 2019-07-12 stsp return err;
1894 07ed472d 2019-07-12 stsp static const struct got_error *
1895 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1896 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1897 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1898 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1900 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1901 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1902 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1903 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1904 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1906 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1907 7f47418f 2019-12-20 stsp if (err) {
1908 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
1909 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
1910 7f47418f 2019-12-20 stsp goto done;
1911 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
1912 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1914 7f47418f 2019-12-20 stsp return err;
1917 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1918 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1920 07ed472d 2019-07-12 stsp goto done;
1922 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1924 07ed472d 2019-07-12 stsp goto done;
1926 07ed472d 2019-07-12 stsp if (entry_name &&
1927 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1928 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1929 07ed472d 2019-07-12 stsp goto done;
1932 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1933 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1934 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1935 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1936 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1937 07ed472d 2019-07-12 stsp arg.repo = repo;
1938 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1939 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1940 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1941 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1942 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1943 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1946 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1947 07ed472d 2019-07-12 stsp if (commit)
1948 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1949 6ced4176 2019-07-12 stsp return err;
1952 9d31a1d8 2018-03-11 stsp const struct got_error *
1953 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1954 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1955 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1956 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1958 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1959 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1960 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1961 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1962 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1963 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1964 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1965 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1966 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1967 f2ea84fa 2019-07-27 stsp int entry_type;
1968 f2ea84fa 2019-07-27 stsp char *relpath;
1969 f2ea84fa 2019-07-27 stsp char *entry_name;
1970 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1971 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1973 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1975 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1977 9d31a1d8 2018-03-11 stsp return err;
1979 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1980 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1981 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1982 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1983 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1984 f2ea84fa 2019-07-27 stsp goto done;
1987 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1988 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1989 f2ea84fa 2019-07-27 stsp if (err) {
1990 f2ea84fa 2019-07-27 stsp free(tpd);
1991 6ced4176 2019-07-12 stsp goto done;
1994 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1995 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1996 f2ea84fa 2019-07-27 stsp if (err) {
1997 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1998 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1999 f2ea84fa 2019-07-27 stsp free(tpd);
2000 f2ea84fa 2019-07-27 stsp goto done;
2003 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2005 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2009 51514078 2018-12-25 stsp * Read the file index.
2010 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2011 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2013 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2015 c4cdcb68 2019-04-03 stsp goto done;
2017 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2018 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2019 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2021 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2022 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2023 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2027 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2028 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2029 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2030 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2031 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2032 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2033 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2034 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2038 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2040 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2041 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2042 af12c6b9 2019-06-04 stsp err = sync_err;
2044 9c6338c4 2019-06-02 stsp free(fileindex_path);
2046 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2047 9d31a1d8 2018-03-11 stsp if (commit)
2048 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2049 5ade8233 2019-07-12 stsp if (fileindex)
2050 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2051 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2052 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2053 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2054 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2055 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2056 f2ea84fa 2019-07-27 stsp free(tpd);
2058 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2059 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2060 9d31a1d8 2018-03-11 stsp err = unlockerr;
2061 f8d1f275 2019-02-04 stsp return err;
2064 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2065 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2066 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2067 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2068 234035bc 2019-06-01 stsp void *progress_arg;
2069 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2070 234035bc 2019-06-01 stsp void *cancel_arg;
2071 f69721c3 2019-10-21 stsp const char *label_orig;
2072 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2075 234035bc 2019-06-01 stsp static const struct got_error *
2076 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2077 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2078 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2079 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2081 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2082 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2083 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2084 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2085 234035bc 2019-06-01 stsp struct stat sb;
2086 234035bc 2019-06-01 stsp unsigned char status;
2087 234035bc 2019-06-01 stsp int local_changes_subsumed;
2089 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2090 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2091 d6c87207 2019-08-02 stsp strlen(path2));
2092 1ee397ad 2019-07-12 stsp if (ie == NULL)
2093 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2094 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2096 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2097 234035bc 2019-06-01 stsp path2) == -1)
2098 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2100 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2103 234035bc 2019-06-01 stsp goto done;
2105 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2106 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2107 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2108 234035bc 2019-06-01 stsp goto done;
2110 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2111 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2112 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2113 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2114 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2115 234035bc 2019-06-01 stsp goto done;
2118 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2119 f69721c3 2019-10-21 stsp ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2120 f69721c3 2019-10-21 stsp a->commit_id2, repo, a->progress_cb, a->progress_arg);
2121 234035bc 2019-06-01 stsp } else if (blob1) {
2122 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2123 d6c87207 2019-08-02 stsp strlen(path1));
2124 1ee397ad 2019-07-12 stsp if (ie == NULL)
2125 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2126 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2128 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2129 2b92fad7 2019-06-02 stsp path1) == -1)
2130 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2132 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2135 2b92fad7 2019-06-02 stsp goto done;
2137 2b92fad7 2019-06-02 stsp switch (status) {
2138 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2139 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2140 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2142 1ee397ad 2019-07-12 stsp goto done;
2143 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2145 2b92fad7 2019-06-02 stsp goto done;
2147 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2149 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2150 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2151 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2152 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2154 1ee397ad 2019-07-12 stsp goto done;
2156 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2158 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2159 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2160 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2161 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2162 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2164 1ee397ad 2019-07-12 stsp goto done;
2166 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2167 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2169 1ee397ad 2019-07-12 stsp goto done;
2174 234035bc 2019-06-01 stsp } else if (blob2) {
2175 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2176 234035bc 2019-06-01 stsp path2) == -1)
2177 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2178 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2179 d6c87207 2019-08-02 stsp strlen(path2));
2181 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2182 7f91a133 2019-12-13 stsp -1, NULL, repo);
2184 234035bc 2019-06-01 stsp goto done;
2185 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2186 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2187 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2188 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2189 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2190 1ee397ad 2019-07-12 stsp status, path2);
2191 234035bc 2019-06-01 stsp goto done;
2193 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2194 f69721c3 2019-10-21 stsp NULL, ondisk_path, path2, sb.st_mode,
2195 f69721c3 2019-10-21 stsp a->label_orig, blob2, a->commit_id2, repo,
2196 f69721c3 2019-10-21 stsp a->progress_cb,
2197 f69721c3 2019-10-21 stsp a->progress_arg);
2198 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2199 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2200 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2201 234035bc 2019-06-01 stsp blob2, 0);
2203 234035bc 2019-06-01 stsp goto done;
2206 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2207 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2208 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2209 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2210 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2211 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2213 234035bc 2019-06-01 stsp goto done;
2214 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2216 234035bc 2019-06-01 stsp goto done;
2217 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2218 3969253a 2020-03-07 stsp NULL, NULL, 1);
2219 3969253a 2020-03-07 stsp if (err) {
2220 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2221 3969253a 2020-03-07 stsp goto done;
2223 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2224 2b92fad7 2019-06-02 stsp if (err) {
2225 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2226 2b92fad7 2019-06-02 stsp goto done;
2231 234035bc 2019-06-01 stsp free(ondisk_path);
2232 234035bc 2019-06-01 stsp return err;
2235 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2236 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2237 234035bc 2019-06-01 stsp struct got_repository *repo;
2240 234035bc 2019-06-01 stsp static const struct got_error *
2241 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2243 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2244 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2245 234035bc 2019-06-01 stsp unsigned char status;
2246 234035bc 2019-06-01 stsp struct stat sb;
2247 234035bc 2019-06-01 stsp char *ondisk_path;
2249 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2250 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2251 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2252 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2254 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2256 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2258 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2259 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2261 234035bc 2019-06-01 stsp return err;
2262 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2263 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2265 234035bc 2019-06-01 stsp return NULL;
2268 818c7501 2019-07-11 stsp static const struct got_error *
2269 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2270 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2271 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2272 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2273 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2275 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2276 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2277 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2278 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2279 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2281 03415a1a 2019-06-02 stsp if (commit_id1) {
2282 f69721c3 2019-10-21 stsp char *id_str;
2284 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2285 03415a1a 2019-06-02 stsp worktree->path_prefix);
2287 03415a1a 2019-06-02 stsp goto done;
2289 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2291 03415a1a 2019-06-02 stsp goto done;
2293 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2295 f69721c3 2019-10-21 stsp goto done;
2297 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2298 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2299 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2300 f69721c3 2019-10-21 stsp free(id_str);
2301 f69721c3 2019-10-21 stsp goto done;
2303 f69721c3 2019-10-21 stsp free(id_str);
2306 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2307 234035bc 2019-06-01 stsp worktree->path_prefix);
2309 234035bc 2019-06-01 stsp goto done;
2311 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2313 234035bc 2019-06-01 stsp goto done;
2315 234035bc 2019-06-01 stsp arg.worktree = worktree;
2316 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2317 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2318 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2319 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2320 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2321 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2322 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2323 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2324 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2325 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2326 af12c6b9 2019-06-04 stsp err = sync_err;
2328 234035bc 2019-06-01 stsp if (tree1)
2329 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2330 234035bc 2019-06-01 stsp if (tree2)
2331 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2332 f69721c3 2019-10-21 stsp free(label_orig);
2333 818c7501 2019-07-11 stsp return err;
2336 818c7501 2019-07-11 stsp const struct got_error *
2337 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2338 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2339 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2340 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2342 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2343 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2344 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2345 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2347 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2349 818c7501 2019-07-11 stsp return err;
2351 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2353 818c7501 2019-07-11 stsp goto done;
2355 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2356 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2357 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2358 818c7501 2019-07-11 stsp &mok_arg);
2360 818c7501 2019-07-11 stsp goto done;
2362 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2363 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2365 818c7501 2019-07-11 stsp if (fileindex)
2366 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2367 818c7501 2019-07-11 stsp free(fileindex_path);
2368 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2369 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2370 234035bc 2019-06-01 stsp err = unlockerr;
2371 234035bc 2019-06-01 stsp return err;
2374 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2375 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2376 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2377 927df6b7 2019-02-10 stsp const char *status_path;
2378 927df6b7 2019-02-10 stsp size_t status_path_len;
2379 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2380 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2381 f8d1f275 2019-02-04 stsp void *status_arg;
2382 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2383 f8d1f275 2019-02-04 stsp void *cancel_arg;
2384 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2385 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2386 f2a9dc41 2019-12-13 tracey int report_unchanged;
2389 f8d1f275 2019-02-04 stsp static const struct got_error *
2390 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2391 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2392 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2393 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2395 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2396 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2397 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2398 927df6b7 2019-02-10 stsp struct stat sb;
2399 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2400 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2401 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2403 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2405 98eaaa12 2019-08-03 stsp return err;
2407 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2408 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2409 98eaaa12 2019-08-03 stsp return NULL;
2411 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2412 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2413 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2415 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2416 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2417 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2419 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2420 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2421 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2422 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2423 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2426 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2427 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2430 927df6b7 2019-02-10 stsp static const struct got_error *
2431 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2432 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
2434 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2435 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2436 f8d1f275 2019-02-04 stsp char *abspath;
2438 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2439 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2441 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2442 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2443 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2444 927df6b7 2019-02-10 stsp return NULL;
2446 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2447 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2448 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2449 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2451 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2452 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2453 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2456 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
2457 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2458 f8d1f275 2019-02-04 stsp free(abspath);
2459 9d31a1d8 2018-03-11 stsp return err;
2462 f8d1f275 2019-02-04 stsp static const struct got_error *
2463 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2465 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2466 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2467 2ec1f75b 2019-03-26 stsp unsigned char status;
2469 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2470 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2472 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2473 927df6b7 2019-02-10 stsp return NULL;
2475 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2476 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2477 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2478 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2480 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2481 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2482 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2486 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
2488 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2490 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
2491 6841da00 2019-08-08 stsp free((char *)pe->path);
2492 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
2496 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
2498 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2500 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
2501 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2502 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2503 6841da00 2019-08-08 stsp free((char *)pe->path);
2505 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
2508 6841da00 2019-08-08 stsp static const struct got_error *
2509 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2511 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2512 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
2513 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
2514 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
2515 6841da00 2019-08-08 stsp size_t linesize = 0;
2516 6841da00 2019-08-08 stsp ssize_t linelen;
2518 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
2519 6841da00 2019-08-08 stsp if (ignorelist == NULL)
2520 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
2521 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
2523 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2524 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
2525 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
2527 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
2528 bd8de430 2019-10-04 stsp if (line[0] == '#')
2531 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
2532 bd8de430 2019-10-04 stsp if (line[0] == '!')
2535 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2536 6841da00 2019-08-08 stsp line) == -1) {
2537 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
2538 6841da00 2019-08-08 stsp goto done;
2540 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2542 6841da00 2019-08-08 stsp goto done;
2544 6841da00 2019-08-08 stsp if (ferror(f)) {
2545 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
2546 6841da00 2019-08-08 stsp goto done;
2549 6841da00 2019-08-08 stsp dirpath = strdup(path);
2550 6841da00 2019-08-08 stsp if (dirpath == NULL) {
2551 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
2552 6841da00 2019-08-08 stsp goto done;
2554 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2556 6841da00 2019-08-08 stsp free(line);
2557 6841da00 2019-08-08 stsp if (err || pe == NULL) {
2558 6841da00 2019-08-08 stsp free(dirpath);
2559 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2561 6841da00 2019-08-08 stsp return err;
2565 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
2567 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2569 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
2570 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
2571 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
2572 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
2574 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2575 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
2577 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
2579 bd8de430 2019-10-04 stsp pattern += 3;
2581 bd8de430 2019-10-04 stsp while (*p) {
2582 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
2583 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
2584 bd8de430 2019-10-04 stsp /* Retry in next directory. */
2585 bd8de430 2019-10-04 stsp while (*p && *p != '/')
2587 bd8de430 2019-10-04 stsp while (*p == '/')
2597 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
2598 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
2599 6841da00 2019-08-08 stsp * ignores backwards.
2601 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
2602 6841da00 2019-08-08 stsp while (pe) {
2603 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
2604 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2605 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
2606 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2607 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
2608 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
2609 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
2610 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
2611 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
2616 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2622 6841da00 2019-08-08 stsp static const struct got_error *
2623 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2624 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
2626 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2627 6841da00 2019-08-08 stsp char *ignorespath;
2628 886cec17 2019-12-15 stsp int fd = -1;
2629 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
2631 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2632 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
2633 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
2635 886cec17 2019-12-15 stsp if (dirfd != -1) {
2636 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2637 886cec17 2019-12-15 stsp if (fd == -1) {
2638 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2639 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
2640 886cec17 2019-12-15 stsp ignorespath);
2642 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
2643 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
2644 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
2645 886cec17 2019-12-15 stsp ignorespath);
2648 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2652 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
2653 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
2654 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2655 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
2656 886cec17 2019-12-15 stsp ignorespath);
2658 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2661 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2662 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
2663 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2664 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
2665 6841da00 2019-08-08 stsp free(ignorespath);
2666 6841da00 2019-08-08 stsp return err;
2669 f8d1f275 2019-02-04 stsp static const struct got_error *
2670 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2672 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2673 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2674 f8d1f275 2019-02-04 stsp char *path = NULL;
2676 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2677 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2679 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2680 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2681 927df6b7 2019-02-10 stsp return NULL;
2683 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2684 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2685 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2687 f8d1f275 2019-02-04 stsp path = de->d_name;
2690 bd8de430 2019-10-04 stsp if (de->d_type == DT_DIR) {
2691 32d2fb48 2019-12-15 stsp int subdirfd = openat(dirfd, de->d_name,
2692 886cec17 2019-12-15 stsp O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2693 32d2fb48 2019-12-15 stsp if (subdirfd == -1) {
2694 32d2fb48 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2695 32d2fb48 2019-12-15 stsp err = got_error_from_errno2("openat", path);
2697 bd8de430 2019-10-04 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
2698 886cec17 2019-12-15 stsp path, subdirfd, ".cvsignore");
2699 886cec17 2019-12-15 stsp if (err == NULL)
2700 886cec17 2019-12-15 stsp err = add_ignores(&a->ignores,
2701 886cec17 2019-12-15 stsp a->worktree->root_path, path,
2702 886cec17 2019-12-15 stsp subdirfd, ".gitignore");
2703 886cec17 2019-12-15 stsp if (close(subdirfd) == -1 && err == NULL)
2704 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
2706 886cec17 2019-12-15 stsp } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2707 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
2708 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2709 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2710 f8d1f275 2019-02-04 stsp if (parent_path[0])
2711 f8d1f275 2019-02-04 stsp free(path);
2712 b72f483a 2019-02-05 stsp return err;
2715 347d1d3e 2019-07-12 stsp static const struct got_error *
2716 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2717 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2718 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
2720 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2721 abb4604f 2019-07-27 stsp struct stat sb;
2723 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2725 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
2726 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
2728 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2729 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2730 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2731 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2732 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2733 abb4604f 2019-07-27 stsp return NULL;
2736 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2737 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2738 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2740 abb4604f 2019-07-27 stsp return NULL;
2743 abb4604f 2019-07-27 stsp static const struct got_error *
2744 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2745 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2746 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2747 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2748 f2a9dc41 2019-12-13 tracey int report_unchanged)
2750 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2751 6fc93f37 2019-12-13 stsp int fd = -1;
2752 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2753 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2754 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2756 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2757 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2758 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2760 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2761 6fc93f37 2019-12-13 stsp if (fd == -1) {
2762 40b289d7 2019-09-07 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2763 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
2765 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2766 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
2767 f2a9dc41 2019-12-13 tracey report_unchanged);
2769 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2770 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2771 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2772 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2773 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2774 abb4604f 2019-07-27 stsp arg.status_path = path;
2775 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2776 abb4604f 2019-07-27 stsp arg.repo = repo;
2777 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2778 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2779 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2780 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2781 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
2782 6841da00 2019-08-08 stsp TAILQ_INIT(&arg.ignores);
2783 022fae89 2019-12-06 tracey if (!no_ignores) {
2784 bd8de430 2019-10-04 stsp err = add_ignores(&arg.ignores, worktree->root_path,
2785 886cec17 2019-12-15 stsp path, fd, ".cvsignore");
2786 022fae89 2019-12-06 tracey if (err == NULL)
2787 022fae89 2019-12-06 tracey err = add_ignores(&arg.ignores,
2788 886cec17 2019-12-15 stsp worktree->root_path, path, fd,
2789 886cec17 2019-12-15 stsp ".gitignore");
2791 bd8de430 2019-10-04 stsp if (err == NULL)
2792 6fc93f37 2019-12-13 stsp err = got_fileindex_diff_dir(fileindex, fd,
2793 6841da00 2019-08-08 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2794 6841da00 2019-08-08 stsp free_ignores(&arg.ignores);
2797 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
2798 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
2799 927df6b7 2019-02-10 stsp free(ondisk_path);
2800 347d1d3e 2019-07-12 stsp return err;
2803 347d1d3e 2019-07-12 stsp const struct got_error *
2804 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2805 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2806 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2807 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2809 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2810 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2811 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2812 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2814 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2816 347d1d3e 2019-07-12 stsp return err;
2818 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2819 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2820 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2824 f8d1f275 2019-02-04 stsp free(fileindex_path);
2825 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2826 f8d1f275 2019-02-04 stsp return err;
2829 6c7ab921 2019-03-18 stsp const struct got_error *
2830 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2831 6c7ab921 2019-03-18 stsp const char *arg)
2833 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2834 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2835 6c7ab921 2019-03-18 stsp size_t len;
2837 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2839 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2840 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2841 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2842 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2843 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2844 d0710d08 2019-07-22 stsp if (cwd == NULL)
2845 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2846 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2847 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2848 d0710d08 2019-07-22 stsp goto done;
2852 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2853 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2854 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2855 6c7ab921 2019-03-18 stsp goto done;
2858 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2859 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2860 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2862 f6d88e1a 2019-05-29 stsp goto done;
2864 f6d88e1a 2019-05-29 stsp path = strdup("");
2865 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2866 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2867 f6d88e1a 2019-05-29 stsp goto done;
2871 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2872 6c7ab921 2019-03-18 stsp len = strlen(path);
2873 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2874 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2878 6c7ab921 2019-03-18 stsp free(resolved);
2879 d0710d08 2019-07-22 stsp free(cwd);
2880 6c7ab921 2019-03-18 stsp if (err == NULL)
2881 6c7ab921 2019-03-18 stsp *wt_path = path;
2883 6c7ab921 2019-03-18 stsp free(path);
2884 d00136be 2019-03-26 stsp return err;
2887 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
2888 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
2889 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
2890 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
2891 4e68cba3 2019-11-23 stsp void *progress_arg;
2892 4e68cba3 2019-11-23 stsp struct got_repository *repo;
2895 4e68cba3 2019-11-23 stsp static const struct got_error *
2896 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2897 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
2898 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2899 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2901 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
2902 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2903 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2904 6d022e97 2019-08-04 stsp struct stat sb;
2905 dbb83fbd 2019-12-12 stsp char *ondisk_path;
2907 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2908 dbb83fbd 2019-12-12 stsp relpath) == -1)
2909 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
2911 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2913 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2914 12463d8b 2019-12-13 stsp de_name, a->repo);
2916 dbb83fbd 2019-12-12 stsp goto done;
2917 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
2918 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
2919 dbb83fbd 2019-12-12 stsp goto done;
2920 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2922 dbb83fbd 2019-12-12 stsp goto done;
2925 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
2926 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2927 dbb83fbd 2019-12-12 stsp goto done;
2930 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
2932 4e68cba3 2019-11-23 stsp goto done;
2933 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2934 3969253a 2020-03-07 stsp if (err) {
2935 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2936 3969253a 2020-03-07 stsp goto done;
2938 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2939 07f5b47a 2019-06-02 stsp if (err) {
2940 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2941 4e68cba3 2019-11-23 stsp goto done;
2944 dbb83fbd 2019-12-12 stsp free(ondisk_path);
2946 dbb83fbd 2019-12-12 stsp return err;
2947 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
2948 dbb83fbd 2019-12-12 stsp return NULL;
2949 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2952 d00136be 2019-03-26 stsp const struct got_error *
2953 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2954 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
2955 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2956 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
2958 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2959 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2960 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2961 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2962 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
2964 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2966 d00136be 2019-03-26 stsp return err;
2968 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2970 d00136be 2019-03-26 stsp goto done;
2972 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
2973 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
2974 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
2975 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
2976 4e68cba3 2019-11-23 stsp saa.repo = repo;
2978 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2979 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2980 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2984 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2985 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2986 af12c6b9 2019-06-04 stsp err = sync_err;
2988 fb399478 2019-07-12 stsp free(fileindex_path);
2989 d00136be 2019-03-26 stsp if (fileindex)
2990 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2991 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2992 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2993 d00136be 2019-03-26 stsp err = unlockerr;
2994 6c7ab921 2019-03-18 stsp return err;
2997 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
2998 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
2999 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3000 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3001 f2a9dc41 2019-12-13 tracey void *progress_arg;
3002 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3003 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3004 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3007 f2a9dc41 2019-12-13 tracey static const struct got_error *
3008 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3009 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3010 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3011 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3013 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3014 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3015 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3016 17ed4618 2019-06-02 stsp struct stat sb;
3017 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3019 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3020 17ed4618 2019-06-02 stsp if (ie == NULL)
3021 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3023 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3024 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3025 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3026 9acbc4fa 2019-08-03 stsp return NULL;
3027 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3030 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3031 f2a9dc41 2019-12-13 tracey relpath) == -1)
3032 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3034 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3037 f2a9dc41 2019-12-13 tracey goto done;
3039 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3040 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3041 f2a9dc41 2019-12-13 tracey goto done;
3042 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3043 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3044 f2a9dc41 2019-12-13 tracey goto done;
3046 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3047 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3048 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3049 f2a9dc41 2019-12-13 tracey goto done;
3053 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3054 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3055 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3056 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3057 12463d8b 2019-12-13 stsp ondisk_path);
3058 12463d8b 2019-12-13 stsp goto done;
3060 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3061 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3062 12463d8b 2019-12-13 stsp goto done;
3065 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3067 15341bfd 2020-03-05 tracey if (parent == NULL) {
3068 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3069 15341bfd 2020-03-05 tracey goto done;
3071 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3072 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3073 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3074 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3075 15341bfd 2020-03-05 tracey parent);
3078 15341bfd 2020-03-05 tracey parent = dirname(parent);
3079 15341bfd 2020-03-05 tracey if (parent == NULL) {
3080 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3081 15341bfd 2020-03-05 tracey goto done;
3086 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3088 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3089 f2a9dc41 2019-12-13 tracey if (err)
3090 f2a9dc41 2019-12-13 tracey return err;
3091 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3092 f2a9dc41 2019-12-13 tracey return NULL;
3093 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3094 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3097 2ec1f75b 2019-03-26 stsp const struct got_error *
3098 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3099 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3100 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3101 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3103 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3104 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3105 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3106 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3107 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3109 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3111 2ec1f75b 2019-03-26 stsp return err;
3113 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3115 2ec1f75b 2019-03-26 stsp goto done;
3117 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3118 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3119 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3120 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3121 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3122 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3123 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3125 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3126 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3127 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3131 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3132 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3133 af12c6b9 2019-06-04 stsp err = sync_err;
3135 fb399478 2019-07-12 stsp free(fileindex_path);
3136 2ec1f75b 2019-03-26 stsp if (fileindex)
3137 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3138 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3139 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3140 2ec1f75b 2019-03-26 stsp err = unlockerr;
3141 33aa809d 2019-08-08 stsp return err;
3144 33aa809d 2019-08-08 stsp static const struct got_error *
3145 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3147 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3148 33aa809d 2019-08-08 stsp char *line = NULL;
3149 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3150 33aa809d 2019-08-08 stsp ssize_t linelen;
3152 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3153 33aa809d 2019-08-08 stsp if (linelen == -1) {
3154 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3155 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3156 33aa809d 2019-08-08 stsp goto done;
3158 33aa809d 2019-08-08 stsp return NULL;
3160 33aa809d 2019-08-08 stsp if (outfile) {
3161 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3162 33aa809d 2019-08-08 stsp if (n != linelen) {
3163 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3164 33aa809d 2019-08-08 stsp goto done;
3167 33aa809d 2019-08-08 stsp if (rejectfile) {
3168 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3169 33aa809d 2019-08-08 stsp if (n != linelen)
3170 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3173 33aa809d 2019-08-08 stsp free(line);
3174 2ec1f75b 2019-03-26 stsp return err;
3177 33aa809d 2019-08-08 stsp static const struct got_error *
3178 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3180 33aa809d 2019-08-08 stsp char *line = NULL;
3181 33aa809d 2019-08-08 stsp size_t linesize = 0;
3182 33aa809d 2019-08-08 stsp ssize_t linelen;
3184 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3185 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3186 500467ff 2019-09-25 hiltjo if (ferror(f))
3187 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3188 500467ff 2019-09-25 hiltjo return NULL;
3190 33aa809d 2019-08-08 stsp free(line);
3191 33aa809d 2019-08-08 stsp return NULL;
3194 33aa809d 2019-08-08 stsp static const struct got_error *
3195 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3196 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3197 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3199 33aa809d 2019-08-08 stsp const struct got_error *err;
3201 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3202 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3203 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3205 33aa809d 2019-08-08 stsp return err;
3206 33aa809d 2019-08-08 stsp (*line_cur1)++;
3208 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3209 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3210 33aa809d 2019-08-08 stsp if (rejectfile)
3211 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3213 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3215 33aa809d 2019-08-08 stsp return err;
3216 33aa809d 2019-08-08 stsp (*line_cur2)++;
3218 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3219 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3220 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3222 33aa809d 2019-08-08 stsp return err;
3223 33aa809d 2019-08-08 stsp (*line_cur2)++;
3225 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3226 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3227 33aa809d 2019-08-08 stsp if (rejectfile)
3228 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3230 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3232 33aa809d 2019-08-08 stsp return err;
3233 33aa809d 2019-08-08 stsp (*line_cur1)++;
3236 f1e81a05 2019-08-10 stsp return NULL;
3239 f1e81a05 2019-08-10 stsp static const struct got_error *
3240 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3241 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3243 f1e81a05 2019-08-10 stsp const struct got_error *err;
3245 f1e81a05 2019-08-10 stsp if (outfile) {
3246 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3247 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3248 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3250 f1e81a05 2019-08-10 stsp return err;
3251 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3254 f1e81a05 2019-08-10 stsp if (rejectfile) {
3255 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3256 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3257 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3259 f1e81a05 2019-08-10 stsp return err;
3260 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3264 33aa809d 2019-08-08 stsp return NULL;
3267 33aa809d 2019-08-08 stsp static const struct got_error *
3268 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3269 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3270 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3271 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3272 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3274 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3275 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3276 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3277 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3278 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3279 33aa809d 2019-08-08 stsp long pos1, pos2;
3280 33aa809d 2019-08-08 stsp FILE *hunkfile;
3282 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3284 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3285 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3286 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3288 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3289 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3291 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3292 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3294 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3295 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3296 33aa809d 2019-08-08 stsp goto done;
3298 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3299 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3300 33aa809d 2019-08-08 stsp goto done;
3302 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3303 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3304 33aa809d 2019-08-08 stsp goto done;
3307 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3308 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3310 33aa809d 2019-08-08 stsp goto done;
3312 33aa809d 2019-08-08 stsp switch (*choice) {
3313 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3314 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3315 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3317 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3318 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3319 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3321 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3324 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3328 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3329 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3330 33aa809d 2019-08-08 stsp return err;
3333 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3334 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3335 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3336 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3337 1f1abb7e 2019-08-08 stsp void *progress_arg;
3338 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3339 33aa809d 2019-08-08 stsp void *patch_arg;
3340 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3343 e20a8b6f 2019-06-04 stsp static const struct got_error *
3344 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3345 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3346 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
3347 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3348 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3350 33aa809d 2019-08-08 stsp const struct got_error *err;
3351 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3352 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3353 1ebedb77 2019-10-19 stsp int fd2 = -1;
3354 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3355 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3356 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3357 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3358 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3359 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3360 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3361 33aa809d 2019-08-08 stsp int n = 0;
3363 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3365 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3367 33aa809d 2019-08-08 stsp return err;
3369 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
3370 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3371 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3372 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
3373 12463d8b 2019-12-13 stsp goto done;
3376 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3377 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3378 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
3379 12463d8b 2019-12-13 stsp goto done;
3382 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
3383 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
3384 1ebedb77 2019-10-19 stsp goto done;
3387 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
3388 33aa809d 2019-08-08 stsp if (f2 == NULL) {
3389 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
3390 33aa809d 2019-08-08 stsp goto done;
3394 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3396 33aa809d 2019-08-08 stsp goto done;
3398 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3400 33aa809d 2019-08-08 stsp goto done;
3402 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3404 33aa809d 2019-08-08 stsp goto done;
3406 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
3407 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
3408 33aa809d 2019-08-08 stsp goto done;
3411 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
3412 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3414 33aa809d 2019-08-08 stsp goto done;
3416 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3418 33aa809d 2019-08-08 stsp goto done;
3420 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
3421 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
3422 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
3423 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
3424 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3425 33aa809d 2019-08-08 stsp int choice;
3426 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
3427 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
3428 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
3429 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
3430 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
3431 e635744c 2019-08-08 stsp patch_cb, patch_arg);
3433 33aa809d 2019-08-08 stsp goto done;
3434 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
3435 33aa809d 2019-08-08 stsp have_content = 1;
3436 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
3439 1ebedb77 2019-10-19 stsp if (have_content) {
3440 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3441 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
3442 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
3444 1ebedb77 2019-10-19 stsp goto done;
3446 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
3447 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
3448 1ebedb77 2019-10-19 stsp goto done;
3452 33aa809d 2019-08-08 stsp free(id_str);
3454 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
3455 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3456 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
3457 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3458 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
3459 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3460 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
3461 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
3462 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
3463 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
3464 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
3465 33aa809d 2019-08-08 stsp if (err || !have_content) {
3466 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3467 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
3468 33aa809d 2019-08-08 stsp free(*path_outfile);
3469 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3471 33aa809d 2019-08-08 stsp free(args);
3473 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
3476 33aa809d 2019-08-08 stsp if (changes)
3477 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
3478 33aa809d 2019-08-08 stsp free(path1);
3479 33aa809d 2019-08-08 stsp return err;
3482 33aa809d 2019-08-08 stsp static const struct got_error *
3483 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
3484 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
3485 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3486 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3488 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
3489 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
3490 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
3491 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
3492 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
3493 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
3494 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
3495 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
3496 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
3497 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
3499 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
3500 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
3501 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
3502 d3bcc3d1 2019-08-08 stsp return NULL;
3504 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
3505 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
3506 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
3508 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3509 65084dad 2019-08-08 stsp if (ie == NULL)
3510 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3512 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
3513 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
3514 a129376b 2019-03-28 stsp if (err) {
3515 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
3516 a129376b 2019-03-28 stsp goto done;
3517 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
3518 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
3519 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
3520 e20a8b6f 2019-06-04 stsp goto done;
3523 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
3524 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
3525 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3526 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3527 a129376b 2019-03-28 stsp goto done;
3530 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
3531 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
3532 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3533 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3534 a129376b 2019-03-28 stsp goto done;
3537 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
3538 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
3539 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3540 a129376b 2019-03-28 stsp goto done;
3545 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
3546 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
3547 a9fa2909 2019-07-27 stsp if (err) {
3548 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3549 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
3550 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
3551 a9fa2909 2019-07-27 stsp goto done;
3553 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
3555 a9fa2909 2019-07-27 stsp goto done;
3557 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
3558 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
3559 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
3560 a9fa2909 2019-07-27 stsp goto done;
3563 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
3564 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
3565 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
3566 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
3567 a9fa2909 2019-07-27 stsp goto done;
3571 a129376b 2019-03-28 stsp switch (status) {
3572 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
3573 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3574 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3575 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3576 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3578 33aa809d 2019-08-08 stsp goto done;
3579 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3582 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3583 1f1abb7e 2019-08-08 stsp ie->path);
3585 1ee397ad 2019-07-12 stsp goto done;
3586 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
3588 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
3589 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3590 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3591 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3592 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3594 33aa809d 2019-08-08 stsp goto done;
3595 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3598 33aa809d 2019-08-08 stsp /* fall through */
3599 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
3600 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
3601 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
3602 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
3603 e20a8b6f 2019-06-04 stsp struct got_object_id id;
3604 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3605 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3606 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
3607 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3609 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
3610 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3611 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3613 65084dad 2019-08-08 stsp goto done;
3615 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3616 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
3617 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
3618 a129376b 2019-03-28 stsp goto done;
3621 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3622 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
3623 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
3624 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
3625 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
3626 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
3628 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
3629 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
3630 33aa809d 2019-08-08 stsp path_content, ondisk_path);
3631 33aa809d 2019-08-08 stsp goto done;
3634 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
3635 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
3636 33aa809d 2019-08-08 stsp got_fileindex_perms_to_st(ie), blob, 0, 1,
3637 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
3639 a129376b 2019-03-28 stsp goto done;
3640 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
3641 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
3642 33aa809d 2019-08-08 stsp err = update_blob_fileindex_entry(a->worktree,
3643 33aa809d 2019-08-08 stsp a->fileindex, ie, ondisk_path, ie->path,
3646 33aa809d 2019-08-08 stsp goto done;
3655 1f1abb7e 2019-08-08 stsp free(ondisk_path);
3656 33aa809d 2019-08-08 stsp free(path_content);
3657 e20a8b6f 2019-06-04 stsp free(parent_path);
3658 a129376b 2019-03-28 stsp free(tree_path);
3660 a129376b 2019-03-28 stsp got_object_blob_close(blob);
3662 a129376b 2019-03-28 stsp got_object_tree_close(tree);
3663 a129376b 2019-03-28 stsp free(tree_id);
3664 e20a8b6f 2019-06-04 stsp return err;
3667 e20a8b6f 2019-06-04 stsp const struct got_error *
3668 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
3669 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
3670 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3671 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
3672 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
3674 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
3675 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
3676 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3677 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
3678 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3679 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
3681 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
3683 e20a8b6f 2019-06-04 stsp return err;
3685 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3687 e20a8b6f 2019-06-04 stsp goto done;
3689 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
3690 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
3691 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
3692 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
3693 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
3694 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
3695 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
3696 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
3697 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3698 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
3702 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3703 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
3704 e20a8b6f 2019-06-04 stsp err = sync_err;
3706 fb399478 2019-07-12 stsp free(fileindex_path);
3707 a129376b 2019-03-28 stsp if (fileindex)
3708 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
3709 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3710 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
3711 a129376b 2019-03-28 stsp err = unlockerr;
3712 c4296144 2019-05-09 stsp return err;
3715 cf066bf8 2019-05-09 stsp static void
3716 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
3718 24519714 2019-05-09 stsp free(ct->path);
3719 44d03001 2019-05-09 stsp free(ct->in_repo_path);
3720 768aea60 2019-05-09 stsp free(ct->ondisk_path);
3721 e75eb4da 2019-05-10 stsp free(ct->blob_id);
3722 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
3723 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
3724 b416585c 2019-05-13 stsp free(ct->base_commit_id);
3728 ed175427 2019-05-09 stsp struct collect_commitables_arg {
3729 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
3730 24519714 2019-05-09 stsp struct got_repository *repo;
3731 24519714 2019-05-09 stsp struct got_worktree *worktree;
3732 5f8a88c6 2019-08-03 stsp int have_staged_files;
3735 c4296144 2019-05-09 stsp static const struct got_error *
3736 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
3737 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
3738 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3739 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3741 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
3742 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
3743 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3744 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
3745 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
3746 768aea60 2019-05-09 stsp struct stat sb;
3748 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
3749 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3750 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3751 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3752 5f8a88c6 2019-08-03 stsp return NULL;
3754 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
3755 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
3757 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3758 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
3759 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
3760 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
3761 5f8a88c6 2019-08-03 stsp return NULL;
3764 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
3765 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3766 036813ee 2019-05-09 stsp goto done;
3768 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
3769 036813ee 2019-05-09 stsp parent_path = strdup("");
3770 69960a46 2019-05-09 stsp if (parent_path == NULL)
3771 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3773 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
3775 69960a46 2019-05-09 stsp return err;
3778 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
3779 cf066bf8 2019-05-09 stsp if (ct == NULL) {
3780 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3781 c4296144 2019-05-09 stsp goto done;
3784 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3785 768aea60 2019-05-09 stsp relpath) == -1) {
3786 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3787 768aea60 2019-05-09 stsp goto done;
3789 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3790 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3792 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3793 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
3794 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
3795 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
3796 12463d8b 2019-12-13 stsp ct->ondisk_path);
3797 12463d8b 2019-12-13 stsp goto done;
3799 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
3800 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
3801 768aea60 2019-05-09 stsp goto done;
3803 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
3806 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3807 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3808 44d03001 2019-05-09 stsp relpath) == -1) {
3809 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3810 44d03001 2019-05-09 stsp goto done;
3813 cf066bf8 2019-05-09 stsp ct->status = status;
3814 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
3815 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
3816 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
3817 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
3818 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
3819 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
3820 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3821 b416585c 2019-05-13 stsp goto done;
3823 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
3824 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
3825 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
3826 f0b75401 2019-08-03 stsp goto done;
3829 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3830 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3831 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3832 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
3833 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3834 036813ee 2019-05-09 stsp goto done;
3837 24519714 2019-05-09 stsp ct->path = strdup(path);
3838 24519714 2019-05-09 stsp if (ct->path == NULL) {
3839 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3840 24519714 2019-05-09 stsp goto done;
3842 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3844 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
3845 ed175427 2019-05-09 stsp free_commitable(ct);
3846 24519714 2019-05-09 stsp free(parent_path);
3847 036813ee 2019-05-09 stsp free(path);
3848 a129376b 2019-03-28 stsp return err;
3851 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
3852 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
3853 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3854 036813ee 2019-05-09 stsp struct got_repository *);
3856 ed175427 2019-05-09 stsp static const struct got_error *
3857 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
3858 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
3859 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3860 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3861 afa376bf 2019-05-09 stsp struct got_repository *repo)
3863 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
3864 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
3865 036813ee 2019-05-09 stsp char *subpath;
3867 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
3868 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3869 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3871 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
3873 ed175427 2019-05-09 stsp return err;
3875 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
3876 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
3877 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
3878 036813ee 2019-05-09 stsp free(subpath);
3879 036813ee 2019-05-09 stsp return err;
3882 036813ee 2019-05-09 stsp static const struct got_error *
3883 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3885 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3886 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
3888 036813ee 2019-05-09 stsp *match = 0;
3890 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
3891 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
3892 0f63689d 2019-05-10 stsp return NULL;
3895 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3897 0f63689d 2019-05-10 stsp return err;
3898 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
3899 036813ee 2019-05-09 stsp free(ct_parent_path);
3900 036813ee 2019-05-09 stsp return err;
3903 768aea60 2019-05-09 stsp static mode_t
3904 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
3906 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3909 036813ee 2019-05-09 stsp static const struct got_error *
3910 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3911 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3913 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3915 036813ee 2019-05-09 stsp *new_te = NULL;
3917 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3919 036813ee 2019-05-09 stsp goto done;
3921 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3923 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
3924 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
3925 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
3927 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3929 036813ee 2019-05-09 stsp if (err && *new_te) {
3930 56e0773d 2019-11-28 stsp free(*new_te);
3931 036813ee 2019-05-09 stsp *new_te = NULL;
3933 036813ee 2019-05-09 stsp return err;
3936 036813ee 2019-05-09 stsp static const struct got_error *
3937 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3938 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3940 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3941 036813ee 2019-05-09 stsp char *ct_name;
3943 036813ee 2019-05-09 stsp *new_te = NULL;
3945 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3946 036813ee 2019-05-09 stsp if (*new_te == NULL)
3947 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3949 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3950 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3951 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3952 036813ee 2019-05-09 stsp goto done;
3954 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3955 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
3956 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
3957 036813ee 2019-05-09 stsp goto done;
3960 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3962 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
3963 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
3964 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
3966 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3968 036813ee 2019-05-09 stsp if (err && *new_te) {
3969 56e0773d 2019-11-28 stsp free(*new_te);
3970 036813ee 2019-05-09 stsp *new_te = NULL;
3972 036813ee 2019-05-09 stsp return err;
3975 036813ee 2019-05-09 stsp static const struct got_error *
3976 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3977 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3979 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3980 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3982 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3984 036813ee 2019-05-09 stsp return err;
3985 036813ee 2019-05-09 stsp if (new_pe == NULL)
3986 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3987 036813ee 2019-05-09 stsp return NULL;
3990 afa376bf 2019-05-09 stsp static const struct got_error *
3991 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3992 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3994 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3995 5f8a88c6 2019-08-03 stsp unsigned char status;
3997 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3998 afa376bf 2019-05-09 stsp ct_path++;
4000 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4001 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4003 5f8a88c6 2019-08-03 stsp status = ct->status;
4005 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4006 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4009 036813ee 2019-05-09 stsp static const struct got_error *
4010 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4011 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4013 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4014 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4015 44d03001 2019-05-09 stsp char *te_path;
4017 44d03001 2019-05-09 stsp *modified = 0;
4019 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4020 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4021 44d03001 2019-05-09 stsp te->name) == -1)
4022 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4024 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4025 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4026 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4027 44d03001 2019-05-09 stsp strlen(te_path));
4028 44d03001 2019-05-09 stsp if (*modified)
4032 44d03001 2019-05-09 stsp free(te_path);
4033 44d03001 2019-05-09 stsp return err;
4036 44d03001 2019-05-09 stsp static const struct got_error *
4037 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4038 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4039 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4041 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4042 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4044 036813ee 2019-05-09 stsp *ctp = NULL;
4046 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4047 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4048 036813ee 2019-05-09 stsp char *ct_name = NULL;
4049 036813ee 2019-05-09 stsp int path_matches;
4051 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4052 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4053 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4054 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4057 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4058 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4062 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4065 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4067 036813ee 2019-05-09 stsp return err;
4068 036813ee 2019-05-09 stsp if (!path_matches)
4071 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4072 036813ee 2019-05-09 stsp if (ct_name == NULL)
4073 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4075 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4078 036813ee 2019-05-09 stsp *ctp = ct;
4082 2b496619 2019-07-10 stsp return err;
4085 2b496619 2019-07-10 stsp static const struct got_error *
4086 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4087 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4088 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4089 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4090 2b496619 2019-07-10 stsp struct got_repository *repo)
4092 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4093 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4094 2b496619 2019-07-10 stsp char *subtree_path;
4095 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4096 ba580f68 2020-03-22 stsp int nentries;
4098 2b496619 2019-07-10 stsp *new_tep = NULL;
4100 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4101 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4102 2b496619 2019-07-10 stsp child_path) == -1)
4103 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4105 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4106 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4107 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4108 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4110 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4111 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4112 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4113 2b496619 2019-07-10 stsp goto done;
4115 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4116 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4117 2b496619 2019-07-10 stsp if (err) {
4118 56e0773d 2019-11-28 stsp free(new_te);
4119 2b496619 2019-07-10 stsp goto done;
4121 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4124 2b496619 2019-07-10 stsp free(subtree_path);
4125 2b496619 2019-07-10 stsp if (err == NULL)
4126 2b496619 2019-07-10 stsp *new_tep = new_te;
4127 036813ee 2019-05-09 stsp return err;
4130 036813ee 2019-05-09 stsp static const struct got_error *
4131 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4132 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4133 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4134 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4135 036813ee 2019-05-09 stsp struct got_repository *repo)
4137 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4138 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4139 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4140 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4142 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4143 ba580f68 2020-03-22 stsp *nentries = 0;
4145 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4146 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4147 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4148 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4150 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4151 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4152 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4155 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4156 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4159 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4160 036813ee 2019-05-09 stsp pe->path);
4162 036813ee 2019-05-09 stsp goto done;
4164 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4165 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4166 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4168 036813ee 2019-05-09 stsp goto done;
4169 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4171 afa376bf 2019-05-09 stsp goto done;
4172 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4173 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4175 2f51b5b3 2019-05-09 stsp goto done;
4176 ba580f68 2020-03-22 stsp (*nentries)++;
4178 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4179 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4180 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4181 2b496619 2019-07-10 stsp == NULL) {
4182 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4183 2b496619 2019-07-10 stsp child_path, path_base_tree,
4184 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4187 2b496619 2019-07-10 stsp goto done;
4188 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4190 2b496619 2019-07-10 stsp goto done;
4191 ba580f68 2020-03-22 stsp (*nentries)++;
4196 2f51b5b3 2019-05-09 stsp if (base_tree) {
4197 56e0773d 2019-11-28 stsp int i, nbase_entries;
4198 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4199 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4200 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4201 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4203 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4204 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4205 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4206 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4208 63c5ca5d 2019-08-24 stsp goto done;
4209 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4211 63c5ca5d 2019-08-24 stsp goto done;
4212 ba580f68 2020-03-22 stsp (*nentries)++;
4216 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4217 44d03001 2019-05-09 stsp int modified;
4218 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4220 036813ee 2019-05-09 stsp goto done;
4221 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4222 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4224 2f51b5b3 2019-05-09 stsp goto done;
4225 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4226 44d03001 2019-05-09 stsp if (modified) {
4227 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4228 ba580f68 2020-03-22 stsp int nsubentries;
4229 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4230 ba580f68 2020-03-22 stsp &nsubentries, te,
4231 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4232 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4234 44d03001 2019-05-09 stsp goto done;
4235 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4236 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4237 ba580f68 2020-03-22 stsp free(new_id);
4240 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4241 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4242 56e0773d 2019-11-28 stsp free(new_id);
4244 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4246 036813ee 2019-05-09 stsp goto done;
4247 ba580f68 2020-03-22 stsp (*nentries)++;
4251 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4252 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4254 f66c734c 2019-09-22 stsp goto done;
4256 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4257 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4258 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4259 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4260 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4261 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4263 2f51b5b3 2019-05-09 stsp goto done;
4264 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4266 2f51b5b3 2019-05-09 stsp goto done;
4267 ba580f68 2020-03-22 stsp (*nentries)++;
4269 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4270 b416585c 2019-05-13 stsp status_arg);
4272 afa376bf 2019-05-09 stsp goto done;
4274 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4275 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4277 2f51b5b3 2019-05-09 stsp goto done;
4278 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4280 2f51b5b3 2019-05-09 stsp goto done;
4281 ba580f68 2020-03-22 stsp (*nentries)++;
4286 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4287 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4289 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4290 ed175427 2019-05-09 stsp return err;
4293 ebf99748 2019-05-09 stsp static const struct got_error *
4294 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4295 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4296 72fd46fa 2019-09-06 stsp int have_staged_files)
4298 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
4299 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
4301 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4302 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
4303 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4305 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4307 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
4308 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
4309 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
4310 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
4311 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4312 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
4313 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
4314 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
4315 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
4316 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4317 72fd46fa 2019-09-06 stsp !have_staged_files);
4319 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
4320 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
4321 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4322 72fd46fa 2019-09-06 stsp !have_staged_files);
4324 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
4327 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
4328 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4329 3969253a 2020-03-07 stsp if (err) {
4330 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4333 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
4334 3969253a 2020-03-07 stsp if (err) {
4335 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4340 d56d26ce 2019-05-10 stsp return err;
4344 d56d26ce 2019-05-10 stsp static const struct got_error *
4345 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
4346 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
4347 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
4348 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
4349 735ef5ac 2019-08-03 stsp int ood_errcode)
4351 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
4352 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
4354 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4355 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
4356 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4357 1a36436d 2019-06-10 stsp return NULL;
4359 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
4360 9bead371 2019-07-28 stsp * on matches file content in the branch head.
4362 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4363 735ef5ac 2019-08-03 stsp in_repo_path);
4364 9bead371 2019-07-28 stsp if (err) {
4365 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
4366 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
4367 1a36436d 2019-06-10 stsp goto done;
4368 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
4369 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
4371 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
4372 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4373 735ef5ac 2019-08-03 stsp in_repo_path);
4374 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4375 1a36436d 2019-06-10 stsp goto done;
4376 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
4380 1a36436d 2019-06-10 stsp return err;
4383 c4296144 2019-05-09 stsp const struct got_error *
4384 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
4385 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
4386 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
4387 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
4388 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4389 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4390 afa376bf 2019-05-09 stsp struct got_repository *repo)
4392 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4393 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
4394 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
4395 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
4396 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
4397 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
4398 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
4399 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
4400 ba580f68 2020-03-22 stsp int nentries;
4401 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
4402 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
4403 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
4405 c4296144 2019-05-09 stsp *new_commit_id = NULL;
4407 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
4409 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4411 588edf97 2019-05-10 stsp goto done;
4413 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4415 588edf97 2019-05-10 stsp goto done;
4417 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
4418 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4423 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
4424 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4428 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
4429 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4430 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4431 cf066bf8 2019-05-09 stsp char *ondisk_path;
4433 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
4434 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4435 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
4438 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
4439 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
4440 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
4443 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
4444 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
4445 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4446 cf066bf8 2019-05-09 stsp goto done;
4448 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4449 a7055788 2019-05-09 stsp free(ondisk_path);
4451 cf066bf8 2019-05-09 stsp goto done;
4454 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
4455 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4456 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4458 036813ee 2019-05-09 stsp goto done;
4460 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4462 de18fc63 2019-05-09 stsp goto done;
4463 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4464 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4465 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4466 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
4467 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
4468 33ad4cbe 2019-05-12 jcs free(logmsg);
4470 9d40349a 2019-05-09 stsp goto done;
4472 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
4473 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
4474 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
4475 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
4476 09f5bd90 2019-05-09 stsp goto done;
4478 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
4479 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4481 09f5bd90 2019-05-09 stsp goto done;
4482 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4484 ebf99748 2019-05-09 stsp goto done;
4485 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4486 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4487 09f5bd90 2019-05-09 stsp goto done;
4489 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
4490 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
4492 f2c16586 2019-05-09 stsp goto done;
4493 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
4495 f2c16586 2019-05-09 stsp goto done;
4497 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4499 09f5bd90 2019-05-09 stsp goto done;
4501 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
4503 ebf99748 2019-05-09 stsp goto done;
4505 588edf97 2019-05-10 stsp if (head_tree)
4506 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
4507 588edf97 2019-05-10 stsp if (head_commit)
4508 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
4509 09f5bd90 2019-05-09 stsp free(head_commit_id2);
4510 2f17228e 2019-05-12 stsp if (head_ref2) {
4511 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
4512 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
4513 2f17228e 2019-05-12 stsp err = unlockerr;
4514 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
4516 39cd0ff6 2019-07-12 stsp return err;
4519 5c1e53bc 2019-07-28 stsp static const struct got_error *
4520 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
4521 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
4523 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
4524 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
4526 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
4527 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
4528 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
4530 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
4531 5c1e53bc 2019-07-28 stsp ct_path++;
4533 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
4534 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
4538 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
4539 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
4541 5c1e53bc 2019-07-28 stsp return NULL;
4544 f0b75401 2019-08-03 stsp static const struct got_error *
4545 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
4547 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
4549 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4550 f0b75401 2019-08-03 stsp *have_staged_files = 1;
4551 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
4554 f0b75401 2019-08-03 stsp return NULL;
4557 5f8a88c6 2019-08-03 stsp static const struct got_error *
4558 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
4559 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
4561 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
4562 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
4564 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
4565 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
4567 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4568 5f8a88c6 2019-08-03 stsp if (ie == NULL)
4569 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4570 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4571 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
4572 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
4575 5f8a88c6 2019-08-03 stsp return NULL;
4578 39cd0ff6 2019-07-12 stsp const struct got_error *
4579 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
4580 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
4581 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
4582 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4583 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4584 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
4586 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4587 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4588 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
4589 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4590 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4591 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
4592 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
4593 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4594 f0b75401 2019-08-03 stsp int have_staged_files = 0;
4596 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
4598 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4600 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
4602 39cd0ff6 2019-07-12 stsp goto done;
4604 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4606 39cd0ff6 2019-07-12 stsp goto done;
4608 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4610 39cd0ff6 2019-07-12 stsp goto done;
4612 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4614 39cd0ff6 2019-07-12 stsp goto done;
4616 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4617 5f8a88c6 2019-08-03 stsp &have_staged_files);
4618 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
4619 5f8a88c6 2019-08-03 stsp goto done;
4620 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
4621 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
4623 5f8a88c6 2019-08-03 stsp goto done;
4626 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4627 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
4628 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
4629 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
4630 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4631 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4632 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4634 5c1e53bc 2019-07-28 stsp goto done;
4637 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4638 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4639 39cd0ff6 2019-07-12 stsp goto done;
4642 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4643 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
4645 5c1e53bc 2019-07-28 stsp goto done;
4648 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4649 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
4650 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
4652 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
4653 f0b75401 2019-08-03 stsp ct_path++;
4654 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
4655 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4656 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4658 b50cabdf 2019-07-12 stsp goto done;
4662 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
4663 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
4664 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4666 39cd0ff6 2019-07-12 stsp goto done;
4668 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4669 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
4670 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4671 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
4672 39cd0ff6 2019-07-12 stsp err = sync_err;
4674 39cd0ff6 2019-07-12 stsp if (fileindex)
4675 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
4676 39cd0ff6 2019-07-12 stsp free(fileindex_path);
4677 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4678 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
4679 39cd0ff6 2019-07-12 stsp err = unlockerr;
4680 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4681 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
4682 39cd0ff6 2019-07-12 stsp free_commitable(ct);
4684 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
4685 c4296144 2019-05-09 stsp return err;
4688 8656d6c4 2019-05-20 stsp const char *
4689 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
4691 8656d6c4 2019-05-20 stsp return ct->path;
4694 8656d6c4 2019-05-20 stsp unsigned int
4695 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
4697 8656d6c4 2019-05-20 stsp return ct->status;
4700 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
4701 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4702 818c7501 2019-07-11 stsp struct got_repository *repo;
4705 818c7501 2019-07-11 stsp static const struct got_error *
4706 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4708 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4709 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
4710 818c7501 2019-07-11 stsp unsigned char status;
4711 818c7501 2019-07-11 stsp struct stat sb;
4712 818c7501 2019-07-11 stsp char *ondisk_path;
4714 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
4715 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4716 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
4717 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
4719 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4721 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4723 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
4724 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4725 818c7501 2019-07-11 stsp free(ondisk_path);
4727 818c7501 2019-07-11 stsp return err;
4729 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
4730 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
4731 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4732 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4734 818c7501 2019-07-11 stsp return NULL;
4737 818c7501 2019-07-11 stsp const struct got_error *
4738 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4739 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4740 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
4741 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4743 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4744 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4745 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
4746 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4747 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
4748 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4749 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
4751 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4752 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4753 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4755 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4757 818c7501 2019-07-11 stsp return err;
4759 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4761 818c7501 2019-07-11 stsp goto done;
4763 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
4764 818c7501 2019-07-11 stsp ok_arg.repo = repo;
4765 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4768 818c7501 2019-07-11 stsp goto done;
4770 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4772 818c7501 2019-07-11 stsp goto done;
4774 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4776 818c7501 2019-07-11 stsp goto done;
4778 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4780 818c7501 2019-07-11 stsp goto done;
4782 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4785 e51d7b55 2020-01-04 stsp goto done;
4787 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4789 818c7501 2019-07-11 stsp goto done;
4790 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4791 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4792 e51d7b55 2020-01-04 stsp goto done;
4795 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
4796 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
4798 818c7501 2019-07-11 stsp goto done;
4799 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
4801 818c7501 2019-07-11 stsp goto done;
4803 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
4805 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4807 818c7501 2019-07-11 stsp goto done;
4809 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
4811 818c7501 2019-07-11 stsp goto done;
4813 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4814 818c7501 2019-07-11 stsp worktree->base_commit_id);
4816 818c7501 2019-07-11 stsp goto done;
4817 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
4819 818c7501 2019-07-11 stsp goto done;
4821 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4823 818c7501 2019-07-11 stsp goto done;
4825 818c7501 2019-07-11 stsp free(fileindex_path);
4826 818c7501 2019-07-11 stsp free(tmp_branch_name);
4827 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
4828 818c7501 2019-07-11 stsp free(branch_ref_name);
4829 818c7501 2019-07-11 stsp if (branch_ref)
4830 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4831 818c7501 2019-07-11 stsp if (wt_branch)
4832 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
4833 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
4834 818c7501 2019-07-11 stsp if (err) {
4835 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
4836 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
4837 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4839 818c7501 2019-07-11 stsp if (*tmp_branch) {
4840 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4841 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4843 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4844 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4845 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4847 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
4849 818c7501 2019-07-11 stsp return err;
4852 818c7501 2019-07-11 stsp const struct got_error *
4853 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
4854 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4855 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
4856 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
4858 818c7501 2019-07-11 stsp const struct got_error *err;
4859 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4860 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4861 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4862 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4863 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
4865 818c7501 2019-07-11 stsp *commit_id = NULL;
4866 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
4867 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
4868 3e3a69f1 2019-07-25 stsp *branch = NULL;
4869 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4871 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4873 3e3a69f1 2019-07-25 stsp return err;
4875 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4877 f032f1f7 2019-08-04 stsp goto done;
4879 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4880 f032f1f7 2019-08-04 stsp &have_staged_files);
4881 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
4882 f032f1f7 2019-08-04 stsp goto done;
4883 f032f1f7 2019-08-04 stsp if (have_staged_files) {
4884 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
4885 3e3a69f1 2019-07-25 stsp goto done;
4888 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4890 f032f1f7 2019-08-04 stsp goto done;
4892 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4894 818c7501 2019-07-11 stsp goto done;
4896 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4898 818c7501 2019-07-11 stsp goto done;
4900 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4902 818c7501 2019-07-11 stsp goto done;
4904 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4906 818c7501 2019-07-11 stsp goto done;
4908 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
4909 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
4911 818c7501 2019-07-11 stsp goto done;
4913 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4915 818c7501 2019-07-11 stsp goto done;
4917 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4919 818c7501 2019-07-11 stsp goto done;
4921 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
4922 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
4924 818c7501 2019-07-11 stsp goto done;
4926 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4928 818c7501 2019-07-11 stsp goto done;
4930 818c7501 2019-07-11 stsp free(commit_ref_name);
4931 818c7501 2019-07-11 stsp free(branch_ref_name);
4932 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4933 818c7501 2019-07-11 stsp if (commit_ref)
4934 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4935 818c7501 2019-07-11 stsp if (branch_ref)
4936 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4937 818c7501 2019-07-11 stsp if (err) {
4938 818c7501 2019-07-11 stsp free(*commit_id);
4939 818c7501 2019-07-11 stsp *commit_id = NULL;
4940 818c7501 2019-07-11 stsp if (*tmp_branch) {
4941 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4942 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4944 818c7501 2019-07-11 stsp if (*new_base_branch) {
4945 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
4946 818c7501 2019-07-11 stsp *new_base_branch = NULL;
4948 818c7501 2019-07-11 stsp if (*branch) {
4949 818c7501 2019-07-11 stsp got_ref_close(*branch);
4950 818c7501 2019-07-11 stsp *branch = NULL;
4952 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4953 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4954 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4956 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
4958 818c7501 2019-07-11 stsp return err;
4961 818c7501 2019-07-11 stsp const struct got_error *
4962 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4964 818c7501 2019-07-11 stsp const struct got_error *err;
4965 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
4967 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4969 818c7501 2019-07-11 stsp return err;
4971 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4972 818c7501 2019-07-11 stsp free(tmp_branch_name);
4973 818c7501 2019-07-11 stsp return NULL;
4976 818c7501 2019-07-11 stsp static const struct got_error *
4977 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4978 818c7501 2019-07-11 stsp char **logmsg, void *arg)
4980 0ebf8283 2019-07-24 stsp *logmsg = arg;
4981 818c7501 2019-07-11 stsp return NULL;
4984 818c7501 2019-07-11 stsp static const struct got_error *
4985 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4986 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4987 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4988 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4990 818c7501 2019-07-11 stsp return NULL;
4993 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
4994 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
4995 01757395 2019-07-12 stsp void *progress_arg;
4996 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
4999 01757395 2019-07-12 stsp static const struct got_error *
5000 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5002 01757395 2019-07-12 stsp const struct got_error *err;
5003 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5005 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5007 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5009 01757395 2019-07-12 stsp return err;
5011 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5012 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5013 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5014 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5015 01757395 2019-07-12 stsp return NULL;
5017 01757395 2019-07-12 stsp p = strdup(path);
5018 01757395 2019-07-12 stsp if (p == NULL)
5019 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5021 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5022 01757395 2019-07-12 stsp if (err || new == NULL)
5024 01757395 2019-07-12 stsp return err;
5028 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5030 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5032 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5033 01757395 2019-07-12 stsp free((char *)pe->path);
5035 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5038 0ebf8283 2019-07-24 stsp static const struct got_error *
5039 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5040 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5042 818c7501 2019-07-11 stsp const struct got_error *err;
5043 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5045 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5046 818c7501 2019-07-11 stsp if (err) {
5047 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5048 818c7501 2019-07-11 stsp goto done;
5049 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5051 818c7501 2019-07-11 stsp goto done;
5052 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5054 818c7501 2019-07-11 stsp goto done;
5055 de05890f 2020-03-05 stsp } else if (is_rebase) {
5056 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5059 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5061 818c7501 2019-07-11 stsp goto done;
5062 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5063 818c7501 2019-07-11 stsp free(stored_id);
5064 818c7501 2019-07-11 stsp if (cmp != 0) {
5065 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5066 818c7501 2019-07-11 stsp goto done;
5070 0ebf8283 2019-07-24 stsp if (commit_ref)
5071 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5072 0ebf8283 2019-07-24 stsp return err;
5075 0ebf8283 2019-07-24 stsp static const struct got_error *
5076 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5077 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5078 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5079 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5080 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5081 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5083 0ebf8283 2019-07-24 stsp const struct got_error *err;
5084 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5085 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5086 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5088 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5090 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5092 0ebf8283 2019-07-24 stsp return err;
5094 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5095 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5096 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5097 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5098 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5099 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5100 818c7501 2019-07-11 stsp if (commit_ref)
5101 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5102 818c7501 2019-07-11 stsp return err;
5105 818c7501 2019-07-11 stsp const struct got_error *
5106 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5107 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5108 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5109 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5110 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5111 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5113 0ebf8283 2019-07-24 stsp const struct got_error *err;
5114 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5116 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5118 0ebf8283 2019-07-24 stsp return err;
5120 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5122 0ebf8283 2019-07-24 stsp goto done;
5124 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5125 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5126 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5128 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5129 0ebf8283 2019-07-24 stsp return err;
5132 0ebf8283 2019-07-24 stsp const struct got_error *
5133 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5134 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5135 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5136 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5137 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5138 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5140 0ebf8283 2019-07-24 stsp const struct got_error *err;
5141 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5143 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5145 0ebf8283 2019-07-24 stsp return err;
5147 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5149 0ebf8283 2019-07-24 stsp goto done;
5151 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5152 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5153 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5155 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5156 0ebf8283 2019-07-24 stsp return err;
5159 0ebf8283 2019-07-24 stsp static const struct got_error *
5160 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5161 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5162 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5163 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5164 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5166 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5167 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5168 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5169 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5170 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5171 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5172 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5174 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5175 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5177 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5179 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5181 3e3a69f1 2019-07-25 stsp return err;
5183 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5184 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5185 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5186 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5188 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5189 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5190 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5191 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5193 01757395 2019-07-12 stsp if (merged_paths) {
5194 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5195 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5196 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5197 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5200 01757395 2019-07-12 stsp goto done;
5203 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5204 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5206 01757395 2019-07-12 stsp goto done;
5209 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5210 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5211 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5213 a0e95631 2019-07-12 stsp goto done;
5214 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5215 a0e95631 2019-07-12 stsp goto done;
5218 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5220 edd02c5e 2019-07-11 stsp goto done;
5222 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5224 818c7501 2019-07-11 stsp goto done;
5226 5943eee2 2019-08-13 stsp if (new_logmsg) {
5227 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
5228 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
5229 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5230 5943eee2 2019-08-13 stsp goto done;
5233 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5235 5943eee2 2019-08-13 stsp goto done;
5238 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
5239 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5240 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
5241 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
5242 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5244 a0e95631 2019-07-12 stsp goto done;
5246 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
5248 a0e95631 2019-07-12 stsp goto done;
5250 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5252 a0e95631 2019-07-12 stsp goto done;
5254 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5255 72fd46fa 2019-09-06 stsp fileindex, 0);
5256 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5257 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
5258 a0e95631 2019-07-12 stsp err = sync_err;
5260 a0e95631 2019-07-12 stsp free(fileindex_path);
5261 a0e95631 2019-07-12 stsp free(head_commit_id);
5262 a0e95631 2019-07-12 stsp if (head_ref)
5263 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
5264 818c7501 2019-07-11 stsp if (err) {
5265 818c7501 2019-07-11 stsp free(*new_commit_id);
5266 818c7501 2019-07-11 stsp *new_commit_id = NULL;
5268 818c7501 2019-07-11 stsp return err;
5271 818c7501 2019-07-11 stsp const struct got_error *
5272 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5273 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5274 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5275 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5276 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
5278 0ebf8283 2019-07-24 stsp const struct got_error *err;
5279 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5280 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5281 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5283 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5285 0ebf8283 2019-07-24 stsp return err;
5287 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5289 0ebf8283 2019-07-24 stsp goto done;
5290 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5292 0ebf8283 2019-07-24 stsp goto done;
5293 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5294 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5295 0ebf8283 2019-07-24 stsp goto done;
5298 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5299 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5301 0ebf8283 2019-07-24 stsp if (commit_ref)
5302 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5303 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5304 0ebf8283 2019-07-24 stsp free(commit_id);
5305 0ebf8283 2019-07-24 stsp return err;
5308 0ebf8283 2019-07-24 stsp const struct got_error *
5309 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5310 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5311 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5312 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5313 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
5314 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5316 0ebf8283 2019-07-24 stsp const struct got_error *err;
5317 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5318 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5320 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5322 0ebf8283 2019-07-24 stsp return err;
5324 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5326 0ebf8283 2019-07-24 stsp goto done;
5328 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5329 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5331 0ebf8283 2019-07-24 stsp if (commit_ref)
5332 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5333 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5334 0ebf8283 2019-07-24 stsp return err;
5337 0ebf8283 2019-07-24 stsp const struct got_error *
5338 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
5339 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5341 3e3a69f1 2019-07-25 stsp if (fileindex)
5342 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5343 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
5346 69844fba 2019-07-11 stsp static const struct got_error *
5347 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
5349 69844fba 2019-07-11 stsp const struct got_error *err;
5350 69844fba 2019-07-11 stsp struct got_reference *ref;
5352 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
5353 69844fba 2019-07-11 stsp if (err) {
5354 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
5355 69844fba 2019-07-11 stsp return NULL;
5356 69844fba 2019-07-11 stsp return err;
5359 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
5360 69844fba 2019-07-11 stsp got_ref_close(ref);
5361 69844fba 2019-07-11 stsp return err;
5364 69844fba 2019-07-11 stsp static const struct got_error *
5365 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5367 69844fba 2019-07-11 stsp const struct got_error *err;
5368 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5369 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5371 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5373 69844fba 2019-07-11 stsp goto done;
5374 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
5376 69844fba 2019-07-11 stsp goto done;
5378 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5380 69844fba 2019-07-11 stsp goto done;
5381 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
5383 69844fba 2019-07-11 stsp goto done;
5385 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5387 69844fba 2019-07-11 stsp goto done;
5388 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
5390 69844fba 2019-07-11 stsp goto done;
5392 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5394 69844fba 2019-07-11 stsp goto done;
5395 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
5397 69844fba 2019-07-11 stsp goto done;
5400 69844fba 2019-07-11 stsp free(tmp_branch_name);
5401 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
5402 69844fba 2019-07-11 stsp free(branch_ref_name);
5403 69844fba 2019-07-11 stsp free(commit_ref_name);
5404 69844fba 2019-07-11 stsp return err;
5407 818c7501 2019-07-11 stsp const struct got_error *
5408 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
5409 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5410 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5411 818c7501 2019-07-11 stsp struct got_repository *repo)
5413 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
5414 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
5416 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5418 818c7501 2019-07-11 stsp return err;
5420 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5422 818c7501 2019-07-11 stsp goto done;
5424 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
5426 818c7501 2019-07-11 stsp goto done;
5428 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
5430 818c7501 2019-07-11 stsp goto done;
5432 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
5434 3e3a69f1 2019-07-25 stsp if (fileindex)
5435 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5436 818c7501 2019-07-11 stsp free(new_head_commit_id);
5437 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5438 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5439 818c7501 2019-07-11 stsp err = unlockerr;
5440 818c7501 2019-07-11 stsp return err;
5443 818c7501 2019-07-11 stsp const struct got_error *
5444 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
5445 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5446 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
5447 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5449 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
5450 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
5451 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
5452 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5453 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5454 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
5456 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5458 818c7501 2019-07-11 stsp return err;
5460 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
5461 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
5463 818c7501 2019-07-11 stsp goto done;
5465 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
5467 818c7501 2019-07-11 stsp goto done;
5470 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
5471 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
5472 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
5474 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
5476 818c7501 2019-07-11 stsp goto done;
5478 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5480 818c7501 2019-07-11 stsp goto done;
5482 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
5483 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
5485 ca355955 2019-07-12 stsp goto done;
5487 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
5489 ca355955 2019-07-12 stsp goto done;
5491 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5493 818c7501 2019-07-11 stsp goto done;
5495 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5496 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5497 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5498 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5499 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5500 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5501 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5502 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5503 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
5505 1f1abb7e 2019-08-08 stsp goto sync;
5507 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5508 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
5510 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5511 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
5512 ca355955 2019-07-12 stsp err = sync_err;
5514 818c7501 2019-07-11 stsp got_ref_close(resolved);
5515 a3a2faf2 2019-07-12 stsp free(tree_id);
5516 818c7501 2019-07-11 stsp free(commit_id);
5517 0ebf8283 2019-07-24 stsp if (fileindex)
5518 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
5519 0ebf8283 2019-07-24 stsp free(fileindex_path);
5521 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5522 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5523 0ebf8283 2019-07-24 stsp err = unlockerr;
5524 0ebf8283 2019-07-24 stsp return err;
5527 0ebf8283 2019-07-24 stsp const struct got_error *
5528 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5529 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5530 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
5531 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5533 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5534 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5535 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
5536 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
5537 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5538 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
5539 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
5540 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5542 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5543 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5544 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5545 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5547 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5549 0ebf8283 2019-07-24 stsp return err;
5551 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5553 0ebf8283 2019-07-24 stsp goto done;
5555 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
5556 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
5557 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5560 0ebf8283 2019-07-24 stsp goto done;
5562 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5564 0ebf8283 2019-07-24 stsp goto done;
5566 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5568 0ebf8283 2019-07-24 stsp goto done;
5570 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5571 0ebf8283 2019-07-24 stsp worktree);
5573 0ebf8283 2019-07-24 stsp goto done;
5575 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5578 0ebf8283 2019-07-24 stsp goto done;
5580 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5582 0ebf8283 2019-07-24 stsp goto done;
5584 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
5586 0ebf8283 2019-07-24 stsp goto done;
5588 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5589 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5591 0ebf8283 2019-07-24 stsp goto done;
5592 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
5594 0ebf8283 2019-07-24 stsp goto done;
5595 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5596 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
5597 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
5598 0ebf8283 2019-07-24 stsp goto done;
5601 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5602 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5604 0ebf8283 2019-07-24 stsp goto done;
5605 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
5607 0ebf8283 2019-07-24 stsp goto done;
5609 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5611 0ebf8283 2019-07-24 stsp goto done;
5613 0ebf8283 2019-07-24 stsp free(fileindex_path);
5614 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5615 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5616 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5617 0ebf8283 2019-07-24 stsp if (wt_branch)
5618 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
5619 0ebf8283 2019-07-24 stsp if (err) {
5620 0ebf8283 2019-07-24 stsp if (*branch_ref) {
5621 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
5622 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5624 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5625 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5626 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5628 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5629 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5630 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5631 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5633 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
5635 0ebf8283 2019-07-24 stsp return err;
5638 0ebf8283 2019-07-24 stsp const struct got_error *
5639 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
5640 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5642 3e3a69f1 2019-07-25 stsp if (fileindex)
5643 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5644 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
5647 0ebf8283 2019-07-24 stsp const struct got_error *
5648 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
5649 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
5651 0ebf8283 2019-07-24 stsp const struct got_error *err;
5652 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5654 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5656 0ebf8283 2019-07-24 stsp return err;
5658 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5659 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5660 0ebf8283 2019-07-24 stsp return NULL;
5663 0ebf8283 2019-07-24 stsp const struct got_error *
5664 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
5665 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
5666 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5667 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5669 0ebf8283 2019-07-24 stsp const struct got_error *err;
5670 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5671 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5672 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5673 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5674 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5675 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5677 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5678 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5679 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5680 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5682 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5684 3e3a69f1 2019-07-25 stsp return err;
5686 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5688 f032f1f7 2019-08-04 stsp goto done;
5690 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5691 f032f1f7 2019-08-04 stsp &have_staged_files);
5692 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5693 f032f1f7 2019-08-04 stsp goto done;
5694 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5695 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5696 3e3a69f1 2019-07-25 stsp goto done;
5699 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5701 f032f1f7 2019-08-04 stsp goto done;
5703 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5705 0ebf8283 2019-07-24 stsp goto done;
5707 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5709 0ebf8283 2019-07-24 stsp goto done;
5711 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5712 0ebf8283 2019-07-24 stsp worktree);
5714 0ebf8283 2019-07-24 stsp goto done;
5716 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5718 0ebf8283 2019-07-24 stsp goto done;
5720 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5722 0ebf8283 2019-07-24 stsp goto done;
5723 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5725 0ebf8283 2019-07-24 stsp goto done;
5727 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5729 0ebf8283 2019-07-24 stsp goto done;
5730 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5732 0ebf8283 2019-07-24 stsp goto done;
5734 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5736 0ebf8283 2019-07-24 stsp goto done;
5738 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5739 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5740 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5741 0ebf8283 2019-07-24 stsp if (commit_ref)
5742 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5743 0ebf8283 2019-07-24 stsp if (base_commit_ref)
5744 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
5745 0ebf8283 2019-07-24 stsp if (err) {
5746 0ebf8283 2019-07-24 stsp free(*commit_id);
5747 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5748 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5749 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5750 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5751 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5752 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5754 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5755 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5756 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5758 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
5760 0ebf8283 2019-07-24 stsp return err;
5763 0ebf8283 2019-07-24 stsp static const struct got_error *
5764 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5766 0ebf8283 2019-07-24 stsp const struct got_error *err;
5767 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5768 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5770 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5772 0ebf8283 2019-07-24 stsp goto done;
5773 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
5775 0ebf8283 2019-07-24 stsp goto done;
5777 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5778 0ebf8283 2019-07-24 stsp worktree);
5780 0ebf8283 2019-07-24 stsp goto done;
5781 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
5783 0ebf8283 2019-07-24 stsp goto done;
5785 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5787 0ebf8283 2019-07-24 stsp goto done;
5788 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
5790 0ebf8283 2019-07-24 stsp goto done;
5792 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5794 0ebf8283 2019-07-24 stsp goto done;
5795 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5797 0ebf8283 2019-07-24 stsp goto done;
5799 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5800 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5801 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5802 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5803 0ebf8283 2019-07-24 stsp return err;
5806 0ebf8283 2019-07-24 stsp const struct got_error *
5807 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
5808 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5809 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
5810 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5812 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
5813 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5814 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5815 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
5816 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5818 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5820 0ebf8283 2019-07-24 stsp return err;
5822 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5823 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
5825 0ebf8283 2019-07-24 stsp goto done;
5827 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5829 0ebf8283 2019-07-24 stsp goto done;
5831 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5833 0ebf8283 2019-07-24 stsp goto done;
5835 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5836 0ebf8283 2019-07-24 stsp worktree->path_prefix);
5838 0ebf8283 2019-07-24 stsp goto done;
5840 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5842 0ebf8283 2019-07-24 stsp goto done;
5844 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5846 0ebf8283 2019-07-24 stsp goto done;
5848 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5849 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5850 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5851 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5852 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5853 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5854 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5855 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
5856 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
5858 1f1abb7e 2019-08-08 stsp goto sync;
5860 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5861 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
5863 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5864 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
5865 0ebf8283 2019-07-24 stsp err = sync_err;
5867 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
5868 0ebf8283 2019-07-24 stsp free(tree_id);
5869 818c7501 2019-07-11 stsp free(fileindex_path);
5871 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5872 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5873 818c7501 2019-07-11 stsp err = unlockerr;
5874 818c7501 2019-07-11 stsp return err;
5877 0ebf8283 2019-07-24 stsp const struct got_error *
5878 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
5879 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5880 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
5882 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
5883 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
5884 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5886 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5888 0ebf8283 2019-07-24 stsp return err;
5890 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5891 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
5893 0ebf8283 2019-07-24 stsp goto done;
5895 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
5897 0ebf8283 2019-07-24 stsp goto done;
5899 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
5901 0ebf8283 2019-07-24 stsp goto done;
5903 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5905 0ebf8283 2019-07-24 stsp goto done;
5907 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5909 3e3a69f1 2019-07-25 stsp if (fileindex)
5910 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5911 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
5912 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5913 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5914 0ebf8283 2019-07-24 stsp err = unlockerr;
5915 0ebf8283 2019-07-24 stsp return err;
5918 0ebf8283 2019-07-24 stsp const struct got_error *
5919 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5920 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5922 0ebf8283 2019-07-24 stsp const struct got_error *err;
5923 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5925 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5927 0ebf8283 2019-07-24 stsp return err;
5929 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5931 0ebf8283 2019-07-24 stsp goto done;
5933 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5935 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5936 2822a352 2019-10-15 stsp return err;
5939 2822a352 2019-10-15 stsp const struct got_error *
5940 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5941 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5942 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
5943 2822a352 2019-10-15 stsp struct got_repository *repo)
5945 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
5946 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
5947 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
5949 2822a352 2019-10-15 stsp *fileindex = NULL;
5950 2822a352 2019-10-15 stsp *branch_ref = NULL;
5951 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
5953 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
5955 2822a352 2019-10-15 stsp return err;
5957 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5958 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5959 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
5960 2822a352 2019-10-15 stsp "update -b or different branch name required");
5961 2822a352 2019-10-15 stsp goto done;
5964 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5966 2822a352 2019-10-15 stsp goto done;
5968 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
5969 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
5970 2822a352 2019-10-15 stsp ok_arg.repo = repo;
5971 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5974 2822a352 2019-10-15 stsp goto done;
5976 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
5978 2822a352 2019-10-15 stsp goto done;
5980 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
5981 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
5983 2822a352 2019-10-15 stsp if (err) {
5984 2822a352 2019-10-15 stsp if (*branch_ref) {
5985 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
5986 2822a352 2019-10-15 stsp *branch_ref = NULL;
5988 2822a352 2019-10-15 stsp if (*base_branch_ref) {
5989 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
5990 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
5992 2822a352 2019-10-15 stsp if (*fileindex) {
5993 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
5994 2822a352 2019-10-15 stsp *fileindex = NULL;
5996 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
5998 0cb83759 2019-08-03 stsp return err;
6001 2822a352 2019-10-15 stsp const struct got_error *
6002 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6003 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6004 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6005 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6006 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6008 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6009 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6010 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6012 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6014 2822a352 2019-10-15 stsp goto done;
6016 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6018 2822a352 2019-10-15 stsp goto done;
6020 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6021 2822a352 2019-10-15 stsp worktree->path_prefix);
6023 2822a352 2019-10-15 stsp goto done;
6025 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6027 2822a352 2019-10-15 stsp goto done;
6029 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6030 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6032 2822a352 2019-10-15 stsp goto sync;
6034 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6036 2822a352 2019-10-15 stsp goto sync;
6038 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6040 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6041 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6042 2822a352 2019-10-15 stsp err = sync_err;
6045 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6046 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6047 2822a352 2019-10-15 stsp err = unlockerr;
6048 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6050 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6051 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6052 2822a352 2019-10-15 stsp err = unlockerr;
6053 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6055 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6056 2822a352 2019-10-15 stsp free(fileindex_path);
6057 2822a352 2019-10-15 stsp free(tree_id);
6059 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6060 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6061 2822a352 2019-10-15 stsp err = unlockerr;
6062 2822a352 2019-10-15 stsp return err;
6065 2822a352 2019-10-15 stsp const struct got_error *
6066 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6067 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6068 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6070 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6072 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6074 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6076 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6077 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6078 8b692cd0 2019-10-21 stsp err = unlockerr;
6079 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6081 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6082 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6083 8b692cd0 2019-10-21 stsp err = unlockerr;
6084 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6086 8b692cd0 2019-10-21 stsp return err;
6089 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6090 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6091 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6092 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6093 2db2652d 2019-08-07 stsp struct got_repository *repo;
6094 2db2652d 2019-08-07 stsp int have_changes;
6097 2db2652d 2019-08-07 stsp const struct got_error *
6098 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6099 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6100 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6101 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6103 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6104 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6105 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6106 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6107 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6108 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6110 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6111 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6112 8b13ce36 2019-08-08 stsp return NULL;
6113 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6114 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6116 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6117 735ef5ac 2019-08-03 stsp if (ie == NULL)
6118 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6120 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6121 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6122 735ef5ac 2019-08-03 stsp relpath) == -1)
6123 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6125 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6126 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6127 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6128 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6131 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6132 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6133 3aa5969e 2019-08-06 stsp goto done;
6134 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6135 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6136 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6137 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6138 735ef5ac 2019-08-03 stsp goto done;
6141 2db2652d 2019-08-07 stsp a->have_changes = 1;
6143 735ef5ac 2019-08-03 stsp p = in_repo_path;
6144 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6146 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6147 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6148 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6150 735ef5ac 2019-08-03 stsp free(in_repo_path);
6151 dc424a06 2019-08-07 stsp return err;
6154 2db2652d 2019-08-07 stsp struct stage_path_arg {
6155 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6156 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6157 2db2652d 2019-08-07 stsp struct got_repository *repo;
6158 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6159 2db2652d 2019-08-07 stsp void *status_arg;
6160 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6161 2db2652d 2019-08-07 stsp void *patch_arg;
6162 7b5dc508 2019-10-28 stsp int staged_something;
6165 2db2652d 2019-08-07 stsp static const struct got_error *
6166 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6167 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6168 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6169 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6171 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6172 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6173 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6174 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6175 0cb83759 2019-08-03 stsp uint32_t stage;
6176 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6178 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6179 8b13ce36 2019-08-08 stsp return NULL;
6181 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6182 d3e7c587 2019-08-03 stsp if (ie == NULL)
6183 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6185 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6186 2db2652d 2019-08-07 stsp relpath)== -1)
6187 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6189 0cb83759 2019-08-03 stsp switch (status) {
6190 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6191 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6192 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6193 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6194 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6195 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6196 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6199 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6202 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6203 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6204 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6205 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6206 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6210 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6211 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6214 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6215 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6216 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6217 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6219 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6220 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6221 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6222 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6224 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6225 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
6226 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
6228 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
6229 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
6231 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6232 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6233 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
6234 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
6237 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6239 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6240 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6244 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
6245 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6246 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6247 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6249 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6250 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6253 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
6255 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
6256 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6258 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
6259 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
6262 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6266 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
6267 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
6268 dc424a06 2019-08-07 stsp free(path_content);
6269 2db2652d 2019-08-07 stsp free(ondisk_path);
6270 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
6271 0ebf8283 2019-07-24 stsp return err;
6274 0cb83759 2019-08-03 stsp const struct got_error *
6275 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
6276 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
6277 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
6278 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6279 1e71573e 2019-08-03 stsp struct got_repository *repo)
6281 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6282 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
6283 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6284 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
6285 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
6286 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
6287 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
6288 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
6290 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6292 0cb83759 2019-08-03 stsp return err;
6294 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
6295 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
6297 735ef5ac 2019-08-03 stsp goto done;
6298 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6300 735ef5ac 2019-08-03 stsp goto done;
6301 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6303 0cb83759 2019-08-03 stsp goto done;
6305 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
6306 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
6307 2db2652d 2019-08-07 stsp oka.worktree = worktree;
6308 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
6309 2db2652d 2019-08-07 stsp oka.repo = repo;
6310 2db2652d 2019-08-07 stsp oka.have_changes = 0;
6311 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6312 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6313 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
6315 735ef5ac 2019-08-03 stsp goto done;
6317 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
6318 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6319 2db2652d 2019-08-07 stsp goto done;
6322 2db2652d 2019-08-07 stsp spa.worktree = worktree;
6323 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
6324 2db2652d 2019-08-07 stsp spa.repo = repo;
6325 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
6326 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
6327 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
6328 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
6329 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
6330 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6331 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6332 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
6334 2db2652d 2019-08-07 stsp goto done;
6336 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
6337 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6338 7b5dc508 2019-10-28 stsp goto done;
6341 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6342 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
6343 0cb83759 2019-08-03 stsp err = sync_err;
6345 735ef5ac 2019-08-03 stsp if (head_ref)
6346 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
6347 735ef5ac 2019-08-03 stsp free(head_commit_id);
6348 0cb83759 2019-08-03 stsp free(fileindex_path);
6349 0cb83759 2019-08-03 stsp if (fileindex)
6350 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
6351 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6352 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
6353 0cb83759 2019-08-03 stsp err = unlockerr;
6354 0cb83759 2019-08-03 stsp return err;
6357 ad493afc 2019-08-03 stsp struct unstage_path_arg {
6358 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
6359 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
6360 ad493afc 2019-08-03 stsp struct got_repository *repo;
6361 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
6362 ad493afc 2019-08-03 stsp void *progress_arg;
6363 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
6364 2e1f37b0 2019-08-08 stsp void *patch_arg;
6367 2e1f37b0 2019-08-08 stsp static const struct got_error *
6368 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
6369 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
6370 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
6371 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
6372 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
6374 2e1f37b0 2019-08-08 stsp const struct got_error *err;
6375 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
6376 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6377 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6378 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
6379 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
6380 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
6381 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
6382 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
6383 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6384 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
6386 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6387 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6389 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
6391 2e1f37b0 2019-08-08 stsp return err;
6392 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6394 2e1f37b0 2019-08-08 stsp goto done;
6396 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6398 2e1f37b0 2019-08-08 stsp goto done;
6400 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6402 2e1f37b0 2019-08-08 stsp goto done;
6404 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6406 2e1f37b0 2019-08-08 stsp goto done;
6408 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6410 2e1f37b0 2019-08-08 stsp goto done;
6412 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6414 2e1f37b0 2019-08-08 stsp goto done;
6416 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
6417 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
6418 2e1f37b0 2019-08-08 stsp goto done;
6421 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
6422 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
6423 2e1f37b0 2019-08-08 stsp goto done;
6426 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
6427 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6429 2e1f37b0 2019-08-08 stsp goto done;
6431 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
6432 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
6434 2e1f37b0 2019-08-08 stsp goto done;
6435 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
6436 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
6438 2e1f37b0 2019-08-08 stsp goto done;
6440 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
6441 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
6442 2e1f37b0 2019-08-08 stsp goto done;
6444 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
6445 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
6446 2e1f37b0 2019-08-08 stsp goto done;
6448 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6449 2e1f37b0 2019-08-08 stsp int choice;
6450 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
6451 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
6452 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
6453 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
6455 2e1f37b0 2019-08-08 stsp goto done;
6456 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
6457 2e1f37b0 2019-08-08 stsp have_content = 1;
6459 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
6460 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
6463 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
6464 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6465 f1e81a05 2019-08-10 stsp outfile, rejectfile);
6467 2e1f37b0 2019-08-08 stsp free(label1);
6469 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
6470 2e1f37b0 2019-08-08 stsp if (staged_blob)
6471 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
6472 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
6473 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
6474 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
6475 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
6476 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
6477 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
6478 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6479 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
6480 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
6481 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
6482 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
6483 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
6484 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
6485 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
6486 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
6487 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6488 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
6489 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
6490 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6492 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
6493 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
6494 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
6495 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6496 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
6497 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
6498 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6500 2e1f37b0 2019-08-08 stsp free(args);
6502 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
6505 2e1f37b0 2019-08-08 stsp if (changes)
6506 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
6507 2e1f37b0 2019-08-08 stsp free(path1);
6508 2e1f37b0 2019-08-08 stsp free(path2);
6509 2e1f37b0 2019-08-08 stsp return err;
6512 ad493afc 2019-08-03 stsp static const struct got_error *
6513 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
6514 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
6515 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6516 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6518 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
6519 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
6520 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
6521 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6522 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
6523 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
6524 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
6525 ad493afc 2019-08-03 stsp int local_changes_subsumed;
6526 9bc94a15 2019-08-03 stsp struct stat sb;
6528 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
6529 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
6530 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
6531 2e1f37b0 2019-08-08 stsp return NULL;
6533 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6534 ad493afc 2019-08-03 stsp if (ie == NULL)
6535 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6537 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6539 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
6541 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
6542 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
6544 f69721c3 2019-10-21 stsp goto done;
6545 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6546 f69721c3 2019-10-21 stsp id_str) == -1) {
6547 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
6548 f69721c3 2019-10-21 stsp goto done;
6551 ad493afc 2019-08-03 stsp switch (staged_status) {
6552 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
6553 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
6554 ad493afc 2019-08-03 stsp blob_id, 8192);
6557 ad493afc 2019-08-03 stsp /* fall through */
6558 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
6559 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6560 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
6561 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6562 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6563 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6566 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
6569 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
6570 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
6571 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
6572 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
6573 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
6574 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
6576 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
6577 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
6578 2e1f37b0 2019-08-08 stsp &staged_blob_id,
6579 2e1f37b0 2019-08-08 stsp path_new_staged_content,
6583 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
6584 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
6585 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
6587 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
6588 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
6589 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
6590 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
6591 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
6592 f69721c3 2019-10-21 stsp a->progress_arg);
6593 2e1f37b0 2019-08-08 stsp if (err == NULL &&
6594 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
6595 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
6596 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
6597 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
6600 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
6601 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
6604 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
6605 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
6606 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6607 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
6608 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
6609 ad493afc 2019-08-03 stsp if (err == NULL)
6610 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6611 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6613 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
6614 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6615 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6616 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6617 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6620 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6622 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6623 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6627 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6628 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
6629 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
6632 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
6636 ad493afc 2019-08-03 stsp free(ondisk_path);
6637 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
6638 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
6639 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
6640 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
6641 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
6642 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
6643 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
6644 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
6645 ad493afc 2019-08-03 stsp if (blob_base)
6646 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
6647 ad493afc 2019-08-03 stsp if (blob_staged)
6648 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
6649 f69721c3 2019-10-21 stsp free(id_str);
6650 f69721c3 2019-10-21 stsp free(label_orig);
6651 ad493afc 2019-08-03 stsp return err;
6654 ad493afc 2019-08-03 stsp const struct got_error *
6655 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
6656 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
6657 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6658 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6659 ad493afc 2019-08-03 stsp struct got_repository *repo)
6661 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6662 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6663 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6664 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
6665 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
6667 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6669 ad493afc 2019-08-03 stsp return err;
6671 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6673 ad493afc 2019-08-03 stsp goto done;
6675 ad493afc 2019-08-03 stsp upa.worktree = worktree;
6676 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
6677 ad493afc 2019-08-03 stsp upa.repo = repo;
6678 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
6679 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
6680 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
6681 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
6682 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6683 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6684 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
6686 ad493afc 2019-08-03 stsp goto done;
6689 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6690 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
6691 ad493afc 2019-08-03 stsp err = sync_err;
6693 ad493afc 2019-08-03 stsp free(fileindex_path);
6694 ad493afc 2019-08-03 stsp if (fileindex)
6695 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
6696 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6697 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
6698 ad493afc 2019-08-03 stsp err = unlockerr;
6699 ad493afc 2019-08-03 stsp return err;