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 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
914 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
916 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
918 13d9040b 2019-03-27 stsp return err;
921 1ebedb77 2019-10-19 stsp static mode_t
922 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
924 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
926 1ebedb77 2019-10-19 stsp if (executable) {
927 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
928 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
929 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
930 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
931 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
932 1ebedb77 2019-10-19 stsp return st_mode | xbits;
935 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 13d9040b 2019-03-27 stsp static const struct got_error *
939 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
940 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
941 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
942 4b55f459 2019-09-08 stsp int reverting_versioned_file, struct got_repository *repo,
943 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
945 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
946 507dc3bb 2018-12-29 stsp int fd = -1;
947 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
948 507dc3bb 2018-12-29 stsp int update = 0;
949 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
951 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
952 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
953 9d31a1d8 2018-03-11 stsp if (fd == -1) {
954 21908da4 2019-01-13 stsp if (errno == ENOENT) {
955 21908da4 2019-01-13 stsp char *parent = dirname(path);
956 21908da4 2019-01-13 stsp if (parent == NULL)
957 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
958 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
960 21908da4 2019-01-13 stsp return err;
961 21908da4 2019-01-13 stsp fd = open(ondisk_path,
962 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
963 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
964 21908da4 2019-01-13 stsp if (fd == -1)
965 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
966 230a42bd 2019-05-11 jcs ondisk_path);
967 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
968 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
969 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
970 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
973 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
974 507dc3bb 2018-12-29 stsp ondisk_path);
977 507dc3bb 2018-12-29 stsp update = 1;
980 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
983 a378724f 2019-02-10 stsp if (restoring_missing_file)
984 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
985 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
986 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
988 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
989 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
993 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
995 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
996 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
999 9d31a1d8 2018-03-11 stsp if (len > 0) {
1000 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1001 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1002 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1003 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1004 b87c6f83 2018-12-24 stsp goto done;
1005 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1006 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1007 b87c6f83 2018-12-24 stsp goto done;
1009 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1011 9d31a1d8 2018-03-11 stsp } while (len != 0);
1013 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1014 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1015 816dc654 2019-02-16 stsp goto done;
1018 507dc3bb 2018-12-29 stsp if (update) {
1019 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1020 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1021 230a42bd 2019-05-11 jcs ondisk_path);
1022 2a57020b 2019-02-20 stsp unlink(tmppath);
1023 68ed9ba5 2019-02-10 stsp goto done;
1027 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1028 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1029 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1030 1ebedb77 2019-10-19 stsp goto done;
1034 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1035 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1036 507dc3bb 2018-12-29 stsp free(tmppath);
1037 6353ad76 2019-02-08 stsp return err;
1040 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1041 6353ad76 2019-02-08 stsp static const struct got_error *
1042 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1044 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1045 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1046 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1047 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1048 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1050 7154f6ce 2019-03-27 stsp int i = 0;
1051 7154f6ce 2019-03-27 stsp char *line;
1052 7154f6ce 2019-03-27 stsp size_t len;
1053 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1055 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1056 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1057 7154f6ce 2019-03-27 stsp if (line == NULL) {
1058 7154f6ce 2019-03-27 stsp if (feof(f))
1060 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1064 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1065 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1067 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1073 7154f6ce 2019-03-27 stsp return err;
1076 e2b1e152 2019-07-13 stsp static int
1077 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1079 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1080 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1083 1ebedb77 2019-10-19 stsp static int
1084 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1086 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1087 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1088 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1089 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1090 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1091 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1094 c363b2c1 2019-08-03 stsp static unsigned char
1095 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1097 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1098 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1099 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1100 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1101 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1102 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1103 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1105 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1109 7154f6ce 2019-03-27 stsp static const struct got_error *
1110 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1111 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1112 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1114 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1115 6353ad76 2019-02-08 stsp struct got_object_id id;
1116 6353ad76 2019-02-08 stsp size_t hdrlen;
1117 1338848f 2019-12-13 stsp int fd = -1;
1118 6353ad76 2019-02-08 stsp FILE *f = NULL;
1119 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1120 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1121 6353ad76 2019-02-08 stsp size_t flen, blen;
1122 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1124 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1127 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1128 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1129 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1131 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1132 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1133 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1134 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1135 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1137 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1138 882ef1b9 2019-12-13 stsp goto done;
1140 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1141 3d35a492 2019-12-13 stsp goto done;
1144 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1145 7f91a133 2019-12-13 stsp if (fd == -1 && errno != ENOENT)
1146 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1147 3d35a492 2019-12-13 stsp if (fd == -1 || fstat(fd, sb) == -1) {
1148 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1149 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1150 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1152 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1153 3d35a492 2019-12-13 stsp goto done;
1155 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1156 1338848f 2019-12-13 stsp goto done;
1160 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1161 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1162 1338848f 2019-12-13 stsp goto done;
1165 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1166 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1167 1338848f 2019-12-13 stsp goto done;
1168 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1169 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1170 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1171 1338848f 2019-12-13 stsp goto done;
1174 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1175 1338848f 2019-12-13 stsp goto done;
1177 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1178 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1179 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1181 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1183 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1185 1338848f 2019-12-13 stsp goto done;
1187 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1188 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1189 ab0d4361 2019-12-13 stsp if (fd == -1) {
1190 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1191 ab0d4361 2019-12-13 stsp goto done;
1195 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1196 6353ad76 2019-02-08 stsp if (f == NULL) {
1197 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1198 6353ad76 2019-02-08 stsp goto done;
1201 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1203 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1204 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1206 7154f6ce 2019-03-27 stsp goto done;
1207 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1208 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1209 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1211 7154f6ce 2019-03-27 stsp goto done;
1213 6353ad76 2019-02-08 stsp if (blen == 0) {
1214 6353ad76 2019-02-08 stsp if (flen != 0)
1215 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1217 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1218 6353ad76 2019-02-08 stsp if (blen != 0)
1219 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1221 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1222 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1223 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1224 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1228 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1231 6353ad76 2019-02-08 stsp hdrlen = 0;
1234 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1235 7154f6ce 2019-03-27 stsp rewind(f);
1236 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1237 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1238 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1241 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1242 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1243 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1244 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1245 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1246 9d31a1d8 2018-03-11 stsp return err;
1250 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1251 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1253 e2b1e152 2019-07-13 stsp static const struct got_error *
1254 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1255 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1257 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1258 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1259 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1261 e2b1e152 2019-07-13 stsp return NULL;
1264 9d31a1d8 2018-03-11 stsp static const struct got_error *
1265 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1266 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1267 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1268 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1269 0584f854 2019-04-06 stsp void *progress_arg)
1271 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1272 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1273 6353ad76 2019-02-08 stsp char *ondisk_path;
1274 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1275 b8f41171 2019-02-10 stsp struct stat sb;
1277 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1278 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1281 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1282 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1283 a76c42e6 2019-08-03 stsp goto done;
1285 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1288 abb4604f 2019-07-27 stsp goto done;
1289 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1290 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1292 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1294 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1295 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1296 b8f41171 2019-02-10 stsp goto done;
1299 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1300 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1301 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1302 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1303 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1304 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1306 e2b1e152 2019-07-13 stsp goto done;
1307 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1309 6353ad76 2019-02-08 stsp goto done;
1311 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1312 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1313 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1314 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1315 b8f41171 2019-02-10 stsp goto done;
1319 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1321 6353ad76 2019-02-08 stsp goto done;
1323 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1324 234035bc 2019-06-01 stsp int update_timestamps;
1325 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1326 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1327 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1328 234035bc 2019-06-01 stsp struct got_object_id id2;
1329 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1330 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1332 f69721c3 2019-10-21 stsp goto done;
1334 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1335 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1336 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1337 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1338 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1339 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1340 f69721c3 2019-10-21 stsp goto done;
1342 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1343 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1344 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1345 234035bc 2019-06-01 stsp goto done;
1348 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1349 f69721c3 2019-10-21 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1350 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1351 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1352 f69721c3 2019-10-21 stsp free(label_orig);
1353 234035bc 2019-06-01 stsp if (blob2)
1354 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1356 909d120e 2019-09-22 stsp goto done;
1358 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1359 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1360 234035bc 2019-06-01 stsp * unmodified files again.
1362 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1363 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1364 234035bc 2019-06-01 stsp update_timestamps);
1365 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1366 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1367 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1368 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1369 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1371 1ee397ad 2019-07-12 stsp goto done;
1372 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1373 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1375 13d9040b 2019-03-27 stsp goto done;
1377 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1378 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1379 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1381 13d9040b 2019-03-27 stsp goto done;
1382 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1383 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1385 13d9040b 2019-03-27 stsp goto done;
1387 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1389 6353ad76 2019-02-08 stsp free(ondisk_path);
1390 8da9e5f4 2019-01-12 stsp return err;
1393 90285c3b 2019-01-08 stsp static const struct got_error *
1394 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1396 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1397 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1399 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1400 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1402 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1403 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1404 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1406 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1407 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1408 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1409 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1410 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1414 90285c3b 2019-01-08 stsp parent = dirname(parent);
1417 90285c3b 2019-01-08 stsp free(ondisk_path);
1418 90285c3b 2019-01-08 stsp return err;
1421 708d8e67 2019-03-27 stsp static const struct got_error *
1422 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1423 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1424 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1427 708d8e67 2019-03-27 stsp unsigned char status;
1428 708d8e67 2019-03-27 stsp struct stat sb;
1429 708d8e67 2019-03-27 stsp char *ondisk_path;
1431 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1432 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1434 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1436 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1438 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1440 708d8e67 2019-03-27 stsp return err;
1442 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1443 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1444 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1446 1ee397ad 2019-07-12 stsp return err;
1448 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1449 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1451 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1454 708d8e67 2019-03-27 stsp return err;
1456 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1458 1ee397ad 2019-07-12 stsp return err;
1459 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1460 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1462 fc6346c4 2019-03-27 stsp return err;
1464 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1467 fc6346c4 2019-03-27 stsp return err;
1470 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1471 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1472 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1473 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1474 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1475 8da9e5f4 2019-01-12 stsp void *progress_arg;
1476 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
1477 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1480 512f0d0e 2019-01-02 stsp static const struct got_error *
1481 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1482 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1484 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1486 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1487 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1489 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1490 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1493 8da9e5f4 2019-01-12 stsp static const struct got_error *
1494 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1496 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1498 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1499 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1501 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1502 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1505 9d31a1d8 2018-03-11 stsp static const struct got_error *
1506 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1508 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1509 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1510 8da9e5f4 2019-01-12 stsp char *path;
1512 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1513 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1515 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
1516 63c5ca5d 2019-08-24 stsp return NULL;
1518 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1519 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1521 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1523 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1524 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1526 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1527 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1529 8da9e5f4 2019-01-12 stsp free(path);
1530 0cd1c46a 2019-03-11 stsp return err;
1533 818c7501 2019-07-11 stsp static const struct got_error *
1534 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1536 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1537 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1538 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1540 517bab73 2019-03-11 stsp *refname = NULL;
1542 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1543 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1544 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
1546 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1548 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1549 0647c563 2019-03-11 stsp *refname = NULL;
1551 517bab73 2019-03-11 stsp free(uuidstr);
1552 517bab73 2019-03-11 stsp return err;
1555 818c7501 2019-07-11 stsp const struct got_error *
1556 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1558 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1561 818c7501 2019-07-11 stsp static const struct got_error *
1562 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1564 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1565 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1568 818c7501 2019-07-11 stsp static const struct got_error *
1569 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1571 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1574 818c7501 2019-07-11 stsp static const struct got_error *
1575 818c7501 2019-07-11 stsp get_rebase_branch_symref_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_BRANCH_REF_PREFIX);
1581 818c7501 2019-07-11 stsp static const struct got_error *
1582 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1584 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1585 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1588 0ebf8283 2019-07-24 stsp static const struct got_error *
1589 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1591 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1592 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1595 0ebf8283 2019-07-24 stsp static const struct got_error *
1596 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1598 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1599 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1602 0ebf8283 2019-07-24 stsp static const struct got_error *
1603 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1605 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1606 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1609 0ebf8283 2019-07-24 stsp static const struct got_error *
1610 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1612 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1613 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1616 0ebf8283 2019-07-24 stsp const struct got_error *
1617 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1618 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1620 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1621 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1622 0ebf8283 2019-07-24 stsp *path = NULL;
1623 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1625 0ebf8283 2019-07-24 stsp return NULL;
1629 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1630 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1632 517bab73 2019-03-11 stsp static const struct got_error *
1633 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1635 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1636 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1637 517bab73 2019-03-11 stsp char *refname;
1639 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1641 517bab73 2019-03-11 stsp return err;
1643 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1645 0cd1c46a 2019-03-11 stsp goto done;
1647 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1649 0cd1c46a 2019-03-11 stsp free(refname);
1651 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1652 3e3a69f1 2019-07-25 stsp return err;
1655 3e3a69f1 2019-07-25 stsp static const struct got_error *
1656 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1658 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1660 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1661 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1662 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1663 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1665 9d31a1d8 2018-03-11 stsp return err;
1669 ebf99748 2019-05-09 stsp static const struct got_error *
1670 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1671 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
1673 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1674 ebf99748 2019-05-09 stsp FILE *index = NULL;
1676 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1677 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1678 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1679 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1681 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1683 ebf99748 2019-05-09 stsp goto done;
1685 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1686 ebf99748 2019-05-09 stsp if (index == NULL) {
1687 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1688 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1690 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1691 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1695 ebf99748 2019-05-09 stsp if (err) {
1696 ebf99748 2019-05-09 stsp free(*fileindex_path);
1697 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1698 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1699 ebf99748 2019-05-09 stsp *fileindex = NULL;
1701 ebf99748 2019-05-09 stsp return err;
1704 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1705 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1706 c932eeeb 2019-05-22 stsp const char *path;
1707 c932eeeb 2019-05-22 stsp size_t path_len;
1708 c932eeeb 2019-05-22 stsp const char *entry_name;
1709 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1710 a484d721 2019-06-10 stsp void *progress_arg;
1713 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1714 c932eeeb 2019-05-22 stsp static const struct got_error *
1715 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1717 1ee397ad 2019-07-12 stsp const struct got_error *err;
1718 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1720 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1721 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1722 c932eeeb 2019-05-22 stsp return NULL;
1723 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1724 102ff934 2019-06-11 stsp return NULL;
1726 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1727 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1728 c932eeeb 2019-05-22 stsp return NULL;
1730 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1731 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1732 edd02c5e 2019-07-11 stsp ie->path);
1734 1ee397ad 2019-07-12 stsp return err;
1736 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1737 c932eeeb 2019-05-22 stsp return NULL;
1740 9c6338c4 2019-06-02 stsp static const struct got_error *
1741 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1743 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1744 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1745 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1746 867630bb 2020-01-17 stsp struct timespec timeout;
1748 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1749 9c6338c4 2019-06-02 stsp fileindex_path);
1751 9c6338c4 2019-06-02 stsp goto done;
1753 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1755 9c6338c4 2019-06-02 stsp goto done;
1757 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1758 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1759 9c6338c4 2019-06-02 stsp fileindex_path);
1760 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1764 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
1765 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
1766 867630bb 2020-01-17 stsp * was recorded in the file index.
1768 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
1769 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
1770 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
1772 9c6338c4 2019-06-02 stsp if (new_index)
1773 9c6338c4 2019-06-02 stsp fclose(new_index);
1774 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1775 9c6338c4 2019-06-02 stsp return err;
1778 6ced4176 2019-07-12 stsp static const struct got_error *
1779 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1780 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1781 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1783 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1784 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1785 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1786 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1788 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1789 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1790 6ced4176 2019-07-12 stsp *tree_id = NULL;
1792 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1793 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1794 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1795 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1796 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1797 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1798 6ced4176 2019-07-12 stsp goto done;
1800 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1801 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1803 6ced4176 2019-07-12 stsp goto done;
1804 6ced4176 2019-07-12 stsp return NULL;
1807 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1809 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1810 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1811 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1812 6ced4176 2019-07-12 stsp goto done;
1815 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1816 6ced4176 2019-07-12 stsp in_repo_path);
1818 6ced4176 2019-07-12 stsp goto done;
1820 6ced4176 2019-07-12 stsp free(in_repo_path);
1821 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1823 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1825 6ced4176 2019-07-12 stsp goto done;
1827 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1828 6ced4176 2019-07-12 stsp /* Check out a single file. */
1829 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1830 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1831 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1832 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1833 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1834 6ced4176 2019-07-12 stsp goto done;
1836 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1837 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1838 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1839 6ced4176 2019-07-12 stsp goto done;
1842 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1843 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1845 6ced4176 2019-07-12 stsp return err;
1846 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1847 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1848 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1849 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1850 6ced4176 2019-07-12 stsp goto done;
1853 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1854 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1856 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1857 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1858 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1859 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1860 6ced4176 2019-07-12 stsp goto done;
1862 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1863 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1864 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1865 6ced4176 2019-07-12 stsp goto done;
1870 6ced4176 2019-07-12 stsp free(in_repo_path);
1871 6ced4176 2019-07-12 stsp if (err) {
1872 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1873 6ced4176 2019-07-12 stsp free(*tree_relpath);
1874 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1875 6ced4176 2019-07-12 stsp free(*tree_id);
1876 6ced4176 2019-07-12 stsp *tree_id = NULL;
1878 07ed472d 2019-07-12 stsp return err;
1881 07ed472d 2019-07-12 stsp static const struct got_error *
1882 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1883 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1884 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1885 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1887 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1888 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1889 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1890 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1891 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1893 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1894 7f47418f 2019-12-20 stsp if (err) {
1895 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
1896 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
1897 7f47418f 2019-12-20 stsp goto done;
1898 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
1899 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1901 7f47418f 2019-12-20 stsp return err;
1904 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1905 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1907 07ed472d 2019-07-12 stsp goto done;
1909 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1911 07ed472d 2019-07-12 stsp goto done;
1913 07ed472d 2019-07-12 stsp if (entry_name &&
1914 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1915 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1916 07ed472d 2019-07-12 stsp goto done;
1919 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1920 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1921 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1922 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1923 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1924 07ed472d 2019-07-12 stsp arg.repo = repo;
1925 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1926 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1927 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1928 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1929 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1930 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1933 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1934 07ed472d 2019-07-12 stsp if (commit)
1935 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1936 6ced4176 2019-07-12 stsp return err;
1939 9d31a1d8 2018-03-11 stsp const struct got_error *
1940 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1941 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1942 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1943 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1945 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1946 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1947 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1948 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1949 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1950 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1951 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1952 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1953 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1954 f2ea84fa 2019-07-27 stsp int entry_type;
1955 f2ea84fa 2019-07-27 stsp char *relpath;
1956 f2ea84fa 2019-07-27 stsp char *entry_name;
1957 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1958 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1960 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1962 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1964 9d31a1d8 2018-03-11 stsp return err;
1966 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1967 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1968 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1969 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1970 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1971 f2ea84fa 2019-07-27 stsp goto done;
1974 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1975 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1976 f2ea84fa 2019-07-27 stsp if (err) {
1977 f2ea84fa 2019-07-27 stsp free(tpd);
1978 6ced4176 2019-07-12 stsp goto done;
1981 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1982 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1983 f2ea84fa 2019-07-27 stsp if (err) {
1984 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1985 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1986 f2ea84fa 2019-07-27 stsp free(tpd);
1987 f2ea84fa 2019-07-27 stsp goto done;
1990 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1992 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1996 51514078 2018-12-25 stsp * Read the file index.
1997 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1998 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2000 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2002 c4cdcb68 2019-04-03 stsp goto done;
2004 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2005 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2006 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2008 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2009 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2010 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2014 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2015 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2016 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2017 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2018 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2019 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2020 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2021 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2025 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2027 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2028 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2029 af12c6b9 2019-06-04 stsp err = sync_err;
2031 9c6338c4 2019-06-02 stsp free(fileindex_path);
2033 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2034 9d31a1d8 2018-03-11 stsp if (commit)
2035 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2036 5ade8233 2019-07-12 stsp if (fileindex)
2037 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2038 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2039 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2040 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2041 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2042 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2043 f2ea84fa 2019-07-27 stsp free(tpd);
2045 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2046 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2047 9d31a1d8 2018-03-11 stsp err = unlockerr;
2048 f8d1f275 2019-02-04 stsp return err;
2051 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2052 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2053 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2054 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2055 234035bc 2019-06-01 stsp void *progress_arg;
2056 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2057 234035bc 2019-06-01 stsp void *cancel_arg;
2058 f69721c3 2019-10-21 stsp const char *label_orig;
2059 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2062 234035bc 2019-06-01 stsp static const struct got_error *
2063 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2064 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2065 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2066 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2068 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2069 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2070 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2071 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2072 234035bc 2019-06-01 stsp struct stat sb;
2073 234035bc 2019-06-01 stsp unsigned char status;
2074 234035bc 2019-06-01 stsp int local_changes_subsumed;
2076 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2077 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2078 d6c87207 2019-08-02 stsp strlen(path2));
2079 1ee397ad 2019-07-12 stsp if (ie == NULL)
2080 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2081 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2083 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2084 234035bc 2019-06-01 stsp path2) == -1)
2085 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2087 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2090 234035bc 2019-06-01 stsp goto done;
2092 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2093 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2094 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2095 234035bc 2019-06-01 stsp goto done;
2097 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2098 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2099 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2100 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2101 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2102 234035bc 2019-06-01 stsp goto done;
2105 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2106 f69721c3 2019-10-21 stsp ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2107 f69721c3 2019-10-21 stsp a->commit_id2, repo, a->progress_cb, a->progress_arg);
2108 234035bc 2019-06-01 stsp } else if (blob1) {
2109 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2110 d6c87207 2019-08-02 stsp strlen(path1));
2111 1ee397ad 2019-07-12 stsp if (ie == NULL)
2112 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2113 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2115 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2116 2b92fad7 2019-06-02 stsp path1) == -1)
2117 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2119 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2122 2b92fad7 2019-06-02 stsp goto done;
2124 2b92fad7 2019-06-02 stsp switch (status) {
2125 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2126 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2127 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2129 1ee397ad 2019-07-12 stsp goto done;
2130 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2132 2b92fad7 2019-06-02 stsp goto done;
2134 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2136 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2137 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2138 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2139 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2141 1ee397ad 2019-07-12 stsp goto done;
2143 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2145 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2146 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2147 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2148 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2149 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2151 1ee397ad 2019-07-12 stsp goto done;
2153 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2154 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2156 1ee397ad 2019-07-12 stsp goto done;
2161 234035bc 2019-06-01 stsp } else if (blob2) {
2162 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2163 234035bc 2019-06-01 stsp path2) == -1)
2164 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2165 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2166 d6c87207 2019-08-02 stsp strlen(path2));
2168 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2169 7f91a133 2019-12-13 stsp -1, NULL, repo);
2171 234035bc 2019-06-01 stsp goto done;
2172 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2173 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2174 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2175 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2176 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2177 1ee397ad 2019-07-12 stsp status, path2);
2178 234035bc 2019-06-01 stsp goto done;
2180 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2181 f69721c3 2019-10-21 stsp NULL, ondisk_path, path2, sb.st_mode,
2182 f69721c3 2019-10-21 stsp a->label_orig, blob2, a->commit_id2, repo,
2183 f69721c3 2019-10-21 stsp a->progress_cb,
2184 f69721c3 2019-10-21 stsp a->progress_arg);
2185 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2186 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2187 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2188 234035bc 2019-06-01 stsp blob2, 0);
2190 234035bc 2019-06-01 stsp goto done;
2193 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2194 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2195 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2196 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2197 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2198 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2200 234035bc 2019-06-01 stsp goto done;
2201 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2202 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2204 234035bc 2019-06-01 stsp goto done;
2205 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2206 2b92fad7 2019-06-02 stsp if (err) {
2207 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2208 2b92fad7 2019-06-02 stsp goto done;
2213 234035bc 2019-06-01 stsp free(ondisk_path);
2214 234035bc 2019-06-01 stsp return err;
2217 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2218 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2219 234035bc 2019-06-01 stsp struct got_repository *repo;
2222 234035bc 2019-06-01 stsp static const struct got_error *
2223 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2225 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2226 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2227 234035bc 2019-06-01 stsp unsigned char status;
2228 234035bc 2019-06-01 stsp struct stat sb;
2229 234035bc 2019-06-01 stsp char *ondisk_path;
2231 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2232 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2233 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2234 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2236 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2238 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2240 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2241 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2243 234035bc 2019-06-01 stsp return err;
2244 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2245 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2247 234035bc 2019-06-01 stsp return NULL;
2250 818c7501 2019-07-11 stsp static const struct got_error *
2251 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2252 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2253 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2254 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2255 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2257 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2258 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2259 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2260 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2261 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2263 03415a1a 2019-06-02 stsp if (commit_id1) {
2264 f69721c3 2019-10-21 stsp char *id_str;
2266 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2267 03415a1a 2019-06-02 stsp worktree->path_prefix);
2269 03415a1a 2019-06-02 stsp goto done;
2271 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2273 03415a1a 2019-06-02 stsp goto done;
2275 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2277 f69721c3 2019-10-21 stsp goto done;
2279 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2280 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2281 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2282 f69721c3 2019-10-21 stsp free(id_str);
2283 f69721c3 2019-10-21 stsp goto done;
2285 f69721c3 2019-10-21 stsp free(id_str);
2288 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2289 234035bc 2019-06-01 stsp worktree->path_prefix);
2291 234035bc 2019-06-01 stsp goto done;
2293 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2295 234035bc 2019-06-01 stsp goto done;
2297 234035bc 2019-06-01 stsp arg.worktree = worktree;
2298 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2299 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2300 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2301 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2302 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2303 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2304 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2305 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2306 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2307 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2308 af12c6b9 2019-06-04 stsp err = sync_err;
2310 234035bc 2019-06-01 stsp if (tree1)
2311 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2312 234035bc 2019-06-01 stsp if (tree2)
2313 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2314 f69721c3 2019-10-21 stsp free(label_orig);
2315 818c7501 2019-07-11 stsp return err;
2318 818c7501 2019-07-11 stsp const struct got_error *
2319 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2320 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2321 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2322 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2324 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2325 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2326 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2327 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2329 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2331 818c7501 2019-07-11 stsp return err;
2333 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2335 818c7501 2019-07-11 stsp goto done;
2337 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2338 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2339 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2340 818c7501 2019-07-11 stsp &mok_arg);
2342 818c7501 2019-07-11 stsp goto done;
2344 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2345 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2347 818c7501 2019-07-11 stsp if (fileindex)
2348 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2349 818c7501 2019-07-11 stsp free(fileindex_path);
2350 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2351 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2352 234035bc 2019-06-01 stsp err = unlockerr;
2353 234035bc 2019-06-01 stsp return err;
2356 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2357 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2358 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2359 927df6b7 2019-02-10 stsp const char *status_path;
2360 927df6b7 2019-02-10 stsp size_t status_path_len;
2361 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2362 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2363 f8d1f275 2019-02-04 stsp void *status_arg;
2364 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2365 f8d1f275 2019-02-04 stsp void *cancel_arg;
2366 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2367 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2368 f2a9dc41 2019-12-13 tracey int report_unchanged;
2371 f8d1f275 2019-02-04 stsp static const struct got_error *
2372 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2373 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2374 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2375 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2377 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2378 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2379 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2380 927df6b7 2019-02-10 stsp struct stat sb;
2381 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2382 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2383 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2385 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2387 98eaaa12 2019-08-03 stsp return err;
2389 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2390 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2391 98eaaa12 2019-08-03 stsp return NULL;
2393 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2394 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2395 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2397 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2398 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2399 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2401 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2402 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2403 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2404 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2405 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2408 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2409 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2412 927df6b7 2019-02-10 stsp static const struct got_error *
2413 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2414 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
2416 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2417 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2418 f8d1f275 2019-02-04 stsp char *abspath;
2420 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2421 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2423 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2424 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2425 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2426 927df6b7 2019-02-10 stsp return NULL;
2428 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2429 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2430 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2431 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2433 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2434 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2435 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2438 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
2439 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2440 f8d1f275 2019-02-04 stsp free(abspath);
2441 9d31a1d8 2018-03-11 stsp return err;
2444 f8d1f275 2019-02-04 stsp static const struct got_error *
2445 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2447 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2448 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2449 2ec1f75b 2019-03-26 stsp unsigned char status;
2451 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2452 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2454 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2455 927df6b7 2019-02-10 stsp return NULL;
2457 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2458 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2459 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2460 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2462 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2463 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2464 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2468 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
2470 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2472 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
2473 6841da00 2019-08-08 stsp free((char *)pe->path);
2474 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
2478 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
2480 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2482 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
2483 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2484 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2485 6841da00 2019-08-08 stsp free((char *)pe->path);
2487 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
2490 6841da00 2019-08-08 stsp static const struct got_error *
2491 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2493 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2494 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
2495 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
2496 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
2497 6841da00 2019-08-08 stsp size_t linesize = 0;
2498 6841da00 2019-08-08 stsp ssize_t linelen;
2500 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
2501 6841da00 2019-08-08 stsp if (ignorelist == NULL)
2502 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
2503 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
2505 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2506 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
2507 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
2509 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
2510 bd8de430 2019-10-04 stsp if (line[0] == '#')
2513 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
2514 bd8de430 2019-10-04 stsp if (line[0] == '!')
2517 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2518 6841da00 2019-08-08 stsp line) == -1) {
2519 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
2520 6841da00 2019-08-08 stsp goto done;
2522 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2524 6841da00 2019-08-08 stsp goto done;
2526 6841da00 2019-08-08 stsp if (ferror(f)) {
2527 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
2528 6841da00 2019-08-08 stsp goto done;
2531 6841da00 2019-08-08 stsp dirpath = strdup(path);
2532 6841da00 2019-08-08 stsp if (dirpath == NULL) {
2533 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
2534 6841da00 2019-08-08 stsp goto done;
2536 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2538 6841da00 2019-08-08 stsp free(line);
2539 6841da00 2019-08-08 stsp if (err || pe == NULL) {
2540 6841da00 2019-08-08 stsp free(dirpath);
2541 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2543 6841da00 2019-08-08 stsp return err;
2547 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
2549 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2551 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
2552 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
2553 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
2554 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
2556 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2557 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
2559 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
2561 bd8de430 2019-10-04 stsp pattern += 3;
2563 bd8de430 2019-10-04 stsp while (*p) {
2564 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
2565 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
2566 bd8de430 2019-10-04 stsp /* Retry in next directory. */
2567 bd8de430 2019-10-04 stsp while (*p && *p != '/')
2569 bd8de430 2019-10-04 stsp while (*p == '/')
2579 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
2580 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
2581 6841da00 2019-08-08 stsp * ignores backwards.
2583 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
2584 6841da00 2019-08-08 stsp while (pe) {
2585 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
2586 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2587 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
2588 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2589 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
2590 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
2591 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
2592 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
2593 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
2598 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2604 6841da00 2019-08-08 stsp static const struct got_error *
2605 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2606 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
2608 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2609 6841da00 2019-08-08 stsp char *ignorespath;
2610 886cec17 2019-12-15 stsp int fd = -1;
2611 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
2613 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2614 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
2615 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
2617 886cec17 2019-12-15 stsp if (dirfd != -1) {
2618 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2619 886cec17 2019-12-15 stsp if (fd == -1) {
2620 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2621 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
2622 886cec17 2019-12-15 stsp ignorespath);
2624 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
2625 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
2626 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
2627 886cec17 2019-12-15 stsp ignorespath);
2630 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2634 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
2635 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
2636 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2637 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
2638 886cec17 2019-12-15 stsp ignorespath);
2640 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2643 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2644 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
2645 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2646 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
2647 6841da00 2019-08-08 stsp free(ignorespath);
2648 6841da00 2019-08-08 stsp return err;
2651 f8d1f275 2019-02-04 stsp static const struct got_error *
2652 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2654 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2655 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2656 f8d1f275 2019-02-04 stsp char *path = NULL;
2658 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2659 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2661 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2662 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2663 927df6b7 2019-02-10 stsp return NULL;
2665 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2666 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2667 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2669 f8d1f275 2019-02-04 stsp path = de->d_name;
2672 bd8de430 2019-10-04 stsp if (de->d_type == DT_DIR) {
2673 32d2fb48 2019-12-15 stsp int subdirfd = openat(dirfd, de->d_name,
2674 886cec17 2019-12-15 stsp O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2675 32d2fb48 2019-12-15 stsp if (subdirfd == -1) {
2676 32d2fb48 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2677 32d2fb48 2019-12-15 stsp err = got_error_from_errno2("openat", path);
2679 bd8de430 2019-10-04 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
2680 886cec17 2019-12-15 stsp path, subdirfd, ".cvsignore");
2681 886cec17 2019-12-15 stsp if (err == NULL)
2682 886cec17 2019-12-15 stsp err = add_ignores(&a->ignores,
2683 886cec17 2019-12-15 stsp a->worktree->root_path, path,
2684 886cec17 2019-12-15 stsp subdirfd, ".gitignore");
2685 886cec17 2019-12-15 stsp if (close(subdirfd) == -1 && err == NULL)
2686 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
2688 886cec17 2019-12-15 stsp } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2689 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
2690 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2691 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2692 f8d1f275 2019-02-04 stsp if (parent_path[0])
2693 f8d1f275 2019-02-04 stsp free(path);
2694 b72f483a 2019-02-05 stsp return err;
2697 347d1d3e 2019-07-12 stsp static const struct got_error *
2698 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2699 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2700 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
2702 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2703 abb4604f 2019-07-27 stsp struct stat sb;
2705 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2707 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
2708 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
2710 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2711 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2712 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2713 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2714 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2715 abb4604f 2019-07-27 stsp return NULL;
2718 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2719 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2720 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2722 abb4604f 2019-07-27 stsp return NULL;
2725 abb4604f 2019-07-27 stsp static const struct got_error *
2726 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2727 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2728 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2729 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2730 f2a9dc41 2019-12-13 tracey int report_unchanged)
2732 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2733 6fc93f37 2019-12-13 stsp int fd = -1;
2734 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2735 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2736 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2738 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2739 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2740 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2742 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2743 6fc93f37 2019-12-13 stsp if (fd == -1) {
2744 40b289d7 2019-09-07 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2745 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
2747 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2748 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
2749 f2a9dc41 2019-12-13 tracey report_unchanged);
2751 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2752 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2753 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2754 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2755 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2756 abb4604f 2019-07-27 stsp arg.status_path = path;
2757 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2758 abb4604f 2019-07-27 stsp arg.repo = repo;
2759 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2760 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2761 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2762 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2763 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
2764 6841da00 2019-08-08 stsp TAILQ_INIT(&arg.ignores);
2765 022fae89 2019-12-06 tracey if (!no_ignores) {
2766 bd8de430 2019-10-04 stsp err = add_ignores(&arg.ignores, worktree->root_path,
2767 886cec17 2019-12-15 stsp path, fd, ".cvsignore");
2768 022fae89 2019-12-06 tracey if (err == NULL)
2769 022fae89 2019-12-06 tracey err = add_ignores(&arg.ignores,
2770 886cec17 2019-12-15 stsp worktree->root_path, path, fd,
2771 886cec17 2019-12-15 stsp ".gitignore");
2773 bd8de430 2019-10-04 stsp if (err == NULL)
2774 6fc93f37 2019-12-13 stsp err = got_fileindex_diff_dir(fileindex, fd,
2775 6841da00 2019-08-08 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2776 6841da00 2019-08-08 stsp free_ignores(&arg.ignores);
2779 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
2780 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
2781 927df6b7 2019-02-10 stsp free(ondisk_path);
2782 347d1d3e 2019-07-12 stsp return err;
2785 347d1d3e 2019-07-12 stsp const struct got_error *
2786 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2787 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2788 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2789 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2791 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2792 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2793 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2794 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2796 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2798 347d1d3e 2019-07-12 stsp return err;
2800 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2801 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2802 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2806 f8d1f275 2019-02-04 stsp free(fileindex_path);
2807 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2808 f8d1f275 2019-02-04 stsp return err;
2811 6c7ab921 2019-03-18 stsp const struct got_error *
2812 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2813 6c7ab921 2019-03-18 stsp const char *arg)
2815 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2816 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2817 6c7ab921 2019-03-18 stsp size_t len;
2819 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2821 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2822 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2823 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2824 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2825 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2826 d0710d08 2019-07-22 stsp if (cwd == NULL)
2827 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2828 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2829 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2830 d0710d08 2019-07-22 stsp goto done;
2834 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2835 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2836 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2837 6c7ab921 2019-03-18 stsp goto done;
2840 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2841 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2842 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2844 f6d88e1a 2019-05-29 stsp goto done;
2846 f6d88e1a 2019-05-29 stsp path = strdup("");
2847 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2848 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2849 f6d88e1a 2019-05-29 stsp goto done;
2853 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2854 6c7ab921 2019-03-18 stsp len = strlen(path);
2855 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2856 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2860 6c7ab921 2019-03-18 stsp free(resolved);
2861 d0710d08 2019-07-22 stsp free(cwd);
2862 6c7ab921 2019-03-18 stsp if (err == NULL)
2863 6c7ab921 2019-03-18 stsp *wt_path = path;
2865 6c7ab921 2019-03-18 stsp free(path);
2866 d00136be 2019-03-26 stsp return err;
2869 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
2870 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
2871 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
2872 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
2873 4e68cba3 2019-11-23 stsp void *progress_arg;
2874 4e68cba3 2019-11-23 stsp struct got_repository *repo;
2877 4e68cba3 2019-11-23 stsp static const struct got_error *
2878 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2879 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
2880 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2881 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2883 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
2884 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2885 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2886 6d022e97 2019-08-04 stsp struct stat sb;
2887 dbb83fbd 2019-12-12 stsp char *ondisk_path;
2889 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 dbb83fbd 2019-12-12 stsp relpath) == -1)
2891 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
2893 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2895 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2896 12463d8b 2019-12-13 stsp de_name, a->repo);
2898 dbb83fbd 2019-12-12 stsp goto done;
2899 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
2900 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
2901 dbb83fbd 2019-12-12 stsp goto done;
2902 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2904 dbb83fbd 2019-12-12 stsp goto done;
2907 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
2908 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2909 dbb83fbd 2019-12-12 stsp goto done;
2912 dbb83fbd 2019-12-12 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2914 4e68cba3 2019-11-23 stsp goto done;
2916 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2917 07f5b47a 2019-06-02 stsp if (err) {
2918 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2919 4e68cba3 2019-11-23 stsp goto done;
2922 dbb83fbd 2019-12-12 stsp free(ondisk_path);
2924 dbb83fbd 2019-12-12 stsp return err;
2925 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
2926 dbb83fbd 2019-12-12 stsp return NULL;
2927 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2930 d00136be 2019-03-26 stsp const struct got_error *
2931 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2932 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
2933 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2934 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
2936 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2937 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2938 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2939 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2940 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
2942 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2944 d00136be 2019-03-26 stsp return err;
2946 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2948 d00136be 2019-03-26 stsp goto done;
2950 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
2951 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
2952 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
2953 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
2954 4e68cba3 2019-11-23 stsp saa.repo = repo;
2956 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2957 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2958 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2962 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2963 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2964 af12c6b9 2019-06-04 stsp err = sync_err;
2966 fb399478 2019-07-12 stsp free(fileindex_path);
2967 d00136be 2019-03-26 stsp if (fileindex)
2968 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2969 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2970 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2971 d00136be 2019-03-26 stsp err = unlockerr;
2972 6c7ab921 2019-03-18 stsp return err;
2975 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
2976 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
2977 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
2978 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
2979 f2a9dc41 2019-12-13 tracey void *progress_arg;
2980 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
2981 f2a9dc41 2019-12-13 tracey int delete_local_mods;
2982 70e3e7f5 2019-12-13 tracey int keep_on_disk;
2985 f2a9dc41 2019-12-13 tracey static const struct got_error *
2986 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
2987 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
2988 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2989 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
2991 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
2992 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2993 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2994 17ed4618 2019-06-02 stsp struct stat sb;
2995 f2a9dc41 2019-12-13 tracey char *ondisk_path;
2997 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2998 17ed4618 2019-06-02 stsp if (ie == NULL)
2999 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
3001 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3002 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3003 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3004 9acbc4fa 2019-08-03 stsp return NULL;
3005 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3008 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3009 f2a9dc41 2019-12-13 tracey relpath) == -1)
3010 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3012 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3015 f2a9dc41 2019-12-13 tracey goto done;
3017 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3018 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3019 f2a9dc41 2019-12-13 tracey goto done;
3020 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3021 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3022 f2a9dc41 2019-12-13 tracey goto done;
3024 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3025 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3026 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3027 f2a9dc41 2019-12-13 tracey goto done;
3031 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3032 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3033 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3034 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3035 12463d8b 2019-12-13 stsp ondisk_path);
3036 12463d8b 2019-12-13 stsp goto done;
3038 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3039 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3040 12463d8b 2019-12-13 stsp goto done;
3044 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3046 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3047 f2a9dc41 2019-12-13 tracey if (err)
3048 f2a9dc41 2019-12-13 tracey return err;
3049 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3050 f2a9dc41 2019-12-13 tracey return NULL;
3051 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3052 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3055 2ec1f75b 2019-03-26 stsp const struct got_error *
3056 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3057 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3058 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3059 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3061 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3062 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3063 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3064 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3065 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3067 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3069 2ec1f75b 2019-03-26 stsp return err;
3071 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3073 2ec1f75b 2019-03-26 stsp goto done;
3075 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3076 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3077 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3078 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3079 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3080 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3081 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3083 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3084 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3085 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3089 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3090 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3091 af12c6b9 2019-06-04 stsp err = sync_err;
3093 fb399478 2019-07-12 stsp free(fileindex_path);
3094 2ec1f75b 2019-03-26 stsp if (fileindex)
3095 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3096 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3097 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3098 2ec1f75b 2019-03-26 stsp err = unlockerr;
3099 33aa809d 2019-08-08 stsp return err;
3102 33aa809d 2019-08-08 stsp static const struct got_error *
3103 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3105 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3106 33aa809d 2019-08-08 stsp char *line = NULL;
3107 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3108 33aa809d 2019-08-08 stsp ssize_t linelen;
3110 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3111 33aa809d 2019-08-08 stsp if (linelen == -1) {
3112 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3113 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3114 33aa809d 2019-08-08 stsp goto done;
3116 33aa809d 2019-08-08 stsp return NULL;
3118 33aa809d 2019-08-08 stsp if (outfile) {
3119 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3120 33aa809d 2019-08-08 stsp if (n != linelen) {
3121 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3122 33aa809d 2019-08-08 stsp goto done;
3125 33aa809d 2019-08-08 stsp if (rejectfile) {
3126 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3127 33aa809d 2019-08-08 stsp if (n != linelen)
3128 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3131 33aa809d 2019-08-08 stsp free(line);
3132 2ec1f75b 2019-03-26 stsp return err;
3135 33aa809d 2019-08-08 stsp static const struct got_error *
3136 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3138 33aa809d 2019-08-08 stsp char *line = NULL;
3139 33aa809d 2019-08-08 stsp size_t linesize = 0;
3140 33aa809d 2019-08-08 stsp ssize_t linelen;
3142 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3143 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3144 500467ff 2019-09-25 hiltjo if (ferror(f))
3145 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3146 500467ff 2019-09-25 hiltjo return NULL;
3148 33aa809d 2019-08-08 stsp free(line);
3149 33aa809d 2019-08-08 stsp return NULL;
3152 33aa809d 2019-08-08 stsp static const struct got_error *
3153 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3154 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3155 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3157 33aa809d 2019-08-08 stsp const struct got_error *err;
3159 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3160 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3161 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3163 33aa809d 2019-08-08 stsp return err;
3164 33aa809d 2019-08-08 stsp (*line_cur1)++;
3166 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3167 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3168 33aa809d 2019-08-08 stsp if (rejectfile)
3169 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3171 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3173 33aa809d 2019-08-08 stsp return err;
3174 33aa809d 2019-08-08 stsp (*line_cur2)++;
3176 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3177 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3178 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3180 33aa809d 2019-08-08 stsp return err;
3181 33aa809d 2019-08-08 stsp (*line_cur2)++;
3183 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3184 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3185 33aa809d 2019-08-08 stsp if (rejectfile)
3186 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3188 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3190 33aa809d 2019-08-08 stsp return err;
3191 33aa809d 2019-08-08 stsp (*line_cur1)++;
3194 f1e81a05 2019-08-10 stsp return NULL;
3197 f1e81a05 2019-08-10 stsp static const struct got_error *
3198 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3199 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3201 f1e81a05 2019-08-10 stsp const struct got_error *err;
3203 f1e81a05 2019-08-10 stsp if (outfile) {
3204 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3205 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3206 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3208 f1e81a05 2019-08-10 stsp return err;
3209 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3212 f1e81a05 2019-08-10 stsp if (rejectfile) {
3213 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3214 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3215 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3217 f1e81a05 2019-08-10 stsp return err;
3218 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3222 33aa809d 2019-08-08 stsp return NULL;
3225 33aa809d 2019-08-08 stsp static const struct got_error *
3226 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3227 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3228 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3229 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3230 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3232 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3233 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3234 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3235 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3236 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3237 33aa809d 2019-08-08 stsp long pos1, pos2;
3238 33aa809d 2019-08-08 stsp FILE *hunkfile;
3240 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3242 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3243 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3244 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3246 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3247 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3249 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3250 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3252 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3253 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3254 33aa809d 2019-08-08 stsp goto done;
3256 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3257 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3258 33aa809d 2019-08-08 stsp goto done;
3260 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3261 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3262 33aa809d 2019-08-08 stsp goto done;
3265 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3266 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3268 33aa809d 2019-08-08 stsp goto done;
3270 33aa809d 2019-08-08 stsp switch (*choice) {
3271 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3272 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3273 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3275 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3276 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3277 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3279 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3282 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3286 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3287 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3288 33aa809d 2019-08-08 stsp return err;
3291 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3292 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3293 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3294 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3295 1f1abb7e 2019-08-08 stsp void *progress_arg;
3296 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3297 33aa809d 2019-08-08 stsp void *patch_arg;
3298 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3301 e20a8b6f 2019-06-04 stsp static const struct got_error *
3302 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3303 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3304 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
3305 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3306 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3308 33aa809d 2019-08-08 stsp const struct got_error *err;
3309 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3310 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3311 1ebedb77 2019-10-19 stsp int fd2 = -1;
3312 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3313 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3314 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3315 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3316 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3317 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3318 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3319 33aa809d 2019-08-08 stsp int n = 0;
3321 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3323 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3325 33aa809d 2019-08-08 stsp return err;
3327 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
3328 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3329 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3330 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
3331 12463d8b 2019-12-13 stsp goto done;
3334 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3335 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3336 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
3337 12463d8b 2019-12-13 stsp goto done;
3340 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
3341 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
3342 1ebedb77 2019-10-19 stsp goto done;
3345 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
3346 33aa809d 2019-08-08 stsp if (f2 == NULL) {
3347 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
3348 33aa809d 2019-08-08 stsp goto done;
3352 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3354 33aa809d 2019-08-08 stsp goto done;
3356 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3358 33aa809d 2019-08-08 stsp goto done;
3360 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3362 33aa809d 2019-08-08 stsp goto done;
3364 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
3365 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
3366 33aa809d 2019-08-08 stsp goto done;
3369 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
3370 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3372 33aa809d 2019-08-08 stsp goto done;
3374 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3376 33aa809d 2019-08-08 stsp goto done;
3378 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
3379 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
3380 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
3381 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
3382 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3383 33aa809d 2019-08-08 stsp int choice;
3384 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
3385 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
3386 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
3387 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
3388 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
3389 e635744c 2019-08-08 stsp patch_cb, patch_arg);
3391 33aa809d 2019-08-08 stsp goto done;
3392 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
3393 33aa809d 2019-08-08 stsp have_content = 1;
3394 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
3397 1ebedb77 2019-10-19 stsp if (have_content) {
3398 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3399 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
3400 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
3402 1ebedb77 2019-10-19 stsp goto done;
3404 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
3405 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
3406 1ebedb77 2019-10-19 stsp goto done;
3410 33aa809d 2019-08-08 stsp free(id_str);
3412 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
3413 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3414 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
3415 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3416 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
3417 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3418 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
3419 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
3420 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
3421 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
3422 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
3423 33aa809d 2019-08-08 stsp if (err || !have_content) {
3424 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3425 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
3426 33aa809d 2019-08-08 stsp free(*path_outfile);
3427 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3429 33aa809d 2019-08-08 stsp free(args);
3431 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
3434 33aa809d 2019-08-08 stsp if (changes)
3435 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
3436 33aa809d 2019-08-08 stsp free(path1);
3437 33aa809d 2019-08-08 stsp return err;
3440 33aa809d 2019-08-08 stsp static const struct got_error *
3441 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
3442 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
3443 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3444 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3446 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
3447 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
3448 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
3449 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
3450 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
3451 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
3452 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
3453 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
3454 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
3455 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
3457 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
3458 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
3459 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
3460 d3bcc3d1 2019-08-08 stsp return NULL;
3462 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
3463 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
3464 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
3466 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3467 65084dad 2019-08-08 stsp if (ie == NULL)
3468 65084dad 2019-08-08 stsp return got_error(GOT_ERR_BAD_PATH);
3470 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
3471 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
3472 a129376b 2019-03-28 stsp if (err) {
3473 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
3474 a129376b 2019-03-28 stsp goto done;
3475 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
3476 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
3477 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
3478 e20a8b6f 2019-06-04 stsp goto done;
3481 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
3482 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
3483 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3484 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3485 a129376b 2019-03-28 stsp goto done;
3488 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
3489 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
3490 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3491 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3492 a129376b 2019-03-28 stsp goto done;
3495 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
3496 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
3497 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3498 a129376b 2019-03-28 stsp goto done;
3503 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
3504 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
3505 a9fa2909 2019-07-27 stsp if (err) {
3506 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3507 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
3508 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
3509 a9fa2909 2019-07-27 stsp goto done;
3511 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
3513 a9fa2909 2019-07-27 stsp goto done;
3515 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
3516 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
3517 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
3518 a9fa2909 2019-07-27 stsp goto done;
3521 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
3522 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
3523 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
3524 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
3525 a9fa2909 2019-07-27 stsp goto done;
3529 a129376b 2019-03-28 stsp switch (status) {
3530 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
3531 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3532 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3533 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3534 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3536 33aa809d 2019-08-08 stsp goto done;
3537 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3540 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3541 1f1abb7e 2019-08-08 stsp ie->path);
3543 1ee397ad 2019-07-12 stsp goto done;
3544 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
3546 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
3547 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3548 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3549 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3550 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3552 33aa809d 2019-08-08 stsp goto done;
3553 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3556 33aa809d 2019-08-08 stsp /* fall through */
3557 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
3558 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
3559 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
3560 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
3561 e20a8b6f 2019-06-04 stsp struct got_object_id id;
3562 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3563 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3564 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
3565 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3567 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
3568 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3569 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3571 65084dad 2019-08-08 stsp goto done;
3573 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3574 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
3575 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
3576 a129376b 2019-03-28 stsp goto done;
3579 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3580 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
3581 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
3582 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
3583 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
3584 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
3586 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
3587 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
3588 33aa809d 2019-08-08 stsp path_content, ondisk_path);
3589 33aa809d 2019-08-08 stsp goto done;
3592 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
3593 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
3594 33aa809d 2019-08-08 stsp got_fileindex_perms_to_st(ie), blob, 0, 1,
3595 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
3597 a129376b 2019-03-28 stsp goto done;
3598 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
3599 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
3600 33aa809d 2019-08-08 stsp err = update_blob_fileindex_entry(a->worktree,
3601 33aa809d 2019-08-08 stsp a->fileindex, ie, ondisk_path, ie->path,
3604 33aa809d 2019-08-08 stsp goto done;
3613 1f1abb7e 2019-08-08 stsp free(ondisk_path);
3614 33aa809d 2019-08-08 stsp free(path_content);
3615 e20a8b6f 2019-06-04 stsp free(parent_path);
3616 a129376b 2019-03-28 stsp free(tree_path);
3618 a129376b 2019-03-28 stsp got_object_blob_close(blob);
3620 a129376b 2019-03-28 stsp got_object_tree_close(tree);
3621 a129376b 2019-03-28 stsp free(tree_id);
3622 e20a8b6f 2019-06-04 stsp return err;
3625 e20a8b6f 2019-06-04 stsp const struct got_error *
3626 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
3627 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
3628 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3629 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
3630 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
3632 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
3633 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
3634 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3635 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
3636 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3637 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
3639 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
3641 e20a8b6f 2019-06-04 stsp return err;
3643 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3645 e20a8b6f 2019-06-04 stsp goto done;
3647 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
3648 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
3649 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
3650 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
3651 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
3652 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
3653 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
3654 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
3655 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3656 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
3660 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3661 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
3662 e20a8b6f 2019-06-04 stsp err = sync_err;
3664 fb399478 2019-07-12 stsp free(fileindex_path);
3665 a129376b 2019-03-28 stsp if (fileindex)
3666 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
3667 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3668 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
3669 a129376b 2019-03-28 stsp err = unlockerr;
3670 c4296144 2019-05-09 stsp return err;
3673 cf066bf8 2019-05-09 stsp static void
3674 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
3676 24519714 2019-05-09 stsp free(ct->path);
3677 44d03001 2019-05-09 stsp free(ct->in_repo_path);
3678 768aea60 2019-05-09 stsp free(ct->ondisk_path);
3679 e75eb4da 2019-05-10 stsp free(ct->blob_id);
3680 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
3681 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
3682 b416585c 2019-05-13 stsp free(ct->base_commit_id);
3686 ed175427 2019-05-09 stsp struct collect_commitables_arg {
3687 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
3688 24519714 2019-05-09 stsp struct got_repository *repo;
3689 24519714 2019-05-09 stsp struct got_worktree *worktree;
3690 5f8a88c6 2019-08-03 stsp int have_staged_files;
3693 c4296144 2019-05-09 stsp static const struct got_error *
3694 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
3695 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
3696 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3697 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3699 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
3700 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
3701 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3702 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
3703 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
3704 768aea60 2019-05-09 stsp struct stat sb;
3706 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
3707 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3708 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3709 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3710 5f8a88c6 2019-08-03 stsp return NULL;
3712 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
3713 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
3715 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3716 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
3717 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
3718 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
3719 5f8a88c6 2019-08-03 stsp return NULL;
3722 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
3723 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3724 036813ee 2019-05-09 stsp goto done;
3726 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
3727 036813ee 2019-05-09 stsp parent_path = strdup("");
3728 69960a46 2019-05-09 stsp if (parent_path == NULL)
3729 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3731 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
3733 69960a46 2019-05-09 stsp return err;
3736 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
3737 cf066bf8 2019-05-09 stsp if (ct == NULL) {
3738 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3739 c4296144 2019-05-09 stsp goto done;
3742 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3743 768aea60 2019-05-09 stsp relpath) == -1) {
3744 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3745 768aea60 2019-05-09 stsp goto done;
3747 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3748 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3750 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3751 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
3752 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
3753 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
3754 12463d8b 2019-12-13 stsp ct->ondisk_path);
3755 12463d8b 2019-12-13 stsp goto done;
3757 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
3758 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
3759 768aea60 2019-05-09 stsp goto done;
3761 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
3764 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3765 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3766 44d03001 2019-05-09 stsp relpath) == -1) {
3767 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3768 44d03001 2019-05-09 stsp goto done;
3771 cf066bf8 2019-05-09 stsp ct->status = status;
3772 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
3773 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
3774 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
3775 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
3776 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
3777 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
3778 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3779 b416585c 2019-05-13 stsp goto done;
3781 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
3782 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
3783 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
3784 f0b75401 2019-08-03 stsp goto done;
3787 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3788 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3789 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3790 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
3791 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3792 036813ee 2019-05-09 stsp goto done;
3795 24519714 2019-05-09 stsp ct->path = strdup(path);
3796 24519714 2019-05-09 stsp if (ct->path == NULL) {
3797 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3798 24519714 2019-05-09 stsp goto done;
3800 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3802 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
3803 ed175427 2019-05-09 stsp free_commitable(ct);
3804 24519714 2019-05-09 stsp free(parent_path);
3805 036813ee 2019-05-09 stsp free(path);
3806 a129376b 2019-03-28 stsp return err;
3809 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
3810 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
3811 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3812 036813ee 2019-05-09 stsp struct got_repository *);
3814 ed175427 2019-05-09 stsp static const struct got_error *
3815 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
3816 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
3817 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3818 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3819 afa376bf 2019-05-09 stsp struct got_repository *repo)
3821 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
3822 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
3823 036813ee 2019-05-09 stsp char *subpath;
3825 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
3826 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3827 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3829 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
3831 ed175427 2019-05-09 stsp return err;
3833 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3834 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3835 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
3836 036813ee 2019-05-09 stsp free(subpath);
3837 036813ee 2019-05-09 stsp return err;
3840 036813ee 2019-05-09 stsp static const struct got_error *
3841 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3843 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3844 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
3846 036813ee 2019-05-09 stsp *match = 0;
3848 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
3849 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
3850 0f63689d 2019-05-10 stsp return NULL;
3853 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3855 0f63689d 2019-05-10 stsp return err;
3856 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
3857 036813ee 2019-05-09 stsp free(ct_parent_path);
3858 036813ee 2019-05-09 stsp return err;
3861 768aea60 2019-05-09 stsp static mode_t
3862 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
3864 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3867 036813ee 2019-05-09 stsp static const struct got_error *
3868 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3869 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3871 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3873 036813ee 2019-05-09 stsp *new_te = NULL;
3875 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3877 036813ee 2019-05-09 stsp goto done;
3879 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3881 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
3882 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
3883 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
3885 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3887 036813ee 2019-05-09 stsp if (err && *new_te) {
3888 56e0773d 2019-11-28 stsp free(*new_te);
3889 036813ee 2019-05-09 stsp *new_te = NULL;
3891 036813ee 2019-05-09 stsp return err;
3894 036813ee 2019-05-09 stsp static const struct got_error *
3895 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3896 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3898 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3899 036813ee 2019-05-09 stsp char *ct_name;
3901 036813ee 2019-05-09 stsp *new_te = NULL;
3903 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3904 036813ee 2019-05-09 stsp if (*new_te == NULL)
3905 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3907 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3908 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3909 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3910 036813ee 2019-05-09 stsp goto done;
3912 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3913 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
3914 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
3915 036813ee 2019-05-09 stsp goto done;
3918 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3920 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
3921 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
3922 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
3924 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3926 036813ee 2019-05-09 stsp if (err && *new_te) {
3927 56e0773d 2019-11-28 stsp free(*new_te);
3928 036813ee 2019-05-09 stsp *new_te = NULL;
3930 036813ee 2019-05-09 stsp return err;
3933 036813ee 2019-05-09 stsp static const struct got_error *
3934 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3935 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3937 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3938 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3940 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3942 036813ee 2019-05-09 stsp return err;
3943 036813ee 2019-05-09 stsp if (new_pe == NULL)
3944 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3945 036813ee 2019-05-09 stsp return NULL;
3948 afa376bf 2019-05-09 stsp static const struct got_error *
3949 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3950 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3952 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3953 5f8a88c6 2019-08-03 stsp unsigned char status;
3955 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3956 afa376bf 2019-05-09 stsp ct_path++;
3958 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3959 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
3961 5f8a88c6 2019-08-03 stsp status = ct->status;
3963 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3964 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3967 036813ee 2019-05-09 stsp static const struct got_error *
3968 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3969 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3971 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3972 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3973 44d03001 2019-05-09 stsp char *te_path;
3975 44d03001 2019-05-09 stsp *modified = 0;
3977 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3978 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3979 44d03001 2019-05-09 stsp te->name) == -1)
3980 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3982 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3983 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3984 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3985 44d03001 2019-05-09 stsp strlen(te_path));
3986 44d03001 2019-05-09 stsp if (*modified)
3990 44d03001 2019-05-09 stsp free(te_path);
3991 44d03001 2019-05-09 stsp return err;
3994 44d03001 2019-05-09 stsp static const struct got_error *
3995 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3996 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3997 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3999 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4000 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4002 036813ee 2019-05-09 stsp *ctp = NULL;
4004 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4005 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4006 036813ee 2019-05-09 stsp char *ct_name = NULL;
4007 036813ee 2019-05-09 stsp int path_matches;
4009 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4010 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4011 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4012 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4015 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4016 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4020 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4023 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4025 036813ee 2019-05-09 stsp return err;
4026 036813ee 2019-05-09 stsp if (!path_matches)
4029 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4030 036813ee 2019-05-09 stsp if (ct_name == NULL)
4031 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4033 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4036 036813ee 2019-05-09 stsp *ctp = ct;
4040 2b496619 2019-07-10 stsp return err;
4043 2b496619 2019-07-10 stsp static const struct got_error *
4044 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4045 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4046 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4047 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4048 2b496619 2019-07-10 stsp struct got_repository *repo)
4050 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4051 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4052 2b496619 2019-07-10 stsp char *subtree_path;
4053 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4055 2b496619 2019-07-10 stsp *new_tep = NULL;
4057 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4058 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4059 2b496619 2019-07-10 stsp child_path) == -1)
4060 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4062 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4063 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4064 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4065 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4067 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4068 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4069 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4070 2b496619 2019-07-10 stsp goto done;
4072 56e0773d 2019-11-28 stsp err = write_tree(&id, NULL, subtree_path,
4073 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4074 2b496619 2019-07-10 stsp if (err) {
4075 56e0773d 2019-11-28 stsp free(new_te);
4076 2b496619 2019-07-10 stsp goto done;
4078 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4081 2b496619 2019-07-10 stsp free(subtree_path);
4082 2b496619 2019-07-10 stsp if (err == NULL)
4083 2b496619 2019-07-10 stsp *new_tep = new_te;
4084 036813ee 2019-05-09 stsp return err;
4087 036813ee 2019-05-09 stsp static const struct got_error *
4088 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
4089 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4090 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4091 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4092 036813ee 2019-05-09 stsp struct got_repository *repo)
4094 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4095 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4096 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4097 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4098 56e0773d 2019-11-28 stsp int nentries = 0;
4100 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4102 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4103 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4104 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4105 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4107 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4108 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4109 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4112 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4113 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4116 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4117 036813ee 2019-05-09 stsp pe->path);
4119 036813ee 2019-05-09 stsp goto done;
4121 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4122 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4123 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4125 036813ee 2019-05-09 stsp goto done;
4126 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4128 afa376bf 2019-05-09 stsp goto done;
4129 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4130 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4132 2f51b5b3 2019-05-09 stsp goto done;
4133 56e0773d 2019-11-28 stsp nentries++;
4135 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4136 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4137 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4138 2b496619 2019-07-10 stsp == NULL) {
4139 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4140 2b496619 2019-07-10 stsp child_path, path_base_tree,
4141 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4144 2b496619 2019-07-10 stsp goto done;
4145 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4147 2b496619 2019-07-10 stsp goto done;
4148 56e0773d 2019-11-28 stsp nentries++;
4153 2f51b5b3 2019-05-09 stsp if (base_tree) {
4154 56e0773d 2019-11-28 stsp int i, nbase_entries;
4155 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4156 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4157 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4158 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4160 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4161 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4162 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4163 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4165 63c5ca5d 2019-08-24 stsp goto done;
4166 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4168 63c5ca5d 2019-08-24 stsp goto done;
4169 56e0773d 2019-11-28 stsp nentries++;
4173 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4174 44d03001 2019-05-09 stsp int modified;
4175 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4177 036813ee 2019-05-09 stsp goto done;
4178 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4179 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4181 2f51b5b3 2019-05-09 stsp goto done;
4182 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4183 44d03001 2019-05-09 stsp if (modified) {
4184 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4185 56e0773d 2019-11-28 stsp err = write_subtree(&new_id, te,
4186 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4187 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4189 44d03001 2019-05-09 stsp goto done;
4190 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4191 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4192 56e0773d 2019-11-28 stsp free(new_id);
4194 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4196 036813ee 2019-05-09 stsp goto done;
4197 56e0773d 2019-11-28 stsp nentries++;
4201 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4202 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4204 f66c734c 2019-09-22 stsp goto done;
4206 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4207 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4208 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4209 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4210 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4211 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4213 2f51b5b3 2019-05-09 stsp goto done;
4214 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4216 2f51b5b3 2019-05-09 stsp goto done;
4217 56e0773d 2019-11-28 stsp nentries++;
4219 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4220 b416585c 2019-05-13 stsp status_arg);
4222 afa376bf 2019-05-09 stsp goto done;
4224 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4225 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4227 2f51b5b3 2019-05-09 stsp goto done;
4228 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4230 2f51b5b3 2019-05-09 stsp goto done;
4231 56e0773d 2019-11-28 stsp nentries++;
4236 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4237 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4239 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4240 ed175427 2019-05-09 stsp return err;
4243 ebf99748 2019-05-09 stsp static const struct got_error *
4244 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4245 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4246 72fd46fa 2019-09-06 stsp int have_staged_files)
4248 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
4249 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
4251 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4252 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
4253 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4255 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4257 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
4258 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
4259 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
4260 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
4261 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4262 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
4263 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
4264 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
4265 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
4266 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4267 72fd46fa 2019-09-06 stsp !have_staged_files);
4269 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
4270 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
4271 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4272 72fd46fa 2019-09-06 stsp !have_staged_files);
4274 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
4275 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
4276 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
4279 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
4284 d56d26ce 2019-05-10 stsp return err;
4288 d56d26ce 2019-05-10 stsp static const struct got_error *
4289 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
4290 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
4291 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
4292 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
4293 735ef5ac 2019-08-03 stsp int ood_errcode)
4295 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
4296 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
4298 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4299 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
4300 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4301 1a36436d 2019-06-10 stsp return NULL;
4303 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
4304 9bead371 2019-07-28 stsp * on matches file content in the branch head.
4306 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4307 735ef5ac 2019-08-03 stsp in_repo_path);
4308 9bead371 2019-07-28 stsp if (err) {
4309 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
4310 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
4311 1a36436d 2019-06-10 stsp goto done;
4312 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
4313 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
4315 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
4316 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4317 735ef5ac 2019-08-03 stsp in_repo_path);
4318 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4319 1a36436d 2019-06-10 stsp goto done;
4320 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
4324 1a36436d 2019-06-10 stsp return err;
4327 c4296144 2019-05-09 stsp const struct got_error *
4328 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
4329 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
4330 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
4331 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
4332 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4333 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4334 afa376bf 2019-05-09 stsp struct got_repository *repo)
4336 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4337 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
4338 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
4339 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
4340 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
4341 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
4342 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
4343 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
4344 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
4345 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
4346 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
4348 c4296144 2019-05-09 stsp *new_commit_id = NULL;
4350 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
4352 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4354 588edf97 2019-05-10 stsp goto done;
4356 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4358 588edf97 2019-05-10 stsp goto done;
4360 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
4361 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4366 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
4367 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4371 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
4372 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4373 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4374 cf066bf8 2019-05-09 stsp char *ondisk_path;
4376 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
4377 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4378 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
4381 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
4382 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
4383 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
4386 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
4387 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
4388 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4389 cf066bf8 2019-05-09 stsp goto done;
4391 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4392 a7055788 2019-05-09 stsp free(ondisk_path);
4394 cf066bf8 2019-05-09 stsp goto done;
4397 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
4398 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4399 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
4401 036813ee 2019-05-09 stsp goto done;
4403 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4405 de18fc63 2019-05-09 stsp goto done;
4406 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4407 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4408 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4409 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
4410 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
4411 33ad4cbe 2019-05-12 jcs free(logmsg);
4413 9d40349a 2019-05-09 stsp goto done;
4415 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
4416 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
4417 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
4418 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
4419 09f5bd90 2019-05-09 stsp goto done;
4421 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
4422 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4424 09f5bd90 2019-05-09 stsp goto done;
4425 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4427 ebf99748 2019-05-09 stsp goto done;
4428 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4429 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4430 09f5bd90 2019-05-09 stsp goto done;
4432 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
4433 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
4435 f2c16586 2019-05-09 stsp goto done;
4436 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
4438 f2c16586 2019-05-09 stsp goto done;
4440 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4442 09f5bd90 2019-05-09 stsp goto done;
4444 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
4446 ebf99748 2019-05-09 stsp goto done;
4448 588edf97 2019-05-10 stsp if (head_tree)
4449 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
4450 588edf97 2019-05-10 stsp if (head_commit)
4451 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
4452 09f5bd90 2019-05-09 stsp free(head_commit_id2);
4453 2f17228e 2019-05-12 stsp if (head_ref2) {
4454 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
4455 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
4456 2f17228e 2019-05-12 stsp err = unlockerr;
4457 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
4459 39cd0ff6 2019-07-12 stsp return err;
4462 5c1e53bc 2019-07-28 stsp static const struct got_error *
4463 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
4464 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
4466 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
4467 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
4469 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
4470 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
4471 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
4473 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
4474 5c1e53bc 2019-07-28 stsp ct_path++;
4476 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
4477 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
4481 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
4482 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
4484 5c1e53bc 2019-07-28 stsp return NULL;
4487 f0b75401 2019-08-03 stsp static const struct got_error *
4488 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
4490 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
4492 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4493 f0b75401 2019-08-03 stsp *have_staged_files = 1;
4494 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
4497 f0b75401 2019-08-03 stsp return NULL;
4500 5f8a88c6 2019-08-03 stsp static const struct got_error *
4501 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
4502 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
4504 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
4505 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
4507 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
4508 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
4510 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4511 5f8a88c6 2019-08-03 stsp if (ie == NULL)
4512 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4513 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4514 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
4515 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
4518 5f8a88c6 2019-08-03 stsp return NULL;
4521 39cd0ff6 2019-07-12 stsp const struct got_error *
4522 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
4523 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
4524 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
4525 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4526 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4527 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
4529 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4530 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4531 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
4532 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4533 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4534 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
4535 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
4536 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4537 f0b75401 2019-08-03 stsp int have_staged_files = 0;
4539 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
4541 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4543 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
4545 39cd0ff6 2019-07-12 stsp goto done;
4547 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4549 39cd0ff6 2019-07-12 stsp goto done;
4551 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4553 39cd0ff6 2019-07-12 stsp goto done;
4555 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4557 39cd0ff6 2019-07-12 stsp goto done;
4559 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4560 5f8a88c6 2019-08-03 stsp &have_staged_files);
4561 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
4562 5f8a88c6 2019-08-03 stsp goto done;
4563 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
4564 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
4566 5f8a88c6 2019-08-03 stsp goto done;
4569 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4570 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
4571 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
4572 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
4573 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4574 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4575 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4577 5c1e53bc 2019-07-28 stsp goto done;
4580 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4581 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4582 39cd0ff6 2019-07-12 stsp goto done;
4585 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4586 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
4588 5c1e53bc 2019-07-28 stsp goto done;
4591 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4592 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
4593 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
4595 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
4596 f0b75401 2019-08-03 stsp ct_path++;
4597 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
4598 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4599 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4601 b50cabdf 2019-07-12 stsp goto done;
4605 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
4606 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
4607 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4609 39cd0ff6 2019-07-12 stsp goto done;
4611 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4612 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
4613 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4614 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
4615 39cd0ff6 2019-07-12 stsp err = sync_err;
4617 39cd0ff6 2019-07-12 stsp if (fileindex)
4618 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
4619 39cd0ff6 2019-07-12 stsp free(fileindex_path);
4620 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4621 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
4622 39cd0ff6 2019-07-12 stsp err = unlockerr;
4623 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4624 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
4625 39cd0ff6 2019-07-12 stsp free_commitable(ct);
4627 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
4628 c4296144 2019-05-09 stsp return err;
4631 8656d6c4 2019-05-20 stsp const char *
4632 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
4634 8656d6c4 2019-05-20 stsp return ct->path;
4637 8656d6c4 2019-05-20 stsp unsigned int
4638 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
4640 8656d6c4 2019-05-20 stsp return ct->status;
4643 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
4644 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4645 818c7501 2019-07-11 stsp struct got_repository *repo;
4648 818c7501 2019-07-11 stsp static const struct got_error *
4649 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4651 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4652 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
4653 818c7501 2019-07-11 stsp unsigned char status;
4654 818c7501 2019-07-11 stsp struct stat sb;
4655 818c7501 2019-07-11 stsp char *ondisk_path;
4657 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
4658 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4659 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
4660 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
4662 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4664 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4666 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
4667 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4668 818c7501 2019-07-11 stsp free(ondisk_path);
4670 818c7501 2019-07-11 stsp return err;
4672 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
4673 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
4674 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4675 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4677 818c7501 2019-07-11 stsp return NULL;
4680 818c7501 2019-07-11 stsp const struct got_error *
4681 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4682 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4683 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
4684 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4686 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4687 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4688 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
4689 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4690 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
4691 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4692 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
4694 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4695 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4696 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4698 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4700 818c7501 2019-07-11 stsp return err;
4702 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4704 818c7501 2019-07-11 stsp goto done;
4706 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
4707 818c7501 2019-07-11 stsp ok_arg.repo = repo;
4708 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4711 818c7501 2019-07-11 stsp goto done;
4713 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4715 818c7501 2019-07-11 stsp goto done;
4717 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4719 818c7501 2019-07-11 stsp goto done;
4721 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4723 818c7501 2019-07-11 stsp goto done;
4725 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4728 e51d7b55 2020-01-04 stsp goto done;
4730 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4732 818c7501 2019-07-11 stsp goto done;
4733 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4734 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4735 e51d7b55 2020-01-04 stsp goto done;
4738 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
4739 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
4741 818c7501 2019-07-11 stsp goto done;
4742 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
4744 818c7501 2019-07-11 stsp goto done;
4746 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
4748 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4750 818c7501 2019-07-11 stsp goto done;
4752 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
4754 818c7501 2019-07-11 stsp goto done;
4756 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4757 818c7501 2019-07-11 stsp worktree->base_commit_id);
4759 818c7501 2019-07-11 stsp goto done;
4760 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
4762 818c7501 2019-07-11 stsp goto done;
4764 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4766 818c7501 2019-07-11 stsp goto done;
4768 818c7501 2019-07-11 stsp free(fileindex_path);
4769 818c7501 2019-07-11 stsp free(tmp_branch_name);
4770 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
4771 818c7501 2019-07-11 stsp free(branch_ref_name);
4772 818c7501 2019-07-11 stsp if (branch_ref)
4773 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4774 818c7501 2019-07-11 stsp if (wt_branch)
4775 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
4776 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
4777 818c7501 2019-07-11 stsp if (err) {
4778 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
4779 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
4780 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4782 818c7501 2019-07-11 stsp if (*tmp_branch) {
4783 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4784 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4786 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4787 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4788 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4790 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
4792 818c7501 2019-07-11 stsp return err;
4795 818c7501 2019-07-11 stsp const struct got_error *
4796 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
4797 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4798 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
4799 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
4801 818c7501 2019-07-11 stsp const struct got_error *err;
4802 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4803 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4804 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4805 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4806 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
4808 818c7501 2019-07-11 stsp *commit_id = NULL;
4809 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
4810 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
4811 3e3a69f1 2019-07-25 stsp *branch = NULL;
4812 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4814 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4816 3e3a69f1 2019-07-25 stsp return err;
4818 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4820 f032f1f7 2019-08-04 stsp goto done;
4822 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4823 f032f1f7 2019-08-04 stsp &have_staged_files);
4824 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
4825 f032f1f7 2019-08-04 stsp goto done;
4826 f032f1f7 2019-08-04 stsp if (have_staged_files) {
4827 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
4828 3e3a69f1 2019-07-25 stsp goto done;
4831 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4833 f032f1f7 2019-08-04 stsp goto done;
4835 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4837 818c7501 2019-07-11 stsp goto done;
4839 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4841 818c7501 2019-07-11 stsp goto done;
4843 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4845 818c7501 2019-07-11 stsp goto done;
4847 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4849 818c7501 2019-07-11 stsp goto done;
4851 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
4852 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
4854 818c7501 2019-07-11 stsp goto done;
4856 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4858 818c7501 2019-07-11 stsp goto done;
4860 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4862 818c7501 2019-07-11 stsp goto done;
4864 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
4865 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
4867 818c7501 2019-07-11 stsp goto done;
4869 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4871 818c7501 2019-07-11 stsp goto done;
4873 818c7501 2019-07-11 stsp free(commit_ref_name);
4874 818c7501 2019-07-11 stsp free(branch_ref_name);
4875 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4876 818c7501 2019-07-11 stsp if (commit_ref)
4877 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4878 818c7501 2019-07-11 stsp if (branch_ref)
4879 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4880 818c7501 2019-07-11 stsp if (err) {
4881 818c7501 2019-07-11 stsp free(*commit_id);
4882 818c7501 2019-07-11 stsp *commit_id = NULL;
4883 818c7501 2019-07-11 stsp if (*tmp_branch) {
4884 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4885 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4887 818c7501 2019-07-11 stsp if (*new_base_branch) {
4888 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
4889 818c7501 2019-07-11 stsp *new_base_branch = NULL;
4891 818c7501 2019-07-11 stsp if (*branch) {
4892 818c7501 2019-07-11 stsp got_ref_close(*branch);
4893 818c7501 2019-07-11 stsp *branch = NULL;
4895 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4896 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4897 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4899 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
4901 818c7501 2019-07-11 stsp return err;
4904 818c7501 2019-07-11 stsp const struct got_error *
4905 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4907 818c7501 2019-07-11 stsp const struct got_error *err;
4908 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
4910 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4912 818c7501 2019-07-11 stsp return err;
4914 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4915 818c7501 2019-07-11 stsp free(tmp_branch_name);
4916 818c7501 2019-07-11 stsp return NULL;
4919 818c7501 2019-07-11 stsp static const struct got_error *
4920 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4921 818c7501 2019-07-11 stsp char **logmsg, void *arg)
4923 0ebf8283 2019-07-24 stsp *logmsg = arg;
4924 818c7501 2019-07-11 stsp return NULL;
4927 818c7501 2019-07-11 stsp static const struct got_error *
4928 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4929 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4930 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4931 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4933 818c7501 2019-07-11 stsp return NULL;
4936 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
4937 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
4938 01757395 2019-07-12 stsp void *progress_arg;
4939 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
4942 01757395 2019-07-12 stsp static const struct got_error *
4943 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
4945 01757395 2019-07-12 stsp const struct got_error *err;
4946 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
4948 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
4950 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
4952 01757395 2019-07-12 stsp return err;
4954 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
4955 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
4956 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
4957 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
4958 01757395 2019-07-12 stsp return NULL;
4960 01757395 2019-07-12 stsp p = strdup(path);
4961 01757395 2019-07-12 stsp if (p == NULL)
4962 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
4964 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4965 01757395 2019-07-12 stsp if (err || new == NULL)
4967 01757395 2019-07-12 stsp return err;
4971 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4973 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4975 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
4976 01757395 2019-07-12 stsp free((char *)pe->path);
4978 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
4981 0ebf8283 2019-07-24 stsp static const struct got_error *
4982 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4983 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4985 818c7501 2019-07-11 stsp const struct got_error *err;
4986 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
4988 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4989 818c7501 2019-07-11 stsp if (err) {
4990 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
4991 818c7501 2019-07-11 stsp goto done;
4992 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4994 818c7501 2019-07-11 stsp goto done;
4995 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
4997 818c7501 2019-07-11 stsp goto done;
4999 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5002 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5004 818c7501 2019-07-11 stsp goto done;
5005 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5006 818c7501 2019-07-11 stsp free(stored_id);
5007 818c7501 2019-07-11 stsp if (cmp != 0) {
5008 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5009 818c7501 2019-07-11 stsp goto done;
5013 0ebf8283 2019-07-24 stsp if (commit_ref)
5014 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5015 0ebf8283 2019-07-24 stsp return err;
5018 0ebf8283 2019-07-24 stsp static const struct got_error *
5019 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5020 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5021 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5022 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5023 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5024 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5026 0ebf8283 2019-07-24 stsp const struct got_error *err;
5027 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5028 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5029 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5031 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5033 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5035 0ebf8283 2019-07-24 stsp return err;
5037 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5038 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5039 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5040 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5041 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5042 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5043 818c7501 2019-07-11 stsp if (commit_ref)
5044 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5045 818c7501 2019-07-11 stsp return err;
5048 818c7501 2019-07-11 stsp const struct got_error *
5049 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5050 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5051 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5052 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5053 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5054 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5056 0ebf8283 2019-07-24 stsp const struct got_error *err;
5057 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5059 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5061 0ebf8283 2019-07-24 stsp return err;
5063 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5065 0ebf8283 2019-07-24 stsp goto done;
5067 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5068 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5069 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5071 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5072 0ebf8283 2019-07-24 stsp return err;
5075 0ebf8283 2019-07-24 stsp const struct got_error *
5076 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5077 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5078 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5079 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5080 0ebf8283 2019-07-24 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 char *commit_ref_name;
5086 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5088 0ebf8283 2019-07-24 stsp return err;
5090 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5092 0ebf8283 2019-07-24 stsp goto done;
5094 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5095 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5096 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5098 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5099 0ebf8283 2019-07-24 stsp return err;
5102 0ebf8283 2019-07-24 stsp static const struct got_error *
5103 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5104 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5105 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5106 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5107 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5109 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5110 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5111 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5112 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5113 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5114 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5115 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5117 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5118 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5120 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5122 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5124 3e3a69f1 2019-07-25 stsp return err;
5126 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5127 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5128 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5129 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5131 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5132 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5133 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5134 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5136 01757395 2019-07-12 stsp if (merged_paths) {
5137 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5138 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
5139 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
5140 8f8646e5 2019-07-25 stsp goto done;
5142 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5143 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5144 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5147 01757395 2019-07-12 stsp goto done;
5150 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5151 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5153 01757395 2019-07-12 stsp goto done;
5156 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5157 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5158 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5160 a0e95631 2019-07-12 stsp goto done;
5161 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5162 a0e95631 2019-07-12 stsp goto done;
5165 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5167 edd02c5e 2019-07-11 stsp goto done;
5169 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5171 818c7501 2019-07-11 stsp goto done;
5173 5943eee2 2019-08-13 stsp if (new_logmsg) {
5174 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
5175 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
5176 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5177 5943eee2 2019-08-13 stsp goto done;
5180 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5182 5943eee2 2019-08-13 stsp goto done;
5185 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
5186 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5187 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
5188 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
5189 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5191 a0e95631 2019-07-12 stsp goto done;
5193 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
5195 a0e95631 2019-07-12 stsp goto done;
5197 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5199 a0e95631 2019-07-12 stsp goto done;
5201 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5202 72fd46fa 2019-09-06 stsp fileindex, 0);
5203 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5204 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
5205 a0e95631 2019-07-12 stsp err = sync_err;
5207 a0e95631 2019-07-12 stsp free(fileindex_path);
5208 a0e95631 2019-07-12 stsp free(head_commit_id);
5209 a0e95631 2019-07-12 stsp if (head_ref)
5210 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
5211 818c7501 2019-07-11 stsp if (err) {
5212 818c7501 2019-07-11 stsp free(*new_commit_id);
5213 818c7501 2019-07-11 stsp *new_commit_id = NULL;
5215 818c7501 2019-07-11 stsp return err;
5218 818c7501 2019-07-11 stsp const struct got_error *
5219 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5220 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5221 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5222 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5223 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
5225 0ebf8283 2019-07-24 stsp const struct got_error *err;
5226 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5227 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5228 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5230 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5232 0ebf8283 2019-07-24 stsp return err;
5234 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5236 0ebf8283 2019-07-24 stsp goto done;
5237 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5239 0ebf8283 2019-07-24 stsp goto done;
5240 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5241 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5242 0ebf8283 2019-07-24 stsp goto done;
5245 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5246 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5248 0ebf8283 2019-07-24 stsp if (commit_ref)
5249 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5250 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5251 0ebf8283 2019-07-24 stsp free(commit_id);
5252 0ebf8283 2019-07-24 stsp return err;
5255 0ebf8283 2019-07-24 stsp const struct got_error *
5256 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5257 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5258 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5259 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5260 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
5261 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5263 0ebf8283 2019-07-24 stsp const struct got_error *err;
5264 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5265 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5266 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5268 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5270 0ebf8283 2019-07-24 stsp return err;
5272 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5274 0ebf8283 2019-07-24 stsp goto done;
5275 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5277 0ebf8283 2019-07-24 stsp goto done;
5278 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5279 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5280 0ebf8283 2019-07-24 stsp goto done;
5283 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5284 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5286 0ebf8283 2019-07-24 stsp if (commit_ref)
5287 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5288 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5289 0ebf8283 2019-07-24 stsp free(commit_id);
5290 0ebf8283 2019-07-24 stsp return err;
5293 0ebf8283 2019-07-24 stsp const struct got_error *
5294 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
5295 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5297 3e3a69f1 2019-07-25 stsp if (fileindex)
5298 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5299 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
5302 69844fba 2019-07-11 stsp static const struct got_error *
5303 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
5305 69844fba 2019-07-11 stsp const struct got_error *err;
5306 69844fba 2019-07-11 stsp struct got_reference *ref;
5308 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
5309 69844fba 2019-07-11 stsp if (err) {
5310 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
5311 69844fba 2019-07-11 stsp return NULL;
5312 69844fba 2019-07-11 stsp return err;
5315 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
5316 69844fba 2019-07-11 stsp got_ref_close(ref);
5317 69844fba 2019-07-11 stsp return err;
5320 69844fba 2019-07-11 stsp static const struct got_error *
5321 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5323 69844fba 2019-07-11 stsp const struct got_error *err;
5324 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5325 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5327 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5329 69844fba 2019-07-11 stsp goto done;
5330 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
5332 69844fba 2019-07-11 stsp goto done;
5334 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5336 69844fba 2019-07-11 stsp goto done;
5337 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
5339 69844fba 2019-07-11 stsp goto done;
5341 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5343 69844fba 2019-07-11 stsp goto done;
5344 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
5346 69844fba 2019-07-11 stsp goto done;
5348 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5350 69844fba 2019-07-11 stsp goto done;
5351 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
5353 69844fba 2019-07-11 stsp goto done;
5356 69844fba 2019-07-11 stsp free(tmp_branch_name);
5357 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
5358 69844fba 2019-07-11 stsp free(branch_ref_name);
5359 69844fba 2019-07-11 stsp free(commit_ref_name);
5360 69844fba 2019-07-11 stsp return err;
5363 818c7501 2019-07-11 stsp const struct got_error *
5364 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
5365 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5366 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5367 818c7501 2019-07-11 stsp struct got_repository *repo)
5369 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
5370 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
5372 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5374 818c7501 2019-07-11 stsp return err;
5376 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5378 818c7501 2019-07-11 stsp goto done;
5380 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
5382 818c7501 2019-07-11 stsp goto done;
5384 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
5386 818c7501 2019-07-11 stsp goto done;
5388 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
5390 3e3a69f1 2019-07-25 stsp if (fileindex)
5391 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5392 818c7501 2019-07-11 stsp free(new_head_commit_id);
5393 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5394 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5395 818c7501 2019-07-11 stsp err = unlockerr;
5396 818c7501 2019-07-11 stsp return err;
5399 818c7501 2019-07-11 stsp const struct got_error *
5400 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
5401 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5402 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
5403 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5405 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
5406 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
5407 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
5408 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5409 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5410 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
5412 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5414 818c7501 2019-07-11 stsp return err;
5416 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
5417 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
5419 818c7501 2019-07-11 stsp goto done;
5421 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
5423 818c7501 2019-07-11 stsp goto done;
5426 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
5427 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
5428 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
5430 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
5432 818c7501 2019-07-11 stsp goto done;
5434 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5436 818c7501 2019-07-11 stsp goto done;
5438 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
5439 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
5441 ca355955 2019-07-12 stsp goto done;
5443 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
5445 ca355955 2019-07-12 stsp goto done;
5447 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5449 818c7501 2019-07-11 stsp goto done;
5451 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5452 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5453 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5454 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5455 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5456 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5457 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5458 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5459 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
5461 1f1abb7e 2019-08-08 stsp goto sync;
5463 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5464 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
5466 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5467 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
5468 ca355955 2019-07-12 stsp err = sync_err;
5470 818c7501 2019-07-11 stsp got_ref_close(resolved);
5471 a3a2faf2 2019-07-12 stsp free(tree_id);
5472 818c7501 2019-07-11 stsp free(commit_id);
5473 0ebf8283 2019-07-24 stsp if (fileindex)
5474 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
5475 0ebf8283 2019-07-24 stsp free(fileindex_path);
5477 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5478 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5479 0ebf8283 2019-07-24 stsp err = unlockerr;
5480 0ebf8283 2019-07-24 stsp return err;
5483 0ebf8283 2019-07-24 stsp const struct got_error *
5484 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5485 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5486 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
5487 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5489 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5490 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5491 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
5492 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
5493 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5494 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
5495 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
5496 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5498 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5499 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5500 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5501 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5503 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5505 0ebf8283 2019-07-24 stsp return err;
5507 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5509 0ebf8283 2019-07-24 stsp goto done;
5511 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
5512 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
5513 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5516 0ebf8283 2019-07-24 stsp goto done;
5518 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5520 0ebf8283 2019-07-24 stsp goto done;
5522 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5524 0ebf8283 2019-07-24 stsp goto done;
5526 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5527 0ebf8283 2019-07-24 stsp worktree);
5529 0ebf8283 2019-07-24 stsp goto done;
5531 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5534 0ebf8283 2019-07-24 stsp goto done;
5536 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5538 0ebf8283 2019-07-24 stsp goto done;
5540 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
5542 0ebf8283 2019-07-24 stsp goto done;
5544 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5545 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5547 0ebf8283 2019-07-24 stsp goto done;
5548 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
5550 0ebf8283 2019-07-24 stsp goto done;
5551 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5552 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
5553 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
5554 0ebf8283 2019-07-24 stsp goto done;
5557 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5558 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5560 0ebf8283 2019-07-24 stsp goto done;
5561 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
5563 0ebf8283 2019-07-24 stsp goto done;
5565 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5567 0ebf8283 2019-07-24 stsp goto done;
5569 0ebf8283 2019-07-24 stsp free(fileindex_path);
5570 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5571 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5572 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5573 0ebf8283 2019-07-24 stsp if (wt_branch)
5574 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
5575 0ebf8283 2019-07-24 stsp if (err) {
5576 0ebf8283 2019-07-24 stsp if (*branch_ref) {
5577 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
5578 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5580 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5581 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5582 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5584 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5585 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5586 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5587 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5589 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
5591 0ebf8283 2019-07-24 stsp return err;
5594 0ebf8283 2019-07-24 stsp const struct got_error *
5595 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
5596 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5598 3e3a69f1 2019-07-25 stsp if (fileindex)
5599 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5600 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
5603 0ebf8283 2019-07-24 stsp const struct got_error *
5604 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
5605 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
5607 0ebf8283 2019-07-24 stsp const struct got_error *err;
5608 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5610 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5612 0ebf8283 2019-07-24 stsp return err;
5614 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5615 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5616 0ebf8283 2019-07-24 stsp return NULL;
5619 0ebf8283 2019-07-24 stsp const struct got_error *
5620 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
5621 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
5622 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5623 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5625 0ebf8283 2019-07-24 stsp const struct got_error *err;
5626 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5627 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5628 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5629 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5630 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5631 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5633 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5634 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5635 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5636 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5638 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5640 3e3a69f1 2019-07-25 stsp return err;
5642 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5644 f032f1f7 2019-08-04 stsp goto done;
5646 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5647 f032f1f7 2019-08-04 stsp &have_staged_files);
5648 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5649 f032f1f7 2019-08-04 stsp goto done;
5650 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5651 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5652 3e3a69f1 2019-07-25 stsp goto done;
5655 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5657 f032f1f7 2019-08-04 stsp goto done;
5659 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5661 0ebf8283 2019-07-24 stsp goto done;
5663 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5665 0ebf8283 2019-07-24 stsp goto done;
5667 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5668 0ebf8283 2019-07-24 stsp worktree);
5670 0ebf8283 2019-07-24 stsp goto done;
5672 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5674 0ebf8283 2019-07-24 stsp goto done;
5676 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5678 0ebf8283 2019-07-24 stsp goto done;
5679 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5681 0ebf8283 2019-07-24 stsp goto done;
5683 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5685 0ebf8283 2019-07-24 stsp goto done;
5686 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5688 0ebf8283 2019-07-24 stsp goto done;
5690 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5692 0ebf8283 2019-07-24 stsp goto done;
5694 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5695 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5696 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5697 0ebf8283 2019-07-24 stsp if (commit_ref)
5698 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5699 0ebf8283 2019-07-24 stsp if (base_commit_ref)
5700 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
5701 0ebf8283 2019-07-24 stsp if (err) {
5702 0ebf8283 2019-07-24 stsp free(*commit_id);
5703 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5704 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5705 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5706 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5707 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5708 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5710 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5711 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5712 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5714 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
5716 0ebf8283 2019-07-24 stsp return err;
5719 0ebf8283 2019-07-24 stsp static const struct got_error *
5720 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5722 0ebf8283 2019-07-24 stsp const struct got_error *err;
5723 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5724 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5726 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5728 0ebf8283 2019-07-24 stsp goto done;
5729 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
5731 0ebf8283 2019-07-24 stsp goto done;
5733 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5734 0ebf8283 2019-07-24 stsp worktree);
5736 0ebf8283 2019-07-24 stsp goto done;
5737 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
5739 0ebf8283 2019-07-24 stsp goto done;
5741 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5743 0ebf8283 2019-07-24 stsp goto done;
5744 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
5746 0ebf8283 2019-07-24 stsp goto done;
5748 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5750 0ebf8283 2019-07-24 stsp goto done;
5751 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5753 0ebf8283 2019-07-24 stsp goto done;
5755 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5756 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5757 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5758 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5759 0ebf8283 2019-07-24 stsp return err;
5762 0ebf8283 2019-07-24 stsp const struct got_error *
5763 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
5764 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5765 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
5766 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5768 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
5769 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5770 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5771 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
5772 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5774 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5776 0ebf8283 2019-07-24 stsp return err;
5778 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5779 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
5781 0ebf8283 2019-07-24 stsp goto done;
5783 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5785 0ebf8283 2019-07-24 stsp goto done;
5787 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5789 0ebf8283 2019-07-24 stsp goto done;
5791 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5792 0ebf8283 2019-07-24 stsp worktree->path_prefix);
5794 0ebf8283 2019-07-24 stsp goto done;
5796 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5798 0ebf8283 2019-07-24 stsp goto done;
5800 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5802 0ebf8283 2019-07-24 stsp goto done;
5804 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5805 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5806 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5807 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5808 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5809 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5810 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5811 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
5812 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
5814 1f1abb7e 2019-08-08 stsp goto sync;
5816 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5817 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
5819 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5820 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
5821 0ebf8283 2019-07-24 stsp err = sync_err;
5823 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
5824 0ebf8283 2019-07-24 stsp free(tree_id);
5825 818c7501 2019-07-11 stsp free(fileindex_path);
5827 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5828 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5829 818c7501 2019-07-11 stsp err = unlockerr;
5830 818c7501 2019-07-11 stsp return err;
5833 0ebf8283 2019-07-24 stsp const struct got_error *
5834 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
5835 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5836 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
5838 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
5839 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
5840 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5842 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5844 0ebf8283 2019-07-24 stsp return err;
5846 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5847 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
5849 0ebf8283 2019-07-24 stsp goto done;
5851 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
5853 0ebf8283 2019-07-24 stsp goto done;
5855 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
5857 0ebf8283 2019-07-24 stsp goto done;
5859 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5861 0ebf8283 2019-07-24 stsp goto done;
5863 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5865 3e3a69f1 2019-07-25 stsp if (fileindex)
5866 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5867 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
5868 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5869 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5870 0ebf8283 2019-07-24 stsp err = unlockerr;
5871 0ebf8283 2019-07-24 stsp return err;
5874 0ebf8283 2019-07-24 stsp const struct got_error *
5875 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5876 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5878 0ebf8283 2019-07-24 stsp const struct got_error *err;
5879 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5881 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5883 0ebf8283 2019-07-24 stsp return err;
5885 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5887 0ebf8283 2019-07-24 stsp goto done;
5889 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5891 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5892 2822a352 2019-10-15 stsp return err;
5895 2822a352 2019-10-15 stsp const struct got_error *
5896 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5897 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5898 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
5899 2822a352 2019-10-15 stsp struct got_repository *repo)
5901 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
5902 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
5903 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
5905 2822a352 2019-10-15 stsp *fileindex = NULL;
5906 2822a352 2019-10-15 stsp *branch_ref = NULL;
5907 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
5909 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
5911 2822a352 2019-10-15 stsp return err;
5913 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5914 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5915 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
5916 2822a352 2019-10-15 stsp "update -b or different branch name required");
5917 2822a352 2019-10-15 stsp goto done;
5920 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5922 2822a352 2019-10-15 stsp goto done;
5924 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
5925 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
5926 2822a352 2019-10-15 stsp ok_arg.repo = repo;
5927 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5930 2822a352 2019-10-15 stsp goto done;
5932 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
5934 2822a352 2019-10-15 stsp goto done;
5936 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
5937 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
5939 2822a352 2019-10-15 stsp if (err) {
5940 2822a352 2019-10-15 stsp if (*branch_ref) {
5941 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
5942 2822a352 2019-10-15 stsp *branch_ref = NULL;
5944 2822a352 2019-10-15 stsp if (*base_branch_ref) {
5945 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
5946 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
5948 2822a352 2019-10-15 stsp if (*fileindex) {
5949 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
5950 2822a352 2019-10-15 stsp *fileindex = NULL;
5952 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
5954 0cb83759 2019-08-03 stsp return err;
5957 2822a352 2019-10-15 stsp const struct got_error *
5958 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
5959 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5960 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5961 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5962 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5964 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5965 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
5966 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
5968 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
5970 2822a352 2019-10-15 stsp goto done;
5972 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
5974 2822a352 2019-10-15 stsp goto done;
5976 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
5977 2822a352 2019-10-15 stsp worktree->path_prefix);
5979 2822a352 2019-10-15 stsp goto done;
5981 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5983 2822a352 2019-10-15 stsp goto done;
5985 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5986 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
5988 2822a352 2019-10-15 stsp goto sync;
5990 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
5992 2822a352 2019-10-15 stsp goto sync;
5994 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
5996 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5997 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
5998 2822a352 2019-10-15 stsp err = sync_err;
6001 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6002 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6003 2822a352 2019-10-15 stsp err = unlockerr;
6004 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6006 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6007 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6008 2822a352 2019-10-15 stsp err = unlockerr;
6009 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6011 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6012 2822a352 2019-10-15 stsp free(fileindex_path);
6013 2822a352 2019-10-15 stsp free(tree_id);
6015 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6016 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6017 2822a352 2019-10-15 stsp err = unlockerr;
6018 2822a352 2019-10-15 stsp return err;
6021 2822a352 2019-10-15 stsp const struct got_error *
6022 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6023 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6024 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6026 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6028 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6030 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6032 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6033 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6034 8b692cd0 2019-10-21 stsp err = unlockerr;
6035 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6037 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6038 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6039 8b692cd0 2019-10-21 stsp err = unlockerr;
6040 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6042 8b692cd0 2019-10-21 stsp return err;
6045 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6046 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6047 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6048 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6049 2db2652d 2019-08-07 stsp struct got_repository *repo;
6050 2db2652d 2019-08-07 stsp int have_changes;
6053 2db2652d 2019-08-07 stsp const struct got_error *
6054 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6055 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6056 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6057 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6059 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6060 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6061 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6062 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6063 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6064 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6066 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6067 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6068 8b13ce36 2019-08-08 stsp return NULL;
6069 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6070 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6072 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6073 735ef5ac 2019-08-03 stsp if (ie == NULL)
6074 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6076 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6077 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6078 735ef5ac 2019-08-03 stsp relpath) == -1)
6079 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6081 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6082 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6083 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6084 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6087 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6088 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6089 3aa5969e 2019-08-06 stsp goto done;
6090 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6091 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6092 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6093 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6094 735ef5ac 2019-08-03 stsp goto done;
6097 2db2652d 2019-08-07 stsp a->have_changes = 1;
6099 735ef5ac 2019-08-03 stsp p = in_repo_path;
6100 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6102 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6103 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6104 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6106 735ef5ac 2019-08-03 stsp free(in_repo_path);
6107 dc424a06 2019-08-07 stsp return err;
6110 2db2652d 2019-08-07 stsp struct stage_path_arg {
6111 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6112 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6113 2db2652d 2019-08-07 stsp struct got_repository *repo;
6114 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6115 2db2652d 2019-08-07 stsp void *status_arg;
6116 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6117 2db2652d 2019-08-07 stsp void *patch_arg;
6118 7b5dc508 2019-10-28 stsp int staged_something;
6121 2db2652d 2019-08-07 stsp static const struct got_error *
6122 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6123 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6124 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6125 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6127 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6128 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6129 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6130 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6131 0cb83759 2019-08-03 stsp uint32_t stage;
6132 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6134 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6135 8b13ce36 2019-08-08 stsp return NULL;
6137 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6138 d3e7c587 2019-08-03 stsp if (ie == NULL)
6139 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6141 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6142 2db2652d 2019-08-07 stsp relpath)== -1)
6143 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6145 0cb83759 2019-08-03 stsp switch (status) {
6146 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6147 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6148 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6149 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6150 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6151 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6152 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6155 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6158 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6159 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6160 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6161 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6162 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6166 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6167 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6170 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6171 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6172 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6173 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6175 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6176 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6177 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6178 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6180 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6181 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
6182 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
6184 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
6185 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
6187 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6188 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6189 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
6190 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
6193 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6195 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6196 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6200 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
6201 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6202 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6203 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6205 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6206 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6209 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
6211 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
6212 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6214 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
6215 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
6218 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6222 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
6223 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
6224 dc424a06 2019-08-07 stsp free(path_content);
6225 2db2652d 2019-08-07 stsp free(ondisk_path);
6226 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
6227 0ebf8283 2019-07-24 stsp return err;
6230 0cb83759 2019-08-03 stsp const struct got_error *
6231 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
6232 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
6233 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
6234 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6235 1e71573e 2019-08-03 stsp struct got_repository *repo)
6237 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6238 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
6239 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6240 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
6241 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
6242 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
6243 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
6244 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
6246 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6248 0cb83759 2019-08-03 stsp return err;
6250 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
6251 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
6253 735ef5ac 2019-08-03 stsp goto done;
6254 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6256 735ef5ac 2019-08-03 stsp goto done;
6257 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6259 0cb83759 2019-08-03 stsp goto done;
6261 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
6262 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
6263 2db2652d 2019-08-07 stsp oka.worktree = worktree;
6264 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
6265 2db2652d 2019-08-07 stsp oka.repo = repo;
6266 2db2652d 2019-08-07 stsp oka.have_changes = 0;
6267 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6268 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6269 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
6271 735ef5ac 2019-08-03 stsp goto done;
6273 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
6274 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6275 2db2652d 2019-08-07 stsp goto done;
6278 2db2652d 2019-08-07 stsp spa.worktree = worktree;
6279 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
6280 2db2652d 2019-08-07 stsp spa.repo = repo;
6281 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
6282 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
6283 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
6284 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
6285 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
6286 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6287 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6288 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
6290 2db2652d 2019-08-07 stsp goto done;
6292 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
6293 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6294 7b5dc508 2019-10-28 stsp goto done;
6297 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6298 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
6299 0cb83759 2019-08-03 stsp err = sync_err;
6301 735ef5ac 2019-08-03 stsp if (head_ref)
6302 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
6303 735ef5ac 2019-08-03 stsp free(head_commit_id);
6304 0cb83759 2019-08-03 stsp free(fileindex_path);
6305 0cb83759 2019-08-03 stsp if (fileindex)
6306 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
6307 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6308 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
6309 0cb83759 2019-08-03 stsp err = unlockerr;
6310 0cb83759 2019-08-03 stsp return err;
6313 ad493afc 2019-08-03 stsp struct unstage_path_arg {
6314 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
6315 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
6316 ad493afc 2019-08-03 stsp struct got_repository *repo;
6317 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
6318 ad493afc 2019-08-03 stsp void *progress_arg;
6319 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
6320 2e1f37b0 2019-08-08 stsp void *patch_arg;
6323 2e1f37b0 2019-08-08 stsp static const struct got_error *
6324 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
6325 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
6326 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
6327 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
6328 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
6330 2e1f37b0 2019-08-08 stsp const struct got_error *err;
6331 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
6332 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6333 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6334 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
6335 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
6336 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
6337 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
6338 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
6339 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6340 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
6342 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6343 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6345 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
6347 2e1f37b0 2019-08-08 stsp return err;
6348 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6350 2e1f37b0 2019-08-08 stsp goto done;
6352 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6354 2e1f37b0 2019-08-08 stsp goto done;
6356 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6358 2e1f37b0 2019-08-08 stsp goto done;
6360 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6362 2e1f37b0 2019-08-08 stsp goto done;
6364 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6366 2e1f37b0 2019-08-08 stsp goto done;
6368 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6370 2e1f37b0 2019-08-08 stsp goto done;
6372 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
6373 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
6374 2e1f37b0 2019-08-08 stsp goto done;
6377 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
6378 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
6379 2e1f37b0 2019-08-08 stsp goto done;
6382 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
6383 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6385 2e1f37b0 2019-08-08 stsp goto done;
6387 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
6388 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
6390 2e1f37b0 2019-08-08 stsp goto done;
6391 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
6392 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
6394 2e1f37b0 2019-08-08 stsp goto done;
6396 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
6397 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
6398 2e1f37b0 2019-08-08 stsp goto done;
6400 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
6401 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
6402 2e1f37b0 2019-08-08 stsp goto done;
6404 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6405 2e1f37b0 2019-08-08 stsp int choice;
6406 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
6407 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
6408 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
6409 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
6411 2e1f37b0 2019-08-08 stsp goto done;
6412 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
6413 2e1f37b0 2019-08-08 stsp have_content = 1;
6415 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
6416 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
6419 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
6420 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6421 f1e81a05 2019-08-10 stsp outfile, rejectfile);
6423 2e1f37b0 2019-08-08 stsp free(label1);
6425 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
6426 2e1f37b0 2019-08-08 stsp if (staged_blob)
6427 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
6428 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
6429 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
6430 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
6431 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
6432 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
6433 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
6434 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6435 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
6436 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
6437 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
6438 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
6439 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
6440 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
6441 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
6442 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
6443 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6444 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
6445 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
6446 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6448 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
6449 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
6450 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
6451 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6452 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
6453 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
6454 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6456 2e1f37b0 2019-08-08 stsp free(args);
6458 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
6461 2e1f37b0 2019-08-08 stsp if (changes)
6462 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
6463 2e1f37b0 2019-08-08 stsp free(path1);
6464 2e1f37b0 2019-08-08 stsp free(path2);
6465 2e1f37b0 2019-08-08 stsp return err;
6468 ad493afc 2019-08-03 stsp static const struct got_error *
6469 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
6470 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
6471 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6472 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6474 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
6475 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
6476 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
6477 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6478 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
6479 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
6480 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
6481 ad493afc 2019-08-03 stsp int local_changes_subsumed;
6482 9bc94a15 2019-08-03 stsp struct stat sb;
6484 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
6485 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
6486 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
6487 2e1f37b0 2019-08-08 stsp return NULL;
6489 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6490 ad493afc 2019-08-03 stsp if (ie == NULL)
6491 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6493 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6495 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
6497 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
6498 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
6500 f69721c3 2019-10-21 stsp goto done;
6501 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6502 f69721c3 2019-10-21 stsp id_str) == -1) {
6503 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
6504 f69721c3 2019-10-21 stsp goto done;
6507 ad493afc 2019-08-03 stsp switch (staged_status) {
6508 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
6509 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
6510 ad493afc 2019-08-03 stsp blob_id, 8192);
6513 ad493afc 2019-08-03 stsp /* fall through */
6514 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
6515 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6516 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
6517 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6518 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6519 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6522 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
6525 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
6526 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
6527 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
6528 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
6529 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
6530 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
6532 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
6533 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
6534 2e1f37b0 2019-08-08 stsp &staged_blob_id,
6535 2e1f37b0 2019-08-08 stsp path_new_staged_content,
6539 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
6540 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
6541 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
6543 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
6544 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
6545 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
6546 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
6547 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
6548 f69721c3 2019-10-21 stsp a->progress_arg);
6549 2e1f37b0 2019-08-08 stsp if (err == NULL &&
6550 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
6551 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
6552 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
6553 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
6556 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
6557 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
6560 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
6561 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
6562 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6563 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
6564 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
6565 ad493afc 2019-08-03 stsp if (err == NULL)
6566 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6567 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6569 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
6570 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6571 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6572 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6573 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6576 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6578 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6579 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6583 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6584 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
6585 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
6588 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
6592 ad493afc 2019-08-03 stsp free(ondisk_path);
6593 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
6594 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
6595 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
6596 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
6597 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
6598 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
6599 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
6600 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
6601 ad493afc 2019-08-03 stsp if (blob_base)
6602 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
6603 ad493afc 2019-08-03 stsp if (blob_staged)
6604 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
6605 f69721c3 2019-10-21 stsp free(id_str);
6606 f69721c3 2019-10-21 stsp free(label_orig);
6607 ad493afc 2019-08-03 stsp return err;
6610 ad493afc 2019-08-03 stsp const struct got_error *
6611 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
6612 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
6613 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6614 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6615 ad493afc 2019-08-03 stsp struct got_repository *repo)
6617 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6618 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6619 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6620 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
6621 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
6623 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6625 ad493afc 2019-08-03 stsp return err;
6627 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6629 ad493afc 2019-08-03 stsp goto done;
6631 ad493afc 2019-08-03 stsp upa.worktree = worktree;
6632 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
6633 ad493afc 2019-08-03 stsp upa.repo = repo;
6634 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
6635 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
6636 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
6637 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
6638 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6639 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6640 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
6642 ad493afc 2019-08-03 stsp goto done;
6645 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6646 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
6647 ad493afc 2019-08-03 stsp err = sync_err;
6649 ad493afc 2019-08-03 stsp free(fileindex_path);
6650 ad493afc 2019-08-03 stsp if (fileindex)
6651 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
6652 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6653 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
6654 ad493afc 2019-08-03 stsp err = unlockerr;
6655 ad493afc 2019-08-03 stsp return err;