2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.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 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
121 09fe317a 2018-03-11 stsp struct stat sb;
123 09fe317a 2018-03-11 stsp *content = NULL;
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
145 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
175 09fe317a 2018-03-11 stsp return err;
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
198 86c3caaf 2018-03-09 stsp const struct got_error *
199 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
200 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
202 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
203 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
204 ec22038e 2019-03-10 stsp uuid_t uuid;
205 ec22038e 2019-03-10 stsp uint32_t uuid_status;
206 65596e15 2018-12-24 stsp int obj_type;
207 7ac97322 2018-03-11 stsp char *path_got = NULL;
208 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
361 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
366 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
367 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
368 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
371 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
374 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
378 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
379 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
383 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
388 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
389 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
393 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
398 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
402 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
406 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 eaccb85f 2018-12-25 stsp base_commit_id_str);
411 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
415 eaccb85f 2018-12-25 stsp got_repo_close(repo);
416 7ac97322 2018-03-11 stsp free(path_got);
417 056e7441 2018-03-11 stsp free(path_lock);
418 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
419 c442a90d 2019-03-10 stsp free(uuidstr);
420 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
425 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
426 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp return err;
433 247140b2 2019-02-05 stsp const struct got_error *
434 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
439 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
440 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 247140b2 2019-02-05 stsp return err;
442 247140b2 2019-02-05 stsp if (*worktree)
443 247140b2 2019-02-05 stsp return NULL;
444 247140b2 2019-02-05 stsp path = dirname(path);
445 d1542a27 2019-02-05 stsp if (path == NULL)
446 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
447 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
452 3a6ce05a 2019-02-11 stsp const struct got_error *
453 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
456 c88eb298 2018-03-11 stsp free(worktree->root_path);
457 cde76477 2018-03-11 stsp free(worktree->repo_path);
458 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
459 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
460 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
461 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
462 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
463 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
464 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
465 6d9d28c3 2018-03-11 stsp free(worktree);
466 3a6ce05a 2019-02-11 stsp return err;
469 2fbdb5ae 2018-12-29 stsp const char *
470 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp return worktree->root_path;
475 c7f4312f 2019-02-05 stsp const char *
476 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
481 49520a32 2018-12-29 stsp const char *
482 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 f609be2e 2018-12-29 stsp return worktree->path_prefix;
487 e5dc7198 2018-12-29 stsp const struct got_error *
488 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
494 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
498 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
499 e5dc7198 2018-12-29 stsp free(absprefix);
500 e5dc7198 2018-12-29 stsp return NULL;
503 bc70eb79 2019-05-09 stsp const char *
504 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 36a38700 2019-05-10 stsp return worktree->head_ref_name;
509 024e9686 2019-05-14 stsp const struct got_error *
510 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
511 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
514 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
518 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
519 024e9686 2019-05-14 stsp path_got = NULL;
523 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
524 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
525 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
529 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
533 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
534 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp free(head_ref_name);
539 024e9686 2019-05-14 stsp return err;
542 b72f483a 2019-02-05 stsp struct got_object_id *
543 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
548 507dc3bb 2018-12-29 stsp const struct got_error *
549 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp const struct got_error *err;
553 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
554 507dc3bb 2018-12-29 stsp char *id_str = NULL;
555 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
560 507dc3bb 2018-12-29 stsp path_got = NULL;
564 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
573 507dc3bb 2018-12-29 stsp /* Record our base commit. */
574 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
577 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
582 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
583 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
589 507dc3bb 2018-12-29 stsp got_object_close(obj);
590 507dc3bb 2018-12-29 stsp free(id_str);
591 507dc3bb 2018-12-29 stsp free(path_got);
592 507dc3bb 2018-12-29 stsp return err;
595 9d31a1d8 2018-03-11 stsp static const struct got_error *
596 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
601 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
602 86c3caaf 2018-03-09 stsp return NULL;
605 21908da4 2019-01-13 stsp static const struct got_error *
606 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
609 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
615 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 ddcd8544 2019-03-11 stsp struct stat sb;
617 ddcd8544 2019-03-11 stsp err = NULL;
618 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
619 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
620 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
621 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
622 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 4a1ddfc2 2019-01-12 stsp free(abspath);
626 68c76935 2019-02-19 stsp return err;
629 68c76935 2019-02-19 stsp static const struct got_error *
630 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
633 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
634 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
635 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
640 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
642 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
645 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
647 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
650 68c76935 2019-02-19 stsp if (flen1 == 0) {
651 68c76935 2019-02-19 stsp if (flen2 != 0)
654 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
655 68c76935 2019-02-19 stsp if (flen1 != 0)
658 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
659 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
669 68c76935 2019-02-19 stsp return err;
672 68c76935 2019-02-19 stsp static const struct got_error *
673 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
676 68c76935 2019-02-19 stsp struct stat sb;
677 68c76935 2019-02-19 stsp size_t size1, size2;
678 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
682 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
686 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
692 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp return NULL;
699 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
700 68c76935 2019-02-19 stsp if (f1 == NULL)
701 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
704 68c76935 2019-02-19 stsp if (f2 == NULL) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
709 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
712 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
713 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 6353ad76 2019-02-08 stsp return err;
720 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
722 234035bc 2019-06-01 stsp * acts as the second derived version.
724 6353ad76 2019-02-08 stsp static const struct got_error *
725 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
727 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
729 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 234035bc 2019-06-01 stsp void *progress_arg)
732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
733 6353ad76 2019-02-08 stsp int merged_fd = -1;
734 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
735 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
737 6353ad76 2019-02-08 stsp char *id_str = NULL;
738 818c7501 2019-07-11 stsp char *label_deriv = NULL;
739 234035bc 2019-06-01 stsp int overlapcnt = 0;
740 af54ae4a 2019-02-19 stsp char *parent;
742 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
744 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
745 af54ae4a 2019-02-19 stsp if (parent == NULL)
746 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
748 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
751 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 af54ae4a 2019-02-19 stsp free(base_path);
756 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
758 af54ae4a 2019-02-19 stsp base_path = NULL;
762 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
765 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 6c4c42e0 2019-06-24 stsp blob_deriv);
770 af54ae4a 2019-02-19 stsp free(base_path);
771 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
773 af54ae4a 2019-02-19 stsp base_path = NULL;
777 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
780 46b6ee73 2019-06-04 stsp if (blob_orig) {
781 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 46b6ee73 2019-06-04 stsp blob_orig);
787 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
788 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
789 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
793 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
796 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
801 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
806 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
807 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
814 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
815 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
816 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
817 68c76935 2019-02-19 stsp merged_path);
822 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
823 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
827 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
828 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
829 230a42bd 2019-05-11 jcs ondisk_path);
830 2a57020b 2019-02-20 stsp unlink(merged_path);
835 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
836 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
837 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
838 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
839 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
840 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
841 6353ad76 2019-02-08 stsp free(merged_path);
842 af54ae4a 2019-02-19 stsp free(base_path);
843 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
844 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
845 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
847 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
848 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
849 46b6ee73 2019-06-04 stsp free(blob_orig_path);
851 6353ad76 2019-02-08 stsp free(id_str);
852 818c7501 2019-07-11 stsp free(label_deriv);
853 4a1ddfc2 2019-01-12 stsp return err;
856 4a1ddfc2 2019-01-12 stsp static const struct got_error *
857 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
858 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
859 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
860 13d9040b 2019-03-27 stsp int update_timestamps)
862 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
864 13d9040b 2019-03-27 stsp if (ie == NULL)
865 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
867 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
868 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
869 13d9040b 2019-03-27 stsp update_timestamps);
871 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
872 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
873 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
875 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
877 13d9040b 2019-03-27 stsp return err;
880 13d9040b 2019-03-27 stsp static const struct got_error *
881 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
882 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
883 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
884 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
885 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
887 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
888 507dc3bb 2018-12-29 stsp int fd = -1;
889 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
890 507dc3bb 2018-12-29 stsp int update = 0;
891 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
893 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
894 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
895 9d31a1d8 2018-03-11 stsp if (fd == -1) {
896 21908da4 2019-01-13 stsp if (errno == ENOENT) {
897 21908da4 2019-01-13 stsp char *parent = dirname(path);
898 21908da4 2019-01-13 stsp if (parent == NULL)
899 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
900 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
902 21908da4 2019-01-13 stsp return err;
903 21908da4 2019-01-13 stsp fd = open(ondisk_path,
904 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
905 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
906 21908da4 2019-01-13 stsp if (fd == -1)
907 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
908 230a42bd 2019-05-11 jcs ondisk_path);
909 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
910 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
911 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
912 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
915 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
916 507dc3bb 2018-12-29 stsp ondisk_path);
919 507dc3bb 2018-12-29 stsp update = 1;
922 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
925 a378724f 2019-02-10 stsp if (restoring_missing_file)
926 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
927 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
928 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
930 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
931 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
933 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
935 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
936 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
939 9d31a1d8 2018-03-11 stsp if (len > 0) {
940 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
941 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
942 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
943 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
945 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
946 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
949 61d6eaa3 2018-12-24 stsp hdrlen = 0;
951 9d31a1d8 2018-03-11 stsp } while (len != 0);
953 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
954 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
958 507dc3bb 2018-12-29 stsp if (update) {
959 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
960 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
961 230a42bd 2019-05-11 jcs ondisk_path);
962 2a57020b 2019-02-20 stsp unlink(tmppath);
967 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
968 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
969 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
973 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
980 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
981 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
982 507dc3bb 2018-12-29 stsp free(tmppath);
983 6353ad76 2019-02-08 stsp return err;
986 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
987 6353ad76 2019-02-08 stsp static const struct got_error *
988 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
990 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
991 7154f6ce 2019-03-27 stsp const char *markers[3] = {
992 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
993 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
994 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
997 7154f6ce 2019-03-27 stsp char *line;
998 7154f6ce 2019-03-27 stsp size_t len;
999 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1001 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1002 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1003 7154f6ce 2019-03-27 stsp if (line == NULL) {
1004 7154f6ce 2019-03-27 stsp if (feof(f))
1006 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1010 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1011 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1013 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1019 7154f6ce 2019-03-27 stsp return err;
1022 7154f6ce 2019-03-27 stsp static const struct got_error *
1023 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1024 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1025 b8f41171 2019-02-10 stsp struct got_repository *repo)
1027 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1028 6353ad76 2019-02-08 stsp struct got_object_id id;
1029 6353ad76 2019-02-08 stsp size_t hdrlen;
1030 6353ad76 2019-02-08 stsp FILE *f = NULL;
1031 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1032 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1033 6353ad76 2019-02-08 stsp size_t flen, blen;
1035 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1037 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1038 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1040 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1041 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1043 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1044 b8f41171 2019-02-10 stsp sb->st_mode =
1045 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1046 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1048 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1049 a378724f 2019-02-10 stsp return NULL;
1051 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1054 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1055 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1056 6353ad76 2019-02-08 stsp return NULL;
1059 b8f41171 2019-02-10 stsp if (ie == NULL)
1060 d00136be 2019-03-26 stsp return NULL;
1062 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1063 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1064 2ec1f75b 2019-03-26 stsp return NULL;
1065 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1066 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1067 b8f41171 2019-02-10 stsp return NULL;
1070 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1071 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1072 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1073 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1074 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1075 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1076 6353ad76 2019-02-08 stsp return NULL;
1078 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1079 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1081 6353ad76 2019-02-08 stsp return err;
1083 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1084 6353ad76 2019-02-08 stsp if (f == NULL) {
1085 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1086 6353ad76 2019-02-08 stsp goto done;
1088 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1090 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1091 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1093 7154f6ce 2019-03-27 stsp goto done;
1094 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1095 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1096 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1097 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1098 7154f6ce 2019-03-27 stsp goto done;
1100 6353ad76 2019-02-08 stsp if (blen == 0) {
1101 6353ad76 2019-02-08 stsp if (flen != 0)
1102 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1104 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1105 6353ad76 2019-02-08 stsp if (blen != 0)
1106 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1108 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1109 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1110 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1111 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1115 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1118 6353ad76 2019-02-08 stsp hdrlen = 0;
1121 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1122 7154f6ce 2019-03-27 stsp rewind(f);
1123 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1127 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1129 6353ad76 2019-02-08 stsp fclose(f);
1130 9d31a1d8 2018-03-11 stsp return err;
1133 9d31a1d8 2018-03-11 stsp static const struct got_error *
1134 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1135 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1136 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1137 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1138 0584f854 2019-04-06 stsp void *progress_arg)
1140 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1141 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1142 6353ad76 2019-02-08 stsp char *ondisk_path;
1143 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1144 b8f41171 2019-02-10 stsp struct stat sb;
1146 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1147 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1149 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1151 b8f41171 2019-02-10 stsp goto done;
1153 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1154 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1155 b8f41171 2019-02-10 stsp goto done;
1158 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1159 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1160 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1161 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1162 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1164 6353ad76 2019-02-08 stsp goto done;
1166 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1167 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1168 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1169 b8f41171 2019-02-10 stsp goto done;
1172 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1174 6353ad76 2019-02-08 stsp goto done;
1176 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1177 234035bc 2019-06-01 stsp int update_timestamps;
1178 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1179 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1180 234035bc 2019-06-01 stsp struct got_object_id id2;
1181 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1182 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1184 234035bc 2019-06-01 stsp goto done;
1186 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1187 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1188 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1189 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1190 234035bc 2019-06-01 stsp if (blob2)
1191 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1193 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1194 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1195 234035bc 2019-06-01 stsp * unmodified files again.
1197 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1198 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1199 234035bc 2019-06-01 stsp update_timestamps);
1200 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1201 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1202 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1203 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1205 13d9040b 2019-03-27 stsp goto done;
1207 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1208 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1209 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1211 13d9040b 2019-03-27 stsp goto done;
1212 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1213 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1215 13d9040b 2019-03-27 stsp goto done;
1217 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1219 6353ad76 2019-02-08 stsp free(ondisk_path);
1220 8da9e5f4 2019-01-12 stsp return err;
1223 90285c3b 2019-01-08 stsp static const struct got_error *
1224 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1226 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1227 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1229 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1230 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1232 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1233 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1234 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1236 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1237 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1238 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1239 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1240 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1244 90285c3b 2019-01-08 stsp parent = dirname(parent);
1247 90285c3b 2019-01-08 stsp free(ondisk_path);
1248 90285c3b 2019-01-08 stsp return err;
1251 708d8e67 2019-03-27 stsp static const struct got_error *
1252 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1253 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1254 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1257 708d8e67 2019-03-27 stsp unsigned char status;
1258 708d8e67 2019-03-27 stsp struct stat sb;
1259 708d8e67 2019-03-27 stsp char *ondisk_path;
1261 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1263 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1265 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1267 708d8e67 2019-03-27 stsp return err;
1269 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1270 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1271 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1273 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1274 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1276 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1279 708d8e67 2019-03-27 stsp return err;
1281 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1282 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1283 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1285 fc6346c4 2019-03-27 stsp return err;
1287 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1290 fc6346c4 2019-03-27 stsp return err;
1293 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1294 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1295 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1296 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1297 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1298 8da9e5f4 2019-01-12 stsp void *progress_arg;
1299 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1300 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1303 512f0d0e 2019-01-02 stsp static const struct got_error *
1304 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1305 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1307 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1309 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1310 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1312 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1313 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1316 8da9e5f4 2019-01-12 stsp static const struct got_error *
1317 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1319 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1321 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1322 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1324 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1325 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1328 9d31a1d8 2018-03-11 stsp static const struct got_error *
1329 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1331 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1332 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1333 8da9e5f4 2019-01-12 stsp char *path;
1335 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1336 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1338 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1339 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1341 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1343 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1344 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1346 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1347 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1349 8da9e5f4 2019-01-12 stsp free(path);
1350 0cd1c46a 2019-03-11 stsp return err;
1353 818c7501 2019-07-11 stsp static const struct got_error *
1354 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1356 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1357 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1358 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1360 517bab73 2019-03-11 stsp *refname = NULL;
1362 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1363 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1364 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1366 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1368 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1369 0647c563 2019-03-11 stsp *refname = NULL;
1371 517bab73 2019-03-11 stsp free(uuidstr);
1372 517bab73 2019-03-11 stsp return err;
1375 818c7501 2019-07-11 stsp const struct got_error *
1376 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1378 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1381 818c7501 2019-07-11 stsp static const struct got_error *
1382 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1384 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1385 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1388 818c7501 2019-07-11 stsp static const struct got_error *
1389 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1391 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1394 818c7501 2019-07-11 stsp static const struct got_error *
1395 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1397 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1398 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1401 818c7501 2019-07-11 stsp static const struct got_error *
1402 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1404 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1405 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1410 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1411 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1413 517bab73 2019-03-11 stsp static const struct got_error *
1414 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1416 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1417 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1418 517bab73 2019-03-11 stsp char *refname;
1420 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1422 517bab73 2019-03-11 stsp return err;
1424 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1426 0cd1c46a 2019-03-11 stsp goto done;
1428 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1430 0cd1c46a 2019-03-11 stsp free(refname);
1432 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1433 9d31a1d8 2018-03-11 stsp return err;
1436 ebf99748 2019-05-09 stsp static const struct got_error *
1437 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1438 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1440 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1441 ebf99748 2019-05-09 stsp FILE *index = NULL;
1443 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1444 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1445 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1446 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1448 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1449 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1450 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1451 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1452 ebf99748 2019-05-09 stsp goto done;
1455 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1456 ebf99748 2019-05-09 stsp if (index == NULL) {
1457 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1458 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1460 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1461 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1462 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1465 ebf99748 2019-05-09 stsp if (err) {
1466 ebf99748 2019-05-09 stsp free(*fileindex_path);
1467 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1468 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1469 ebf99748 2019-05-09 stsp *fileindex = NULL;
1471 ebf99748 2019-05-09 stsp return err;
1474 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1475 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1476 c932eeeb 2019-05-22 stsp const char *path;
1477 c932eeeb 2019-05-22 stsp size_t path_len;
1478 c932eeeb 2019-05-22 stsp const char *entry_name;
1479 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1480 a484d721 2019-06-10 stsp void *progress_arg;
1483 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1484 c932eeeb 2019-05-22 stsp static const struct got_error *
1485 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1487 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1489 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1490 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1491 c932eeeb 2019-05-22 stsp return NULL;
1492 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1493 102ff934 2019-06-11 stsp return NULL;
1495 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1496 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1497 c932eeeb 2019-05-22 stsp return NULL;
1499 a484d721 2019-06-10 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1500 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1501 c932eeeb 2019-05-22 stsp return NULL;
1504 9c6338c4 2019-06-02 stsp static const struct got_error *
1505 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1507 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1508 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1509 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1511 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1512 9c6338c4 2019-06-02 stsp fileindex_path);
1514 9c6338c4 2019-06-02 stsp goto done;
1516 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1518 9c6338c4 2019-06-02 stsp goto done;
1520 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1521 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1522 9c6338c4 2019-06-02 stsp fileindex_path);
1523 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1526 9c6338c4 2019-06-02 stsp if (new_index)
1527 9c6338c4 2019-06-02 stsp fclose(new_index);
1528 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1529 9c6338c4 2019-06-02 stsp return err;
1532 9d31a1d8 2018-03-11 stsp const struct got_error *
1533 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1534 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1535 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1537 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1538 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1539 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1540 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1541 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1542 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1543 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1544 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1545 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1546 af12c6b9 2019-06-04 stsp struct bump_base_commit_id_arg bbc_arg;
1548 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1550 9d31a1d8 2018-03-11 stsp return err;
1553 51514078 2018-12-25 stsp * Read the file index.
1554 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1555 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1557 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1559 ebf99748 2019-05-09 stsp goto done;
1561 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1563 0cd1c46a 2019-03-11 stsp goto done;
1565 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1566 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1568 9d31a1d8 2018-03-11 stsp goto done;
1570 c4cdcb68 2019-04-03 stsp if (path[0]) {
1571 c4cdcb68 2019-04-03 stsp char *tree_path;
1572 c4cdcb68 2019-04-03 stsp int obj_type;
1573 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1574 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1575 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1576 c4cdcb68 2019-04-03 stsp goto done;
1578 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1579 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1580 c4cdcb68 2019-04-03 stsp path) == -1) {
1581 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1582 c4cdcb68 2019-04-03 stsp goto done;
1584 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1585 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1586 c4cdcb68 2019-04-03 stsp free(tree_path);
1588 c4cdcb68 2019-04-03 stsp goto done;
1589 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1591 c4cdcb68 2019-04-03 stsp goto done;
1592 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1593 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1594 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1595 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1596 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1597 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1598 c4cdcb68 2019-04-03 stsp goto done;
1600 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1601 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1602 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1603 c4cdcb68 2019-04-03 stsp goto done;
1606 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1608 c4cdcb68 2019-04-03 stsp goto done;
1609 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1610 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1611 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1612 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1613 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1615 c4cdcb68 2019-04-03 stsp goto done;
1618 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1619 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1620 c4cdcb68 2019-04-03 stsp free(tree_path);
1622 c4cdcb68 2019-04-03 stsp goto done;
1623 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1624 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1625 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1626 c4cdcb68 2019-04-03 stsp goto done;
1630 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1631 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1632 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1633 c4cdcb68 2019-04-03 stsp goto done;
1635 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1636 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1638 c4cdcb68 2019-04-03 stsp goto done;
1641 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1643 c4cdcb68 2019-04-03 stsp goto done;
1645 c4cdcb68 2019-04-03 stsp if (entry_name &&
1646 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1647 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1648 8da9e5f4 2019-01-12 stsp goto done;
1651 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1652 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1653 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1654 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1655 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1656 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1657 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1658 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1659 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1660 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1661 af12c6b9 2019-06-04 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1662 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1664 af12c6b9 2019-06-04 stsp goto sync;
1666 af12c6b9 2019-06-04 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1667 af12c6b9 2019-06-04 stsp bbc_arg.entry_name = entry_name;
1668 af12c6b9 2019-06-04 stsp bbc_arg.path = path;
1669 af12c6b9 2019-06-04 stsp bbc_arg.path_len = strlen(path);
1670 a484d721 2019-06-10 stsp bbc_arg.progress_cb = progress_cb;
1671 a484d721 2019-06-10 stsp bbc_arg.progress_arg = progress_arg;
1672 af12c6b9 2019-06-04 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1673 af12c6b9 2019-06-04 stsp bump_base_commit_id, &bbc_arg);
1675 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1676 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1677 af12c6b9 2019-06-04 stsp err = sync_err;
1679 9c6338c4 2019-06-02 stsp free(fileindex_path);
1680 c4cdcb68 2019-04-03 stsp free(relpath);
1682 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1683 9d31a1d8 2018-03-11 stsp if (commit)
1684 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1685 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1686 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1687 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1688 9d31a1d8 2018-03-11 stsp err = unlockerr;
1689 f8d1f275 2019-02-04 stsp return err;
1692 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1693 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1694 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1695 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1696 234035bc 2019-06-01 stsp void *progress_arg;
1697 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1698 234035bc 2019-06-01 stsp void *cancel_arg;
1699 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1702 234035bc 2019-06-01 stsp static const struct got_error *
1703 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1704 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1705 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1706 234035bc 2019-06-01 stsp struct got_repository *repo)
1708 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1709 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1710 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1711 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1712 234035bc 2019-06-01 stsp struct stat sb;
1713 234035bc 2019-06-01 stsp unsigned char status;
1714 234035bc 2019-06-01 stsp int local_changes_subsumed;
1716 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1717 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1718 234035bc 2019-06-01 stsp if (ie == NULL) {
1719 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1721 234035bc 2019-06-01 stsp return NULL;
1724 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1725 234035bc 2019-06-01 stsp path2) == -1)
1726 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1728 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1730 234035bc 2019-06-01 stsp goto done;
1732 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1733 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1735 234035bc 2019-06-01 stsp goto done;
1737 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1738 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1739 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1740 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1741 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status, path2);
1742 234035bc 2019-06-01 stsp goto done;
1745 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1746 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1747 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1748 234035bc 2019-06-01 stsp } else if (blob1) {
1749 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1750 234035bc 2019-06-01 stsp if (ie == NULL) {
1751 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1753 234035bc 2019-06-01 stsp return NULL;
1756 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1757 2b92fad7 2019-06-02 stsp path1) == -1)
1758 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1760 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1762 2b92fad7 2019-06-02 stsp goto done;
1764 2b92fad7 2019-06-02 stsp switch (status) {
1765 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1766 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1768 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1770 2b92fad7 2019-06-02 stsp goto done;
1772 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1774 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1775 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1776 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1779 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1781 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1782 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1783 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1784 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg,
1785 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1787 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1788 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, status, path1);
1793 234035bc 2019-06-01 stsp } else if (blob2) {
1794 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1795 234035bc 2019-06-01 stsp path2) == -1)
1796 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1797 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1799 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1802 234035bc 2019-06-01 stsp goto done;
1803 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1804 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1805 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1806 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1807 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status,
1809 234035bc 2019-06-01 stsp goto done;
1811 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1812 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
1813 818c7501 2019-07-11 stsp a->commit_id2, repo,
1814 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1815 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1816 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1817 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1818 234035bc 2019-06-01 stsp blob2, 0);
1820 234035bc 2019-06-01 stsp goto done;
1823 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1824 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1825 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1826 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1827 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1828 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1830 234035bc 2019-06-01 stsp goto done;
1831 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1832 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1834 234035bc 2019-06-01 stsp goto done;
1835 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1836 2b92fad7 2019-06-02 stsp if (err) {
1837 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1838 2b92fad7 2019-06-02 stsp goto done;
1843 234035bc 2019-06-01 stsp free(ondisk_path);
1844 234035bc 2019-06-01 stsp return err;
1847 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1848 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1849 234035bc 2019-06-01 stsp struct got_repository *repo;
1852 234035bc 2019-06-01 stsp static const struct got_error *
1853 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1855 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1856 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1857 234035bc 2019-06-01 stsp unsigned char status;
1858 234035bc 2019-06-01 stsp struct stat sb;
1859 234035bc 2019-06-01 stsp char *ondisk_path;
1861 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1862 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1863 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1864 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1866 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1868 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1870 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1871 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1873 234035bc 2019-06-01 stsp return err;
1874 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1875 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1877 234035bc 2019-06-01 stsp return NULL;
1880 818c7501 2019-07-11 stsp static const struct got_error *
1881 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1882 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
1883 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
1884 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1885 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1887 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
1888 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1889 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1890 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
1892 03415a1a 2019-06-02 stsp if (commit_id1) {
1893 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1894 03415a1a 2019-06-02 stsp worktree->path_prefix);
1896 03415a1a 2019-06-02 stsp goto done;
1898 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1900 03415a1a 2019-06-02 stsp goto done;
1903 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1904 234035bc 2019-06-01 stsp worktree->path_prefix);
1906 234035bc 2019-06-01 stsp goto done;
1908 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1910 234035bc 2019-06-01 stsp goto done;
1912 234035bc 2019-06-01 stsp arg.worktree = worktree;
1913 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
1914 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
1915 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
1916 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
1917 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
1918 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
1919 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1920 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1921 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1922 af12c6b9 2019-06-04 stsp err = sync_err;
1924 234035bc 2019-06-01 stsp if (tree1)
1925 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
1926 234035bc 2019-06-01 stsp if (tree2)
1927 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
1928 818c7501 2019-07-11 stsp return err;
1931 818c7501 2019-07-11 stsp const struct got_error *
1932 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
1933 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1934 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1935 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1937 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
1938 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
1939 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
1940 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
1942 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
1944 818c7501 2019-07-11 stsp return err;
1946 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1948 818c7501 2019-07-11 stsp goto done;
1950 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
1951 818c7501 2019-07-11 stsp mok_arg.repo = repo;
1952 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1953 818c7501 2019-07-11 stsp &mok_arg);
1955 818c7501 2019-07-11 stsp goto done;
1957 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
1958 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1960 818c7501 2019-07-11 stsp if (fileindex)
1961 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
1962 818c7501 2019-07-11 stsp free(fileindex_path);
1963 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1964 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
1965 234035bc 2019-06-01 stsp err = unlockerr;
1966 234035bc 2019-06-01 stsp return err;
1969 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1970 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1971 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1972 927df6b7 2019-02-10 stsp const char *status_path;
1973 927df6b7 2019-02-10 stsp size_t status_path_len;
1974 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1975 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1976 f8d1f275 2019-02-04 stsp void *status_arg;
1977 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1978 f8d1f275 2019-02-04 stsp void *cancel_arg;
1981 f8d1f275 2019-02-04 stsp static const struct got_error *
1982 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1983 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1984 927df6b7 2019-02-10 stsp struct got_repository *repo)
1986 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1987 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1988 927df6b7 2019-02-10 stsp struct stat sb;
1989 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1991 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1992 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1993 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1994 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1995 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1996 016a88dd 2019-05-13 stsp &commit_id);
1998 927df6b7 2019-02-10 stsp return err;
2001 927df6b7 2019-02-10 stsp static const struct got_error *
2002 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2003 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2005 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2006 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2007 f8d1f275 2019-02-04 stsp char *abspath;
2009 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2010 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2012 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
2013 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2014 927df6b7 2019-02-10 stsp return NULL;
2016 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2017 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2018 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2019 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2021 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2022 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2023 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2026 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2028 f8d1f275 2019-02-04 stsp free(abspath);
2029 9d31a1d8 2018-03-11 stsp return err;
2032 f8d1f275 2019-02-04 stsp static const struct got_error *
2033 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2035 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2036 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2037 2ec1f75b 2019-03-26 stsp unsigned char status;
2039 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2040 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2042 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2043 927df6b7 2019-02-10 stsp return NULL;
2045 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2046 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2047 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2048 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2050 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2051 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2052 016a88dd 2019-05-13 stsp &commit_id);
2055 f8d1f275 2019-02-04 stsp static const struct got_error *
2056 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2058 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2059 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2060 f8d1f275 2019-02-04 stsp char *path = NULL;
2062 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2063 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2065 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2066 2c201a36 2019-02-10 stsp return NULL;
2068 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2069 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2070 f8d1f275 2019-02-04 stsp return NULL;
2072 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2073 927df6b7 2019-02-10 stsp return NULL;
2075 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2076 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2077 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2079 f8d1f275 2019-02-04 stsp path = de->d_name;
2082 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2083 016a88dd 2019-05-13 stsp NULL, NULL);
2084 f8d1f275 2019-02-04 stsp if (parent_path[0])
2085 f8d1f275 2019-02-04 stsp free(path);
2086 b72f483a 2019-02-05 stsp return err;
2089 f8d1f275 2019-02-04 stsp const struct got_error *
2090 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2091 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2092 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2094 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2095 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2096 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
2097 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
2098 f8d1f275 2019-02-04 stsp FILE *index = NULL;
2099 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2100 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2101 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2103 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
2104 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
2105 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2106 f8d1f275 2019-02-04 stsp goto done;
2109 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2110 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2111 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2112 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
2113 f8d1f275 2019-02-04 stsp goto done;
2116 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
2117 f8d1f275 2019-02-04 stsp if (index == NULL) {
2118 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
2119 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2120 f8d1f275 2019-02-04 stsp goto done;
2123 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
2124 f8d1f275 2019-02-04 stsp fclose(index);
2126 f8d1f275 2019-02-04 stsp goto done;
2129 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2130 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2132 c513d110 2019-02-05 stsp goto done;
2134 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2135 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2136 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2137 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2138 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2139 927df6b7 2019-02-10 stsp if (ie == NULL) {
2140 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2141 927df6b7 2019-02-10 stsp goto done;
2143 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2144 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2145 927df6b7 2019-02-10 stsp goto done;
2147 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2148 927df6b7 2019-02-10 stsp goto done;
2151 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2152 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2153 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2154 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2155 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2156 927df6b7 2019-02-10 stsp arg.status_path = path;
2157 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2158 f8d1f275 2019-02-04 stsp arg.repo = repo;
2159 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2160 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2161 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2162 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2163 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2164 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2166 f8d1f275 2019-02-04 stsp if (workdir)
2167 f8d1f275 2019-02-04 stsp closedir(workdir);
2168 927df6b7 2019-02-10 stsp free(ondisk_path);
2169 f8d1f275 2019-02-04 stsp free(fileindex_path);
2170 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2171 f8d1f275 2019-02-04 stsp return err;
2174 6c7ab921 2019-03-18 stsp const struct got_error *
2175 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2176 6c7ab921 2019-03-18 stsp const char *arg)
2178 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2179 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
2180 6c7ab921 2019-03-18 stsp size_t len;
2182 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2184 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2185 6c7ab921 2019-03-18 stsp if (resolved == NULL)
2186 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
2188 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2189 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2190 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2191 6c7ab921 2019-03-18 stsp goto done;
2194 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2195 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2196 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2198 f6d88e1a 2019-05-29 stsp goto done;
2200 f6d88e1a 2019-05-29 stsp path = strdup("");
2201 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2202 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2203 f6d88e1a 2019-05-29 stsp goto done;
2207 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2208 6c7ab921 2019-03-18 stsp len = strlen(path);
2209 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2210 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2214 6c7ab921 2019-03-18 stsp free(resolved);
2215 6c7ab921 2019-03-18 stsp if (err == NULL)
2216 6c7ab921 2019-03-18 stsp *wt_path = path;
2218 6c7ab921 2019-03-18 stsp free(path);
2219 d00136be 2019-03-26 stsp return err;
2222 07f5b47a 2019-06-02 stsp static const struct got_error *
2223 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2224 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2225 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2227 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2228 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2230 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2231 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2232 07f5b47a 2019-06-02 stsp return NULL;
2234 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2236 07f5b47a 2019-06-02 stsp return err;
2238 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2239 07f5b47a 2019-06-02 stsp if (err) {
2240 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2241 07f5b47a 2019-06-02 stsp return err;
2244 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2247 d00136be 2019-03-26 stsp const struct got_error *
2248 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2249 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2250 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2251 031a5338 2019-03-26 stsp struct got_repository *repo)
2253 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2254 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2255 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2256 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2257 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2259 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2261 d00136be 2019-03-26 stsp return err;
2264 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
2265 d00136be 2019-03-26 stsp if (fileindex == NULL) {
2266 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2267 d00136be 2019-03-26 stsp goto done;
2270 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2271 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2272 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2273 d00136be 2019-03-26 stsp fileindex_path = NULL;
2274 d00136be 2019-03-26 stsp goto done;
2277 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2278 d00136be 2019-03-26 stsp if (index == NULL) {
2279 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2280 d00136be 2019-03-26 stsp goto done;
2283 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2285 d00136be 2019-03-26 stsp goto done;
2287 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2288 1dd54920 2019-05-11 stsp char *relpath;
2289 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2290 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2293 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2294 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2295 1dd54920 2019-05-11 stsp free(relpath);
2299 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2300 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2301 af12c6b9 2019-06-04 stsp err = sync_err;
2303 d00136be 2019-03-26 stsp if (index) {
2304 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2305 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2307 d00136be 2019-03-26 stsp if (fileindex)
2308 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2309 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2310 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2311 d00136be 2019-03-26 stsp err = unlockerr;
2312 6c7ab921 2019-03-18 stsp return err;
2315 17ed4618 2019-06-02 stsp static const struct got_error *
2316 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2317 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2318 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2319 17ed4618 2019-06-02 stsp struct got_repository *repo)
2321 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2322 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2323 17ed4618 2019-06-02 stsp unsigned char status;
2324 17ed4618 2019-06-02 stsp struct stat sb;
2326 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2327 17ed4618 2019-06-02 stsp if (ie == NULL)
2328 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2330 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2332 17ed4618 2019-06-02 stsp return err;
2334 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2335 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2336 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2337 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2338 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2339 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2340 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2343 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2344 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2346 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2347 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2350 2ec1f75b 2019-03-26 stsp const struct got_error *
2351 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2352 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2353 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2354 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2356 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2357 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2358 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2359 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2360 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2362 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2364 2ec1f75b 2019-03-26 stsp return err;
2366 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2367 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2368 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2369 2ec1f75b 2019-03-26 stsp goto done;
2372 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2373 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2374 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2375 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2376 2ec1f75b 2019-03-26 stsp goto done;
2379 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2380 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2381 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2382 2ec1f75b 2019-03-26 stsp goto done;
2385 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2387 2ec1f75b 2019-03-26 stsp goto done;
2389 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2390 17ed4618 2019-06-02 stsp char *relpath;
2391 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2392 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2395 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2396 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2397 17ed4618 2019-06-02 stsp free(relpath);
2401 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2402 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2403 af12c6b9 2019-06-04 stsp err = sync_err;
2405 2ec1f75b 2019-03-26 stsp if (index) {
2406 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2407 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2409 2ec1f75b 2019-03-26 stsp if (fileindex)
2410 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2411 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2412 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2413 2ec1f75b 2019-03-26 stsp err = unlockerr;
2414 2ec1f75b 2019-03-26 stsp return err;
2417 e20a8b6f 2019-06-04 stsp static const struct got_error *
2418 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2419 a129376b 2019-03-28 stsp const char *ondisk_path,
2420 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2421 a129376b 2019-03-28 stsp struct got_repository *repo)
2423 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2424 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2425 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2426 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2427 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2428 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2429 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2430 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2431 a129376b 2019-03-28 stsp unsigned char status;
2432 a129376b 2019-03-28 stsp struct stat sb;
2434 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2435 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2437 a129376b 2019-03-28 stsp goto done;
2439 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2440 a129376b 2019-03-28 stsp if (ie == NULL) {
2441 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2442 a129376b 2019-03-28 stsp goto done;
2445 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2446 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2447 a129376b 2019-03-28 stsp if (err) {
2448 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2449 a129376b 2019-03-28 stsp goto done;
2450 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2451 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2452 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2453 e20a8b6f 2019-06-04 stsp goto done;
2456 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2457 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2458 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2459 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2460 a129376b 2019-03-28 stsp goto done;
2463 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2464 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2465 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2466 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2467 a129376b 2019-03-28 stsp goto done;
2470 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2471 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2472 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2473 a129376b 2019-03-28 stsp goto done;
2478 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2479 a129376b 2019-03-28 stsp tree_path);
2481 a129376b 2019-03-28 stsp goto done;
2483 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2485 a129376b 2019-03-28 stsp goto done;
2487 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2488 a129376b 2019-03-28 stsp if (te_name == NULL) {
2489 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2490 a129376b 2019-03-28 stsp goto done;
2493 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2495 a129376b 2019-03-28 stsp goto done;
2497 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2498 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2499 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2500 a129376b 2019-03-28 stsp goto done;
2503 a129376b 2019-03-28 stsp switch (status) {
2504 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2505 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2506 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2508 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2509 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2510 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2511 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2512 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2513 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2514 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2516 a129376b 2019-03-28 stsp goto done;
2517 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2518 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2519 a129376b 2019-03-28 stsp progress_arg);
2521 a129376b 2019-03-28 stsp goto done;
2522 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2523 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2524 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2526 a129376b 2019-03-28 stsp goto done;
2531 a129376b 2019-03-28 stsp goto done;
2534 a129376b 2019-03-28 stsp free(relpath);
2535 e20a8b6f 2019-06-04 stsp free(parent_path);
2536 a129376b 2019-03-28 stsp free(tree_path);
2538 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2540 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2541 a129376b 2019-03-28 stsp free(tree_id);
2542 e20a8b6f 2019-06-04 stsp return err;
2545 e20a8b6f 2019-06-04 stsp const struct got_error *
2546 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2547 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2548 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2549 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2551 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2552 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2553 e20a8b6f 2019-06-04 stsp FILE *index = NULL;
2554 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2555 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2556 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2558 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2560 e20a8b6f 2019-06-04 stsp return err;
2562 e20a8b6f 2019-06-04 stsp fileindex = got_fileindex_alloc();
2563 e20a8b6f 2019-06-04 stsp if (fileindex == NULL) {
2564 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("got_fileindex_alloc");
2565 e20a8b6f 2019-06-04 stsp goto done;
2568 e20a8b6f 2019-06-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2569 e20a8b6f 2019-06-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2570 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("asprintf");
2571 e20a8b6f 2019-06-04 stsp fileindex_path = NULL;
2572 e20a8b6f 2019-06-04 stsp goto done;
2575 e20a8b6f 2019-06-04 stsp index = fopen(fileindex_path, "rb");
2576 e20a8b6f 2019-06-04 stsp if (index == NULL) {
2577 e20a8b6f 2019-06-04 stsp err = got_error_from_errno2("fopen", fileindex_path);
2578 e20a8b6f 2019-06-04 stsp goto done;
2581 e20a8b6f 2019-06-04 stsp err = got_fileindex_read(fileindex, index);
2583 e20a8b6f 2019-06-04 stsp goto done;
2585 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2586 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2587 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2591 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2592 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2593 e20a8b6f 2019-06-04 stsp err = sync_err;
2595 a129376b 2019-03-28 stsp if (index) {
2596 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2597 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2599 a129376b 2019-03-28 stsp if (fileindex)
2600 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2601 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2602 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2603 a129376b 2019-03-28 stsp err = unlockerr;
2604 c4296144 2019-05-09 stsp return err;
2607 cf066bf8 2019-05-09 stsp static void
2608 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2610 24519714 2019-05-09 stsp free(ct->path);
2611 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2612 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2613 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2614 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2615 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2619 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2620 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2621 24519714 2019-05-09 stsp struct got_repository *repo;
2622 24519714 2019-05-09 stsp struct got_worktree *worktree;
2625 c4296144 2019-05-09 stsp static const struct got_error *
2626 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2627 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2629 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2630 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2631 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2632 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2633 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2634 768aea60 2019-05-09 stsp struct stat sb;
2636 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2637 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2639 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2640 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2641 c4296144 2019-05-09 stsp return NULL;
2643 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2644 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2645 036813ee 2019-05-09 stsp goto done;
2647 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2648 036813ee 2019-05-09 stsp parent_path = strdup("");
2649 69960a46 2019-05-09 stsp if (parent_path == NULL)
2650 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2652 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2654 69960a46 2019-05-09 stsp return err;
2657 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2658 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2659 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2660 c4296144 2019-05-09 stsp goto done;
2663 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2664 768aea60 2019-05-09 stsp relpath) == -1) {
2665 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2666 768aea60 2019-05-09 stsp goto done;
2668 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2669 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2671 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2672 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2673 768aea60 2019-05-09 stsp goto done;
2675 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2678 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2679 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2680 44d03001 2019-05-09 stsp relpath) == -1) {
2681 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2682 44d03001 2019-05-09 stsp goto done;
2685 cf066bf8 2019-05-09 stsp ct->status = status;
2686 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2687 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2688 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2689 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2690 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2691 b416585c 2019-05-13 stsp goto done;
2693 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2694 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2695 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2696 036813ee 2019-05-09 stsp goto done;
2699 24519714 2019-05-09 stsp ct->path = strdup(path);
2700 24519714 2019-05-09 stsp if (ct->path == NULL) {
2701 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2702 24519714 2019-05-09 stsp goto done;
2704 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2706 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2707 ed175427 2019-05-09 stsp free_commitable(ct);
2708 24519714 2019-05-09 stsp free(parent_path);
2709 036813ee 2019-05-09 stsp free(path);
2710 a129376b 2019-03-28 stsp return err;
2713 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2714 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2715 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2716 036813ee 2019-05-09 stsp struct got_repository *);
2718 ed175427 2019-05-09 stsp static const struct got_error *
2719 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2720 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2721 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2722 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2723 afa376bf 2019-05-09 stsp struct got_repository *repo)
2725 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2726 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2727 036813ee 2019-05-09 stsp char *subpath;
2729 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2730 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2731 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2733 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2735 ed175427 2019-05-09 stsp return err;
2737 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2738 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2739 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2740 036813ee 2019-05-09 stsp free(subpath);
2741 036813ee 2019-05-09 stsp return err;
2744 036813ee 2019-05-09 stsp static const struct got_error *
2745 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2747 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2748 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2750 036813ee 2019-05-09 stsp *match = 0;
2752 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2753 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2754 0f63689d 2019-05-10 stsp return NULL;
2757 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2759 0f63689d 2019-05-10 stsp return err;
2760 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2761 036813ee 2019-05-09 stsp free(ct_parent_path);
2762 036813ee 2019-05-09 stsp return err;
2765 768aea60 2019-05-09 stsp static mode_t
2766 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2768 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2771 036813ee 2019-05-09 stsp static const struct got_error *
2772 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2773 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2775 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2777 036813ee 2019-05-09 stsp *new_te = NULL;
2779 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2781 036813ee 2019-05-09 stsp goto done;
2783 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2785 036813ee 2019-05-09 stsp free((*new_te)->id);
2786 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2787 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2788 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2789 036813ee 2019-05-09 stsp goto done;
2792 036813ee 2019-05-09 stsp if (err && *new_te) {
2793 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2794 036813ee 2019-05-09 stsp *new_te = NULL;
2796 036813ee 2019-05-09 stsp return err;
2799 036813ee 2019-05-09 stsp static const struct got_error *
2800 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2801 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2803 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2804 036813ee 2019-05-09 stsp char *ct_name;
2806 036813ee 2019-05-09 stsp *new_te = NULL;
2808 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2809 036813ee 2019-05-09 stsp if (*new_te == NULL)
2810 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2812 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2813 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2814 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2815 036813ee 2019-05-09 stsp goto done;
2817 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2818 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2819 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2820 036813ee 2019-05-09 stsp goto done;
2823 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2825 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2826 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2827 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2828 036813ee 2019-05-09 stsp goto done;
2831 036813ee 2019-05-09 stsp if (err && *new_te) {
2832 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2833 036813ee 2019-05-09 stsp *new_te = NULL;
2835 036813ee 2019-05-09 stsp return err;
2838 036813ee 2019-05-09 stsp static const struct got_error *
2839 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2840 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2842 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2843 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2845 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2847 036813ee 2019-05-09 stsp return err;
2848 036813ee 2019-05-09 stsp if (new_pe == NULL)
2849 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2850 036813ee 2019-05-09 stsp return NULL;
2853 afa376bf 2019-05-09 stsp static const struct got_error *
2854 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2855 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2857 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2858 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2859 afa376bf 2019-05-09 stsp ct_path++;
2860 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2863 036813ee 2019-05-09 stsp static const struct got_error *
2864 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2865 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2867 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2868 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2869 44d03001 2019-05-09 stsp char *te_path;
2871 44d03001 2019-05-09 stsp *modified = 0;
2873 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2874 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2875 44d03001 2019-05-09 stsp te->name) == -1)
2876 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2878 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2879 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2880 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2881 44d03001 2019-05-09 stsp strlen(te_path));
2882 44d03001 2019-05-09 stsp if (*modified)
2886 44d03001 2019-05-09 stsp free(te_path);
2887 44d03001 2019-05-09 stsp return err;
2890 44d03001 2019-05-09 stsp static const struct got_error *
2891 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2892 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2893 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2895 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2896 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2898 036813ee 2019-05-09 stsp *ctp = NULL;
2900 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2901 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2902 036813ee 2019-05-09 stsp char *ct_name = NULL;
2903 036813ee 2019-05-09 stsp int path_matches;
2905 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2906 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2909 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2912 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2914 036813ee 2019-05-09 stsp return err;
2915 036813ee 2019-05-09 stsp if (!path_matches)
2918 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2919 036813ee 2019-05-09 stsp if (ct_name == NULL)
2920 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2922 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2925 036813ee 2019-05-09 stsp *ctp = ct;
2929 2b496619 2019-07-10 stsp return err;
2932 2b496619 2019-07-10 stsp static const struct got_error *
2933 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2934 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
2935 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
2936 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2937 2b496619 2019-07-10 stsp struct got_repository *repo)
2939 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
2940 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
2941 2b496619 2019-07-10 stsp char *subtree_path;
2943 2b496619 2019-07-10 stsp *new_tep = NULL;
2945 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2946 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2947 2b496619 2019-07-10 stsp child_path) == -1)
2948 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
2950 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
2951 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
2952 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
2953 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
2954 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
2955 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
2956 2b496619 2019-07-10 stsp goto done;
2958 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2959 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
2960 2b496619 2019-07-10 stsp if (err) {
2961 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
2962 2b496619 2019-07-10 stsp goto done;
2965 2b496619 2019-07-10 stsp free(subtree_path);
2966 2b496619 2019-07-10 stsp if (err == NULL)
2967 2b496619 2019-07-10 stsp *new_tep = new_te;
2968 036813ee 2019-05-09 stsp return err;
2971 036813ee 2019-05-09 stsp static const struct got_error *
2972 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2973 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2974 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2975 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2976 036813ee 2019-05-09 stsp struct got_repository *repo)
2978 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2979 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2980 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2981 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2982 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2983 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2985 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2986 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2987 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2989 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2990 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2991 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2992 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2994 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2995 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2998 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2999 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3002 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3003 036813ee 2019-05-09 stsp pe->path);
3005 036813ee 2019-05-09 stsp goto done;
3007 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3008 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3009 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3011 036813ee 2019-05-09 stsp goto done;
3012 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3014 afa376bf 2019-05-09 stsp goto done;
3015 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3016 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3018 2f51b5b3 2019-05-09 stsp goto done;
3020 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3021 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3022 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3023 2b496619 2019-07-10 stsp == NULL) {
3024 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3025 2b496619 2019-07-10 stsp child_path, path_base_tree,
3026 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3029 2b496619 2019-07-10 stsp goto done;
3030 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3032 2b496619 2019-07-10 stsp goto done;
3037 2f51b5b3 2019-05-09 stsp if (base_tree) {
3038 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3039 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3040 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3041 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3043 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3044 44d03001 2019-05-09 stsp int modified;
3045 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3047 036813ee 2019-05-09 stsp goto done;
3048 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3049 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3051 2f51b5b3 2019-05-09 stsp goto done;
3052 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3053 44d03001 2019-05-09 stsp if (modified) {
3054 44d03001 2019-05-09 stsp free(new_te->id);
3055 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3056 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3057 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3059 44d03001 2019-05-09 stsp goto done;
3061 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3063 036813ee 2019-05-09 stsp goto done;
3067 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3068 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3070 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3071 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
3072 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3073 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3075 2f51b5b3 2019-05-09 stsp goto done;
3076 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3078 2f51b5b3 2019-05-09 stsp goto done;
3080 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3081 b416585c 2019-05-13 stsp status_arg);
3083 afa376bf 2019-05-09 stsp goto done;
3085 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3086 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3088 2f51b5b3 2019-05-09 stsp goto done;
3089 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3091 2f51b5b3 2019-05-09 stsp goto done;
3096 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3097 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3098 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3099 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3100 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3102 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3104 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3105 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3106 ed175427 2019-05-09 stsp return err;
3109 ebf99748 2019-05-09 stsp static const struct got_error *
3110 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3111 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3113 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err;
3114 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3115 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
3116 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3118 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3120 ebf99748 2019-05-09 stsp return err;
3122 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3123 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3124 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3126 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3128 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3129 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3130 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3132 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3133 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3134 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3136 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3137 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3138 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3141 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3146 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3147 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3148 af12c6b9 2019-06-04 stsp err = sync_err;
3149 ebf99748 2019-05-09 stsp free(fileindex_path);
3150 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
3151 d56d26ce 2019-05-10 stsp return err;
3154 d56d26ce 2019-05-10 stsp static const struct got_error *
3155 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3156 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3158 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3159 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3160 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3161 1a36436d 2019-06-10 stsp char *path = NULL;
3162 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3164 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3165 1a36436d 2019-06-10 stsp ct_path++;
3168 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3169 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3171 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3172 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3173 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3174 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3176 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3177 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3179 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3180 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3181 1a36436d 2019-06-10 stsp char *slash;
3182 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3184 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3185 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3186 1a36436d 2019-06-10 stsp return NULL;
3188 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3189 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3190 1a36436d 2019-06-10 stsp if (path == NULL) {
3191 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3192 1a36436d 2019-06-10 stsp goto done;
3194 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3195 1a36436d 2019-06-10 stsp if (slash)
3196 1a36436d 2019-06-10 stsp *slash = '\0';
3198 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3200 1a36436d 2019-06-10 stsp goto done;
3202 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3203 1a36436d 2019-06-10 stsp head_commit_id, path);
3205 1a36436d 2019-06-10 stsp goto done;
3207 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3208 1a36436d 2019-06-10 stsp while (pid) {
3209 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3211 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3212 1a36436d 2019-06-10 stsp if (err) {
3213 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3214 1a36436d 2019-06-10 stsp goto done;
3215 1a36436d 2019-06-10 stsp err = NULL;
3219 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3221 1a36436d 2019-06-10 stsp goto done;
3223 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3224 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3228 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3229 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3231 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3234 1a36436d 2019-06-10 stsp goto done;
3236 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3237 1a36436d 2019-06-10 stsp commit = pcommit;
3238 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3242 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3243 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3245 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3246 1a36436d 2019-06-10 stsp goto done;
3247 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3250 1a36436d 2019-06-10 stsp if (commit)
3251 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3252 d56d26ce 2019-05-10 stsp free(id_in_head);
3254 1a36436d 2019-06-10 stsp free(path);
3255 1a36436d 2019-06-10 stsp return err;
3258 c4296144 2019-05-09 stsp const struct got_error *
3259 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3260 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
3261 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
3262 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3263 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3264 afa376bf 2019-05-09 stsp struct got_repository *repo)
3266 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3267 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
3268 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
3269 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3270 bc70eb79 2019-05-09 stsp char *relpath = NULL;
3271 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3272 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
3273 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3274 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
3275 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3276 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3277 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3278 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3279 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3280 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3281 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3283 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3285 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
3286 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3288 c4296144 2019-05-09 stsp if (ondisk_path) {
3289 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
3290 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
3292 c4296144 2019-05-09 stsp return err;
3295 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
3297 c4296144 2019-05-09 stsp goto done;
3299 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3301 36a38700 2019-05-10 stsp goto done;
3302 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3304 09f5bd90 2019-05-09 stsp goto done;
3306 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
3307 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
3308 24519714 2019-05-09 stsp cc_arg.repo = repo;
3309 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
3310 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3312 675c7539 2019-05-09 stsp goto done;
3314 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
3315 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3319 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3321 588edf97 2019-05-10 stsp goto done;
3323 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3324 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3325 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3327 819f385b 2019-05-10 stsp goto done;
3330 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3332 588edf97 2019-05-10 stsp goto done;
3334 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3335 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3340 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3341 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3345 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3346 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3347 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3348 cf066bf8 2019-05-09 stsp char *ondisk_path;
3350 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3351 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3354 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3355 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3356 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3357 cf066bf8 2019-05-09 stsp goto done;
3359 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3360 a7055788 2019-05-09 stsp free(ondisk_path);
3362 cf066bf8 2019-05-09 stsp goto done;
3365 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3366 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3367 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3369 036813ee 2019-05-09 stsp goto done;
3371 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3373 de18fc63 2019-05-09 stsp goto done;
3374 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3375 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3376 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3377 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3378 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3379 33ad4cbe 2019-05-12 jcs free(logmsg);
3381 9d40349a 2019-05-09 stsp goto done;
3383 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3384 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3385 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3386 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3387 09f5bd90 2019-05-09 stsp goto done;
3389 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3390 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3392 09f5bd90 2019-05-09 stsp goto done;
3393 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3395 ebf99748 2019-05-09 stsp goto done;
3396 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3397 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3398 09f5bd90 2019-05-09 stsp goto done;
3400 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3401 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3403 f2c16586 2019-05-09 stsp goto done;
3404 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3406 f2c16586 2019-05-09 stsp goto done;
3408 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3410 09f5bd90 2019-05-09 stsp goto done;
3412 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3414 ebf99748 2019-05-09 stsp goto done;
3416 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3417 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3419 ebf99748 2019-05-09 stsp goto done;
3421 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3422 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3423 c4296144 2019-05-09 stsp err = unlockerr;
3424 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3425 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3426 ed175427 2019-05-09 stsp free_commitable(ct);
3428 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3429 588edf97 2019-05-10 stsp if (head_tree)
3430 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3431 588edf97 2019-05-10 stsp if (head_commit)
3432 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3433 c4296144 2019-05-09 stsp free(relpath);
3434 09f5bd90 2019-05-09 stsp free(head_commit_id);
3435 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3436 36a38700 2019-05-10 stsp if (head_ref)
3437 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3438 2f17228e 2019-05-12 stsp if (head_ref2) {
3439 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3440 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3441 2f17228e 2019-05-12 stsp err = unlockerr;
3442 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3444 c4296144 2019-05-09 stsp return err;
3447 8656d6c4 2019-05-20 stsp const char *
3448 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3450 8656d6c4 2019-05-20 stsp return ct->path;
3453 8656d6c4 2019-05-20 stsp unsigned int
3454 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3456 8656d6c4 2019-05-20 stsp return ct->status;
3459 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3460 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3461 818c7501 2019-07-11 stsp struct got_repository *repo;
3462 818c7501 2019-07-11 stsp int rebase_in_progress;
3465 818c7501 2019-07-11 stsp static const struct got_error *
3466 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3468 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3469 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3470 818c7501 2019-07-11 stsp unsigned char status;
3471 818c7501 2019-07-11 stsp struct stat sb;
3472 818c7501 2019-07-11 stsp char *ondisk_path;
3474 818c7501 2019-07-11 stsp if (!a->rebase_in_progress) {
3475 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with mixed base commits. */
3476 818c7501 2019-07-11 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3477 818c7501 2019-07-11 stsp SHA1_DIGEST_LENGTH))
3478 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3481 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3483 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3485 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with modified or conflicted files. */
3486 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3487 818c7501 2019-07-11 stsp free(ondisk_path);
3489 818c7501 2019-07-11 stsp return err;
3491 818c7501 2019-07-11 stsp if (a->rebase_in_progress) {
3492 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT)
3493 818c7501 2019-07-11 stsp return got_error(GOT_ERR_CONFLICTS);
3494 818c7501 2019-07-11 stsp } else if (status != GOT_STATUS_NO_CHANGE)
3495 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3497 818c7501 2019-07-11 stsp return NULL;
3500 818c7501 2019-07-11 stsp const struct got_error *
3501 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3502 818c7501 2019-07-11 stsp struct got_reference **tmp_branch, struct got_worktree *worktree,
3503 818c7501 2019-07-11 stsp struct got_reference *branch, struct got_repository *repo)
3505 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3506 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3507 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3508 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3509 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3510 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3511 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3513 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3514 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3516 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3518 818c7501 2019-07-11 stsp return err;
3520 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3522 818c7501 2019-07-11 stsp goto done;
3524 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3525 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3526 818c7501 2019-07-11 stsp ok_arg.rebase_in_progress = 0;
3527 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3530 818c7501 2019-07-11 stsp goto done;
3532 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3534 818c7501 2019-07-11 stsp goto done;
3536 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3538 818c7501 2019-07-11 stsp goto done;
3540 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3542 818c7501 2019-07-11 stsp goto done;
3544 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3547 818c7501 2019-07-11 stsp goto done;
3549 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3550 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3552 818c7501 2019-07-11 stsp goto done;
3553 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3555 818c7501 2019-07-11 stsp goto done;
3557 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3559 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3561 818c7501 2019-07-11 stsp goto done;
3563 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3565 818c7501 2019-07-11 stsp goto done;
3567 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3568 818c7501 2019-07-11 stsp worktree->base_commit_id);
3570 818c7501 2019-07-11 stsp goto done;
3571 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3573 818c7501 2019-07-11 stsp goto done;
3575 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3577 818c7501 2019-07-11 stsp goto done;
3579 818c7501 2019-07-11 stsp free(fileindex_path);
3580 818c7501 2019-07-11 stsp if (fileindex)
3581 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3582 818c7501 2019-07-11 stsp free(tmp_branch_name);
3583 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3584 818c7501 2019-07-11 stsp free(branch_ref_name);
3585 818c7501 2019-07-11 stsp if (branch_ref)
3586 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3587 818c7501 2019-07-11 stsp if (wt_branch)
3588 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3589 818c7501 2019-07-11 stsp if (err) {
3590 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3591 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3592 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3594 818c7501 2019-07-11 stsp if (*tmp_branch) {
3595 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3596 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3598 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3600 818c7501 2019-07-11 stsp return err;
3603 818c7501 2019-07-11 stsp const struct got_error *
3604 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3605 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3606 818c7501 2019-07-11 stsp struct got_reference **branch, struct got_worktree *worktree,
3607 818c7501 2019-07-11 stsp struct got_repository *repo)
3609 818c7501 2019-07-11 stsp const struct got_error *err;
3610 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3611 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3612 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3614 818c7501 2019-07-11 stsp *commit_id = NULL;
3616 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3618 818c7501 2019-07-11 stsp return err;
3620 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3622 818c7501 2019-07-11 stsp goto done;
3624 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3626 818c7501 2019-07-11 stsp goto done;
3628 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3630 818c7501 2019-07-11 stsp goto done;
3632 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3634 818c7501 2019-07-11 stsp goto done;
3636 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3637 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3639 818c7501 2019-07-11 stsp goto done;
3641 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3643 818c7501 2019-07-11 stsp goto done;
3645 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
3647 818c7501 2019-07-11 stsp goto done;
3649 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
3650 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
3652 818c7501 2019-07-11 stsp goto done;
3654 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3656 818c7501 2019-07-11 stsp goto done;
3658 818c7501 2019-07-11 stsp free(commit_ref_name);
3659 818c7501 2019-07-11 stsp free(branch_ref_name);
3660 818c7501 2019-07-11 stsp if (commit_ref)
3661 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3662 818c7501 2019-07-11 stsp if (branch_ref)
3663 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3664 818c7501 2019-07-11 stsp if (err) {
3665 818c7501 2019-07-11 stsp free(*commit_id);
3666 818c7501 2019-07-11 stsp *commit_id = NULL;
3667 818c7501 2019-07-11 stsp if (*tmp_branch) {
3668 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3669 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3671 818c7501 2019-07-11 stsp if (*new_base_branch) {
3672 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
3673 818c7501 2019-07-11 stsp *new_base_branch = NULL;
3675 818c7501 2019-07-11 stsp if (*branch) {
3676 818c7501 2019-07-11 stsp got_ref_close(*branch);
3677 818c7501 2019-07-11 stsp *branch = NULL;
3680 818c7501 2019-07-11 stsp return err;
3683 818c7501 2019-07-11 stsp const struct got_error *
3684 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3686 818c7501 2019-07-11 stsp const struct got_error *err;
3687 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
3689 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3691 818c7501 2019-07-11 stsp return err;
3693 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3694 818c7501 2019-07-11 stsp free(tmp_branch_name);
3695 818c7501 2019-07-11 stsp return NULL;
3698 818c7501 2019-07-11 stsp static const struct got_error *
3699 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3700 818c7501 2019-07-11 stsp char **logmsg, void *arg)
3702 818c7501 2019-07-11 stsp struct got_commit_object *commit = arg;
3704 818c7501 2019-07-11 stsp *logmsg = strdup(got_object_commit_get_logmsg(commit));
3705 818c7501 2019-07-11 stsp if (*logmsg == NULL)
3706 818c7501 2019-07-11 stsp return got_error_from_errno("strdup");
3708 818c7501 2019-07-11 stsp return NULL;
3711 818c7501 2019-07-11 stsp static const struct got_error *
3712 818c7501 2019-07-11 stsp rebase_status(void *arg, unsigned char status, const char *path,
3713 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3715 818c7501 2019-07-11 stsp return NULL;
3718 818c7501 2019-07-11 stsp const struct got_error *
3719 818c7501 2019-07-11 stsp got_worktree_rebase_merge_files(struct got_worktree *worktree,
3720 818c7501 2019-07-11 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3721 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3722 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3724 818c7501 2019-07-11 stsp const struct got_error *err;
3725 818c7501 2019-07-11 stsp struct got_fileindex *fileindex;
3726 818c7501 2019-07-11 stsp char *fileindex_path, *commit_ref_name = NULL;
3727 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3729 818c7501 2019-07-11 stsp /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3731 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3733 818c7501 2019-07-11 stsp return err;
3735 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3737 818c7501 2019-07-11 stsp goto done;
3738 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3739 818c7501 2019-07-11 stsp if (err) {
3740 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
3741 818c7501 2019-07-11 stsp goto done;
3742 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3744 818c7501 2019-07-11 stsp goto done;
3745 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
3747 818c7501 2019-07-11 stsp goto done;
3749 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
3752 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
3754 818c7501 2019-07-11 stsp goto done;
3755 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
3756 818c7501 2019-07-11 stsp free(stored_id);
3757 818c7501 2019-07-11 stsp if (cmp != 0) {
3758 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3759 818c7501 2019-07-11 stsp goto done;
3763 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
3764 818c7501 2019-07-11 stsp parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3765 818c7501 2019-07-11 stsp cancel_cb, cancel_arg);
3767 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3768 818c7501 2019-07-11 stsp free(fileindex_path);
3769 818c7501 2019-07-11 stsp if (commit_ref)
3770 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3771 818c7501 2019-07-11 stsp return err;
3774 818c7501 2019-07-11 stsp const struct got_error *
3775 818c7501 2019-07-11 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3776 818c7501 2019-07-11 stsp struct got_worktree *worktree, struct got_reference *tmp_branch,
3777 818c7501 2019-07-11 stsp struct got_commit_object *orig_commit,
3778 818c7501 2019-07-11 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
3780 818c7501 2019-07-11 stsp const struct got_error *err;
3781 818c7501 2019-07-11 stsp char *commit_ref_name = NULL;
3782 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3783 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
3785 818c7501 2019-07-11 stsp /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3787 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3789 818c7501 2019-07-11 stsp goto done;
3790 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3792 818c7501 2019-07-11 stsp goto done;
3793 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
3795 818c7501 2019-07-11 stsp goto done;
3796 818c7501 2019-07-11 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3797 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3798 818c7501 2019-07-11 stsp goto done;
3801 818c7501 2019-07-11 stsp err = got_worktree_commit(new_commit_id, worktree, NULL,
3802 818c7501 2019-07-11 stsp got_object_commit_get_author(orig_commit),
3803 818c7501 2019-07-11 stsp got_object_commit_get_committer(orig_commit),
3804 818c7501 2019-07-11 stsp collect_rebase_commit_msg, orig_commit,
3805 818c7501 2019-07-11 stsp rebase_status, NULL, repo);
3807 818c7501 2019-07-11 stsp goto done;
3809 818c7501 2019-07-11 stsp err = got_ref_delete(commit_ref, repo);
3811 818c7501 2019-07-11 stsp goto done;
3813 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
3815 818c7501 2019-07-11 stsp free(commit_ref_name);
3816 818c7501 2019-07-11 stsp if (commit_ref)
3817 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3818 818c7501 2019-07-11 stsp if (err) {
3819 818c7501 2019-07-11 stsp free(*new_commit_id);
3820 818c7501 2019-07-11 stsp *new_commit_id = NULL;
3822 818c7501 2019-07-11 stsp return err;
3825 818c7501 2019-07-11 stsp const struct got_error *
3826 818c7501 2019-07-11 stsp got_worktree_rebase_postpone(struct got_worktree *worktree)
3828 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
3831 818c7501 2019-07-11 stsp const struct got_error *
3832 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
3833 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3834 818c7501 2019-07-11 stsp struct got_reference *rebased_branch,
3835 818c7501 2019-07-11 stsp struct got_repository *repo)
3837 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3838 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
3840 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3842 818c7501 2019-07-11 stsp return err;
3844 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3846 818c7501 2019-07-11 stsp goto done;
3848 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
3850 818c7501 2019-07-11 stsp goto done;
3852 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
3854 818c7501 2019-07-11 stsp goto done;
3856 818c7501 2019-07-11 stsp err = got_ref_delete(tmp_branch, repo);
3858 818c7501 2019-07-11 stsp goto done;
3860 818c7501 2019-07-11 stsp err = got_ref_delete(new_base_branch, repo);
3862 818c7501 2019-07-11 stsp goto done;
3864 818c7501 2019-07-11 stsp free(new_head_commit_id);
3865 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3866 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
3867 818c7501 2019-07-11 stsp err = unlockerr;
3868 818c7501 2019-07-11 stsp return err;
3871 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
3872 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
3873 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3876 818c7501 2019-07-11 stsp static const struct got_error *
3877 818c7501 2019-07-11 stsp collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
3878 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3880 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
3881 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3882 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
3883 818c7501 2019-07-11 stsp char *path = NULL;
3885 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
3886 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
3887 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
3888 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
3889 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
3890 818c7501 2019-07-11 stsp return NULL;
3892 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
3893 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3895 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
3896 818c7501 2019-07-11 stsp if (err || new == NULL)
3897 818c7501 2019-07-11 stsp free(path);
3898 818c7501 2019-07-11 stsp return err;
3901 818c7501 2019-07-11 stsp const struct got_error *
3902 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
3903 818c7501 2019-07-11 stsp struct got_repository *repo, struct got_reference *new_base_branch,
3904 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
3906 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3907 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
3908 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
3909 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3910 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3911 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
3912 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
3913 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
3915 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
3917 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3919 818c7501 2019-07-11 stsp return err;
3921 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3923 818c7501 2019-07-11 stsp goto done;
3925 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
3926 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
3928 818c7501 2019-07-11 stsp goto done;
3930 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
3932 818c7501 2019-07-11 stsp goto done;
3935 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
3936 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
3937 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
3939 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
3941 818c7501 2019-07-11 stsp goto done;
3943 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
3945 818c7501 2019-07-11 stsp goto done;
3947 818c7501 2019-07-11 stsp err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
3948 818c7501 2019-07-11 stsp progress_arg, NULL, NULL);
3950 818c7501 2019-07-11 stsp goto done;
3952 818c7501 2019-07-11 stsp crp_arg.revertible_paths = &revertible_paths;
3953 818c7501 2019-07-11 stsp crp_arg.worktree = worktree;
3954 818c7501 2019-07-11 stsp err = got_worktree_status(worktree, "", repo,
3955 818c7501 2019-07-11 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
3957 818c7501 2019-07-11 stsp goto done;
3959 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
3960 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
3961 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
3966 818c7501 2019-07-11 stsp got_ref_close(resolved);
3967 818c7501 2019-07-11 stsp free(commit_id);
3968 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3969 818c7501 2019-07-11 stsp free(fileindex_path);
3970 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
3971 818c7501 2019-07-11 stsp free((char *)pe->path);
3972 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
3974 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3975 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
3976 818c7501 2019-07-11 stsp err = unlockerr;
3977 818c7501 2019-07-11 stsp return err;