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 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
729 234035bc 2019-06-01 stsp void *progress_arg)
731 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
732 6353ad76 2019-02-08 stsp int merged_fd = -1;
733 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
734 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
735 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
736 6353ad76 2019-02-08 stsp char *id_str = NULL;
737 6353ad76 2019-02-08 stsp char *label1 = NULL;
738 234035bc 2019-06-01 stsp int overlapcnt = 0;
739 af54ae4a 2019-02-19 stsp char *parent;
741 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
743 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
744 af54ae4a 2019-02-19 stsp if (parent == NULL)
745 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
747 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
750 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
754 af54ae4a 2019-02-19 stsp free(base_path);
755 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
756 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
757 af54ae4a 2019-02-19 stsp base_path = NULL;
761 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
765 6c4c42e0 2019-06-24 stsp blob_deriv);
769 af54ae4a 2019-02-19 stsp free(base_path);
770 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
771 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
772 af54ae4a 2019-02-19 stsp base_path = NULL;
776 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 46b6ee73 2019-06-04 stsp if (blob_orig) {
780 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
781 46b6ee73 2019-06-04 stsp blob_orig);
786 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
787 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
788 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
792 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
795 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
796 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
800 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
801 46b6ee73 2019-06-04 stsp blob_orig_path, ondisk_path, label1, path);
805 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
806 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
808 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
809 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
813 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
814 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
815 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
816 68c76935 2019-02-19 stsp merged_path);
821 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
822 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
826 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
827 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
828 230a42bd 2019-05-11 jcs ondisk_path);
829 2a57020b 2019-02-20 stsp unlink(merged_path);
834 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
835 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
836 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
837 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
838 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
839 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
840 6353ad76 2019-02-08 stsp free(merged_path);
841 af54ae4a 2019-02-19 stsp free(base_path);
842 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
843 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
844 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
846 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
847 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
848 46b6ee73 2019-06-04 stsp free(blob_orig_path);
850 6353ad76 2019-02-08 stsp free(id_str);
851 6353ad76 2019-02-08 stsp free(label1);
852 4a1ddfc2 2019-01-12 stsp return err;
855 4a1ddfc2 2019-01-12 stsp static const struct got_error *
856 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
857 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
858 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
859 13d9040b 2019-03-27 stsp int update_timestamps)
861 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
863 13d9040b 2019-03-27 stsp if (ie == NULL)
864 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
866 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
867 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
868 13d9040b 2019-03-27 stsp update_timestamps);
870 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
871 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
872 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
874 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
876 13d9040b 2019-03-27 stsp return err;
879 13d9040b 2019-03-27 stsp static const struct got_error *
880 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
881 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
882 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
883 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
884 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
886 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
887 507dc3bb 2018-12-29 stsp int fd = -1;
888 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
889 507dc3bb 2018-12-29 stsp int update = 0;
890 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
892 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
893 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
894 9d31a1d8 2018-03-11 stsp if (fd == -1) {
895 21908da4 2019-01-13 stsp if (errno == ENOENT) {
896 21908da4 2019-01-13 stsp char *parent = dirname(path);
897 21908da4 2019-01-13 stsp if (parent == NULL)
898 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
899 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
901 21908da4 2019-01-13 stsp return err;
902 21908da4 2019-01-13 stsp fd = open(ondisk_path,
903 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
904 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
905 21908da4 2019-01-13 stsp if (fd == -1)
906 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
907 230a42bd 2019-05-11 jcs ondisk_path);
908 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
909 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
910 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
911 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
914 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
915 507dc3bb 2018-12-29 stsp ondisk_path);
918 507dc3bb 2018-12-29 stsp update = 1;
921 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
924 a378724f 2019-02-10 stsp if (restoring_missing_file)
925 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
926 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
927 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
929 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
930 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
932 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
934 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
935 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
938 9d31a1d8 2018-03-11 stsp if (len > 0) {
939 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
940 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
941 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
942 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
944 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
945 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
948 61d6eaa3 2018-12-24 stsp hdrlen = 0;
950 9d31a1d8 2018-03-11 stsp } while (len != 0);
952 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
953 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
957 507dc3bb 2018-12-29 stsp if (update) {
958 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
959 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
960 230a42bd 2019-05-11 jcs ondisk_path);
961 2a57020b 2019-02-20 stsp unlink(tmppath);
966 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
967 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
968 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
972 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
973 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
979 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
980 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
981 507dc3bb 2018-12-29 stsp free(tmppath);
982 6353ad76 2019-02-08 stsp return err;
985 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
986 6353ad76 2019-02-08 stsp static const struct got_error *
987 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
989 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
990 7154f6ce 2019-03-27 stsp const char *markers[3] = {
991 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
992 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
993 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
996 7154f6ce 2019-03-27 stsp char *line;
997 7154f6ce 2019-03-27 stsp size_t len;
998 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1000 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1001 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1002 7154f6ce 2019-03-27 stsp if (line == NULL) {
1003 7154f6ce 2019-03-27 stsp if (feof(f))
1005 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1009 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1010 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1012 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1018 7154f6ce 2019-03-27 stsp return err;
1021 7154f6ce 2019-03-27 stsp static const struct got_error *
1022 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1023 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1024 b8f41171 2019-02-10 stsp struct got_repository *repo)
1026 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1027 6353ad76 2019-02-08 stsp struct got_object_id id;
1028 6353ad76 2019-02-08 stsp size_t hdrlen;
1029 6353ad76 2019-02-08 stsp FILE *f = NULL;
1030 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1031 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1032 6353ad76 2019-02-08 stsp size_t flen, blen;
1034 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1036 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1037 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1039 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1040 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1042 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1043 b8f41171 2019-02-10 stsp sb->st_mode =
1044 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1045 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1047 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1048 a378724f 2019-02-10 stsp return NULL;
1050 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1053 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1054 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1055 6353ad76 2019-02-08 stsp return NULL;
1058 b8f41171 2019-02-10 stsp if (ie == NULL)
1059 d00136be 2019-03-26 stsp return NULL;
1061 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1062 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1063 2ec1f75b 2019-03-26 stsp return NULL;
1064 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1065 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1066 b8f41171 2019-02-10 stsp return NULL;
1069 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1070 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1071 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1072 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1073 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1074 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1075 6353ad76 2019-02-08 stsp return NULL;
1077 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1078 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1080 6353ad76 2019-02-08 stsp return err;
1082 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1083 6353ad76 2019-02-08 stsp if (f == NULL) {
1084 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1085 6353ad76 2019-02-08 stsp goto done;
1087 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1089 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1090 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1092 7154f6ce 2019-03-27 stsp goto done;
1093 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1094 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1095 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1096 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1097 7154f6ce 2019-03-27 stsp goto done;
1099 6353ad76 2019-02-08 stsp if (blen == 0) {
1100 6353ad76 2019-02-08 stsp if (flen != 0)
1101 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1103 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1104 6353ad76 2019-02-08 stsp if (blen != 0)
1105 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1107 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1108 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1109 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1110 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1114 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1117 6353ad76 2019-02-08 stsp hdrlen = 0;
1120 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1121 7154f6ce 2019-03-27 stsp rewind(f);
1122 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1126 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1128 6353ad76 2019-02-08 stsp fclose(f);
1129 9d31a1d8 2018-03-11 stsp return err;
1132 9d31a1d8 2018-03-11 stsp static const struct got_error *
1133 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1134 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1135 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1136 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1137 0584f854 2019-04-06 stsp void *progress_arg)
1139 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1140 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1141 6353ad76 2019-02-08 stsp char *ondisk_path;
1142 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1143 b8f41171 2019-02-10 stsp struct stat sb;
1145 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1146 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1148 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1150 b8f41171 2019-02-10 stsp goto done;
1152 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1153 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1154 b8f41171 2019-02-10 stsp goto done;
1157 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1158 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1159 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1160 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1161 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1163 6353ad76 2019-02-08 stsp goto done;
1165 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1166 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1167 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1168 b8f41171 2019-02-10 stsp goto done;
1171 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1173 6353ad76 2019-02-08 stsp goto done;
1175 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1176 234035bc 2019-06-01 stsp int update_timestamps;
1177 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1178 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1179 234035bc 2019-06-01 stsp struct got_object_id id2;
1180 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1181 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1183 234035bc 2019-06-01 stsp goto done;
1185 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1186 234035bc 2019-06-01 stsp ondisk_path, path, sb.st_mode, blob, repo,
1187 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1188 234035bc 2019-06-01 stsp if (blob2)
1189 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1191 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1192 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1193 234035bc 2019-06-01 stsp * unmodified files again.
1195 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1196 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1197 234035bc 2019-06-01 stsp update_timestamps);
1198 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1199 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1200 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1201 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1203 13d9040b 2019-03-27 stsp goto done;
1205 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1206 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1207 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1209 13d9040b 2019-03-27 stsp goto done;
1210 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1211 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1213 13d9040b 2019-03-27 stsp goto done;
1215 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1217 6353ad76 2019-02-08 stsp free(ondisk_path);
1218 8da9e5f4 2019-01-12 stsp return err;
1221 90285c3b 2019-01-08 stsp static const struct got_error *
1222 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1224 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1225 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1227 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1228 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1230 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1231 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1232 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1234 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1235 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1236 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1237 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1238 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1242 90285c3b 2019-01-08 stsp parent = dirname(parent);
1245 90285c3b 2019-01-08 stsp free(ondisk_path);
1246 90285c3b 2019-01-08 stsp return err;
1249 708d8e67 2019-03-27 stsp static const struct got_error *
1250 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1251 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1252 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1255 708d8e67 2019-03-27 stsp unsigned char status;
1256 708d8e67 2019-03-27 stsp struct stat sb;
1257 708d8e67 2019-03-27 stsp char *ondisk_path;
1259 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1261 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1263 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1265 708d8e67 2019-03-27 stsp return err;
1267 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1268 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1269 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1271 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1272 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1274 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1277 708d8e67 2019-03-27 stsp return err;
1279 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1280 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1281 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1283 fc6346c4 2019-03-27 stsp return err;
1285 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1288 fc6346c4 2019-03-27 stsp return err;
1291 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1292 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1293 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1294 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1295 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1296 8da9e5f4 2019-01-12 stsp void *progress_arg;
1297 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1298 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1301 512f0d0e 2019-01-02 stsp static const struct got_error *
1302 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1303 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1305 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1307 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1308 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1310 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1311 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1314 8da9e5f4 2019-01-12 stsp static const struct got_error *
1315 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1317 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1319 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1322 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1323 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1326 9d31a1d8 2018-03-11 stsp static const struct got_error *
1327 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1329 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1330 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1331 8da9e5f4 2019-01-12 stsp char *path;
1333 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1334 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1336 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1337 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1339 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1341 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1342 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1344 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1345 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1347 8da9e5f4 2019-01-12 stsp free(path);
1348 0cd1c46a 2019-03-11 stsp return err;
1351 517bab73 2019-03-11 stsp const struct got_error *
1352 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1354 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1355 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1356 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1358 517bab73 2019-03-11 stsp *refname = NULL;
1360 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1361 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1362 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1364 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1366 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1367 0647c563 2019-03-11 stsp *refname = NULL;
1369 517bab73 2019-03-11 stsp free(uuidstr);
1370 517bab73 2019-03-11 stsp return err;
1374 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1375 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1377 517bab73 2019-03-11 stsp static const struct got_error *
1378 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1380 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1381 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1382 517bab73 2019-03-11 stsp char *refname;
1384 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1386 517bab73 2019-03-11 stsp return err;
1388 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1390 0cd1c46a 2019-03-11 stsp goto done;
1392 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1394 0cd1c46a 2019-03-11 stsp free(refname);
1396 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1397 9d31a1d8 2018-03-11 stsp return err;
1400 ebf99748 2019-05-09 stsp static const struct got_error *
1401 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1402 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1404 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1405 ebf99748 2019-05-09 stsp FILE *index = NULL;
1407 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1408 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1409 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1410 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1412 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1413 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1414 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1415 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1416 ebf99748 2019-05-09 stsp goto done;
1419 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1420 ebf99748 2019-05-09 stsp if (index == NULL) {
1421 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1422 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1424 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1425 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1426 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1429 ebf99748 2019-05-09 stsp if (err) {
1430 ebf99748 2019-05-09 stsp free(*fileindex_path);
1431 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1432 ebf99748 2019-05-09 stsp free(*fileindex);
1433 ebf99748 2019-05-09 stsp *fileindex = NULL;
1435 ebf99748 2019-05-09 stsp return err;
1438 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1439 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1440 c932eeeb 2019-05-22 stsp const char *path;
1441 c932eeeb 2019-05-22 stsp size_t path_len;
1442 c932eeeb 2019-05-22 stsp const char *entry_name;
1443 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1444 a484d721 2019-06-10 stsp void *progress_arg;
1447 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1448 c932eeeb 2019-05-22 stsp static const struct got_error *
1449 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1451 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1453 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1454 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1455 c932eeeb 2019-05-22 stsp return NULL;
1456 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1457 102ff934 2019-06-11 stsp return NULL;
1459 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1460 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1461 c932eeeb 2019-05-22 stsp return NULL;
1463 a484d721 2019-06-10 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1464 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1465 c932eeeb 2019-05-22 stsp return NULL;
1468 9c6338c4 2019-06-02 stsp static const struct got_error *
1469 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1471 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1472 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1473 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1475 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1476 9c6338c4 2019-06-02 stsp fileindex_path);
1478 9c6338c4 2019-06-02 stsp goto done;
1480 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1482 9c6338c4 2019-06-02 stsp goto done;
1484 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1485 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1486 9c6338c4 2019-06-02 stsp fileindex_path);
1487 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1490 9c6338c4 2019-06-02 stsp if (new_index)
1491 9c6338c4 2019-06-02 stsp fclose(new_index);
1492 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1493 9c6338c4 2019-06-02 stsp return err;
1496 9d31a1d8 2018-03-11 stsp const struct got_error *
1497 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1498 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1499 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1501 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1502 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1503 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1504 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1505 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1506 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1507 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1508 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1509 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1510 af12c6b9 2019-06-04 stsp struct bump_base_commit_id_arg bbc_arg;
1512 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1514 9d31a1d8 2018-03-11 stsp return err;
1517 51514078 2018-12-25 stsp * Read the file index.
1518 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1519 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1521 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1523 ebf99748 2019-05-09 stsp goto done;
1525 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1527 0cd1c46a 2019-03-11 stsp goto done;
1529 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1530 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1532 9d31a1d8 2018-03-11 stsp goto done;
1534 c4cdcb68 2019-04-03 stsp if (path[0]) {
1535 c4cdcb68 2019-04-03 stsp char *tree_path;
1536 c4cdcb68 2019-04-03 stsp int obj_type;
1537 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1538 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1539 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1540 c4cdcb68 2019-04-03 stsp goto done;
1542 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1543 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1544 c4cdcb68 2019-04-03 stsp path) == -1) {
1545 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1546 c4cdcb68 2019-04-03 stsp goto done;
1548 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1549 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1550 c4cdcb68 2019-04-03 stsp free(tree_path);
1552 c4cdcb68 2019-04-03 stsp goto done;
1553 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1555 c4cdcb68 2019-04-03 stsp goto done;
1556 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1557 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1558 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1559 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1560 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1561 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1562 c4cdcb68 2019-04-03 stsp goto done;
1564 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1565 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1566 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1567 c4cdcb68 2019-04-03 stsp goto done;
1570 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1572 c4cdcb68 2019-04-03 stsp goto done;
1573 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1574 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1575 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1576 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1577 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1578 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1579 c4cdcb68 2019-04-03 stsp goto done;
1582 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1583 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1584 c4cdcb68 2019-04-03 stsp free(tree_path);
1586 c4cdcb68 2019-04-03 stsp goto done;
1587 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1588 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1589 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1590 c4cdcb68 2019-04-03 stsp goto done;
1594 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1595 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1597 c4cdcb68 2019-04-03 stsp goto done;
1599 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1600 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1602 c4cdcb68 2019-04-03 stsp goto done;
1605 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1607 c4cdcb68 2019-04-03 stsp goto done;
1609 c4cdcb68 2019-04-03 stsp if (entry_name &&
1610 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1611 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1612 8da9e5f4 2019-01-12 stsp goto done;
1615 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1616 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1617 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1618 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1619 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1620 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1621 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1622 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1623 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1624 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1625 af12c6b9 2019-06-04 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1626 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1628 af12c6b9 2019-06-04 stsp goto sync;
1630 af12c6b9 2019-06-04 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1631 af12c6b9 2019-06-04 stsp bbc_arg.entry_name = entry_name;
1632 af12c6b9 2019-06-04 stsp bbc_arg.path = path;
1633 af12c6b9 2019-06-04 stsp bbc_arg.path_len = strlen(path);
1634 a484d721 2019-06-10 stsp bbc_arg.progress_cb = progress_cb;
1635 a484d721 2019-06-10 stsp bbc_arg.progress_arg = progress_arg;
1636 af12c6b9 2019-06-04 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1637 af12c6b9 2019-06-04 stsp bump_base_commit_id, &bbc_arg);
1639 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1640 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1641 af12c6b9 2019-06-04 stsp err = sync_err;
1643 9c6338c4 2019-06-02 stsp free(fileindex_path);
1644 c4cdcb68 2019-04-03 stsp free(relpath);
1646 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1647 9d31a1d8 2018-03-11 stsp if (commit)
1648 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1649 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1650 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1651 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1652 9d31a1d8 2018-03-11 stsp err = unlockerr;
1653 f8d1f275 2019-02-04 stsp return err;
1656 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1657 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1658 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1659 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1660 234035bc 2019-06-01 stsp void *progress_arg;
1661 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1662 234035bc 2019-06-01 stsp void *cancel_arg;
1665 234035bc 2019-06-01 stsp static const struct got_error *
1666 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1667 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1668 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1669 234035bc 2019-06-01 stsp struct got_repository *repo)
1671 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1672 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1673 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1674 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1675 234035bc 2019-06-01 stsp struct stat sb;
1676 234035bc 2019-06-01 stsp unsigned char status;
1677 234035bc 2019-06-01 stsp int local_changes_subsumed;
1679 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1680 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1681 234035bc 2019-06-01 stsp if (ie == NULL) {
1682 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1684 234035bc 2019-06-01 stsp return NULL;
1687 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1688 234035bc 2019-06-01 stsp path2) == -1)
1689 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1691 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1693 234035bc 2019-06-01 stsp goto done;
1695 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1696 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1698 234035bc 2019-06-01 stsp goto done;
1700 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1701 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1702 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1703 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1704 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status, path2);
1705 234035bc 2019-06-01 stsp goto done;
1708 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1709 234035bc 2019-06-01 stsp ondisk_path, path2, sb.st_mode, blob2, repo,
1710 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1711 234035bc 2019-06-01 stsp } else if (blob1) {
1712 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1713 234035bc 2019-06-01 stsp if (ie == NULL) {
1714 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1716 234035bc 2019-06-01 stsp return NULL;
1719 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1720 2b92fad7 2019-06-02 stsp path1) == -1)
1721 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1723 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1725 2b92fad7 2019-06-02 stsp goto done;
1727 2b92fad7 2019-06-02 stsp switch (status) {
1728 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1729 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1731 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1733 2b92fad7 2019-06-02 stsp goto done;
1735 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1737 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1738 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1739 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1742 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1744 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1745 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1746 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1747 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg,
1748 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1750 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1751 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, status, path1);
1756 234035bc 2019-06-01 stsp } else if (blob2) {
1757 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1758 234035bc 2019-06-01 stsp path2) == -1)
1759 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1760 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1762 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1765 234035bc 2019-06-01 stsp goto done;
1766 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1767 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1768 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1769 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1770 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status,
1772 234035bc 2019-06-01 stsp goto done;
1774 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1775 234035bc 2019-06-01 stsp NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1776 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1777 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1778 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1779 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1780 234035bc 2019-06-01 stsp blob2, 0);
1782 234035bc 2019-06-01 stsp goto done;
1785 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1786 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1787 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1788 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1789 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1790 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1792 234035bc 2019-06-01 stsp goto done;
1793 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1794 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1796 234035bc 2019-06-01 stsp goto done;
1797 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1798 2b92fad7 2019-06-02 stsp if (err) {
1799 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1800 2b92fad7 2019-06-02 stsp goto done;
1805 234035bc 2019-06-01 stsp free(ondisk_path);
1806 234035bc 2019-06-01 stsp return err;
1809 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1810 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1811 234035bc 2019-06-01 stsp struct got_repository *repo;
1814 234035bc 2019-06-01 stsp static const struct got_error *
1815 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1817 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1818 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1819 234035bc 2019-06-01 stsp unsigned char status;
1820 234035bc 2019-06-01 stsp struct stat sb;
1821 234035bc 2019-06-01 stsp char *ondisk_path;
1823 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1824 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1825 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1826 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1828 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1830 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1832 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1833 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1835 234035bc 2019-06-01 stsp return err;
1836 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1837 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1839 234035bc 2019-06-01 stsp return NULL;
1842 234035bc 2019-06-01 stsp const struct got_error *
1843 234035bc 2019-06-01 stsp got_worktree_merge_files(struct got_worktree *worktree,
1844 234035bc 2019-06-01 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1845 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1846 234035bc 2019-06-01 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1848 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1849 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1850 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
1852 234035bc 2019-06-01 stsp char *fileindex_path = NULL;
1853 234035bc 2019-06-01 stsp struct got_fileindex *fileindex = NULL;
1854 234035bc 2019-06-01 stsp struct check_merge_ok_arg mok_arg;
1856 234035bc 2019-06-01 stsp err = lock_worktree(worktree, LOCK_EX);
1858 234035bc 2019-06-01 stsp return err;
1860 234035bc 2019-06-01 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1862 234035bc 2019-06-01 stsp goto done;
1864 234035bc 2019-06-01 stsp mok_arg.worktree = worktree;
1865 234035bc 2019-06-01 stsp mok_arg.repo = repo;
1866 234035bc 2019-06-01 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1867 234035bc 2019-06-01 stsp &mok_arg);
1869 234035bc 2019-06-01 stsp goto done;
1871 03415a1a 2019-06-02 stsp if (commit_id1) {
1872 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1873 03415a1a 2019-06-02 stsp worktree->path_prefix);
1875 03415a1a 2019-06-02 stsp goto done;
1877 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1879 03415a1a 2019-06-02 stsp goto done;
1882 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1883 234035bc 2019-06-01 stsp worktree->path_prefix);
1885 234035bc 2019-06-01 stsp goto done;
1887 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1889 234035bc 2019-06-01 stsp goto done;
1891 234035bc 2019-06-01 stsp arg.worktree = worktree;
1892 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
1893 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
1894 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
1895 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
1896 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
1897 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1898 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1899 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1900 af12c6b9 2019-06-04 stsp err = sync_err;
1902 234035bc 2019-06-01 stsp got_fileindex_free(fileindex);
1903 234035bc 2019-06-01 stsp if (tree1)
1904 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
1905 234035bc 2019-06-01 stsp if (tree2)
1906 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
1908 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1909 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
1910 234035bc 2019-06-01 stsp err = unlockerr;
1911 234035bc 2019-06-01 stsp return err;
1914 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1915 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1916 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1917 927df6b7 2019-02-10 stsp const char *status_path;
1918 927df6b7 2019-02-10 stsp size_t status_path_len;
1919 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1920 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1921 f8d1f275 2019-02-04 stsp void *status_arg;
1922 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1923 f8d1f275 2019-02-04 stsp void *cancel_arg;
1926 f8d1f275 2019-02-04 stsp static const struct got_error *
1927 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1928 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1929 927df6b7 2019-02-10 stsp struct got_repository *repo)
1931 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1932 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1933 927df6b7 2019-02-10 stsp struct stat sb;
1934 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1936 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1937 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1938 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1939 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1940 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1941 016a88dd 2019-05-13 stsp &commit_id);
1943 927df6b7 2019-02-10 stsp return err;
1946 927df6b7 2019-02-10 stsp static const struct got_error *
1947 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1948 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1950 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1951 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1952 f8d1f275 2019-02-04 stsp char *abspath;
1954 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1955 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1957 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1958 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1959 927df6b7 2019-02-10 stsp return NULL;
1961 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1962 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1963 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1964 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1966 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1967 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1968 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1971 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1973 f8d1f275 2019-02-04 stsp free(abspath);
1974 9d31a1d8 2018-03-11 stsp return err;
1977 f8d1f275 2019-02-04 stsp static const struct got_error *
1978 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1980 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1981 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1982 2ec1f75b 2019-03-26 stsp unsigned char status;
1984 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1985 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1987 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1988 927df6b7 2019-02-10 stsp return NULL;
1990 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1991 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1992 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1993 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1995 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1996 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1997 016a88dd 2019-05-13 stsp &commit_id);
2000 f8d1f275 2019-02-04 stsp static const struct got_error *
2001 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2003 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2004 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2005 f8d1f275 2019-02-04 stsp char *path = NULL;
2007 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2008 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2010 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2011 2c201a36 2019-02-10 stsp return NULL;
2013 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2014 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2015 f8d1f275 2019-02-04 stsp return NULL;
2017 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2018 927df6b7 2019-02-10 stsp return NULL;
2020 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2021 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2022 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2024 f8d1f275 2019-02-04 stsp path = de->d_name;
2027 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2028 016a88dd 2019-05-13 stsp NULL, NULL);
2029 f8d1f275 2019-02-04 stsp if (parent_path[0])
2030 f8d1f275 2019-02-04 stsp free(path);
2031 b72f483a 2019-02-05 stsp return err;
2034 f8d1f275 2019-02-04 stsp const struct got_error *
2035 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2036 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2037 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2039 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2040 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2041 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
2042 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
2043 f8d1f275 2019-02-04 stsp FILE *index = NULL;
2044 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2045 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2046 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2048 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
2049 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
2050 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2051 f8d1f275 2019-02-04 stsp goto done;
2054 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2055 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2056 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2057 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
2058 f8d1f275 2019-02-04 stsp goto done;
2061 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
2062 f8d1f275 2019-02-04 stsp if (index == NULL) {
2063 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
2064 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2065 f8d1f275 2019-02-04 stsp goto done;
2068 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
2069 f8d1f275 2019-02-04 stsp fclose(index);
2071 f8d1f275 2019-02-04 stsp goto done;
2074 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2075 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2076 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2077 c513d110 2019-02-05 stsp goto done;
2079 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2080 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2081 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2082 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2083 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2084 927df6b7 2019-02-10 stsp if (ie == NULL) {
2085 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2086 927df6b7 2019-02-10 stsp goto done;
2088 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2089 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2090 927df6b7 2019-02-10 stsp goto done;
2092 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2093 927df6b7 2019-02-10 stsp goto done;
2096 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2097 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2098 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2099 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2100 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2101 927df6b7 2019-02-10 stsp arg.status_path = path;
2102 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2103 f8d1f275 2019-02-04 stsp arg.repo = repo;
2104 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2105 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2106 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2107 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2108 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2109 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2111 f8d1f275 2019-02-04 stsp if (workdir)
2112 f8d1f275 2019-02-04 stsp closedir(workdir);
2113 927df6b7 2019-02-10 stsp free(ondisk_path);
2114 f8d1f275 2019-02-04 stsp free(fileindex_path);
2115 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2116 f8d1f275 2019-02-04 stsp return err;
2119 6c7ab921 2019-03-18 stsp const struct got_error *
2120 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2121 6c7ab921 2019-03-18 stsp const char *arg)
2123 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2124 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
2125 6c7ab921 2019-03-18 stsp size_t len;
2127 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2129 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2130 6c7ab921 2019-03-18 stsp if (resolved == NULL)
2131 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
2133 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2134 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2135 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2136 6c7ab921 2019-03-18 stsp goto done;
2139 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2140 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2141 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2143 f6d88e1a 2019-05-29 stsp goto done;
2145 f6d88e1a 2019-05-29 stsp path = strdup("");
2146 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2147 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2148 f6d88e1a 2019-05-29 stsp goto done;
2152 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2153 6c7ab921 2019-03-18 stsp len = strlen(path);
2154 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2155 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2159 6c7ab921 2019-03-18 stsp free(resolved);
2160 6c7ab921 2019-03-18 stsp if (err == NULL)
2161 6c7ab921 2019-03-18 stsp *wt_path = path;
2163 6c7ab921 2019-03-18 stsp free(path);
2164 d00136be 2019-03-26 stsp return err;
2167 07f5b47a 2019-06-02 stsp static const struct got_error *
2168 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2169 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2170 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2172 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2173 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2175 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2176 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2177 07f5b47a 2019-06-02 stsp return NULL;
2179 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2181 07f5b47a 2019-06-02 stsp return err;
2183 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2184 07f5b47a 2019-06-02 stsp if (err) {
2185 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2186 07f5b47a 2019-06-02 stsp return err;
2189 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2192 d00136be 2019-03-26 stsp const struct got_error *
2193 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2194 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2195 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2196 031a5338 2019-03-26 stsp struct got_repository *repo)
2198 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2199 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2200 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2201 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2202 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2204 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2206 d00136be 2019-03-26 stsp return err;
2209 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
2210 d00136be 2019-03-26 stsp if (fileindex == NULL) {
2211 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2212 d00136be 2019-03-26 stsp goto done;
2215 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2216 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2217 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2218 d00136be 2019-03-26 stsp fileindex_path = NULL;
2219 d00136be 2019-03-26 stsp goto done;
2222 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2223 d00136be 2019-03-26 stsp if (index == NULL) {
2224 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2225 d00136be 2019-03-26 stsp goto done;
2228 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2230 d00136be 2019-03-26 stsp goto done;
2232 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2233 1dd54920 2019-05-11 stsp char *relpath;
2234 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2235 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2238 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2239 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2240 1dd54920 2019-05-11 stsp free(relpath);
2244 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2245 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2246 af12c6b9 2019-06-04 stsp err = sync_err;
2248 d00136be 2019-03-26 stsp if (index) {
2249 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2250 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2252 d00136be 2019-03-26 stsp if (fileindex)
2253 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2254 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2255 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2256 d00136be 2019-03-26 stsp err = unlockerr;
2257 6c7ab921 2019-03-18 stsp return err;
2260 17ed4618 2019-06-02 stsp static const struct got_error *
2261 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2262 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2263 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2264 17ed4618 2019-06-02 stsp struct got_repository *repo)
2266 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2267 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2268 17ed4618 2019-06-02 stsp unsigned char status;
2269 17ed4618 2019-06-02 stsp struct stat sb;
2271 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2272 17ed4618 2019-06-02 stsp if (ie == NULL)
2273 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2275 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2277 17ed4618 2019-06-02 stsp return err;
2279 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2280 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2281 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2282 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2283 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2284 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2285 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2288 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2289 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2291 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2292 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2295 2ec1f75b 2019-03-26 stsp const struct got_error *
2296 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2297 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2298 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2299 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2301 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2302 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2303 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2304 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2305 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2307 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2309 2ec1f75b 2019-03-26 stsp return err;
2311 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2312 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2313 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2314 2ec1f75b 2019-03-26 stsp goto done;
2317 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2318 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2320 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2321 2ec1f75b 2019-03-26 stsp goto done;
2324 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2325 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2326 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2327 2ec1f75b 2019-03-26 stsp goto done;
2330 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2332 2ec1f75b 2019-03-26 stsp goto done;
2334 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2335 17ed4618 2019-06-02 stsp char *relpath;
2336 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2337 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2340 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2341 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2342 17ed4618 2019-06-02 stsp free(relpath);
2346 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2347 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2348 af12c6b9 2019-06-04 stsp err = sync_err;
2350 2ec1f75b 2019-03-26 stsp if (index) {
2351 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2354 2ec1f75b 2019-03-26 stsp if (fileindex)
2355 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2356 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2357 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2358 2ec1f75b 2019-03-26 stsp err = unlockerr;
2359 2ec1f75b 2019-03-26 stsp return err;
2362 e20a8b6f 2019-06-04 stsp static const struct got_error *
2363 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2364 a129376b 2019-03-28 stsp const char *ondisk_path,
2365 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2366 a129376b 2019-03-28 stsp struct got_repository *repo)
2368 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2369 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2370 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2371 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2372 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2373 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2374 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2375 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2376 a129376b 2019-03-28 stsp unsigned char status;
2377 a129376b 2019-03-28 stsp struct stat sb;
2379 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2380 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2382 a129376b 2019-03-28 stsp goto done;
2384 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2385 a129376b 2019-03-28 stsp if (ie == NULL) {
2386 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2387 a129376b 2019-03-28 stsp goto done;
2390 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2391 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2392 a129376b 2019-03-28 stsp if (err) {
2393 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2394 a129376b 2019-03-28 stsp goto done;
2395 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2396 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2397 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2398 e20a8b6f 2019-06-04 stsp goto done;
2401 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2402 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2403 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2404 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2405 a129376b 2019-03-28 stsp goto done;
2408 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2409 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2410 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2411 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2412 a129376b 2019-03-28 stsp goto done;
2415 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2416 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2417 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2418 a129376b 2019-03-28 stsp goto done;
2423 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2424 a129376b 2019-03-28 stsp tree_path);
2426 a129376b 2019-03-28 stsp goto done;
2428 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2430 a129376b 2019-03-28 stsp goto done;
2432 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2433 a129376b 2019-03-28 stsp if (te_name == NULL) {
2434 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2435 a129376b 2019-03-28 stsp goto done;
2438 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2440 a129376b 2019-03-28 stsp goto done;
2442 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2443 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2444 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2445 a129376b 2019-03-28 stsp goto done;
2448 a129376b 2019-03-28 stsp switch (status) {
2449 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2450 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2451 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2453 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2454 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2455 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2456 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2457 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2458 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2459 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2461 a129376b 2019-03-28 stsp goto done;
2462 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2463 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2464 a129376b 2019-03-28 stsp progress_arg);
2466 a129376b 2019-03-28 stsp goto done;
2467 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2468 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2469 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2471 a129376b 2019-03-28 stsp goto done;
2476 a129376b 2019-03-28 stsp goto done;
2479 a129376b 2019-03-28 stsp free(relpath);
2480 e20a8b6f 2019-06-04 stsp free(parent_path);
2481 a129376b 2019-03-28 stsp free(tree_path);
2483 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2485 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2486 a129376b 2019-03-28 stsp free(tree_id);
2487 e20a8b6f 2019-06-04 stsp return err;
2490 e20a8b6f 2019-06-04 stsp const struct got_error *
2491 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2492 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2493 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2494 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2496 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2497 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2498 e20a8b6f 2019-06-04 stsp FILE *index = NULL;
2499 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2500 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2501 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2503 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2505 e20a8b6f 2019-06-04 stsp return err;
2507 e20a8b6f 2019-06-04 stsp fileindex = got_fileindex_alloc();
2508 e20a8b6f 2019-06-04 stsp if (fileindex == NULL) {
2509 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("got_fileindex_alloc");
2510 e20a8b6f 2019-06-04 stsp goto done;
2513 e20a8b6f 2019-06-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2514 e20a8b6f 2019-06-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2515 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("asprintf");
2516 e20a8b6f 2019-06-04 stsp fileindex_path = NULL;
2517 e20a8b6f 2019-06-04 stsp goto done;
2520 e20a8b6f 2019-06-04 stsp index = fopen(fileindex_path, "rb");
2521 e20a8b6f 2019-06-04 stsp if (index == NULL) {
2522 e20a8b6f 2019-06-04 stsp err = got_error_from_errno2("fopen", fileindex_path);
2523 e20a8b6f 2019-06-04 stsp goto done;
2526 e20a8b6f 2019-06-04 stsp err = got_fileindex_read(fileindex, index);
2528 e20a8b6f 2019-06-04 stsp goto done;
2530 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2531 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2532 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2536 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2537 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2538 e20a8b6f 2019-06-04 stsp err = sync_err;
2540 a129376b 2019-03-28 stsp if (index) {
2541 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2542 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2544 a129376b 2019-03-28 stsp if (fileindex)
2545 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2546 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2547 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2548 a129376b 2019-03-28 stsp err = unlockerr;
2549 c4296144 2019-05-09 stsp return err;
2552 cf066bf8 2019-05-09 stsp static void
2553 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2555 24519714 2019-05-09 stsp free(ct->path);
2556 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2557 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2558 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2559 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2560 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2564 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2565 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2566 24519714 2019-05-09 stsp struct got_repository *repo;
2567 24519714 2019-05-09 stsp struct got_worktree *worktree;
2570 c4296144 2019-05-09 stsp static const struct got_error *
2571 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2572 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2574 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2575 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2576 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2577 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2578 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2579 768aea60 2019-05-09 stsp struct stat sb;
2581 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2582 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2584 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2585 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2586 c4296144 2019-05-09 stsp return NULL;
2588 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2589 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2590 036813ee 2019-05-09 stsp goto done;
2592 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2593 036813ee 2019-05-09 stsp parent_path = strdup("");
2594 69960a46 2019-05-09 stsp if (parent_path == NULL)
2595 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2597 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2599 69960a46 2019-05-09 stsp return err;
2602 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2603 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2604 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2605 c4296144 2019-05-09 stsp goto done;
2608 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2609 768aea60 2019-05-09 stsp relpath) == -1) {
2610 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2611 768aea60 2019-05-09 stsp goto done;
2613 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2614 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2616 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2617 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2618 768aea60 2019-05-09 stsp goto done;
2620 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2623 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2624 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2625 44d03001 2019-05-09 stsp relpath) == -1) {
2626 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2627 44d03001 2019-05-09 stsp goto done;
2630 cf066bf8 2019-05-09 stsp ct->status = status;
2631 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2632 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2633 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2634 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2635 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2636 b416585c 2019-05-13 stsp goto done;
2638 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2639 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2640 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2641 036813ee 2019-05-09 stsp goto done;
2644 24519714 2019-05-09 stsp ct->path = strdup(path);
2645 24519714 2019-05-09 stsp if (ct->path == NULL) {
2646 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2647 24519714 2019-05-09 stsp goto done;
2649 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2651 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2652 ed175427 2019-05-09 stsp free_commitable(ct);
2653 24519714 2019-05-09 stsp free(parent_path);
2654 036813ee 2019-05-09 stsp free(path);
2655 a129376b 2019-03-28 stsp return err;
2658 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2659 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2660 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2661 036813ee 2019-05-09 stsp struct got_repository *);
2663 ed175427 2019-05-09 stsp static const struct got_error *
2664 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2665 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2666 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2667 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2668 afa376bf 2019-05-09 stsp struct got_repository *repo)
2670 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2671 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2672 036813ee 2019-05-09 stsp char *subpath;
2674 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2675 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2678 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2680 ed175427 2019-05-09 stsp return err;
2682 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2683 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2684 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2685 036813ee 2019-05-09 stsp free(subpath);
2686 036813ee 2019-05-09 stsp return err;
2689 036813ee 2019-05-09 stsp static const struct got_error *
2690 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2692 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2693 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2695 036813ee 2019-05-09 stsp *match = 0;
2697 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2698 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2699 0f63689d 2019-05-10 stsp return NULL;
2702 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2704 0f63689d 2019-05-10 stsp return err;
2705 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2706 036813ee 2019-05-09 stsp free(ct_parent_path);
2707 036813ee 2019-05-09 stsp return err;
2710 768aea60 2019-05-09 stsp static mode_t
2711 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2713 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2716 036813ee 2019-05-09 stsp static const struct got_error *
2717 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2718 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2720 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2722 036813ee 2019-05-09 stsp *new_te = NULL;
2724 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2726 036813ee 2019-05-09 stsp goto done;
2728 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2730 036813ee 2019-05-09 stsp free((*new_te)->id);
2731 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2732 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2733 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2734 036813ee 2019-05-09 stsp goto done;
2737 036813ee 2019-05-09 stsp if (err && *new_te) {
2738 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2739 036813ee 2019-05-09 stsp *new_te = NULL;
2741 036813ee 2019-05-09 stsp return err;
2744 036813ee 2019-05-09 stsp static const struct got_error *
2745 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2746 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2748 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2749 036813ee 2019-05-09 stsp char *ct_name;
2751 036813ee 2019-05-09 stsp *new_te = NULL;
2753 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2754 036813ee 2019-05-09 stsp if (*new_te == NULL)
2755 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2757 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2758 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2759 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2760 036813ee 2019-05-09 stsp goto done;
2762 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2763 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2764 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2765 036813ee 2019-05-09 stsp goto done;
2768 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2770 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2771 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2772 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2773 036813ee 2019-05-09 stsp goto done;
2776 036813ee 2019-05-09 stsp if (err && *new_te) {
2777 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2778 036813ee 2019-05-09 stsp *new_te = NULL;
2780 036813ee 2019-05-09 stsp return err;
2783 036813ee 2019-05-09 stsp static const struct got_error *
2784 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2785 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2787 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2788 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2790 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2792 036813ee 2019-05-09 stsp return err;
2793 036813ee 2019-05-09 stsp if (new_pe == NULL)
2794 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2795 036813ee 2019-05-09 stsp return NULL;
2798 afa376bf 2019-05-09 stsp static const struct got_error *
2799 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2800 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2802 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2803 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2804 afa376bf 2019-05-09 stsp ct_path++;
2805 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2808 036813ee 2019-05-09 stsp static const struct got_error *
2809 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2810 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2812 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2813 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2814 44d03001 2019-05-09 stsp char *te_path;
2816 44d03001 2019-05-09 stsp *modified = 0;
2818 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2819 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2820 44d03001 2019-05-09 stsp te->name) == -1)
2821 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2823 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2824 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2825 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2826 44d03001 2019-05-09 stsp strlen(te_path));
2827 44d03001 2019-05-09 stsp if (*modified)
2831 44d03001 2019-05-09 stsp free(te_path);
2832 44d03001 2019-05-09 stsp return err;
2835 44d03001 2019-05-09 stsp static const struct got_error *
2836 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2837 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2838 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2840 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2841 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2843 036813ee 2019-05-09 stsp *ctp = NULL;
2845 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2846 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2847 036813ee 2019-05-09 stsp char *ct_name = NULL;
2848 036813ee 2019-05-09 stsp int path_matches;
2850 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2851 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2854 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2857 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2859 036813ee 2019-05-09 stsp return err;
2860 036813ee 2019-05-09 stsp if (!path_matches)
2863 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2864 036813ee 2019-05-09 stsp if (ct_name == NULL)
2865 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2867 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2870 036813ee 2019-05-09 stsp *ctp = ct;
2874 036813ee 2019-05-09 stsp return err;
2877 036813ee 2019-05-09 stsp static const struct got_error *
2878 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2879 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2880 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2881 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2882 036813ee 2019-05-09 stsp struct got_repository *repo)
2884 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2885 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2886 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2887 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2888 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2889 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2891 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2892 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2893 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2895 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2896 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2897 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2898 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2900 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2901 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2904 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2905 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2908 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2909 036813ee 2019-05-09 stsp pe->path);
2911 036813ee 2019-05-09 stsp goto done;
2913 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2914 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2915 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2917 036813ee 2019-05-09 stsp goto done;
2918 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2920 afa376bf 2019-05-09 stsp goto done;
2921 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
2923 2f51b5b3 2019-05-09 stsp char *subtree_path;
2925 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2926 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2927 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2928 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2929 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2930 2f51b5b3 2019-05-09 stsp goto done;
2933 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2934 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2935 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2936 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2937 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2938 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2939 9ba0479c 2019-05-10 stsp new_te = NULL;
2940 9ba0479c 2019-05-10 stsp goto done;
2942 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2943 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2944 2f51b5b3 2019-05-09 stsp free(subtree_path);
2945 9ba0479c 2019-05-10 stsp if (err) {
2946 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2947 9ba0479c 2019-05-10 stsp new_te = NULL;
2948 036813ee 2019-05-09 stsp goto done;
2951 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2953 2f51b5b3 2019-05-09 stsp goto done;
2956 2f51b5b3 2019-05-09 stsp if (base_tree) {
2957 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2958 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2959 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2960 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2962 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2963 44d03001 2019-05-09 stsp int modified;
2964 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2966 036813ee 2019-05-09 stsp goto done;
2967 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2968 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2970 2f51b5b3 2019-05-09 stsp goto done;
2971 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2972 44d03001 2019-05-09 stsp if (modified) {
2973 44d03001 2019-05-09 stsp free(new_te->id);
2974 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2975 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2976 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2978 44d03001 2019-05-09 stsp goto done;
2980 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2982 036813ee 2019-05-09 stsp goto done;
2986 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2987 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2989 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2990 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2991 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2992 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2994 2f51b5b3 2019-05-09 stsp goto done;
2995 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2997 2f51b5b3 2019-05-09 stsp goto done;
2999 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3000 b416585c 2019-05-13 stsp status_arg);
3002 afa376bf 2019-05-09 stsp goto done;
3004 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3005 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3007 2f51b5b3 2019-05-09 stsp goto done;
3008 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3010 2f51b5b3 2019-05-09 stsp goto done;
3015 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3016 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3017 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3018 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3019 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3021 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3023 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3024 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3025 ed175427 2019-05-09 stsp return err;
3028 ebf99748 2019-05-09 stsp static const struct got_error *
3029 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3030 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3032 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err;
3033 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3034 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
3035 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3037 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3039 ebf99748 2019-05-09 stsp return err;
3041 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3042 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3043 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3045 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3047 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3048 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3049 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3051 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3052 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3053 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3055 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3056 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3057 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3060 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3065 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3066 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3067 af12c6b9 2019-06-04 stsp err = sync_err;
3068 ebf99748 2019-05-09 stsp free(fileindex_path);
3069 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
3070 d56d26ce 2019-05-10 stsp return err;
3073 d56d26ce 2019-05-10 stsp static const struct got_error *
3074 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3075 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3077 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3078 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3079 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3080 1a36436d 2019-06-10 stsp char *path = NULL;
3081 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3083 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3084 1a36436d 2019-06-10 stsp ct_path++;
3087 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3088 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3090 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3091 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3092 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3093 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3095 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3096 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3098 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3099 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3100 1a36436d 2019-06-10 stsp char *slash;
3101 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3103 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3104 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3105 1a36436d 2019-06-10 stsp return NULL;
3107 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3108 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3109 1a36436d 2019-06-10 stsp if (path == NULL) {
3110 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3111 1a36436d 2019-06-10 stsp goto done;
3113 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3114 1a36436d 2019-06-10 stsp if (slash)
3115 1a36436d 2019-06-10 stsp *slash = '\0';
3117 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3119 1a36436d 2019-06-10 stsp goto done;
3121 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3122 1a36436d 2019-06-10 stsp head_commit_id, path);
3124 1a36436d 2019-06-10 stsp goto done;
3126 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3127 1a36436d 2019-06-10 stsp while (pid) {
3128 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3130 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3131 1a36436d 2019-06-10 stsp if (err) {
3132 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3133 1a36436d 2019-06-10 stsp goto done;
3134 1a36436d 2019-06-10 stsp err = NULL;
3138 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3140 1a36436d 2019-06-10 stsp goto done;
3142 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3143 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3147 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3148 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3150 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3153 1a36436d 2019-06-10 stsp goto done;
3155 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3156 1a36436d 2019-06-10 stsp commit = pcommit;
3157 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3161 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3162 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3164 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3165 1a36436d 2019-06-10 stsp goto done;
3166 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3169 1a36436d 2019-06-10 stsp if (commit)
3170 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3171 d56d26ce 2019-05-10 stsp free(id_in_head);
3173 1a36436d 2019-06-10 stsp free(path);
3174 1a36436d 2019-06-10 stsp return err;
3177 c4296144 2019-05-09 stsp const struct got_error *
3178 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3179 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
3180 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
3181 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3182 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3183 afa376bf 2019-05-09 stsp struct got_repository *repo)
3185 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3186 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
3187 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
3188 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3189 bc70eb79 2019-05-09 stsp char *relpath = NULL;
3190 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3191 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
3192 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3193 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
3194 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3195 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3196 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3197 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3198 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3199 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3200 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3202 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3204 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
3205 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3207 c4296144 2019-05-09 stsp if (ondisk_path) {
3208 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
3209 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
3211 c4296144 2019-05-09 stsp return err;
3214 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
3216 c4296144 2019-05-09 stsp goto done;
3218 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3220 36a38700 2019-05-10 stsp goto done;
3221 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3223 09f5bd90 2019-05-09 stsp goto done;
3225 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
3226 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
3227 24519714 2019-05-09 stsp cc_arg.repo = repo;
3228 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
3229 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3231 675c7539 2019-05-09 stsp goto done;
3233 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
3234 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3238 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3240 588edf97 2019-05-10 stsp goto done;
3242 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3243 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3244 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3246 819f385b 2019-05-10 stsp goto done;
3249 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3251 588edf97 2019-05-10 stsp goto done;
3253 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3254 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3259 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3260 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3264 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3265 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3266 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3267 cf066bf8 2019-05-09 stsp char *ondisk_path;
3269 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3270 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3273 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3274 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3275 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3276 cf066bf8 2019-05-09 stsp goto done;
3278 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3279 a7055788 2019-05-09 stsp free(ondisk_path);
3281 cf066bf8 2019-05-09 stsp goto done;
3284 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3285 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3286 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3288 036813ee 2019-05-09 stsp goto done;
3290 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3292 de18fc63 2019-05-09 stsp goto done;
3293 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3294 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3295 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3296 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3297 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3298 33ad4cbe 2019-05-12 jcs free(logmsg);
3300 9d40349a 2019-05-09 stsp goto done;
3302 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3303 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3304 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3305 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3306 09f5bd90 2019-05-09 stsp goto done;
3308 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3309 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3311 09f5bd90 2019-05-09 stsp goto done;
3312 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3314 ebf99748 2019-05-09 stsp goto done;
3315 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3316 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3317 09f5bd90 2019-05-09 stsp goto done;
3319 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3320 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3322 f2c16586 2019-05-09 stsp goto done;
3323 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3325 f2c16586 2019-05-09 stsp goto done;
3327 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3329 09f5bd90 2019-05-09 stsp goto done;
3331 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3333 ebf99748 2019-05-09 stsp goto done;
3335 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3336 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3338 ebf99748 2019-05-09 stsp goto done;
3340 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3341 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3342 c4296144 2019-05-09 stsp err = unlockerr;
3343 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3344 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3345 ed175427 2019-05-09 stsp free_commitable(ct);
3347 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3348 588edf97 2019-05-10 stsp if (head_tree)
3349 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3350 588edf97 2019-05-10 stsp if (head_commit)
3351 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3352 c4296144 2019-05-09 stsp free(relpath);
3353 09f5bd90 2019-05-09 stsp free(head_commit_id);
3354 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3355 36a38700 2019-05-10 stsp if (head_ref)
3356 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3357 2f17228e 2019-05-12 stsp if (head_ref2) {
3358 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3359 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3360 2f17228e 2019-05-12 stsp err = unlockerr;
3361 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3363 c4296144 2019-05-09 stsp return err;
3366 8656d6c4 2019-05-20 stsp const char *
3367 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3369 8656d6c4 2019-05-20 stsp return ct->path;
3372 8656d6c4 2019-05-20 stsp unsigned int
3373 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3375 8656d6c4 2019-05-20 stsp return ct->status;