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 46b6ee73 2019-06-04 stsp err = got_object_blob_dump_to_file(NULL, NULL, f_deriv, blob_deriv);
768 af54ae4a 2019-02-19 stsp free(base_path);
769 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
770 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
771 af54ae4a 2019-02-19 stsp base_path = NULL;
775 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
778 46b6ee73 2019-06-04 stsp if (blob_orig) {
779 46b6ee73 2019-06-04 stsp err = got_object_blob_dump_to_file(NULL, NULL, f_orig,
780 46b6ee73 2019-06-04 stsp blob_orig);
785 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
786 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
787 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
791 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
794 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
795 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
799 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
800 46b6ee73 2019-06-04 stsp blob_orig_path, ondisk_path, label1, path);
804 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
805 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
807 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
808 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
812 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
813 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
814 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
815 68c76935 2019-02-19 stsp merged_path);
820 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
821 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
825 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
826 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
827 230a42bd 2019-05-11 jcs ondisk_path);
828 2a57020b 2019-02-20 stsp unlink(merged_path);
833 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
834 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
835 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
836 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
837 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
838 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
839 6353ad76 2019-02-08 stsp free(merged_path);
840 af54ae4a 2019-02-19 stsp free(base_path);
841 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
842 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
843 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
845 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
846 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
847 46b6ee73 2019-06-04 stsp free(blob_orig_path);
849 6353ad76 2019-02-08 stsp free(id_str);
850 6353ad76 2019-02-08 stsp free(label1);
851 4a1ddfc2 2019-01-12 stsp return err;
854 4a1ddfc2 2019-01-12 stsp static const struct got_error *
855 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
856 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
857 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
858 13d9040b 2019-03-27 stsp int update_timestamps)
860 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
862 13d9040b 2019-03-27 stsp if (ie == NULL)
863 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
865 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
866 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
867 13d9040b 2019-03-27 stsp update_timestamps);
869 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
870 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
871 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
873 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
875 13d9040b 2019-03-27 stsp return err;
878 13d9040b 2019-03-27 stsp static const struct got_error *
879 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
880 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
881 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
882 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
883 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
885 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
886 507dc3bb 2018-12-29 stsp int fd = -1;
887 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
888 507dc3bb 2018-12-29 stsp int update = 0;
889 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
891 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
892 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
893 9d31a1d8 2018-03-11 stsp if (fd == -1) {
894 21908da4 2019-01-13 stsp if (errno == ENOENT) {
895 21908da4 2019-01-13 stsp char *parent = dirname(path);
896 21908da4 2019-01-13 stsp if (parent == NULL)
897 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
898 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
900 21908da4 2019-01-13 stsp return err;
901 21908da4 2019-01-13 stsp fd = open(ondisk_path,
902 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
903 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
904 21908da4 2019-01-13 stsp if (fd == -1)
905 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
906 230a42bd 2019-05-11 jcs ondisk_path);
907 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
908 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
909 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
910 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
913 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
914 507dc3bb 2018-12-29 stsp ondisk_path);
917 507dc3bb 2018-12-29 stsp update = 1;
920 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
923 a378724f 2019-02-10 stsp if (restoring_missing_file)
924 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
925 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
926 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
928 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
929 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
931 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
933 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
934 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
937 9d31a1d8 2018-03-11 stsp if (len > 0) {
938 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
939 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
940 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
941 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
943 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
944 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
947 61d6eaa3 2018-12-24 stsp hdrlen = 0;
949 9d31a1d8 2018-03-11 stsp } while (len != 0);
951 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
952 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
956 507dc3bb 2018-12-29 stsp if (update) {
957 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
958 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
959 230a42bd 2019-05-11 jcs ondisk_path);
960 2a57020b 2019-02-20 stsp unlink(tmppath);
965 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
966 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
967 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
971 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
972 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
978 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
979 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
980 507dc3bb 2018-12-29 stsp free(tmppath);
981 6353ad76 2019-02-08 stsp return err;
984 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
985 6353ad76 2019-02-08 stsp static const struct got_error *
986 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
988 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
989 7154f6ce 2019-03-27 stsp const char *markers[3] = {
990 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
991 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
992 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
995 7154f6ce 2019-03-27 stsp char *line;
996 7154f6ce 2019-03-27 stsp size_t len;
997 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
999 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1000 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1001 7154f6ce 2019-03-27 stsp if (line == NULL) {
1002 7154f6ce 2019-03-27 stsp if (feof(f))
1004 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1008 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1009 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1011 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1017 7154f6ce 2019-03-27 stsp return err;
1020 7154f6ce 2019-03-27 stsp static const struct got_error *
1021 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1022 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1023 b8f41171 2019-02-10 stsp struct got_repository *repo)
1025 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1026 6353ad76 2019-02-08 stsp struct got_object_id id;
1027 6353ad76 2019-02-08 stsp size_t hdrlen;
1028 6353ad76 2019-02-08 stsp FILE *f = NULL;
1029 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1030 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1031 6353ad76 2019-02-08 stsp size_t flen, blen;
1033 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1035 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1036 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1038 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1039 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1041 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1042 b8f41171 2019-02-10 stsp sb->st_mode =
1043 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1044 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1046 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1047 a378724f 2019-02-10 stsp return NULL;
1049 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1052 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1053 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1054 6353ad76 2019-02-08 stsp return NULL;
1057 b8f41171 2019-02-10 stsp if (ie == NULL)
1058 d00136be 2019-03-26 stsp return NULL;
1060 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1061 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1062 2ec1f75b 2019-03-26 stsp return NULL;
1063 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1064 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1065 b8f41171 2019-02-10 stsp return NULL;
1068 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1069 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1070 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1071 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1072 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1073 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1074 6353ad76 2019-02-08 stsp return NULL;
1076 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1077 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1079 6353ad76 2019-02-08 stsp return err;
1081 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1082 6353ad76 2019-02-08 stsp if (f == NULL) {
1083 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1084 6353ad76 2019-02-08 stsp goto done;
1086 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1088 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1089 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1091 7154f6ce 2019-03-27 stsp goto done;
1092 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1093 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1094 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1095 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1096 7154f6ce 2019-03-27 stsp goto done;
1098 6353ad76 2019-02-08 stsp if (blen == 0) {
1099 6353ad76 2019-02-08 stsp if (flen != 0)
1100 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1102 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1103 6353ad76 2019-02-08 stsp if (blen != 0)
1104 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1106 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1107 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1108 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1109 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1113 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1116 6353ad76 2019-02-08 stsp hdrlen = 0;
1119 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1120 7154f6ce 2019-03-27 stsp rewind(f);
1121 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1125 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1127 6353ad76 2019-02-08 stsp fclose(f);
1128 9d31a1d8 2018-03-11 stsp return err;
1131 9d31a1d8 2018-03-11 stsp static const struct got_error *
1132 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1133 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1134 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1135 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1136 0584f854 2019-04-06 stsp void *progress_arg)
1138 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1139 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1140 6353ad76 2019-02-08 stsp char *ondisk_path;
1141 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1142 b8f41171 2019-02-10 stsp struct stat sb;
1144 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1145 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1147 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1149 b8f41171 2019-02-10 stsp goto done;
1151 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1152 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1153 b8f41171 2019-02-10 stsp goto done;
1156 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1157 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1158 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1159 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1160 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1162 6353ad76 2019-02-08 stsp goto done;
1164 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1165 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1166 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1167 b8f41171 2019-02-10 stsp goto done;
1170 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1172 6353ad76 2019-02-08 stsp goto done;
1174 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1175 234035bc 2019-06-01 stsp int update_timestamps;
1176 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1177 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1178 234035bc 2019-06-01 stsp struct got_object_id id2;
1179 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1180 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1182 234035bc 2019-06-01 stsp goto done;
1184 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1185 234035bc 2019-06-01 stsp ondisk_path, path, sb.st_mode, blob, repo,
1186 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1187 234035bc 2019-06-01 stsp if (blob2)
1188 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1190 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1191 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1192 234035bc 2019-06-01 stsp * unmodified files again.
1194 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1195 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1196 234035bc 2019-06-01 stsp update_timestamps);
1197 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1198 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1199 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1200 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1202 13d9040b 2019-03-27 stsp goto done;
1204 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1205 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1206 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1208 13d9040b 2019-03-27 stsp goto done;
1209 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1210 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1212 13d9040b 2019-03-27 stsp goto done;
1214 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1216 6353ad76 2019-02-08 stsp free(ondisk_path);
1217 8da9e5f4 2019-01-12 stsp return err;
1220 90285c3b 2019-01-08 stsp static const struct got_error *
1221 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1223 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1224 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1226 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1227 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1229 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1230 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1231 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1233 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1234 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1235 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1236 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1237 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1241 90285c3b 2019-01-08 stsp parent = dirname(parent);
1244 90285c3b 2019-01-08 stsp free(ondisk_path);
1245 90285c3b 2019-01-08 stsp return err;
1248 708d8e67 2019-03-27 stsp static const struct got_error *
1249 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1250 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1251 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1254 708d8e67 2019-03-27 stsp unsigned char status;
1255 708d8e67 2019-03-27 stsp struct stat sb;
1256 708d8e67 2019-03-27 stsp char *ondisk_path;
1258 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1260 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1262 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1264 708d8e67 2019-03-27 stsp return err;
1266 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1267 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1268 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1270 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1271 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1273 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1276 708d8e67 2019-03-27 stsp return err;
1278 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1279 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1280 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1282 fc6346c4 2019-03-27 stsp return err;
1284 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1287 fc6346c4 2019-03-27 stsp return err;
1290 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1291 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1292 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1293 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1294 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1295 8da9e5f4 2019-01-12 stsp void *progress_arg;
1296 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1297 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1300 512f0d0e 2019-01-02 stsp static const struct got_error *
1301 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1302 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1304 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1306 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1307 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1309 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1310 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1313 8da9e5f4 2019-01-12 stsp static const struct got_error *
1314 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1316 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1318 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1319 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1321 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1322 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1325 9d31a1d8 2018-03-11 stsp static const struct got_error *
1326 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1328 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1329 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1330 8da9e5f4 2019-01-12 stsp char *path;
1332 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1333 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1335 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1336 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1338 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1340 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1341 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1343 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1344 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1346 8da9e5f4 2019-01-12 stsp free(path);
1347 0cd1c46a 2019-03-11 stsp return err;
1350 517bab73 2019-03-11 stsp const struct got_error *
1351 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1353 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1354 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1355 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1357 517bab73 2019-03-11 stsp *refname = NULL;
1359 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1360 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1361 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1363 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1365 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1366 0647c563 2019-03-11 stsp *refname = NULL;
1368 517bab73 2019-03-11 stsp free(uuidstr);
1369 517bab73 2019-03-11 stsp return err;
1373 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1374 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1376 517bab73 2019-03-11 stsp static const struct got_error *
1377 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1379 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1380 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1381 517bab73 2019-03-11 stsp char *refname;
1383 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1385 517bab73 2019-03-11 stsp return err;
1387 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1389 0cd1c46a 2019-03-11 stsp goto done;
1391 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1393 0cd1c46a 2019-03-11 stsp free(refname);
1395 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1396 9d31a1d8 2018-03-11 stsp return err;
1399 ebf99748 2019-05-09 stsp static const struct got_error *
1400 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1401 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1403 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1404 ebf99748 2019-05-09 stsp FILE *index = NULL;
1406 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1407 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1408 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1409 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1411 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1412 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1413 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1414 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1415 ebf99748 2019-05-09 stsp goto done;
1418 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1419 ebf99748 2019-05-09 stsp if (index == NULL) {
1420 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1421 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1423 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1424 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1425 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1428 ebf99748 2019-05-09 stsp if (err) {
1429 ebf99748 2019-05-09 stsp free(*fileindex_path);
1430 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1431 ebf99748 2019-05-09 stsp free(*fileindex);
1432 ebf99748 2019-05-09 stsp *fileindex = NULL;
1434 ebf99748 2019-05-09 stsp return err;
1437 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1438 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1439 c932eeeb 2019-05-22 stsp const char *path;
1440 c932eeeb 2019-05-22 stsp size_t path_len;
1441 c932eeeb 2019-05-22 stsp const char *entry_name;
1442 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1443 a484d721 2019-06-10 stsp void *progress_arg;
1446 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1447 c932eeeb 2019-05-22 stsp static const struct got_error *
1448 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1450 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1452 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1453 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1454 c932eeeb 2019-05-22 stsp return NULL;
1455 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1456 102ff934 2019-06-11 stsp return NULL;
1458 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1459 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1460 c932eeeb 2019-05-22 stsp return NULL;
1462 a484d721 2019-06-10 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1463 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1464 c932eeeb 2019-05-22 stsp return NULL;
1467 9c6338c4 2019-06-02 stsp static const struct got_error *
1468 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1470 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1471 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1472 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1474 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1475 9c6338c4 2019-06-02 stsp fileindex_path);
1477 9c6338c4 2019-06-02 stsp goto done;
1479 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1481 9c6338c4 2019-06-02 stsp goto done;
1483 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1484 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1485 9c6338c4 2019-06-02 stsp fileindex_path);
1486 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1489 9c6338c4 2019-06-02 stsp if (new_index)
1490 9c6338c4 2019-06-02 stsp fclose(new_index);
1491 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1492 9c6338c4 2019-06-02 stsp return err;
1495 9d31a1d8 2018-03-11 stsp const struct got_error *
1496 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1497 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1498 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1500 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1501 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1502 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1503 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1504 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1505 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1506 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1507 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1508 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1509 af12c6b9 2019-06-04 stsp struct bump_base_commit_id_arg bbc_arg;
1511 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1513 9d31a1d8 2018-03-11 stsp return err;
1516 51514078 2018-12-25 stsp * Read the file index.
1517 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1518 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1520 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1522 ebf99748 2019-05-09 stsp goto done;
1524 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1526 0cd1c46a 2019-03-11 stsp goto done;
1528 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1529 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1531 9d31a1d8 2018-03-11 stsp goto done;
1533 c4cdcb68 2019-04-03 stsp if (path[0]) {
1534 c4cdcb68 2019-04-03 stsp char *tree_path;
1535 c4cdcb68 2019-04-03 stsp int obj_type;
1536 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1537 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1538 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1539 c4cdcb68 2019-04-03 stsp goto done;
1541 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1542 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1543 c4cdcb68 2019-04-03 stsp path) == -1) {
1544 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1545 c4cdcb68 2019-04-03 stsp goto done;
1547 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1548 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1549 c4cdcb68 2019-04-03 stsp free(tree_path);
1551 c4cdcb68 2019-04-03 stsp goto done;
1552 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1554 c4cdcb68 2019-04-03 stsp goto done;
1555 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1556 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1557 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1558 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1559 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1560 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1561 c4cdcb68 2019-04-03 stsp goto done;
1563 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1564 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1565 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1566 c4cdcb68 2019-04-03 stsp goto done;
1569 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1571 c4cdcb68 2019-04-03 stsp goto done;
1572 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1573 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1574 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1575 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1576 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1577 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1578 c4cdcb68 2019-04-03 stsp goto done;
1581 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1582 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1583 c4cdcb68 2019-04-03 stsp free(tree_path);
1585 c4cdcb68 2019-04-03 stsp goto done;
1586 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1587 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1588 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1589 c4cdcb68 2019-04-03 stsp goto done;
1593 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1594 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1595 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1596 c4cdcb68 2019-04-03 stsp goto done;
1598 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1599 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1601 c4cdcb68 2019-04-03 stsp goto done;
1604 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1606 c4cdcb68 2019-04-03 stsp goto done;
1608 c4cdcb68 2019-04-03 stsp if (entry_name &&
1609 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1610 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1611 8da9e5f4 2019-01-12 stsp goto done;
1614 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1615 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1616 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1617 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1618 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1619 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1620 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1621 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1622 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1623 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1624 af12c6b9 2019-06-04 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1625 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1627 af12c6b9 2019-06-04 stsp goto sync;
1629 af12c6b9 2019-06-04 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1630 af12c6b9 2019-06-04 stsp bbc_arg.entry_name = entry_name;
1631 af12c6b9 2019-06-04 stsp bbc_arg.path = path;
1632 af12c6b9 2019-06-04 stsp bbc_arg.path_len = strlen(path);
1633 a484d721 2019-06-10 stsp bbc_arg.progress_cb = progress_cb;
1634 a484d721 2019-06-10 stsp bbc_arg.progress_arg = progress_arg;
1635 af12c6b9 2019-06-04 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1636 af12c6b9 2019-06-04 stsp bump_base_commit_id, &bbc_arg);
1638 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1639 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1640 af12c6b9 2019-06-04 stsp err = sync_err;
1642 9c6338c4 2019-06-02 stsp free(fileindex_path);
1643 c4cdcb68 2019-04-03 stsp free(relpath);
1645 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1646 9d31a1d8 2018-03-11 stsp if (commit)
1647 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1648 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1649 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1650 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1651 9d31a1d8 2018-03-11 stsp err = unlockerr;
1652 f8d1f275 2019-02-04 stsp return err;
1655 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1656 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1657 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1658 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1659 234035bc 2019-06-01 stsp void *progress_arg;
1660 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1661 234035bc 2019-06-01 stsp void *cancel_arg;
1664 234035bc 2019-06-01 stsp static const struct got_error *
1665 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1666 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1667 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1668 234035bc 2019-06-01 stsp struct got_repository *repo)
1670 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1671 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1672 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1673 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1674 234035bc 2019-06-01 stsp struct stat sb;
1675 234035bc 2019-06-01 stsp unsigned char status;
1676 234035bc 2019-06-01 stsp int local_changes_subsumed;
1678 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1679 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1680 234035bc 2019-06-01 stsp if (ie == NULL) {
1681 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1683 234035bc 2019-06-01 stsp return NULL;
1686 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1687 234035bc 2019-06-01 stsp path2) == -1)
1688 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1690 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1692 234035bc 2019-06-01 stsp goto done;
1694 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1695 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1697 234035bc 2019-06-01 stsp goto done;
1699 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1700 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1701 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1702 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1703 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status, path2);
1704 234035bc 2019-06-01 stsp goto done;
1707 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1708 234035bc 2019-06-01 stsp ondisk_path, path2, sb.st_mode, blob2, repo,
1709 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1710 234035bc 2019-06-01 stsp } else if (blob1) {
1711 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1712 234035bc 2019-06-01 stsp if (ie == NULL) {
1713 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1715 234035bc 2019-06-01 stsp return NULL;
1718 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1719 2b92fad7 2019-06-02 stsp path1) == -1)
1720 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1722 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1724 2b92fad7 2019-06-02 stsp goto done;
1726 2b92fad7 2019-06-02 stsp switch (status) {
1727 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1728 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1730 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1732 2b92fad7 2019-06-02 stsp goto done;
1734 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1736 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1737 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1738 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1741 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1743 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1744 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1745 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1746 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg,
1747 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1749 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1750 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, status, path1);
1755 234035bc 2019-06-01 stsp } else if (blob2) {
1756 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1757 234035bc 2019-06-01 stsp path2) == -1)
1758 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1759 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1761 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1764 234035bc 2019-06-01 stsp goto done;
1765 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1766 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1767 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1768 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1769 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status,
1771 234035bc 2019-06-01 stsp goto done;
1773 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1774 234035bc 2019-06-01 stsp NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1775 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1776 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1777 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1778 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1779 234035bc 2019-06-01 stsp blob2, 0);
1781 234035bc 2019-06-01 stsp goto done;
1784 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1785 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1786 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1787 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1788 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1789 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1791 234035bc 2019-06-01 stsp goto done;
1792 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1793 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1795 234035bc 2019-06-01 stsp goto done;
1796 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1797 2b92fad7 2019-06-02 stsp if (err) {
1798 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1799 2b92fad7 2019-06-02 stsp goto done;
1804 234035bc 2019-06-01 stsp free(ondisk_path);
1805 234035bc 2019-06-01 stsp return err;
1808 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1809 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1810 234035bc 2019-06-01 stsp struct got_repository *repo;
1813 234035bc 2019-06-01 stsp static const struct got_error *
1814 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1816 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1817 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1818 234035bc 2019-06-01 stsp unsigned char status;
1819 234035bc 2019-06-01 stsp struct stat sb;
1820 234035bc 2019-06-01 stsp char *ondisk_path;
1822 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1823 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1824 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1825 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1827 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1829 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1831 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1832 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1834 234035bc 2019-06-01 stsp return err;
1835 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1836 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1838 234035bc 2019-06-01 stsp return NULL;
1841 234035bc 2019-06-01 stsp const struct got_error *
1842 234035bc 2019-06-01 stsp got_worktree_merge_files(struct got_worktree *worktree,
1843 234035bc 2019-06-01 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1844 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1845 234035bc 2019-06-01 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1847 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1848 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1849 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1850 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
1851 234035bc 2019-06-01 stsp char *fileindex_path = NULL;
1852 234035bc 2019-06-01 stsp struct got_fileindex *fileindex = NULL;
1853 234035bc 2019-06-01 stsp struct check_merge_ok_arg mok_arg;
1855 234035bc 2019-06-01 stsp err = lock_worktree(worktree, LOCK_EX);
1857 234035bc 2019-06-01 stsp return err;
1859 234035bc 2019-06-01 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1861 234035bc 2019-06-01 stsp goto done;
1863 234035bc 2019-06-01 stsp mok_arg.worktree = worktree;
1864 234035bc 2019-06-01 stsp mok_arg.repo = repo;
1865 234035bc 2019-06-01 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1866 234035bc 2019-06-01 stsp &mok_arg);
1868 234035bc 2019-06-01 stsp goto done;
1870 03415a1a 2019-06-02 stsp if (commit_id1) {
1871 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1872 03415a1a 2019-06-02 stsp worktree->path_prefix);
1874 03415a1a 2019-06-02 stsp goto done;
1876 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1878 03415a1a 2019-06-02 stsp goto done;
1881 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1882 234035bc 2019-06-01 stsp worktree->path_prefix);
1884 234035bc 2019-06-01 stsp goto done;
1886 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1888 234035bc 2019-06-01 stsp goto done;
1890 234035bc 2019-06-01 stsp arg.worktree = worktree;
1891 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
1892 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
1893 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
1894 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
1895 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
1896 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1897 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1898 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1899 af12c6b9 2019-06-04 stsp err = sync_err;
1901 234035bc 2019-06-01 stsp got_fileindex_free(fileindex);
1902 234035bc 2019-06-01 stsp if (tree1)
1903 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
1904 234035bc 2019-06-01 stsp if (tree2)
1905 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
1907 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1908 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
1909 234035bc 2019-06-01 stsp err = unlockerr;
1910 234035bc 2019-06-01 stsp return err;
1913 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1914 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1915 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1916 927df6b7 2019-02-10 stsp const char *status_path;
1917 927df6b7 2019-02-10 stsp size_t status_path_len;
1918 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1919 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1920 f8d1f275 2019-02-04 stsp void *status_arg;
1921 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1922 f8d1f275 2019-02-04 stsp void *cancel_arg;
1925 f8d1f275 2019-02-04 stsp static const struct got_error *
1926 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1927 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1928 927df6b7 2019-02-10 stsp struct got_repository *repo)
1930 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1931 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1932 927df6b7 2019-02-10 stsp struct stat sb;
1933 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1935 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1936 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1937 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1938 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1939 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1940 016a88dd 2019-05-13 stsp &commit_id);
1942 927df6b7 2019-02-10 stsp return err;
1945 927df6b7 2019-02-10 stsp static const struct got_error *
1946 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1947 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1949 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1950 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1951 f8d1f275 2019-02-04 stsp char *abspath;
1953 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1954 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1956 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1957 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1958 927df6b7 2019-02-10 stsp return NULL;
1960 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1961 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1962 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1963 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1965 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1966 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1967 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1970 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1972 f8d1f275 2019-02-04 stsp free(abspath);
1973 9d31a1d8 2018-03-11 stsp return err;
1976 f8d1f275 2019-02-04 stsp static const struct got_error *
1977 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1979 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1980 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1981 2ec1f75b 2019-03-26 stsp unsigned char status;
1983 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1984 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1986 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1987 927df6b7 2019-02-10 stsp return NULL;
1989 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1990 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1991 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1992 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1994 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1995 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1996 016a88dd 2019-05-13 stsp &commit_id);
1999 f8d1f275 2019-02-04 stsp static const struct got_error *
2000 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2002 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2003 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2004 f8d1f275 2019-02-04 stsp char *path = NULL;
2006 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2007 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2009 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2010 2c201a36 2019-02-10 stsp return NULL;
2012 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2013 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2014 f8d1f275 2019-02-04 stsp return NULL;
2016 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2017 927df6b7 2019-02-10 stsp return NULL;
2019 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2020 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2021 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2023 f8d1f275 2019-02-04 stsp path = de->d_name;
2026 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2027 016a88dd 2019-05-13 stsp NULL, NULL);
2028 f8d1f275 2019-02-04 stsp if (parent_path[0])
2029 f8d1f275 2019-02-04 stsp free(path);
2030 b72f483a 2019-02-05 stsp return err;
2033 f8d1f275 2019-02-04 stsp const struct got_error *
2034 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2035 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2036 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2038 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2039 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2040 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
2041 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
2042 f8d1f275 2019-02-04 stsp FILE *index = NULL;
2043 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2044 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2045 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2047 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
2048 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
2049 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2050 f8d1f275 2019-02-04 stsp goto done;
2053 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2054 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2056 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
2057 f8d1f275 2019-02-04 stsp goto done;
2060 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
2061 f8d1f275 2019-02-04 stsp if (index == NULL) {
2062 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
2063 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2064 f8d1f275 2019-02-04 stsp goto done;
2067 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
2068 f8d1f275 2019-02-04 stsp fclose(index);
2070 f8d1f275 2019-02-04 stsp goto done;
2073 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2074 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2075 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2076 c513d110 2019-02-05 stsp goto done;
2078 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2079 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2080 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2081 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2082 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2083 927df6b7 2019-02-10 stsp if (ie == NULL) {
2084 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2085 927df6b7 2019-02-10 stsp goto done;
2087 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2088 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2089 927df6b7 2019-02-10 stsp goto done;
2091 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2092 927df6b7 2019-02-10 stsp goto done;
2095 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2096 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2097 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2098 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2099 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2100 927df6b7 2019-02-10 stsp arg.status_path = path;
2101 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2102 f8d1f275 2019-02-04 stsp arg.repo = repo;
2103 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2104 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2105 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2106 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2107 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2108 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2110 f8d1f275 2019-02-04 stsp if (workdir)
2111 f8d1f275 2019-02-04 stsp closedir(workdir);
2112 927df6b7 2019-02-10 stsp free(ondisk_path);
2113 f8d1f275 2019-02-04 stsp free(fileindex_path);
2114 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2115 f8d1f275 2019-02-04 stsp return err;
2118 6c7ab921 2019-03-18 stsp const struct got_error *
2119 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2120 6c7ab921 2019-03-18 stsp const char *arg)
2122 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2123 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
2124 6c7ab921 2019-03-18 stsp size_t len;
2126 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2128 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2129 6c7ab921 2019-03-18 stsp if (resolved == NULL)
2130 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
2132 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2133 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2134 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2135 6c7ab921 2019-03-18 stsp goto done;
2138 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2139 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2140 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2142 f6d88e1a 2019-05-29 stsp goto done;
2144 f6d88e1a 2019-05-29 stsp path = strdup("");
2145 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2146 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2147 f6d88e1a 2019-05-29 stsp goto done;
2151 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2152 6c7ab921 2019-03-18 stsp len = strlen(path);
2153 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2154 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2158 6c7ab921 2019-03-18 stsp free(resolved);
2159 6c7ab921 2019-03-18 stsp if (err == NULL)
2160 6c7ab921 2019-03-18 stsp *wt_path = path;
2162 6c7ab921 2019-03-18 stsp free(path);
2163 d00136be 2019-03-26 stsp return err;
2166 07f5b47a 2019-06-02 stsp static const struct got_error *
2167 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2168 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2169 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2171 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2172 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2174 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2175 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2176 07f5b47a 2019-06-02 stsp return NULL;
2178 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2180 07f5b47a 2019-06-02 stsp return err;
2182 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2183 07f5b47a 2019-06-02 stsp if (err) {
2184 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2185 07f5b47a 2019-06-02 stsp return err;
2188 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2191 d00136be 2019-03-26 stsp const struct got_error *
2192 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2193 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2194 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2195 031a5338 2019-03-26 stsp struct got_repository *repo)
2197 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2198 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2199 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2200 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2201 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2203 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2205 d00136be 2019-03-26 stsp return err;
2208 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
2209 d00136be 2019-03-26 stsp if (fileindex == NULL) {
2210 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2211 d00136be 2019-03-26 stsp goto done;
2214 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2215 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2216 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2217 d00136be 2019-03-26 stsp fileindex_path = NULL;
2218 d00136be 2019-03-26 stsp goto done;
2221 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2222 d00136be 2019-03-26 stsp if (index == NULL) {
2223 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2224 d00136be 2019-03-26 stsp goto done;
2227 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2229 d00136be 2019-03-26 stsp goto done;
2231 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2232 1dd54920 2019-05-11 stsp char *relpath;
2233 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2234 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2237 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2238 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2239 1dd54920 2019-05-11 stsp free(relpath);
2243 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2244 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2245 af12c6b9 2019-06-04 stsp err = sync_err;
2247 d00136be 2019-03-26 stsp if (index) {
2248 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2249 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2251 d00136be 2019-03-26 stsp if (fileindex)
2252 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2253 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2254 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2255 d00136be 2019-03-26 stsp err = unlockerr;
2256 6c7ab921 2019-03-18 stsp return err;
2259 17ed4618 2019-06-02 stsp static const struct got_error *
2260 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2261 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2262 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2263 17ed4618 2019-06-02 stsp struct got_repository *repo)
2265 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2266 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2267 17ed4618 2019-06-02 stsp unsigned char status;
2268 17ed4618 2019-06-02 stsp struct stat sb;
2270 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2271 17ed4618 2019-06-02 stsp if (ie == NULL)
2272 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2274 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2276 17ed4618 2019-06-02 stsp return err;
2278 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2279 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2280 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2281 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2282 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2283 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2284 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2287 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2288 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2290 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2291 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2294 2ec1f75b 2019-03-26 stsp const struct got_error *
2295 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2296 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2297 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2298 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2300 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2301 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2302 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2303 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2304 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2306 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2308 2ec1f75b 2019-03-26 stsp return err;
2310 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2311 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2312 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2313 2ec1f75b 2019-03-26 stsp goto done;
2316 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2317 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2318 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2319 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2320 2ec1f75b 2019-03-26 stsp goto done;
2323 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2324 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2325 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2326 2ec1f75b 2019-03-26 stsp goto done;
2329 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2331 2ec1f75b 2019-03-26 stsp goto done;
2333 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2334 17ed4618 2019-06-02 stsp char *relpath;
2335 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2336 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2339 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2340 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2341 17ed4618 2019-06-02 stsp free(relpath);
2345 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2346 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2347 af12c6b9 2019-06-04 stsp err = sync_err;
2349 2ec1f75b 2019-03-26 stsp if (index) {
2350 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2351 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2353 2ec1f75b 2019-03-26 stsp if (fileindex)
2354 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2355 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2356 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2357 2ec1f75b 2019-03-26 stsp err = unlockerr;
2358 2ec1f75b 2019-03-26 stsp return err;
2361 e20a8b6f 2019-06-04 stsp static const struct got_error *
2362 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2363 a129376b 2019-03-28 stsp const char *ondisk_path,
2364 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2365 a129376b 2019-03-28 stsp struct got_repository *repo)
2367 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2368 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2369 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2370 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2371 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2372 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2373 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2374 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2375 a129376b 2019-03-28 stsp unsigned char status;
2376 a129376b 2019-03-28 stsp struct stat sb;
2378 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2379 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2381 a129376b 2019-03-28 stsp goto done;
2383 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2384 a129376b 2019-03-28 stsp if (ie == NULL) {
2385 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2386 a129376b 2019-03-28 stsp goto done;
2389 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2390 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2391 a129376b 2019-03-28 stsp if (err) {
2392 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2393 a129376b 2019-03-28 stsp goto done;
2394 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2395 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2396 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2397 e20a8b6f 2019-06-04 stsp goto done;
2400 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2401 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2402 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2403 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2404 a129376b 2019-03-28 stsp goto done;
2407 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2408 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2409 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2410 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2411 a129376b 2019-03-28 stsp goto done;
2414 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2415 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2416 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2417 a129376b 2019-03-28 stsp goto done;
2422 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2423 a129376b 2019-03-28 stsp tree_path);
2425 a129376b 2019-03-28 stsp goto done;
2427 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2429 a129376b 2019-03-28 stsp goto done;
2431 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2432 a129376b 2019-03-28 stsp if (te_name == NULL) {
2433 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2434 a129376b 2019-03-28 stsp goto done;
2437 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2439 a129376b 2019-03-28 stsp goto done;
2441 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2442 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2443 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2444 a129376b 2019-03-28 stsp goto done;
2447 a129376b 2019-03-28 stsp switch (status) {
2448 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2449 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2450 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2452 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2453 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2454 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2455 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2456 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2457 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2458 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2460 a129376b 2019-03-28 stsp goto done;
2461 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2462 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2463 a129376b 2019-03-28 stsp progress_arg);
2465 a129376b 2019-03-28 stsp goto done;
2466 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2467 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2468 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2470 a129376b 2019-03-28 stsp goto done;
2475 a129376b 2019-03-28 stsp goto done;
2478 a129376b 2019-03-28 stsp free(relpath);
2479 e20a8b6f 2019-06-04 stsp free(parent_path);
2480 a129376b 2019-03-28 stsp free(tree_path);
2482 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2484 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2485 a129376b 2019-03-28 stsp free(tree_id);
2486 e20a8b6f 2019-06-04 stsp return err;
2489 e20a8b6f 2019-06-04 stsp const struct got_error *
2490 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2491 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2492 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2493 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2495 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2496 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2497 e20a8b6f 2019-06-04 stsp FILE *index = NULL;
2498 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2499 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2500 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2502 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2504 e20a8b6f 2019-06-04 stsp return err;
2506 e20a8b6f 2019-06-04 stsp fileindex = got_fileindex_alloc();
2507 e20a8b6f 2019-06-04 stsp if (fileindex == NULL) {
2508 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("got_fileindex_alloc");
2509 e20a8b6f 2019-06-04 stsp goto done;
2512 e20a8b6f 2019-06-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2513 e20a8b6f 2019-06-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2514 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("asprintf");
2515 e20a8b6f 2019-06-04 stsp fileindex_path = NULL;
2516 e20a8b6f 2019-06-04 stsp goto done;
2519 e20a8b6f 2019-06-04 stsp index = fopen(fileindex_path, "rb");
2520 e20a8b6f 2019-06-04 stsp if (index == NULL) {
2521 e20a8b6f 2019-06-04 stsp err = got_error_from_errno2("fopen", fileindex_path);
2522 e20a8b6f 2019-06-04 stsp goto done;
2525 e20a8b6f 2019-06-04 stsp err = got_fileindex_read(fileindex, index);
2527 e20a8b6f 2019-06-04 stsp goto done;
2529 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2530 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2531 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2535 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2536 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2537 e20a8b6f 2019-06-04 stsp err = sync_err;
2539 a129376b 2019-03-28 stsp if (index) {
2540 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2541 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2543 a129376b 2019-03-28 stsp if (fileindex)
2544 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2545 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2546 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2547 a129376b 2019-03-28 stsp err = unlockerr;
2548 c4296144 2019-05-09 stsp return err;
2551 cf066bf8 2019-05-09 stsp static void
2552 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2554 24519714 2019-05-09 stsp free(ct->path);
2555 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2556 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2557 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2558 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2559 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2563 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2564 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2565 24519714 2019-05-09 stsp struct got_repository *repo;
2566 24519714 2019-05-09 stsp struct got_worktree *worktree;
2569 c4296144 2019-05-09 stsp static const struct got_error *
2570 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2571 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2573 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2574 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2575 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2576 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2577 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2578 768aea60 2019-05-09 stsp struct stat sb;
2580 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2581 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2583 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2584 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2585 c4296144 2019-05-09 stsp return NULL;
2587 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2588 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2589 036813ee 2019-05-09 stsp goto done;
2591 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2592 036813ee 2019-05-09 stsp parent_path = strdup("");
2593 69960a46 2019-05-09 stsp if (parent_path == NULL)
2594 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2596 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2598 69960a46 2019-05-09 stsp return err;
2601 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2602 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2603 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2604 c4296144 2019-05-09 stsp goto done;
2607 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2608 768aea60 2019-05-09 stsp relpath) == -1) {
2609 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2610 768aea60 2019-05-09 stsp goto done;
2612 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2613 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2615 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2616 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2617 768aea60 2019-05-09 stsp goto done;
2619 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2622 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2623 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2624 44d03001 2019-05-09 stsp relpath) == -1) {
2625 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2626 44d03001 2019-05-09 stsp goto done;
2629 cf066bf8 2019-05-09 stsp ct->status = status;
2630 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2631 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2632 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2633 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2634 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2635 b416585c 2019-05-13 stsp goto done;
2637 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2638 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2639 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2640 036813ee 2019-05-09 stsp goto done;
2643 24519714 2019-05-09 stsp ct->path = strdup(path);
2644 24519714 2019-05-09 stsp if (ct->path == NULL) {
2645 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2646 24519714 2019-05-09 stsp goto done;
2648 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2650 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2651 ed175427 2019-05-09 stsp free_commitable(ct);
2652 24519714 2019-05-09 stsp free(parent_path);
2653 036813ee 2019-05-09 stsp free(path);
2654 a129376b 2019-03-28 stsp return err;
2657 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2658 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2659 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2660 036813ee 2019-05-09 stsp struct got_repository *);
2662 ed175427 2019-05-09 stsp static const struct got_error *
2663 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2664 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2665 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2666 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2667 afa376bf 2019-05-09 stsp struct got_repository *repo)
2669 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2670 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2671 036813ee 2019-05-09 stsp char *subpath;
2673 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2674 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2677 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2679 ed175427 2019-05-09 stsp return err;
2681 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2682 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2683 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2684 036813ee 2019-05-09 stsp free(subpath);
2685 036813ee 2019-05-09 stsp return err;
2688 036813ee 2019-05-09 stsp static const struct got_error *
2689 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2691 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2692 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2694 036813ee 2019-05-09 stsp *match = 0;
2696 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2697 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2698 0f63689d 2019-05-10 stsp return NULL;
2701 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2703 0f63689d 2019-05-10 stsp return err;
2704 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2705 036813ee 2019-05-09 stsp free(ct_parent_path);
2706 036813ee 2019-05-09 stsp return err;
2709 768aea60 2019-05-09 stsp static mode_t
2710 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2712 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2715 036813ee 2019-05-09 stsp static const struct got_error *
2716 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2717 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2719 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2721 036813ee 2019-05-09 stsp *new_te = NULL;
2723 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2725 036813ee 2019-05-09 stsp goto done;
2727 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2729 036813ee 2019-05-09 stsp free((*new_te)->id);
2730 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2731 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2732 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2733 036813ee 2019-05-09 stsp goto done;
2736 036813ee 2019-05-09 stsp if (err && *new_te) {
2737 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2738 036813ee 2019-05-09 stsp *new_te = NULL;
2740 036813ee 2019-05-09 stsp return err;
2743 036813ee 2019-05-09 stsp static const struct got_error *
2744 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2745 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2747 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2748 036813ee 2019-05-09 stsp char *ct_name;
2750 036813ee 2019-05-09 stsp *new_te = NULL;
2752 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2753 036813ee 2019-05-09 stsp if (*new_te == NULL)
2754 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2756 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2757 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2758 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2759 036813ee 2019-05-09 stsp goto done;
2761 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2762 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2763 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2764 036813ee 2019-05-09 stsp goto done;
2767 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2769 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2770 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2771 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2772 036813ee 2019-05-09 stsp goto done;
2775 036813ee 2019-05-09 stsp if (err && *new_te) {
2776 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2777 036813ee 2019-05-09 stsp *new_te = NULL;
2779 036813ee 2019-05-09 stsp return err;
2782 036813ee 2019-05-09 stsp static const struct got_error *
2783 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2784 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2786 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2787 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2789 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2791 036813ee 2019-05-09 stsp return err;
2792 036813ee 2019-05-09 stsp if (new_pe == NULL)
2793 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2794 036813ee 2019-05-09 stsp return NULL;
2797 afa376bf 2019-05-09 stsp static const struct got_error *
2798 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2799 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2801 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2802 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2803 afa376bf 2019-05-09 stsp ct_path++;
2804 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2807 036813ee 2019-05-09 stsp static const struct got_error *
2808 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2809 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2811 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2812 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2813 44d03001 2019-05-09 stsp char *te_path;
2815 44d03001 2019-05-09 stsp *modified = 0;
2817 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2818 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2819 44d03001 2019-05-09 stsp te->name) == -1)
2820 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2822 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2823 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2824 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2825 44d03001 2019-05-09 stsp strlen(te_path));
2826 44d03001 2019-05-09 stsp if (*modified)
2830 44d03001 2019-05-09 stsp free(te_path);
2831 44d03001 2019-05-09 stsp return err;
2834 44d03001 2019-05-09 stsp static const struct got_error *
2835 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2836 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2837 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2839 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2840 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2842 036813ee 2019-05-09 stsp *ctp = NULL;
2844 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2845 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2846 036813ee 2019-05-09 stsp char *ct_name = NULL;
2847 036813ee 2019-05-09 stsp int path_matches;
2849 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2850 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2853 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2856 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2858 036813ee 2019-05-09 stsp return err;
2859 036813ee 2019-05-09 stsp if (!path_matches)
2862 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2863 036813ee 2019-05-09 stsp if (ct_name == NULL)
2864 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2866 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2869 036813ee 2019-05-09 stsp *ctp = ct;
2873 036813ee 2019-05-09 stsp return err;
2876 036813ee 2019-05-09 stsp static const struct got_error *
2877 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2878 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2879 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2880 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2881 036813ee 2019-05-09 stsp struct got_repository *repo)
2883 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2884 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2885 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2886 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2887 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2888 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2890 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2891 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2892 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2894 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2895 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2896 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2897 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2899 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2900 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2903 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2904 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2907 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2908 036813ee 2019-05-09 stsp pe->path);
2910 036813ee 2019-05-09 stsp goto done;
2912 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2913 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2914 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2916 036813ee 2019-05-09 stsp goto done;
2917 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2919 afa376bf 2019-05-09 stsp goto done;
2920 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
2922 2f51b5b3 2019-05-09 stsp char *subtree_path;
2924 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2925 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2926 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2927 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2928 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2929 2f51b5b3 2019-05-09 stsp goto done;
2932 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2933 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2934 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2935 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2936 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2937 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2938 9ba0479c 2019-05-10 stsp new_te = NULL;
2939 9ba0479c 2019-05-10 stsp goto done;
2941 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2942 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2943 2f51b5b3 2019-05-09 stsp free(subtree_path);
2944 9ba0479c 2019-05-10 stsp if (err) {
2945 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2946 9ba0479c 2019-05-10 stsp new_te = NULL;
2947 036813ee 2019-05-09 stsp goto done;
2950 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2952 2f51b5b3 2019-05-09 stsp goto done;
2955 2f51b5b3 2019-05-09 stsp if (base_tree) {
2956 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2957 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2958 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2959 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2961 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2962 44d03001 2019-05-09 stsp int modified;
2963 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2965 036813ee 2019-05-09 stsp goto done;
2966 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2967 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2969 2f51b5b3 2019-05-09 stsp goto done;
2970 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2971 44d03001 2019-05-09 stsp if (modified) {
2972 44d03001 2019-05-09 stsp free(new_te->id);
2973 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2974 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2975 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2977 44d03001 2019-05-09 stsp goto done;
2979 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2981 036813ee 2019-05-09 stsp goto done;
2985 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2986 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2988 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2989 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2990 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2991 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2993 2f51b5b3 2019-05-09 stsp goto done;
2994 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2996 2f51b5b3 2019-05-09 stsp goto done;
2998 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
2999 b416585c 2019-05-13 stsp status_arg);
3001 afa376bf 2019-05-09 stsp goto done;
3003 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3004 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3006 2f51b5b3 2019-05-09 stsp goto done;
3007 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3009 2f51b5b3 2019-05-09 stsp goto done;
3014 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3015 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3016 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3017 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3018 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3020 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3022 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3023 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3024 ed175427 2019-05-09 stsp return err;
3027 ebf99748 2019-05-09 stsp static const struct got_error *
3028 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3029 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3031 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err;
3032 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3033 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
3034 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3036 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3038 ebf99748 2019-05-09 stsp return err;
3040 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3041 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3042 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3044 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3046 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3047 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3048 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3050 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3051 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3052 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3054 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3055 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3056 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3059 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3064 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3065 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3066 af12c6b9 2019-06-04 stsp err = sync_err;
3067 ebf99748 2019-05-09 stsp free(fileindex_path);
3068 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
3069 d56d26ce 2019-05-10 stsp return err;
3072 d56d26ce 2019-05-10 stsp static const struct got_error *
3073 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3074 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3076 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3077 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3078 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3079 1a36436d 2019-06-10 stsp char *path = NULL;
3080 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3082 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3083 1a36436d 2019-06-10 stsp ct_path++;
3086 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3087 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3089 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3090 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3091 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3092 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3094 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3095 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3097 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3098 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3099 1a36436d 2019-06-10 stsp char *slash;
3100 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3102 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3103 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3104 1a36436d 2019-06-10 stsp return NULL;
3106 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3107 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3108 1a36436d 2019-06-10 stsp if (path == NULL) {
3109 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3110 1a36436d 2019-06-10 stsp goto done;
3112 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3113 1a36436d 2019-06-10 stsp if (slash)
3114 1a36436d 2019-06-10 stsp *slash = '\0';
3116 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3118 1a36436d 2019-06-10 stsp goto done;
3120 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3121 1a36436d 2019-06-10 stsp head_commit_id, path);
3123 1a36436d 2019-06-10 stsp goto done;
3125 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3126 1a36436d 2019-06-10 stsp while (pid) {
3127 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3129 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3130 1a36436d 2019-06-10 stsp if (err) {
3131 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3132 1a36436d 2019-06-10 stsp goto done;
3133 1a36436d 2019-06-10 stsp err = NULL;
3137 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3139 1a36436d 2019-06-10 stsp goto done;
3141 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3142 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3146 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3147 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3149 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3152 1a36436d 2019-06-10 stsp goto done;
3154 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3155 1a36436d 2019-06-10 stsp commit = pcommit;
3156 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3160 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3161 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3163 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3164 1a36436d 2019-06-10 stsp goto done;
3165 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3168 1a36436d 2019-06-10 stsp if (commit)
3169 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3170 d56d26ce 2019-05-10 stsp free(id_in_head);
3172 1a36436d 2019-06-10 stsp free(path);
3173 1a36436d 2019-06-10 stsp return err;
3176 c4296144 2019-05-09 stsp const struct got_error *
3177 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3178 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
3179 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
3180 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3181 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3182 afa376bf 2019-05-09 stsp struct got_repository *repo)
3184 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3185 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
3186 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
3187 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3188 bc70eb79 2019-05-09 stsp char *relpath = NULL;
3189 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3190 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
3191 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3192 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
3193 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3194 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3195 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3196 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3197 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3198 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3199 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3201 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3203 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
3204 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3206 c4296144 2019-05-09 stsp if (ondisk_path) {
3207 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
3208 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
3210 c4296144 2019-05-09 stsp return err;
3213 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
3215 c4296144 2019-05-09 stsp goto done;
3217 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3219 36a38700 2019-05-10 stsp goto done;
3220 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3222 09f5bd90 2019-05-09 stsp goto done;
3224 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
3225 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
3226 24519714 2019-05-09 stsp cc_arg.repo = repo;
3227 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
3228 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3230 675c7539 2019-05-09 stsp goto done;
3232 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
3233 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3237 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3239 588edf97 2019-05-10 stsp goto done;
3241 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3242 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3243 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3245 819f385b 2019-05-10 stsp goto done;
3248 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3250 588edf97 2019-05-10 stsp goto done;
3252 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3253 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3258 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3259 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3263 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3264 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3265 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3266 cf066bf8 2019-05-09 stsp char *ondisk_path;
3268 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3269 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3272 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3273 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3274 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3275 cf066bf8 2019-05-09 stsp goto done;
3277 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3278 a7055788 2019-05-09 stsp free(ondisk_path);
3280 cf066bf8 2019-05-09 stsp goto done;
3283 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3284 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3285 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3287 036813ee 2019-05-09 stsp goto done;
3289 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3291 de18fc63 2019-05-09 stsp goto done;
3292 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3293 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3294 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3295 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3296 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3297 33ad4cbe 2019-05-12 jcs free(logmsg);
3299 9d40349a 2019-05-09 stsp goto done;
3301 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3302 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3303 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3304 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3305 09f5bd90 2019-05-09 stsp goto done;
3307 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3308 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3310 09f5bd90 2019-05-09 stsp goto done;
3311 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3313 ebf99748 2019-05-09 stsp goto done;
3314 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3315 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3316 09f5bd90 2019-05-09 stsp goto done;
3318 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3319 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3321 f2c16586 2019-05-09 stsp goto done;
3322 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3324 f2c16586 2019-05-09 stsp goto done;
3326 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3328 09f5bd90 2019-05-09 stsp goto done;
3330 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3332 ebf99748 2019-05-09 stsp goto done;
3334 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3335 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3337 ebf99748 2019-05-09 stsp goto done;
3339 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3340 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3341 c4296144 2019-05-09 stsp err = unlockerr;
3342 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3343 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3344 ed175427 2019-05-09 stsp free_commitable(ct);
3346 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3347 588edf97 2019-05-10 stsp if (head_tree)
3348 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3349 588edf97 2019-05-10 stsp if (head_commit)
3350 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3351 c4296144 2019-05-09 stsp free(relpath);
3352 09f5bd90 2019-05-09 stsp free(head_commit_id);
3353 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3354 36a38700 2019-05-10 stsp if (head_ref)
3355 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3356 2f17228e 2019-05-12 stsp if (head_ref2) {
3357 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3358 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3359 2f17228e 2019-05-12 stsp err = unlockerr;
3360 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3362 c4296144 2019-05-09 stsp return err;
3365 8656d6c4 2019-05-20 stsp const char *
3366 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3368 8656d6c4 2019-05-20 stsp return ct->path;
3371 8656d6c4 2019-05-20 stsp unsigned int
3372 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3374 8656d6c4 2019-05-20 stsp return ct->status;