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"
45 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
52 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
53 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
54 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
57 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 99724ed4 2018-03-10 stsp static const struct got_error *
61 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
63 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
65 99724ed4 2018-03-10 stsp int fd = -1;
67 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
69 99724ed4 2018-03-10 stsp path = NULL;
73 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
75 99724ed4 2018-03-10 stsp if (fd == -1) {
76 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
80 99724ed4 2018-03-10 stsp if (content) {
81 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
82 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
83 638f9024 2019-05-13 stsp err = got_error_from_errno("dprintf");
89 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
90 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
95 507dc3bb 2018-12-29 stsp static const struct got_error *
96 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
98 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
99 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
100 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
101 507dc3bb 2018-12-29 stsp char *path = NULL;
103 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
105 507dc3bb 2018-12-29 stsp path = NULL;
109 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
113 507dc3bb 2018-12-29 stsp if (content) {
114 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
115 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
116 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
121 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
122 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
123 2a57020b 2019-02-20 stsp unlink(tmppath);
128 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
129 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
130 230a42bd 2019-05-11 jcs free(tmppath);
131 99724ed4 2018-03-10 stsp return err;
134 09fe317a 2018-03-11 stsp static const struct got_error *
135 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
137 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
138 09fe317a 2018-03-11 stsp char *path;
139 09fe317a 2018-03-11 stsp int fd = -1;
141 09fe317a 2018-03-11 stsp struct stat sb;
143 09fe317a 2018-03-11 stsp *content = NULL;
145 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
147 09fe317a 2018-03-11 stsp path = NULL;
151 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
152 09fe317a 2018-03-11 stsp if (fd == -1) {
153 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
154 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
156 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
159 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
165 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
166 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
169 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
170 09fe317a 2018-03-11 stsp if (*content == NULL) {
171 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
175 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
176 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
177 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
178 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
181 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
182 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
185 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
188 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
189 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
190 09fe317a 2018-03-11 stsp free(path);
192 09fe317a 2018-03-11 stsp free(*content);
193 09fe317a 2018-03-11 stsp *content = NULL;
195 09fe317a 2018-03-11 stsp return err;
198 024e9686 2019-05-14 stsp static const struct got_error *
199 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
201 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
202 024e9686 2019-05-14 stsp char *refstr = NULL;
204 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
205 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
206 024e9686 2019-05-14 stsp if (refstr == NULL)
207 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
209 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
210 024e9686 2019-05-14 stsp if (refstr == NULL)
211 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
213 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
214 024e9686 2019-05-14 stsp free(refstr);
215 024e9686 2019-05-14 stsp return err;
218 86c3caaf 2018-03-09 stsp const struct got_error *
219 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
220 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
222 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
223 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
224 ec22038e 2019-03-10 stsp uuid_t uuid;
225 ec22038e 2019-03-10 stsp uint32_t uuid_status;
226 65596e15 2018-12-24 stsp int obj_type;
227 7ac97322 2018-03-11 stsp char *path_got = NULL;
228 1451e70d 2018-03-10 stsp char *formatstr = NULL;
229 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
230 65596e15 2018-12-24 stsp char *basestr = NULL;
231 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
233 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
234 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
238 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
240 65596e15 2018-12-24 stsp return err;
241 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
243 65596e15 2018-12-24 stsp return err;
244 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
245 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
247 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
248 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
249 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
252 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
253 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
254 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
258 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
259 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
260 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
263 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
264 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
268 056e7441 2018-03-11 stsp /* Create an empty lock file. */
269 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
273 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
274 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
278 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
279 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
283 65596e15 2018-12-24 stsp /* Record our base commit. */
284 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
287 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
291 1451e70d 2018-03-10 stsp /* Store path to repository. */
292 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
293 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
297 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
298 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
299 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
303 ec22038e 2019-03-10 stsp /* Generate UUID. */
304 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
305 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
306 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
309 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
310 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
311 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
314 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
318 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
319 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
320 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
323 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
328 65596e15 2018-12-24 stsp free(commit_id);
329 7ac97322 2018-03-11 stsp free(path_got);
330 1451e70d 2018-03-10 stsp free(formatstr);
331 0bb8a95e 2018-03-12 stsp free(absprefix);
332 65596e15 2018-12-24 stsp free(basestr);
333 ec22038e 2019-03-10 stsp free(uuidstr);
334 86c3caaf 2018-03-09 stsp return err;
337 247140b2 2019-02-05 stsp static const struct got_error *
338 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
340 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
341 7ac97322 2018-03-11 stsp char *path_got;
342 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
343 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
344 056e7441 2018-03-11 stsp char *path_lock = NULL;
345 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
346 6d9d28c3 2018-03-11 stsp int version, fd = -1;
347 6d9d28c3 2018-03-11 stsp const char *errstr;
348 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
349 c442a90d 2019-03-10 stsp uint32_t uuid_status;
351 6d9d28c3 2018-03-11 stsp *worktree = NULL;
353 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
354 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
355 7ac97322 2018-03-11 stsp path_got = NULL;
359 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
360 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
361 056e7441 2018-03-11 stsp path_lock = NULL;
365 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
366 6d9d28c3 2018-03-11 stsp if (fd == -1) {
367 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
368 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
372 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
376 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
377 6d9d28c3 2018-03-11 stsp if (errstr) {
378 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
381 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
382 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
386 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
387 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
388 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
391 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
393 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
394 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
395 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
398 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
399 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
403 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
404 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
408 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
409 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
413 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
416 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
417 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
418 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
422 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
426 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
427 eaccb85f 2018-12-25 stsp base_commit_id_str);
431 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
432 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
435 eaccb85f 2018-12-25 stsp got_repo_close(repo);
436 7ac97322 2018-03-11 stsp free(path_got);
437 056e7441 2018-03-11 stsp free(path_lock);
438 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
439 c442a90d 2019-03-10 stsp free(uuidstr);
440 bd165944 2019-03-10 stsp free(formatstr);
442 6d9d28c3 2018-03-11 stsp if (fd != -1)
444 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
445 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
446 6d9d28c3 2018-03-11 stsp *worktree = NULL;
448 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
450 6d9d28c3 2018-03-11 stsp return err;
453 247140b2 2019-02-05 stsp const struct got_error *
454 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
456 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
459 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
460 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
461 247140b2 2019-02-05 stsp return err;
462 247140b2 2019-02-05 stsp if (*worktree)
463 247140b2 2019-02-05 stsp return NULL;
464 247140b2 2019-02-05 stsp path = dirname(path);
465 d1542a27 2019-02-05 stsp if (path == NULL)
466 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
467 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
469 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
472 3a6ce05a 2019-02-11 stsp const struct got_error *
473 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
475 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
476 c88eb298 2018-03-11 stsp free(worktree->root_path);
477 cde76477 2018-03-11 stsp free(worktree->repo_path);
478 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
479 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
480 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
481 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
482 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
483 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
484 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
485 6d9d28c3 2018-03-11 stsp free(worktree);
486 3a6ce05a 2019-02-11 stsp return err;
489 2fbdb5ae 2018-12-29 stsp const char *
490 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
492 c7f4312f 2019-02-05 stsp return worktree->root_path;
495 c7f4312f 2019-02-05 stsp const char *
496 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
498 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
501 49520a32 2018-12-29 stsp const char *
502 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
504 f609be2e 2018-12-29 stsp return worktree->path_prefix;
507 e5dc7198 2018-12-29 stsp const struct got_error *
508 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 e5dc7198 2018-12-29 stsp const char *path_prefix)
511 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
513 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
514 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
517 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
518 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
519 e5dc7198 2018-12-29 stsp free(absprefix);
520 e5dc7198 2018-12-29 stsp return NULL;
523 bc70eb79 2019-05-09 stsp const char *
524 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 36a38700 2019-05-10 stsp return worktree->head_ref_name;
529 024e9686 2019-05-14 stsp const struct got_error *
530 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
531 024e9686 2019-05-14 stsp struct got_reference *head_ref)
533 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
534 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
536 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
538 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
539 024e9686 2019-05-14 stsp path_got = NULL;
543 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
544 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
545 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
549 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
553 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
554 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
556 024e9686 2019-05-14 stsp free(path_got);
558 024e9686 2019-05-14 stsp free(head_ref_name);
559 024e9686 2019-05-14 stsp return err;
562 b72f483a 2019-02-05 stsp struct got_object_id *
563 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
568 507dc3bb 2018-12-29 stsp const struct got_error *
569 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
572 507dc3bb 2018-12-29 stsp const struct got_error *err;
573 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
574 507dc3bb 2018-12-29 stsp char *id_str = NULL;
575 507dc3bb 2018-12-29 stsp char *path_got = NULL;
577 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
580 507dc3bb 2018-12-29 stsp path_got = NULL;
584 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
586 507dc3bb 2018-12-29 stsp return err;
588 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
593 507dc3bb 2018-12-29 stsp /* Record our base commit. */
594 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
597 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
601 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
602 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
603 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
609 507dc3bb 2018-12-29 stsp got_object_close(obj);
610 507dc3bb 2018-12-29 stsp free(id_str);
611 507dc3bb 2018-12-29 stsp free(path_got);
612 507dc3bb 2018-12-29 stsp return err;
615 9d31a1d8 2018-03-11 stsp static const struct got_error *
616 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
618 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
619 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
620 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
621 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
622 86c3caaf 2018-03-09 stsp return NULL;
625 21908da4 2019-01-13 stsp static const struct got_error *
626 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
628 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
629 4a1ddfc2 2019-01-12 stsp char *abspath;
631 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
632 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
634 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
635 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
636 ddcd8544 2019-03-11 stsp struct stat sb;
637 ddcd8544 2019-03-11 stsp err = NULL;
638 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
640 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
641 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
642 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
645 4a1ddfc2 2019-01-12 stsp free(abspath);
646 68c76935 2019-02-19 stsp return err;
649 68c76935 2019-02-19 stsp static const struct got_error *
650 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
652 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
653 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
654 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
655 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
660 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
661 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
662 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
665 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
666 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
667 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
670 68c76935 2019-02-19 stsp if (flen1 == 0) {
671 68c76935 2019-02-19 stsp if (flen2 != 0)
674 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
675 68c76935 2019-02-19 stsp if (flen1 != 0)
678 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
679 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
689 68c76935 2019-02-19 stsp return err;
692 68c76935 2019-02-19 stsp static const struct got_error *
693 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
695 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
696 68c76935 2019-02-19 stsp struct stat sb;
697 68c76935 2019-02-19 stsp size_t size1, size2;
698 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
702 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
703 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
706 68c76935 2019-02-19 stsp size1 = sb.st_size;
708 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
712 68c76935 2019-02-19 stsp size2 = sb.st_size;
714 68c76935 2019-02-19 stsp if (size1 != size2) {
716 68c76935 2019-02-19 stsp return NULL;
719 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
720 68c76935 2019-02-19 stsp if (f1 == NULL)
721 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
723 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
724 68c76935 2019-02-19 stsp if (f2 == NULL) {
725 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
729 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
731 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
732 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
733 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
734 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
736 6353ad76 2019-02-08 stsp return err;
740 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
741 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
742 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
744 6353ad76 2019-02-08 stsp static const struct got_error *
745 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
746 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
747 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
748 b8f41171 2019-02-10 stsp struct got_repository *repo,
749 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
751 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
752 6353ad76 2019-02-08 stsp int merged_fd = -1;
753 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
754 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
755 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
756 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
757 6353ad76 2019-02-08 stsp char *id_str = NULL;
758 6353ad76 2019-02-08 stsp char *label1 = NULL;
759 68c76935 2019-02-19 stsp int overlapcnt = 0, update_timestamps = 0;
760 af54ae4a 2019-02-19 stsp char *parent;
762 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
763 af54ae4a 2019-02-19 stsp if (parent == NULL)
764 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
766 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
767 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
769 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
773 af54ae4a 2019-02-19 stsp free(base_path);
774 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
775 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
776 af54ae4a 2019-02-19 stsp base_path = NULL;
780 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob1_path, &f1, base_path);
783 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
787 af54ae4a 2019-02-19 stsp free(base_path);
788 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
789 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
790 af54ae4a 2019-02-19 stsp base_path = NULL;
794 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob2_path, &f2, base_path);
797 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie)) {
798 1430b4e0 2019-03-27 stsp struct got_object_id id2;
799 1430b4e0 2019-03-27 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
800 1430b4e0 2019-03-27 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
803 1430b4e0 2019-03-27 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
808 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
809 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
810 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
814 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
817 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
818 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
822 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
823 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
827 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
828 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
830 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
831 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
835 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
836 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
837 68c76935 2019-02-19 stsp err = check_files_equal(&update_timestamps, blob1_path,
838 68c76935 2019-02-19 stsp merged_path);
843 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
844 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
848 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
849 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
850 230a42bd 2019-05-11 jcs ondisk_path);
851 2a57020b 2019-02-20 stsp unlink(merged_path);
856 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
857 65ad4e61 2019-02-20 stsp * a future status walk would treat them as unmodified files again.
859 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
860 68c76935 2019-02-19 stsp blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
862 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
863 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
864 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
865 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
866 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
867 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
869 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
870 6353ad76 2019-02-08 stsp free(merged_path);
871 af54ae4a 2019-02-19 stsp free(base_path);
872 6353ad76 2019-02-08 stsp if (blob1_path) {
873 6353ad76 2019-02-08 stsp unlink(blob1_path);
874 6353ad76 2019-02-08 stsp free(blob1_path);
876 6353ad76 2019-02-08 stsp if (blob2_path) {
877 6353ad76 2019-02-08 stsp unlink(blob2_path);
878 6353ad76 2019-02-08 stsp free(blob2_path);
880 6353ad76 2019-02-08 stsp free(id_str);
881 6353ad76 2019-02-08 stsp free(label1);
882 4a1ddfc2 2019-01-12 stsp return err;
885 4a1ddfc2 2019-01-12 stsp static const struct got_error *
886 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
887 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
888 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
889 13d9040b 2019-03-27 stsp int update_timestamps)
891 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
893 13d9040b 2019-03-27 stsp if (ie == NULL)
894 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
896 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
897 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
898 13d9040b 2019-03-27 stsp update_timestamps);
900 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
901 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
902 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
904 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
906 13d9040b 2019-03-27 stsp return err;
909 13d9040b 2019-03-27 stsp static const struct got_error *
910 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
911 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
912 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
913 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
914 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
916 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
917 507dc3bb 2018-12-29 stsp int fd = -1;
918 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
919 507dc3bb 2018-12-29 stsp int update = 0;
920 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
922 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
923 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
924 9d31a1d8 2018-03-11 stsp if (fd == -1) {
925 21908da4 2019-01-13 stsp if (errno == ENOENT) {
926 21908da4 2019-01-13 stsp char *parent = dirname(path);
927 21908da4 2019-01-13 stsp if (parent == NULL)
928 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
929 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
931 21908da4 2019-01-13 stsp return err;
932 21908da4 2019-01-13 stsp fd = open(ondisk_path,
933 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
934 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
935 21908da4 2019-01-13 stsp if (fd == -1)
936 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
937 230a42bd 2019-05-11 jcs ondisk_path);
938 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
939 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
940 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
941 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
944 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
945 507dc3bb 2018-12-29 stsp ondisk_path);
948 507dc3bb 2018-12-29 stsp update = 1;
951 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
954 a378724f 2019-02-10 stsp if (restoring_missing_file)
955 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
956 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
957 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
959 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
960 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
962 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
964 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
965 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
968 9d31a1d8 2018-03-11 stsp if (len > 0) {
969 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
970 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
971 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
972 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
974 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
975 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
978 61d6eaa3 2018-12-24 stsp hdrlen = 0;
980 9d31a1d8 2018-03-11 stsp } while (len != 0);
982 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
983 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
987 507dc3bb 2018-12-29 stsp if (update) {
988 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
989 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
990 230a42bd 2019-05-11 jcs ondisk_path);
991 2a57020b 2019-02-20 stsp unlink(tmppath);
996 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
997 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
998 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1002 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1003 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1004 68ed9ba5 2019-02-10 stsp goto done;
1009 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1010 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1011 507dc3bb 2018-12-29 stsp free(tmppath);
1012 6353ad76 2019-02-08 stsp return err;
1015 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1016 6353ad76 2019-02-08 stsp static const struct got_error *
1017 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1019 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1020 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1021 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1022 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1023 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1025 7154f6ce 2019-03-27 stsp int i = 0;
1026 7154f6ce 2019-03-27 stsp char *line;
1027 7154f6ce 2019-03-27 stsp size_t len;
1028 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1030 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1031 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1032 7154f6ce 2019-03-27 stsp if (line == NULL) {
1033 7154f6ce 2019-03-27 stsp if (feof(f))
1035 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1039 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1040 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1042 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1048 7154f6ce 2019-03-27 stsp return err;
1051 7154f6ce 2019-03-27 stsp static const struct got_error *
1052 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1053 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1054 b8f41171 2019-02-10 stsp struct got_repository *repo)
1056 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1057 6353ad76 2019-02-08 stsp struct got_object_id id;
1058 6353ad76 2019-02-08 stsp size_t hdrlen;
1059 6353ad76 2019-02-08 stsp FILE *f = NULL;
1060 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1061 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1062 6353ad76 2019-02-08 stsp size_t flen, blen;
1064 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1066 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1067 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1069 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1070 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1072 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1073 b8f41171 2019-02-10 stsp sb->st_mode =
1074 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1075 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1077 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1078 a378724f 2019-02-10 stsp return NULL;
1080 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1083 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1084 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1085 6353ad76 2019-02-08 stsp return NULL;
1088 b8f41171 2019-02-10 stsp if (ie == NULL)
1089 d00136be 2019-03-26 stsp return NULL;
1091 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1092 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1093 2ec1f75b 2019-03-26 stsp return NULL;
1094 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1095 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1096 b8f41171 2019-02-10 stsp return NULL;
1099 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1100 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1101 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1102 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1103 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1104 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1105 6353ad76 2019-02-08 stsp return NULL;
1107 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1108 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1110 6353ad76 2019-02-08 stsp return err;
1112 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1113 6353ad76 2019-02-08 stsp if (f == NULL) {
1114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1115 6353ad76 2019-02-08 stsp goto done;
1117 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1119 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1120 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1122 7154f6ce 2019-03-27 stsp goto done;
1123 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1124 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1125 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1126 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1127 7154f6ce 2019-03-27 stsp goto done;
1129 6353ad76 2019-02-08 stsp if (blen == 0) {
1130 6353ad76 2019-02-08 stsp if (flen != 0)
1131 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1133 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1134 6353ad76 2019-02-08 stsp if (blen != 0)
1135 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1137 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1138 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1139 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1140 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1144 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1147 6353ad76 2019-02-08 stsp hdrlen = 0;
1150 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1151 7154f6ce 2019-03-27 stsp rewind(f);
1152 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1156 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1158 6353ad76 2019-02-08 stsp fclose(f);
1159 9d31a1d8 2018-03-11 stsp return err;
1162 9d31a1d8 2018-03-11 stsp static const struct got_error *
1163 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1164 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1165 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1166 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1167 0584f854 2019-04-06 stsp void *progress_arg)
1169 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1170 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1171 6353ad76 2019-02-08 stsp char *ondisk_path;
1172 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1173 b8f41171 2019-02-10 stsp struct stat sb;
1175 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1176 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1178 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1180 b8f41171 2019-02-10 stsp goto done;
1182 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1183 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1184 b8f41171 2019-02-10 stsp goto done;
1187 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1188 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1189 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1190 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1191 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1193 6353ad76 2019-02-08 stsp goto done;
1195 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1196 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1197 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1198 b8f41171 2019-02-10 stsp goto done;
1201 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1203 6353ad76 2019-02-08 stsp goto done;
1205 1430b4e0 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1206 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1207 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1208 b8f41171 2019-02-10 stsp progress_arg);
1209 13d9040b 2019-03-27 stsp else if (status == GOT_STATUS_DELETE) {
1210 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1211 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1212 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1214 13d9040b 2019-03-27 stsp goto done;
1216 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1217 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1218 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1220 13d9040b 2019-03-27 stsp goto done;
1221 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1222 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1224 13d9040b 2019-03-27 stsp goto done;
1226 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1228 6353ad76 2019-02-08 stsp free(ondisk_path);
1229 8da9e5f4 2019-01-12 stsp return err;
1232 90285c3b 2019-01-08 stsp static const struct got_error *
1233 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1235 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1236 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1238 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1239 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1241 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1242 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1243 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1245 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1246 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1247 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1248 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1249 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1253 90285c3b 2019-01-08 stsp parent = dirname(parent);
1256 90285c3b 2019-01-08 stsp free(ondisk_path);
1257 90285c3b 2019-01-08 stsp return err;
1260 708d8e67 2019-03-27 stsp static const struct got_error *
1261 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1262 708d8e67 2019-03-27 stsp struct got_fileindex_entry *ie, const char *parent_path,
1263 708d8e67 2019-03-27 stsp struct got_repository *repo,
1264 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1267 708d8e67 2019-03-27 stsp unsigned char status;
1268 708d8e67 2019-03-27 stsp struct stat sb;
1269 708d8e67 2019-03-27 stsp char *ondisk_path;
1271 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1273 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1275 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1277 708d8e67 2019-03-27 stsp return err;
1279 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1280 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1281 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1283 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1284 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1286 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1289 708d8e67 2019-03-27 stsp return err;
1291 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1292 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1293 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1295 fc6346c4 2019-03-27 stsp return err;
1297 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1300 fc6346c4 2019-03-27 stsp return err;
1303 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1304 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1305 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1306 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1307 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1308 8da9e5f4 2019-01-12 stsp void *progress_arg;
1309 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1310 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1313 512f0d0e 2019-01-02 stsp static const struct got_error *
1314 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1315 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1317 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1319 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1322 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1323 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1326 8da9e5f4 2019-01-12 stsp static const struct got_error *
1327 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1329 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1331 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1332 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1334 708d8e67 2019-03-27 stsp return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1335 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1338 9d31a1d8 2018-03-11 stsp static const struct got_error *
1339 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1341 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1342 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1343 8da9e5f4 2019-01-12 stsp char *path;
1345 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1346 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1348 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1349 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1351 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1353 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1354 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1356 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1357 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1359 8da9e5f4 2019-01-12 stsp free(path);
1360 0cd1c46a 2019-03-11 stsp return err;
1363 517bab73 2019-03-11 stsp const struct got_error *
1364 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1366 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1367 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1368 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1370 517bab73 2019-03-11 stsp *refname = NULL;
1372 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1373 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1374 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1376 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1379 0647c563 2019-03-11 stsp *refname = NULL;
1381 517bab73 2019-03-11 stsp free(uuidstr);
1382 517bab73 2019-03-11 stsp return err;
1386 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1387 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1389 517bab73 2019-03-11 stsp static const struct got_error *
1390 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1392 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1393 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1394 517bab73 2019-03-11 stsp char *refname;
1396 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1398 517bab73 2019-03-11 stsp return err;
1400 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1402 0cd1c46a 2019-03-11 stsp goto done;
1404 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1406 0cd1c46a 2019-03-11 stsp free(refname);
1408 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1409 9d31a1d8 2018-03-11 stsp return err;
1412 ebf99748 2019-05-09 stsp static const struct got_error *
1413 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1414 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1416 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1417 ebf99748 2019-05-09 stsp FILE *index = NULL;
1419 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1420 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1421 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1422 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1424 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1425 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1426 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1427 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1428 ebf99748 2019-05-09 stsp goto done;
1431 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1432 ebf99748 2019-05-09 stsp if (index == NULL) {
1433 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1434 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1436 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1437 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1438 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1441 ebf99748 2019-05-09 stsp if (err) {
1442 ebf99748 2019-05-09 stsp free(*fileindex_path);
1443 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1444 ebf99748 2019-05-09 stsp free(*fileindex);
1445 ebf99748 2019-05-09 stsp *fileindex = NULL;
1447 ebf99748 2019-05-09 stsp return err;
1450 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1451 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1452 c932eeeb 2019-05-22 stsp const char *path;
1453 c932eeeb 2019-05-22 stsp size_t path_len;
1454 c932eeeb 2019-05-22 stsp const char *entry_name;
1457 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1458 c932eeeb 2019-05-22 stsp static const struct got_error *
1459 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1461 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1463 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1464 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1465 c932eeeb 2019-05-22 stsp return NULL;
1466 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1467 c932eeeb 2019-05-22 stsp return NULL;
1469 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1470 c932eeeb 2019-05-22 stsp return NULL;
1473 9d31a1d8 2018-03-11 stsp const struct got_error *
1474 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1475 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1476 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1478 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1479 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1480 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1481 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1482 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1483 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1484 ebf99748 2019-05-09 stsp FILE *new_index = NULL;
1485 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1486 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1487 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1489 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1491 9d31a1d8 2018-03-11 stsp return err;
1494 51514078 2018-12-25 stsp * Read the file index.
1495 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1496 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1498 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1500 ebf99748 2019-05-09 stsp goto done;
1502 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1503 b8bdcc21 2018-12-24 stsp fileindex_path);
1505 51664889 2018-03-12 stsp goto done;
1507 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1509 0cd1c46a 2019-03-11 stsp goto done;
1511 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1512 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1514 9d31a1d8 2018-03-11 stsp goto done;
1516 c4cdcb68 2019-04-03 stsp if (path[0]) {
1517 c4cdcb68 2019-04-03 stsp char *tree_path;
1518 c4cdcb68 2019-04-03 stsp int obj_type;
1519 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1520 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1521 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1522 c4cdcb68 2019-04-03 stsp goto done;
1524 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1525 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1526 c4cdcb68 2019-04-03 stsp path) == -1) {
1527 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1528 c4cdcb68 2019-04-03 stsp goto done;
1530 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1531 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1532 c4cdcb68 2019-04-03 stsp free(tree_path);
1534 c4cdcb68 2019-04-03 stsp goto done;
1535 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1537 c4cdcb68 2019-04-03 stsp goto done;
1538 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1539 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1540 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1541 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1542 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1543 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1544 c4cdcb68 2019-04-03 stsp goto done;
1546 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1547 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1548 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1549 c4cdcb68 2019-04-03 stsp goto done;
1552 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1554 c4cdcb68 2019-04-03 stsp goto done;
1555 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1556 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1557 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1558 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1559 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1561 c4cdcb68 2019-04-03 stsp goto done;
1564 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1565 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1566 c4cdcb68 2019-04-03 stsp free(tree_path);
1568 c4cdcb68 2019-04-03 stsp goto done;
1569 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1570 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1571 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1572 c4cdcb68 2019-04-03 stsp goto done;
1576 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1577 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1578 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1579 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, worktree->path_prefix);
1584 c4cdcb68 2019-04-03 stsp goto done;
1587 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1589 c4cdcb68 2019-04-03 stsp goto done;
1591 c4cdcb68 2019-04-03 stsp if (entry_name &&
1592 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1593 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1594 8da9e5f4 2019-01-12 stsp goto done;
1597 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1598 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1599 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1600 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1601 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1602 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1603 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1604 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1605 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1606 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1607 c4cdcb68 2019-04-03 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1608 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1610 c932eeeb 2019-05-22 stsp if (checkout_err == NULL) {
1611 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg bbc_arg;
1612 c932eeeb 2019-05-22 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1613 c932eeeb 2019-05-22 stsp bbc_arg.entry_name = entry_name;
1614 c932eeeb 2019-05-22 stsp bbc_arg.path = path;
1615 c932eeeb 2019-05-22 stsp bbc_arg.path_len = strlen(path);
1616 c932eeeb 2019-05-22 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1617 c932eeeb 2019-05-22 stsp bump_base_commit_id, &bbc_arg);
1619 c932eeeb 2019-05-22 stsp goto done;
1622 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1623 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1625 9d31a1d8 2018-03-11 stsp goto done;
1627 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1628 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1629 230a42bd 2019-05-11 jcs fileindex_path);
1630 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1631 9d31a1d8 2018-03-11 stsp goto done;
1634 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1635 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1638 c4cdcb68 2019-04-03 stsp free(relpath);
1640 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1641 9d31a1d8 2018-03-11 stsp if (commit)
1642 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1643 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1644 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1645 b8bdcc21 2018-12-24 stsp if (new_index)
1646 b8bdcc21 2018-12-24 stsp fclose(new_index);
1647 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1648 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1649 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1650 a143fb78 2018-12-25 stsp if (checkout_err)
1651 a143fb78 2018-12-25 stsp err = checkout_err;
1652 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1653 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1654 9d31a1d8 2018-03-11 stsp err = unlockerr;
1655 f8d1f275 2019-02-04 stsp return err;
1658 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1659 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1660 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1661 927df6b7 2019-02-10 stsp const char *status_path;
1662 927df6b7 2019-02-10 stsp size_t status_path_len;
1663 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1664 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1665 f8d1f275 2019-02-04 stsp void *status_arg;
1666 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1667 f8d1f275 2019-02-04 stsp void *cancel_arg;
1670 f8d1f275 2019-02-04 stsp static const struct got_error *
1671 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1672 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1673 927df6b7 2019-02-10 stsp struct got_repository *repo)
1675 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1676 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1677 927df6b7 2019-02-10 stsp struct stat sb;
1678 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1680 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1681 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1682 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1683 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1684 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1685 016a88dd 2019-05-13 stsp &commit_id);
1687 927df6b7 2019-02-10 stsp return err;
1690 927df6b7 2019-02-10 stsp static const struct got_error *
1691 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1692 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1694 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1695 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1696 f8d1f275 2019-02-04 stsp char *abspath;
1698 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1699 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1701 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1702 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1703 927df6b7 2019-02-10 stsp return NULL;
1705 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1706 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1707 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1708 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1710 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1711 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1712 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1715 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1717 f8d1f275 2019-02-04 stsp free(abspath);
1718 9d31a1d8 2018-03-11 stsp return err;
1721 f8d1f275 2019-02-04 stsp static const struct got_error *
1722 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1724 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1725 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1726 2ec1f75b 2019-03-26 stsp unsigned char status;
1728 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1729 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1731 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1732 927df6b7 2019-02-10 stsp return NULL;
1734 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1735 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1736 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1737 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1739 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1740 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1741 016a88dd 2019-05-13 stsp &commit_id);
1744 f8d1f275 2019-02-04 stsp static const struct got_error *
1745 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1747 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1748 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1749 f8d1f275 2019-02-04 stsp char *path = NULL;
1751 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1752 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1754 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1755 2c201a36 2019-02-10 stsp return NULL;
1757 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1758 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1759 f8d1f275 2019-02-04 stsp return NULL;
1761 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1762 927df6b7 2019-02-10 stsp return NULL;
1764 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1765 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1766 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1768 f8d1f275 2019-02-04 stsp path = de->d_name;
1771 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1772 016a88dd 2019-05-13 stsp NULL, NULL);
1773 f8d1f275 2019-02-04 stsp if (parent_path[0])
1774 f8d1f275 2019-02-04 stsp free(path);
1775 b72f483a 2019-02-05 stsp return err;
1778 f8d1f275 2019-02-04 stsp const struct got_error *
1779 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1780 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1781 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1783 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1784 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1785 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1786 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1787 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1788 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1789 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1790 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1792 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1793 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1794 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
1795 f8d1f275 2019-02-04 stsp goto done;
1798 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1799 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1800 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1801 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1802 f8d1f275 2019-02-04 stsp goto done;
1805 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1806 f8d1f275 2019-02-04 stsp if (index == NULL) {
1807 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1808 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
1809 f8d1f275 2019-02-04 stsp goto done;
1812 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1813 f8d1f275 2019-02-04 stsp fclose(index);
1815 f8d1f275 2019-02-04 stsp goto done;
1818 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1819 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1820 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1821 c513d110 2019-02-05 stsp goto done;
1823 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1824 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1825 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
1826 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1827 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1828 927df6b7 2019-02-10 stsp if (ie == NULL) {
1829 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1830 927df6b7 2019-02-10 stsp goto done;
1832 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1833 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1834 927df6b7 2019-02-10 stsp goto done;
1836 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
1837 927df6b7 2019-02-10 stsp goto done;
1840 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1841 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1842 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1843 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1844 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1845 927df6b7 2019-02-10 stsp arg.status_path = path;
1846 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1847 f8d1f275 2019-02-04 stsp arg.repo = repo;
1848 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1849 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1850 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1851 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1852 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1853 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1855 f8d1f275 2019-02-04 stsp if (workdir)
1856 f8d1f275 2019-02-04 stsp closedir(workdir);
1857 927df6b7 2019-02-10 stsp free(ondisk_path);
1858 f8d1f275 2019-02-04 stsp free(fileindex_path);
1859 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1860 f8d1f275 2019-02-04 stsp return err;
1863 6c7ab921 2019-03-18 stsp const struct got_error *
1864 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1865 6c7ab921 2019-03-18 stsp const char *arg)
1867 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
1868 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
1869 6c7ab921 2019-03-18 stsp size_t len;
1871 6c7ab921 2019-03-18 stsp *wt_path = NULL;
1873 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
1874 6c7ab921 2019-03-18 stsp if (resolved == NULL)
1875 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
1877 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1878 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
1879 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
1880 6c7ab921 2019-03-18 stsp goto done;
1883 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
1884 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
1885 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
1887 f6d88e1a 2019-05-29 stsp goto done;
1889 f6d88e1a 2019-05-29 stsp path = strdup("");
1890 f6d88e1a 2019-05-29 stsp if (path == NULL) {
1891 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
1892 f6d88e1a 2019-05-29 stsp goto done;
1896 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
1897 6c7ab921 2019-03-18 stsp len = strlen(path);
1898 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
1899 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
1903 6c7ab921 2019-03-18 stsp free(resolved);
1904 6c7ab921 2019-03-18 stsp if (err == NULL)
1905 6c7ab921 2019-03-18 stsp *wt_path = path;
1907 6c7ab921 2019-03-18 stsp free(path);
1908 d00136be 2019-03-26 stsp return err;
1911 d00136be 2019-03-26 stsp const struct got_error *
1912 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
1913 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
1914 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
1915 031a5338 2019-03-26 stsp struct got_repository *repo)
1917 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1918 1dd54920 2019-05-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1919 d00136be 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1920 d00136be 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1921 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
1923 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1925 d00136be 2019-03-26 stsp return err;
1928 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
1929 d00136be 2019-03-26 stsp if (fileindex == NULL) {
1930 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
1931 d00136be 2019-03-26 stsp goto done;
1934 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1935 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1936 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1937 d00136be 2019-03-26 stsp fileindex_path = NULL;
1938 d00136be 2019-03-26 stsp goto done;
1941 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1942 d00136be 2019-03-26 stsp if (index == NULL) {
1943 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
1944 d00136be 2019-03-26 stsp goto done;
1947 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1949 d00136be 2019-03-26 stsp goto done;
1951 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
1952 1dd54920 2019-05-11 stsp struct got_fileindex_entry *ie = NULL;
1953 1dd54920 2019-05-11 stsp char *relpath;
1955 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
1956 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
1958 1dd54920 2019-05-11 stsp goto done;
1960 1dd54920 2019-05-11 stsp /* Re-adding an existing entry is a no-op. */
1961 1dd54920 2019-05-11 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1964 1dd54920 2019-05-11 stsp err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1965 1dd54920 2019-05-11 stsp NULL, NULL);
1966 1dd54920 2019-05-11 stsp free(relpath);
1968 1dd54920 2019-05-11 stsp goto done;
1970 1dd54920 2019-05-11 stsp err = got_fileindex_entry_add(fileindex, ie);
1971 1dd54920 2019-05-11 stsp if (err) {
1972 1dd54920 2019-05-11 stsp got_fileindex_entry_free(ie);
1973 1dd54920 2019-05-11 stsp goto done;
1976 1dd54920 2019-05-11 stsp err = report_file_status(ie, pe->path, status_cb, status_arg,
1979 1dd54920 2019-05-11 stsp goto done;
1982 d00136be 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1983 d00136be 2019-03-26 stsp fileindex_path);
1985 d00136be 2019-03-26 stsp goto done;
1987 d00136be 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1989 d00136be 2019-03-26 stsp goto done;
1991 d00136be 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1992 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1993 230a42bd 2019-05-11 jcs fileindex_path);
1994 d00136be 2019-03-26 stsp goto done;
1997 d00136be 2019-03-26 stsp free(new_fileindex_path);
1998 d00136be 2019-03-26 stsp new_fileindex_path = NULL;
2001 d00136be 2019-03-26 stsp if (index) {
2002 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2003 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2005 d00136be 2019-03-26 stsp if (new_fileindex_path) {
2006 d00136be 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2007 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink",
2008 230a42bd 2019-05-11 jcs new_fileindex_path);
2009 d00136be 2019-03-26 stsp free(new_fileindex_path);
2011 d00136be 2019-03-26 stsp if (fileindex)
2012 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2013 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2014 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2015 d00136be 2019-03-26 stsp err = unlockerr;
2016 6c7ab921 2019-03-18 stsp return err;
2019 2ec1f75b 2019-03-26 stsp const struct got_error *
2020 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2021 2ec1f75b 2019-03-26 stsp const char *ondisk_path, int delete_local_mods,
2022 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2023 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2025 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2026 2ec1f75b 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
2027 2ec1f75b 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2028 2ec1f75b 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
2029 2ec1f75b 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2030 2ec1f75b 2019-03-26 stsp unsigned char status;
2031 2ec1f75b 2019-03-26 stsp struct stat sb;
2033 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2035 2ec1f75b 2019-03-26 stsp return err;
2037 2ec1f75b 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
2038 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
2040 2ec1f75b 2019-03-26 stsp goto done;
2042 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2043 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2044 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2045 2ec1f75b 2019-03-26 stsp goto done;
2048 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2049 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2050 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2051 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2052 2ec1f75b 2019-03-26 stsp goto done;
2055 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2056 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2057 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2058 2ec1f75b 2019-03-26 stsp goto done;
2061 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2063 2ec1f75b 2019-03-26 stsp goto done;
2065 2ec1f75b 2019-03-26 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2066 2ec1f75b 2019-03-26 stsp if (ie == NULL) {
2067 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_BAD_PATH);
2068 2ec1f75b 2019-03-26 stsp goto done;
2071 2ec1f75b 2019-03-26 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2073 2ec1f75b 2019-03-26 stsp goto done;
2075 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_NO_CHANGE) {
2076 71a29355 2019-03-27 stsp if (status == GOT_STATUS_DELETE) {
2077 2af4a041 2019-05-11 jcs err = got_error_set_errno(ENOENT, ondisk_path);
2078 71a29355 2019-03-27 stsp goto done;
2080 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY) {
2081 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_STATUS);
2082 2ec1f75b 2019-03-26 stsp goto done;
2084 2ec1f75b 2019-03-26 stsp if (!delete_local_mods) {
2085 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_MODIFIED);
2086 2ec1f75b 2019-03-26 stsp goto done;
2090 2ec1f75b 2019-03-26 stsp if (unlink(ondisk_path) != 0) {
2091 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2092 2ec1f75b 2019-03-26 stsp goto done;
2095 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2097 2ec1f75b 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2098 2ec1f75b 2019-03-26 stsp fileindex_path);
2100 2ec1f75b 2019-03-26 stsp goto done;
2102 2ec1f75b 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
2104 2ec1f75b 2019-03-26 stsp goto done;
2106 2ec1f75b 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2108 230a42bd 2019-05-11 jcs fileindex_path);
2109 2ec1f75b 2019-03-26 stsp goto done;
2112 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
2113 2ec1f75b 2019-03-26 stsp new_fileindex_path = NULL;
2115 2ec1f75b 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2117 2ec1f75b 2019-03-26 stsp free(relpath);
2118 2ec1f75b 2019-03-26 stsp if (index) {
2119 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2120 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2122 2ec1f75b 2019-03-26 stsp if (new_fileindex_path) {
2123 2ec1f75b 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2124 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink",
2125 230a42bd 2019-05-11 jcs new_fileindex_path);
2126 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
2128 2ec1f75b 2019-03-26 stsp if (fileindex)
2129 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2130 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2131 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2132 2ec1f75b 2019-03-26 stsp err = unlockerr;
2133 2ec1f75b 2019-03-26 stsp return err;
2136 a129376b 2019-03-28 stsp const struct got_error *
2137 a129376b 2019-03-28 stsp got_worktree_revert(struct got_worktree *worktree,
2138 a129376b 2019-03-28 stsp const char *ondisk_path,
2139 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2140 a129376b 2019-03-28 stsp struct got_repository *repo)
2142 a129376b 2019-03-28 stsp struct got_fileindex *fileindex = NULL;
2143 a129376b 2019-03-28 stsp struct got_fileindex_entry *ie = NULL;
2144 a129376b 2019-03-28 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2145 a129376b 2019-03-28 stsp char *tree_path = NULL, *parent_path, *te_name;
2146 a129376b 2019-03-28 stsp FILE *index = NULL, *new_index = NULL;
2147 a129376b 2019-03-28 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2148 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2149 a129376b 2019-03-28 stsp struct got_object_id id, *tree_id = NULL;
2150 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2151 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2152 a129376b 2019-03-28 stsp unsigned char status;
2153 a129376b 2019-03-28 stsp struct stat sb;
2155 a129376b 2019-03-28 stsp err = lock_worktree(worktree, LOCK_EX);
2157 a129376b 2019-03-28 stsp return err;
2159 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2160 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2162 a129376b 2019-03-28 stsp goto done;
2164 a129376b 2019-03-28 stsp fileindex = got_fileindex_alloc();
2165 a129376b 2019-03-28 stsp if (fileindex == NULL) {
2166 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2167 a129376b 2019-03-28 stsp goto done;
2170 a129376b 2019-03-28 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2171 a129376b 2019-03-28 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2172 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2173 a129376b 2019-03-28 stsp fileindex_path = NULL;
2174 a129376b 2019-03-28 stsp goto done;
2177 a129376b 2019-03-28 stsp index = fopen(fileindex_path, "rb");
2178 a129376b 2019-03-28 stsp if (index == NULL) {
2179 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2180 a129376b 2019-03-28 stsp goto done;
2183 a129376b 2019-03-28 stsp err = got_fileindex_read(fileindex, index);
2185 a129376b 2019-03-28 stsp goto done;
2187 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2188 a129376b 2019-03-28 stsp if (ie == NULL) {
2189 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2190 a129376b 2019-03-28 stsp goto done;
2193 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2194 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2195 a129376b 2019-03-28 stsp if (err) {
2196 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2197 a129376b 2019-03-28 stsp goto done;
2198 a129376b 2019-03-28 stsp parent_path = "/";
2200 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2201 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2202 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2203 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2204 a129376b 2019-03-28 stsp goto done;
2207 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2208 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2209 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2210 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2211 a129376b 2019-03-28 stsp goto done;
2214 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2215 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2216 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2217 a129376b 2019-03-28 stsp goto done;
2222 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2223 a129376b 2019-03-28 stsp tree_path);
2225 a129376b 2019-03-28 stsp goto done;
2227 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2229 a129376b 2019-03-28 stsp goto done;
2231 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2232 a129376b 2019-03-28 stsp if (te_name == NULL) {
2233 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2234 a129376b 2019-03-28 stsp goto done;
2237 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2239 a129376b 2019-03-28 stsp goto done;
2241 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2242 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2243 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2244 a129376b 2019-03-28 stsp goto done;
2247 a129376b 2019-03-28 stsp switch (status) {
2248 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2249 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2250 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2252 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2253 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2254 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2255 a129376b 2019-03-28 stsp case GOT_STATUS_MISSING:
2256 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2257 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2259 a129376b 2019-03-28 stsp goto done;
2260 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2261 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2262 a129376b 2019-03-28 stsp progress_arg);
2264 a129376b 2019-03-28 stsp goto done;
2265 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2266 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2267 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2269 a129376b 2019-03-28 stsp goto done;
2273 a129376b 2019-03-28 stsp goto done;
2276 a129376b 2019-03-28 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2277 a129376b 2019-03-28 stsp fileindex_path);
2279 a129376b 2019-03-28 stsp goto done;
2281 a129376b 2019-03-28 stsp err = got_fileindex_write(fileindex, new_index);
2283 a129376b 2019-03-28 stsp goto done;
2285 a129376b 2019-03-28 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2286 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2287 230a42bd 2019-05-11 jcs fileindex_path);
2288 a129376b 2019-03-28 stsp goto done;
2291 a129376b 2019-03-28 stsp free(new_fileindex_path);
2292 a129376b 2019-03-28 stsp new_fileindex_path = NULL;
2294 a129376b 2019-03-28 stsp free(relpath);
2295 a129376b 2019-03-28 stsp free(tree_path);
2297 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2299 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2300 a129376b 2019-03-28 stsp free(tree_id);
2301 a129376b 2019-03-28 stsp if (index) {
2302 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2303 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2305 a129376b 2019-03-28 stsp if (new_fileindex_path) {
2306 a129376b 2019-03-28 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2307 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink",
2308 230a42bd 2019-05-11 jcs new_fileindex_path);
2309 a129376b 2019-03-28 stsp free(new_fileindex_path);
2311 a129376b 2019-03-28 stsp if (fileindex)
2312 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2313 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2314 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2315 a129376b 2019-03-28 stsp err = unlockerr;
2316 c4296144 2019-05-09 stsp return err;
2319 cf066bf8 2019-05-09 stsp static void
2320 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2322 24519714 2019-05-09 stsp free(ct->path);
2323 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2324 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2325 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2326 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2327 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2331 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2332 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2333 24519714 2019-05-09 stsp struct got_repository *repo;
2334 24519714 2019-05-09 stsp struct got_worktree *worktree;
2337 c4296144 2019-05-09 stsp static const struct got_error *
2338 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2339 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2341 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2342 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2343 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2344 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2345 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2346 768aea60 2019-05-09 stsp struct stat sb;
2348 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2349 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2351 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2352 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2353 c4296144 2019-05-09 stsp return NULL;
2355 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2356 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2357 036813ee 2019-05-09 stsp goto done;
2359 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2360 036813ee 2019-05-09 stsp parent_path = strdup("");
2361 69960a46 2019-05-09 stsp if (parent_path == NULL)
2362 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2364 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2366 69960a46 2019-05-09 stsp return err;
2369 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2370 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2372 c4296144 2019-05-09 stsp goto done;
2375 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2376 768aea60 2019-05-09 stsp relpath) == -1) {
2377 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2378 768aea60 2019-05-09 stsp goto done;
2380 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2381 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2383 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2384 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2385 768aea60 2019-05-09 stsp goto done;
2387 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2390 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2391 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2392 44d03001 2019-05-09 stsp relpath) == -1) {
2393 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2394 44d03001 2019-05-09 stsp goto done;
2397 cf066bf8 2019-05-09 stsp ct->status = status;
2398 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2399 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2400 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2401 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2402 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2403 b416585c 2019-05-13 stsp goto done;
2405 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2406 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2407 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2408 036813ee 2019-05-09 stsp goto done;
2411 24519714 2019-05-09 stsp ct->path = strdup(path);
2412 24519714 2019-05-09 stsp if (ct->path == NULL) {
2413 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2414 24519714 2019-05-09 stsp goto done;
2416 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2418 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2419 ed175427 2019-05-09 stsp free_commitable(ct);
2420 24519714 2019-05-09 stsp free(parent_path);
2421 036813ee 2019-05-09 stsp free(path);
2422 a129376b 2019-03-28 stsp return err;
2425 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2426 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2427 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2428 036813ee 2019-05-09 stsp struct got_repository *);
2430 ed175427 2019-05-09 stsp static const struct got_error *
2431 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2432 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2433 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2434 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2435 afa376bf 2019-05-09 stsp struct got_repository *repo)
2437 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2438 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2439 036813ee 2019-05-09 stsp char *subpath;
2441 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2442 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2443 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2445 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2447 ed175427 2019-05-09 stsp return err;
2449 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2450 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2451 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2452 036813ee 2019-05-09 stsp free(subpath);
2453 036813ee 2019-05-09 stsp return err;
2456 036813ee 2019-05-09 stsp static const struct got_error *
2457 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2459 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2460 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2462 036813ee 2019-05-09 stsp *match = 0;
2464 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2465 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2466 0f63689d 2019-05-10 stsp return NULL;
2469 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2471 0f63689d 2019-05-10 stsp return err;
2472 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2473 036813ee 2019-05-09 stsp free(ct_parent_path);
2474 036813ee 2019-05-09 stsp return err;
2477 768aea60 2019-05-09 stsp static mode_t
2478 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2480 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2483 036813ee 2019-05-09 stsp static const struct got_error *
2484 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2485 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2487 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2489 036813ee 2019-05-09 stsp *new_te = NULL;
2491 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2493 036813ee 2019-05-09 stsp goto done;
2495 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2497 036813ee 2019-05-09 stsp free((*new_te)->id);
2498 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2499 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2500 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2501 036813ee 2019-05-09 stsp goto done;
2504 036813ee 2019-05-09 stsp if (err && *new_te) {
2505 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2506 036813ee 2019-05-09 stsp *new_te = NULL;
2508 036813ee 2019-05-09 stsp return err;
2511 036813ee 2019-05-09 stsp static const struct got_error *
2512 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2513 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2515 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2516 036813ee 2019-05-09 stsp char *ct_name;
2518 036813ee 2019-05-09 stsp *new_te = NULL;
2520 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2521 036813ee 2019-05-09 stsp if (*new_te == NULL)
2522 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2524 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2525 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2526 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2527 036813ee 2019-05-09 stsp goto done;
2529 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2530 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2531 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2532 036813ee 2019-05-09 stsp goto done;
2535 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2537 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2538 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2540 036813ee 2019-05-09 stsp goto done;
2543 036813ee 2019-05-09 stsp if (err && *new_te) {
2544 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2545 036813ee 2019-05-09 stsp *new_te = NULL;
2547 036813ee 2019-05-09 stsp return err;
2550 036813ee 2019-05-09 stsp static const struct got_error *
2551 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2552 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2554 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2555 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2557 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2559 036813ee 2019-05-09 stsp return err;
2560 036813ee 2019-05-09 stsp if (new_pe == NULL)
2561 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2562 036813ee 2019-05-09 stsp return NULL;
2565 afa376bf 2019-05-09 stsp static const struct got_error *
2566 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2567 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2569 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2570 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2571 afa376bf 2019-05-09 stsp ct_path++;
2572 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2575 036813ee 2019-05-09 stsp static const struct got_error *
2576 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2577 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2579 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2580 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2581 44d03001 2019-05-09 stsp char *te_path;
2583 44d03001 2019-05-09 stsp *modified = 0;
2585 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2586 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2587 44d03001 2019-05-09 stsp te->name) == -1)
2588 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2590 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2591 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2592 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2593 44d03001 2019-05-09 stsp strlen(te_path));
2594 44d03001 2019-05-09 stsp if (*modified)
2598 44d03001 2019-05-09 stsp free(te_path);
2599 44d03001 2019-05-09 stsp return err;
2602 44d03001 2019-05-09 stsp static const struct got_error *
2603 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2604 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2605 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2607 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2608 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2610 036813ee 2019-05-09 stsp *ctp = NULL;
2612 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2613 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2614 036813ee 2019-05-09 stsp char *ct_name = NULL;
2615 036813ee 2019-05-09 stsp int path_matches;
2617 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2618 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2621 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2624 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2626 036813ee 2019-05-09 stsp return err;
2627 036813ee 2019-05-09 stsp if (!path_matches)
2630 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2631 036813ee 2019-05-09 stsp if (ct_name == NULL)
2632 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2634 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2637 036813ee 2019-05-09 stsp *ctp = ct;
2641 036813ee 2019-05-09 stsp return err;
2644 036813ee 2019-05-09 stsp static const struct got_error *
2645 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2646 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2647 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2648 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2649 036813ee 2019-05-09 stsp struct got_repository *repo)
2651 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2652 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2653 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2654 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2655 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2656 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2658 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2659 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2660 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2662 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2663 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2664 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2665 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2667 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2668 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2671 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2672 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2675 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2676 036813ee 2019-05-09 stsp pe->path);
2678 036813ee 2019-05-09 stsp goto done;
2680 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2681 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2682 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2684 036813ee 2019-05-09 stsp goto done;
2685 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2687 afa376bf 2019-05-09 stsp goto done;
2688 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
2690 2f51b5b3 2019-05-09 stsp char *subtree_path;
2692 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2693 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2694 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2695 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2696 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2697 2f51b5b3 2019-05-09 stsp goto done;
2700 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2701 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2702 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2703 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2704 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2705 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2706 9ba0479c 2019-05-10 stsp new_te = NULL;
2707 9ba0479c 2019-05-10 stsp goto done;
2709 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2710 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2711 2f51b5b3 2019-05-09 stsp free(subtree_path);
2712 9ba0479c 2019-05-10 stsp if (err) {
2713 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2714 9ba0479c 2019-05-10 stsp new_te = NULL;
2715 036813ee 2019-05-09 stsp goto done;
2718 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2720 2f51b5b3 2019-05-09 stsp goto done;
2723 2f51b5b3 2019-05-09 stsp if (base_tree) {
2724 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2725 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2726 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2727 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2729 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2730 44d03001 2019-05-09 stsp int modified;
2731 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2733 036813ee 2019-05-09 stsp goto done;
2734 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2735 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2737 2f51b5b3 2019-05-09 stsp goto done;
2738 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2739 44d03001 2019-05-09 stsp if (modified) {
2740 44d03001 2019-05-09 stsp free(new_te->id);
2741 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2742 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2743 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2745 44d03001 2019-05-09 stsp goto done;
2747 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2749 036813ee 2019-05-09 stsp goto done;
2753 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2754 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2756 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2757 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2758 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2759 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2761 2f51b5b3 2019-05-09 stsp goto done;
2762 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2764 2f51b5b3 2019-05-09 stsp goto done;
2766 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
2767 b416585c 2019-05-13 stsp status_arg);
2769 afa376bf 2019-05-09 stsp goto done;
2771 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
2772 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2774 2f51b5b3 2019-05-09 stsp goto done;
2775 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2777 2f51b5b3 2019-05-09 stsp goto done;
2782 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
2783 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
2784 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
2785 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
2786 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2788 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2790 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
2791 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
2792 ed175427 2019-05-09 stsp return err;
2795 ebf99748 2019-05-09 stsp static const struct got_error *
2796 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2797 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2799 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2800 ebf99748 2019-05-09 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
2801 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
2802 ebf99748 2019-05-09 stsp FILE *new_index = NULL;
2803 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
2805 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2807 ebf99748 2019-05-09 stsp return err;
2809 ebf99748 2019-05-09 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2810 ebf99748 2019-05-09 stsp fileindex_path);
2812 ebf99748 2019-05-09 stsp goto done;
2814 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2815 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
2816 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2818 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
2820 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
2821 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
2822 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
2824 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
2825 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
2826 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
2828 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
2829 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
2830 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
2832 ebf99748 2019-05-09 stsp goto done;
2833 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
2835 ebf99748 2019-05-09 stsp goto done;
2839 ebf99748 2019-05-09 stsp err = got_fileindex_write(fileindex, new_index);
2841 ebf99748 2019-05-09 stsp goto done;
2843 ebf99748 2019-05-09 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2844 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2845 230a42bd 2019-05-11 jcs fileindex_path);
2846 ebf99748 2019-05-09 stsp unlink(new_fileindex_path);
2847 ebf99748 2019-05-09 stsp goto done;
2850 ebf99748 2019-05-09 stsp free(new_fileindex_path);
2851 ebf99748 2019-05-09 stsp new_fileindex_path = NULL;
2854 ebf99748 2019-05-09 stsp if (new_fileindex_path)
2855 ebf99748 2019-05-09 stsp unlink(new_fileindex_path);
2856 ebf99748 2019-05-09 stsp if (new_index)
2857 ebf99748 2019-05-09 stsp fclose(new_index);
2858 ebf99748 2019-05-09 stsp free(new_fileindex_path);
2859 ebf99748 2019-05-09 stsp free(fileindex_path);
2860 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
2861 d56d26ce 2019-05-10 stsp return err;
2864 d56d26ce 2019-05-10 stsp static const struct got_error *
2865 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2866 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
2868 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
2869 d56d26ce 2019-05-10 stsp struct got_object_id *id_in_head;
2872 b416585c 2019-05-13 stsp * Require that modified/deleted files are based on the branch head.
2873 b416585c 2019-05-13 stsp * This requirement could be relaxed to force less update operations
2874 b416585c 2019-05-13 stsp * on users but, for now, we want to play it safe and see how it goes.
2876 b416585c 2019-05-13 stsp * If this check is relaxed, it must still ensure that no modifications
2877 b416585c 2019-05-13 stsp * were made to files *and their parents* in commits between the file's
2878 b416585c 2019-05-13 stsp * base commit and the branch head. Otherwise, tree conflicts will not
2879 b416585c 2019-05-13 stsp * be detected reliably.
2881 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
2882 b416585c 2019-05-13 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2883 b416585c 2019-05-13 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2884 b416585c 2019-05-13 stsp return NULL;
2887 b416585c 2019-05-13 stsp /* Require that added files don't exist in the branch head. */
2888 b416585c 2019-05-13 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2889 b416585c 2019-05-13 stsp ct->in_repo_path);
2890 b416585c 2019-05-13 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2891 b416585c 2019-05-13 stsp return err;
2892 b416585c 2019-05-13 stsp if (id_in_head) {
2893 b416585c 2019-05-13 stsp free(id_in_head);
2894 b416585c 2019-05-13 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2897 d56d26ce 2019-05-10 stsp free(id_in_head);
2898 b416585c 2019-05-13 stsp return NULL;
2901 c4296144 2019-05-09 stsp const struct got_error *
2902 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
2903 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
2904 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
2905 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2906 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2907 afa376bf 2019-05-09 stsp struct got_repository *repo)
2909 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2910 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
2911 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
2912 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
2913 bc70eb79 2019-05-09 stsp char *relpath = NULL;
2914 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
2915 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
2916 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
2917 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
2918 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
2919 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
2920 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
2921 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
2922 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
2923 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
2924 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
2926 c4296144 2019-05-09 stsp *new_commit_id = NULL;
2928 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
2929 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
2931 c4296144 2019-05-09 stsp if (ondisk_path) {
2932 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
2933 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
2935 c4296144 2019-05-09 stsp return err;
2938 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
2940 c4296144 2019-05-09 stsp goto done;
2942 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2944 36a38700 2019-05-10 stsp goto done;
2945 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2947 09f5bd90 2019-05-09 stsp goto done;
2949 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
2950 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
2951 24519714 2019-05-09 stsp cc_arg.repo = repo;
2952 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
2953 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
2955 675c7539 2019-05-09 stsp goto done;
2957 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
2958 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2962 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2964 588edf97 2019-05-10 stsp goto done;
2966 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2967 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2968 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
2970 819f385b 2019-05-10 stsp goto done;
2973 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2975 588edf97 2019-05-10 stsp goto done;
2977 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
2978 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2983 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
2984 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2988 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
2989 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2990 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2991 cf066bf8 2019-05-09 stsp char *ondisk_path;
2993 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
2994 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
2997 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
2998 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
2999 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3000 cf066bf8 2019-05-09 stsp goto done;
3002 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3003 a7055788 2019-05-09 stsp free(ondisk_path);
3005 cf066bf8 2019-05-09 stsp goto done;
3008 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3009 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3010 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3012 036813ee 2019-05-09 stsp goto done;
3014 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3016 de18fc63 2019-05-09 stsp goto done;
3017 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3018 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3019 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3020 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3021 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3022 33ad4cbe 2019-05-12 jcs free(logmsg);
3024 9d40349a 2019-05-09 stsp goto done;
3026 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3027 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3028 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3029 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3030 09f5bd90 2019-05-09 stsp goto done;
3032 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3033 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3035 09f5bd90 2019-05-09 stsp goto done;
3036 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3038 ebf99748 2019-05-09 stsp goto done;
3039 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3040 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3041 09f5bd90 2019-05-09 stsp goto done;
3043 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3044 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3046 f2c16586 2019-05-09 stsp goto done;
3047 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3049 f2c16586 2019-05-09 stsp goto done;
3051 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3053 09f5bd90 2019-05-09 stsp goto done;
3055 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3057 ebf99748 2019-05-09 stsp goto done;
3059 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3060 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3062 ebf99748 2019-05-09 stsp goto done;
3064 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3065 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3066 c4296144 2019-05-09 stsp err = unlockerr;
3067 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3068 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3069 ed175427 2019-05-09 stsp free_commitable(ct);
3071 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3072 588edf97 2019-05-10 stsp if (head_tree)
3073 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3074 588edf97 2019-05-10 stsp if (head_commit)
3075 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3076 c4296144 2019-05-09 stsp free(relpath);
3077 09f5bd90 2019-05-09 stsp free(head_commit_id);
3078 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3079 36a38700 2019-05-10 stsp if (head_ref)
3080 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3081 2f17228e 2019-05-12 stsp if (head_ref2) {
3082 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3083 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3084 2f17228e 2019-05-12 stsp err = unlockerr;
3085 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3087 c4296144 2019-05-09 stsp return err;
3090 8656d6c4 2019-05-20 stsp const char *
3091 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3093 8656d6c4 2019-05-20 stsp return ct->path;
3096 8656d6c4 2019-05-20 stsp unsigned int
3097 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3099 8656d6c4 2019-05-20 stsp return ct->status;