2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
121 09fe317a 2018-03-11 stsp struct stat sb;
123 09fe317a 2018-03-11 stsp *content = NULL;
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
145 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
175 09fe317a 2018-03-11 stsp return err;
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
198 86c3caaf 2018-03-09 stsp const struct got_error *
199 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
200 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
202 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
203 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
204 ec22038e 2019-03-10 stsp uuid_t uuid;
205 ec22038e 2019-03-10 stsp uint32_t uuid_status;
206 65596e15 2018-12-24 stsp int obj_type;
207 7ac97322 2018-03-11 stsp char *path_got = NULL;
208 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
361 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
366 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
367 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
368 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
371 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
374 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
378 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
379 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
383 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
388 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
389 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
393 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
398 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
402 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
406 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 eaccb85f 2018-12-25 stsp base_commit_id_str);
411 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
415 eaccb85f 2018-12-25 stsp got_repo_close(repo);
416 7ac97322 2018-03-11 stsp free(path_got);
417 056e7441 2018-03-11 stsp free(path_lock);
418 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
419 c442a90d 2019-03-10 stsp free(uuidstr);
420 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
425 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
426 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp return err;
433 247140b2 2019-02-05 stsp const struct got_error *
434 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
439 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
440 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 247140b2 2019-02-05 stsp return err;
442 247140b2 2019-02-05 stsp if (*worktree)
443 247140b2 2019-02-05 stsp return NULL;
444 247140b2 2019-02-05 stsp path = dirname(path);
445 d1542a27 2019-02-05 stsp if (path == NULL)
446 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
447 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
452 3a6ce05a 2019-02-11 stsp const struct got_error *
453 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
456 c88eb298 2018-03-11 stsp free(worktree->root_path);
457 cde76477 2018-03-11 stsp free(worktree->repo_path);
458 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
459 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
460 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
461 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
462 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
463 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
464 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
465 6d9d28c3 2018-03-11 stsp free(worktree);
466 3a6ce05a 2019-02-11 stsp return err;
469 2fbdb5ae 2018-12-29 stsp const char *
470 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp return worktree->root_path;
475 c7f4312f 2019-02-05 stsp const char *
476 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
481 49520a32 2018-12-29 stsp const char *
482 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 f609be2e 2018-12-29 stsp return worktree->path_prefix;
487 e5dc7198 2018-12-29 stsp const struct got_error *
488 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
494 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
498 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
499 e5dc7198 2018-12-29 stsp free(absprefix);
500 e5dc7198 2018-12-29 stsp return NULL;
503 bc70eb79 2019-05-09 stsp const char *
504 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 36a38700 2019-05-10 stsp return worktree->head_ref_name;
509 024e9686 2019-05-14 stsp const struct got_error *
510 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
511 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
514 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
518 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
519 024e9686 2019-05-14 stsp path_got = NULL;
523 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
524 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
525 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
529 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
533 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
534 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp free(head_ref_name);
539 024e9686 2019-05-14 stsp return err;
542 b72f483a 2019-02-05 stsp struct got_object_id *
543 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
548 507dc3bb 2018-12-29 stsp const struct got_error *
549 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp const struct got_error *err;
553 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
554 507dc3bb 2018-12-29 stsp char *id_str = NULL;
555 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
560 507dc3bb 2018-12-29 stsp path_got = NULL;
564 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
573 507dc3bb 2018-12-29 stsp /* Record our base commit. */
574 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
577 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
582 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
583 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
589 507dc3bb 2018-12-29 stsp got_object_close(obj);
590 507dc3bb 2018-12-29 stsp free(id_str);
591 507dc3bb 2018-12-29 stsp free(path_got);
592 507dc3bb 2018-12-29 stsp return err;
595 9d31a1d8 2018-03-11 stsp static const struct got_error *
596 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
601 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
602 86c3caaf 2018-03-09 stsp return NULL;
605 21908da4 2019-01-13 stsp static const struct got_error *
606 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
609 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
615 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 ddcd8544 2019-03-11 stsp struct stat sb;
617 ddcd8544 2019-03-11 stsp err = NULL;
618 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
619 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
620 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
621 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
622 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 4a1ddfc2 2019-01-12 stsp free(abspath);
626 68c76935 2019-02-19 stsp return err;
629 68c76935 2019-02-19 stsp static const struct got_error *
630 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
633 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
634 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
635 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
640 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
642 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
645 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
647 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
650 68c76935 2019-02-19 stsp if (flen1 == 0) {
651 68c76935 2019-02-19 stsp if (flen2 != 0)
654 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
655 68c76935 2019-02-19 stsp if (flen1 != 0)
658 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
659 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
669 68c76935 2019-02-19 stsp return err;
672 68c76935 2019-02-19 stsp static const struct got_error *
673 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
676 68c76935 2019-02-19 stsp struct stat sb;
677 68c76935 2019-02-19 stsp size_t size1, size2;
678 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
682 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
686 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
692 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp return NULL;
699 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
700 68c76935 2019-02-19 stsp if (f1 == NULL)
701 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
704 68c76935 2019-02-19 stsp if (f2 == NULL) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
709 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
712 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
713 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 6353ad76 2019-02-08 stsp return err;
720 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
722 234035bc 2019-06-01 stsp * acts as the second derived version.
724 6353ad76 2019-02-08 stsp static const struct got_error *
725 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
727 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
729 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 234035bc 2019-06-01 stsp void *progress_arg)
732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
733 6353ad76 2019-02-08 stsp int merged_fd = -1;
734 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
735 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
737 6353ad76 2019-02-08 stsp char *id_str = NULL;
738 818c7501 2019-07-11 stsp char *label_deriv = NULL;
739 234035bc 2019-06-01 stsp int overlapcnt = 0;
740 af54ae4a 2019-02-19 stsp char *parent;
742 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
744 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
745 af54ae4a 2019-02-19 stsp if (parent == NULL)
746 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
748 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
751 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 af54ae4a 2019-02-19 stsp free(base_path);
756 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
758 af54ae4a 2019-02-19 stsp base_path = NULL;
762 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
765 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 6c4c42e0 2019-06-24 stsp blob_deriv);
770 af54ae4a 2019-02-19 stsp free(base_path);
771 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
773 af54ae4a 2019-02-19 stsp base_path = NULL;
777 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
780 46b6ee73 2019-06-04 stsp if (blob_orig) {
781 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 46b6ee73 2019-06-04 stsp blob_orig);
787 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
788 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
789 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
793 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
796 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
801 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
806 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
807 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
811 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
812 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
816 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
817 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
818 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
819 68c76935 2019-02-19 stsp merged_path);
824 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
825 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
829 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
830 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
831 230a42bd 2019-05-11 jcs ondisk_path);
832 2a57020b 2019-02-20 stsp unlink(merged_path);
837 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
838 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
839 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
840 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
841 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
842 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
843 6353ad76 2019-02-08 stsp free(merged_path);
844 af54ae4a 2019-02-19 stsp free(base_path);
845 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
846 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
847 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
849 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
850 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
851 46b6ee73 2019-06-04 stsp free(blob_orig_path);
853 6353ad76 2019-02-08 stsp free(id_str);
854 818c7501 2019-07-11 stsp free(label_deriv);
855 4a1ddfc2 2019-01-12 stsp return err;
858 4a1ddfc2 2019-01-12 stsp static const struct got_error *
859 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
860 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
861 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
862 13d9040b 2019-03-27 stsp int update_timestamps)
864 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
866 13d9040b 2019-03-27 stsp if (ie == NULL)
867 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
869 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
870 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
871 13d9040b 2019-03-27 stsp update_timestamps);
873 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
874 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
875 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
877 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
879 13d9040b 2019-03-27 stsp return err;
882 13d9040b 2019-03-27 stsp static const struct got_error *
883 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
884 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
885 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
886 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
887 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
889 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
890 507dc3bb 2018-12-29 stsp int fd = -1;
891 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
892 507dc3bb 2018-12-29 stsp int update = 0;
893 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
895 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
897 9d31a1d8 2018-03-11 stsp if (fd == -1) {
898 21908da4 2019-01-13 stsp if (errno == ENOENT) {
899 21908da4 2019-01-13 stsp char *parent = dirname(path);
900 21908da4 2019-01-13 stsp if (parent == NULL)
901 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
902 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
904 21908da4 2019-01-13 stsp return err;
905 21908da4 2019-01-13 stsp fd = open(ondisk_path,
906 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
907 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
908 21908da4 2019-01-13 stsp if (fd == -1)
909 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
910 230a42bd 2019-05-11 jcs ondisk_path);
911 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
912 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
913 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
914 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
917 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
918 507dc3bb 2018-12-29 stsp ondisk_path);
921 507dc3bb 2018-12-29 stsp update = 1;
924 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
927 a378724f 2019-02-10 stsp if (restoring_missing_file)
928 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
929 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
930 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
933 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
937 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
939 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
940 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
943 9d31a1d8 2018-03-11 stsp if (len > 0) {
944 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
945 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
946 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
947 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
949 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
950 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
953 61d6eaa3 2018-12-24 stsp hdrlen = 0;
955 9d31a1d8 2018-03-11 stsp } while (len != 0);
957 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
958 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
962 507dc3bb 2018-12-29 stsp if (update) {
963 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
964 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
965 230a42bd 2019-05-11 jcs ondisk_path);
966 2a57020b 2019-02-20 stsp unlink(tmppath);
971 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
972 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
973 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
977 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
978 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
984 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
985 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
986 507dc3bb 2018-12-29 stsp free(tmppath);
987 6353ad76 2019-02-08 stsp return err;
990 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
991 6353ad76 2019-02-08 stsp static const struct got_error *
992 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
994 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
995 7154f6ce 2019-03-27 stsp const char *markers[3] = {
996 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
997 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
998 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1000 7154f6ce 2019-03-27 stsp int i = 0;
1001 7154f6ce 2019-03-27 stsp char *line;
1002 7154f6ce 2019-03-27 stsp size_t len;
1003 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1005 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1006 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1007 7154f6ce 2019-03-27 stsp if (line == NULL) {
1008 7154f6ce 2019-03-27 stsp if (feof(f))
1010 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1014 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1015 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1023 7154f6ce 2019-03-27 stsp return err;
1026 e2b1e152 2019-07-13 stsp static int
1027 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1030 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1031 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1032 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1033 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1036 7154f6ce 2019-03-27 stsp static const struct got_error *
1037 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1038 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1039 b8f41171 2019-02-10 stsp struct got_repository *repo)
1041 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1042 6353ad76 2019-02-08 stsp struct got_object_id id;
1043 6353ad76 2019-02-08 stsp size_t hdrlen;
1044 6353ad76 2019-02-08 stsp FILE *f = NULL;
1045 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1046 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1047 6353ad76 2019-02-08 stsp size_t flen, blen;
1049 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1051 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1052 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1054 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1055 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1057 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1058 b8f41171 2019-02-10 stsp sb->st_mode =
1059 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1060 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1062 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1063 a378724f 2019-02-10 stsp return NULL;
1065 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1068 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1069 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1070 6353ad76 2019-02-08 stsp return NULL;
1073 b8f41171 2019-02-10 stsp if (ie == NULL)
1074 d00136be 2019-03-26 stsp return NULL;
1076 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1077 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1078 2ec1f75b 2019-03-26 stsp return NULL;
1079 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1080 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1081 b8f41171 2019-02-10 stsp return NULL;
1084 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1085 6353ad76 2019-02-08 stsp return NULL;
1087 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1088 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1090 6353ad76 2019-02-08 stsp return err;
1092 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1093 6353ad76 2019-02-08 stsp if (f == NULL) {
1094 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1095 6353ad76 2019-02-08 stsp goto done;
1097 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1099 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1100 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1102 7154f6ce 2019-03-27 stsp goto done;
1103 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1104 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1105 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1106 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1107 7154f6ce 2019-03-27 stsp goto done;
1109 6353ad76 2019-02-08 stsp if (blen == 0) {
1110 6353ad76 2019-02-08 stsp if (flen != 0)
1111 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1113 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1114 6353ad76 2019-02-08 stsp if (blen != 0)
1115 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1117 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1118 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1119 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1120 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1124 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1127 6353ad76 2019-02-08 stsp hdrlen = 0;
1130 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1131 7154f6ce 2019-03-27 stsp rewind(f);
1132 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1136 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1138 6353ad76 2019-02-08 stsp fclose(f);
1139 9d31a1d8 2018-03-11 stsp return err;
1143 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1144 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1146 e2b1e152 2019-07-13 stsp static const struct got_error *
1147 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1148 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1150 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1151 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1152 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1154 e2b1e152 2019-07-13 stsp return NULL;
1157 9d31a1d8 2018-03-11 stsp static const struct got_error *
1158 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1159 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1160 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1161 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1162 0584f854 2019-04-06 stsp void *progress_arg)
1164 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1165 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1166 6353ad76 2019-02-08 stsp char *ondisk_path;
1167 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1168 b8f41171 2019-02-10 stsp struct stat sb;
1170 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1173 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1175 b8f41171 2019-02-10 stsp goto done;
1177 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1178 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1179 b8f41171 2019-02-10 stsp goto done;
1182 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1183 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1184 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1185 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1186 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1188 e2b1e152 2019-07-13 stsp goto done;
1189 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1191 6353ad76 2019-02-08 stsp goto done;
1193 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1194 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1195 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1196 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1197 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 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1206 234035bc 2019-06-01 stsp int update_timestamps;
1207 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1208 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1209 234035bc 2019-06-01 stsp struct got_object_id id2;
1210 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1211 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1213 234035bc 2019-06-01 stsp goto done;
1215 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1216 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1217 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1218 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1219 234035bc 2019-06-01 stsp if (blob2)
1220 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1222 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1223 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1224 234035bc 2019-06-01 stsp * unmodified files again.
1226 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1227 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1228 234035bc 2019-06-01 stsp update_timestamps);
1229 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1230 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1232 1ee397ad 2019-07-12 stsp goto done;
1233 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1234 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1236 13d9040b 2019-03-27 stsp goto done;
1238 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1239 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1240 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1242 13d9040b 2019-03-27 stsp goto done;
1243 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1244 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1246 13d9040b 2019-03-27 stsp goto done;
1248 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1250 6353ad76 2019-02-08 stsp free(ondisk_path);
1251 8da9e5f4 2019-01-12 stsp return err;
1254 90285c3b 2019-01-08 stsp static const struct got_error *
1255 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1257 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1258 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1260 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1261 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1263 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1264 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1265 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1267 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1268 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1269 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1270 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1271 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1275 90285c3b 2019-01-08 stsp parent = dirname(parent);
1278 90285c3b 2019-01-08 stsp free(ondisk_path);
1279 90285c3b 2019-01-08 stsp return err;
1282 708d8e67 2019-03-27 stsp static const struct got_error *
1283 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1284 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1285 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1287 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1288 708d8e67 2019-03-27 stsp unsigned char status;
1289 708d8e67 2019-03-27 stsp struct stat sb;
1290 708d8e67 2019-03-27 stsp char *ondisk_path;
1292 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1294 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1296 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1298 708d8e67 2019-03-27 stsp return err;
1300 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1301 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1302 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1304 1ee397ad 2019-07-12 stsp return err;
1306 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1307 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1309 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1312 708d8e67 2019-03-27 stsp return err;
1314 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1316 1ee397ad 2019-07-12 stsp return err;
1317 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1318 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1320 fc6346c4 2019-03-27 stsp return err;
1322 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1325 fc6346c4 2019-03-27 stsp return err;
1328 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1329 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1330 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1331 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1332 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1333 8da9e5f4 2019-01-12 stsp void *progress_arg;
1334 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1335 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1338 512f0d0e 2019-01-02 stsp static const struct got_error *
1339 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1340 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1342 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1344 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1347 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1348 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1351 8da9e5f4 2019-01-12 stsp static const struct got_error *
1352 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1354 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1356 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1357 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1359 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1360 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1363 9d31a1d8 2018-03-11 stsp static const struct got_error *
1364 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1366 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1367 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1368 8da9e5f4 2019-01-12 stsp char *path;
1370 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1373 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1374 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1376 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1378 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1379 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1381 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1382 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1384 8da9e5f4 2019-01-12 stsp free(path);
1385 0cd1c46a 2019-03-11 stsp return err;
1388 818c7501 2019-07-11 stsp static const struct got_error *
1389 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1391 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1392 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1393 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1395 517bab73 2019-03-11 stsp *refname = NULL;
1397 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1398 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1399 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1401 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1403 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1404 0647c563 2019-03-11 stsp *refname = NULL;
1406 517bab73 2019-03-11 stsp free(uuidstr);
1407 517bab73 2019-03-11 stsp return err;
1410 818c7501 2019-07-11 stsp const struct got_error *
1411 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1413 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1416 818c7501 2019-07-11 stsp static const struct got_error *
1417 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1419 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1420 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1423 818c7501 2019-07-11 stsp static const struct got_error *
1424 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1426 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1429 818c7501 2019-07-11 stsp static const struct got_error *
1430 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1432 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1433 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1436 818c7501 2019-07-11 stsp static const struct got_error *
1437 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1439 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1440 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1445 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1446 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1448 517bab73 2019-03-11 stsp static const struct got_error *
1449 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1451 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1452 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1453 517bab73 2019-03-11 stsp char *refname;
1455 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1457 517bab73 2019-03-11 stsp return err;
1459 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1461 0cd1c46a 2019-03-11 stsp goto done;
1463 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1465 0cd1c46a 2019-03-11 stsp free(refname);
1467 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1468 9d31a1d8 2018-03-11 stsp return err;
1471 ebf99748 2019-05-09 stsp static const struct got_error *
1472 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1473 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1475 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1476 ebf99748 2019-05-09 stsp FILE *index = NULL;
1478 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1479 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1480 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1481 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1483 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1484 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1485 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1486 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1487 ebf99748 2019-05-09 stsp goto done;
1490 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1491 ebf99748 2019-05-09 stsp if (index == NULL) {
1492 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1493 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1495 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1496 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1497 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1500 ebf99748 2019-05-09 stsp if (err) {
1501 ebf99748 2019-05-09 stsp free(*fileindex_path);
1502 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1503 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1504 ebf99748 2019-05-09 stsp *fileindex = NULL;
1506 ebf99748 2019-05-09 stsp return err;
1509 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1510 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1511 c932eeeb 2019-05-22 stsp const char *path;
1512 c932eeeb 2019-05-22 stsp size_t path_len;
1513 c932eeeb 2019-05-22 stsp const char *entry_name;
1514 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1515 a484d721 2019-06-10 stsp void *progress_arg;
1518 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1519 c932eeeb 2019-05-22 stsp static const struct got_error *
1520 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1522 1ee397ad 2019-07-12 stsp const struct got_error *err;
1523 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1525 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1526 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1527 c932eeeb 2019-05-22 stsp return NULL;
1528 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1529 102ff934 2019-06-11 stsp return NULL;
1531 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1532 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1533 c932eeeb 2019-05-22 stsp return NULL;
1535 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1536 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1537 edd02c5e 2019-07-11 stsp ie->path);
1539 1ee397ad 2019-07-12 stsp return err;
1541 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1542 c932eeeb 2019-05-22 stsp return NULL;
1545 9c6338c4 2019-06-02 stsp static const struct got_error *
1546 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1548 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1549 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1550 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1552 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1553 9c6338c4 2019-06-02 stsp fileindex_path);
1555 9c6338c4 2019-06-02 stsp goto done;
1557 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1559 9c6338c4 2019-06-02 stsp goto done;
1561 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1562 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1563 9c6338c4 2019-06-02 stsp fileindex_path);
1564 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1567 9c6338c4 2019-06-02 stsp if (new_index)
1568 9c6338c4 2019-06-02 stsp fclose(new_index);
1569 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1570 9c6338c4 2019-06-02 stsp return err;
1573 6ced4176 2019-07-12 stsp static const struct got_error *
1574 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1575 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1576 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1578 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1579 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1580 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1581 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1583 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1584 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1585 6ced4176 2019-07-12 stsp *tree_id = NULL;
1587 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1588 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1589 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1590 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1591 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1592 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1593 6ced4176 2019-07-12 stsp goto done;
1595 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1596 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1598 6ced4176 2019-07-12 stsp goto done;
1599 6ced4176 2019-07-12 stsp return NULL;
1602 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1604 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1605 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1606 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1607 6ced4176 2019-07-12 stsp goto done;
1610 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1611 6ced4176 2019-07-12 stsp in_repo_path);
1613 6ced4176 2019-07-12 stsp goto done;
1615 6ced4176 2019-07-12 stsp free(in_repo_path);
1616 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1618 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1620 6ced4176 2019-07-12 stsp goto done;
1622 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1623 6ced4176 2019-07-12 stsp /* Check out a single file. */
1624 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1625 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1626 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1627 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1628 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1629 6ced4176 2019-07-12 stsp goto done;
1631 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1632 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1633 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1634 6ced4176 2019-07-12 stsp goto done;
1637 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1638 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1640 6ced4176 2019-07-12 stsp return err;
1641 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1642 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1643 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1644 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1645 6ced4176 2019-07-12 stsp goto done;
1648 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1649 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1651 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1652 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1653 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1654 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1655 6ced4176 2019-07-12 stsp goto done;
1657 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1658 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1659 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1660 6ced4176 2019-07-12 stsp goto done;
1665 6ced4176 2019-07-12 stsp free(in_repo_path);
1666 6ced4176 2019-07-12 stsp if (err) {
1667 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1668 6ced4176 2019-07-12 stsp free(*tree_relpath);
1669 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1670 6ced4176 2019-07-12 stsp free(*tree_id);
1671 6ced4176 2019-07-12 stsp *tree_id = NULL;
1673 07ed472d 2019-07-12 stsp return err;
1676 07ed472d 2019-07-12 stsp static const struct got_error *
1677 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1678 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1679 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1680 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1682 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1683 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1684 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1685 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1686 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1688 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1690 07ed472d 2019-07-12 stsp goto done;
1692 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1693 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1695 07ed472d 2019-07-12 stsp goto done;
1697 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1699 07ed472d 2019-07-12 stsp goto done;
1701 07ed472d 2019-07-12 stsp if (entry_name &&
1702 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1703 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1704 07ed472d 2019-07-12 stsp goto done;
1707 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1708 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1709 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1710 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1711 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1712 07ed472d 2019-07-12 stsp arg.repo = repo;
1713 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1714 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1715 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1716 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1717 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1718 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1721 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1722 07ed472d 2019-07-12 stsp if (commit)
1723 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1724 6ced4176 2019-07-12 stsp return err;
1727 9d31a1d8 2018-03-11 stsp const struct got_error *
1728 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1729 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1730 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1732 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1733 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1734 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1735 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1736 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1737 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1738 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1739 6ced4176 2019-07-12 stsp int entry_type;
1741 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1743 9d31a1d8 2018-03-11 stsp return err;
1745 6ced4176 2019-07-12 stsp err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1746 6ced4176 2019-07-12 stsp path, worktree, repo);
1748 6ced4176 2019-07-12 stsp goto done;
1750 6ced4176 2019-07-12 stsp if (entry_type == GOT_OBJ_TYPE_BLOB) {
1751 6ced4176 2019-07-12 stsp entry_name = basename(path);
1752 6ced4176 2019-07-12 stsp if (entry_name == NULL) {
1753 6ced4176 2019-07-12 stsp err = got_error_from_errno2("basename", path);
1754 6ced4176 2019-07-12 stsp goto done;
1759 51514078 2018-12-25 stsp * Read the file index.
1760 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1761 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1763 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1765 c4cdcb68 2019-04-03 stsp goto done;
1767 07ed472d 2019-07-12 stsp err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1768 07ed472d 2019-07-12 stsp repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1769 711d9cd9 2019-07-12 stsp if (err == NULL) {
1770 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1771 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1772 711d9cd9 2019-07-12 stsp bbc_arg.entry_name = entry_name;
1773 711d9cd9 2019-07-12 stsp bbc_arg.path = path;
1774 711d9cd9 2019-07-12 stsp bbc_arg.path_len = strlen(path);
1775 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1776 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1777 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1778 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1780 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1781 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1782 af12c6b9 2019-06-04 stsp err = sync_err;
1784 9c6338c4 2019-06-02 stsp free(fileindex_path);
1785 c4cdcb68 2019-04-03 stsp free(relpath);
1787 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1788 9d31a1d8 2018-03-11 stsp if (commit)
1789 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1790 5ade8233 2019-07-12 stsp if (fileindex)
1791 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1792 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1793 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1794 9d31a1d8 2018-03-11 stsp err = unlockerr;
1795 f8d1f275 2019-02-04 stsp return err;
1798 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1799 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1800 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1801 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1802 234035bc 2019-06-01 stsp void *progress_arg;
1803 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1804 234035bc 2019-06-01 stsp void *cancel_arg;
1805 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1808 234035bc 2019-06-01 stsp static const struct got_error *
1809 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1810 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1811 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1812 234035bc 2019-06-01 stsp struct got_repository *repo)
1814 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1815 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1816 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1817 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1818 234035bc 2019-06-01 stsp struct stat sb;
1819 234035bc 2019-06-01 stsp unsigned char status;
1820 234035bc 2019-06-01 stsp int local_changes_subsumed;
1822 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1823 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1824 1ee397ad 2019-07-12 stsp if (ie == NULL)
1825 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1826 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1828 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1829 234035bc 2019-06-01 stsp path2) == -1)
1830 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1832 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1834 234035bc 2019-06-01 stsp goto done;
1836 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1837 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1838 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1839 234035bc 2019-06-01 stsp goto done;
1841 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1842 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1843 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1844 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1845 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1846 234035bc 2019-06-01 stsp goto done;
1849 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1850 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1851 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1852 234035bc 2019-06-01 stsp } else if (blob1) {
1853 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1854 1ee397ad 2019-07-12 stsp if (ie == NULL)
1855 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1856 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1858 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1859 2b92fad7 2019-06-02 stsp path1) == -1)
1860 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1862 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1864 2b92fad7 2019-06-02 stsp goto done;
1866 2b92fad7 2019-06-02 stsp switch (status) {
1867 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1868 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1869 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1871 1ee397ad 2019-07-12 stsp goto done;
1872 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1874 2b92fad7 2019-06-02 stsp goto done;
1876 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1878 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1879 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1880 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1881 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1883 1ee397ad 2019-07-12 stsp goto done;
1885 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1887 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1888 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1889 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1890 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1891 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1893 1ee397ad 2019-07-12 stsp goto done;
1895 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1896 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
1898 1ee397ad 2019-07-12 stsp goto done;
1903 234035bc 2019-06-01 stsp } else if (blob2) {
1904 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1905 234035bc 2019-06-01 stsp path2) == -1)
1906 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1907 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1909 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1912 234035bc 2019-06-01 stsp goto done;
1913 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1914 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1915 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1916 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1917 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1918 1ee397ad 2019-07-12 stsp status, path2);
1919 234035bc 2019-06-01 stsp goto done;
1921 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1922 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
1923 818c7501 2019-07-11 stsp a->commit_id2, repo,
1924 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1925 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1926 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1927 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1928 234035bc 2019-06-01 stsp blob2, 0);
1930 234035bc 2019-06-01 stsp goto done;
1933 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1935 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1936 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1937 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1938 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1940 234035bc 2019-06-01 stsp goto done;
1941 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1942 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1944 234035bc 2019-06-01 stsp goto done;
1945 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1946 2b92fad7 2019-06-02 stsp if (err) {
1947 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1948 2b92fad7 2019-06-02 stsp goto done;
1953 234035bc 2019-06-01 stsp free(ondisk_path);
1954 234035bc 2019-06-01 stsp return err;
1957 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1958 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1959 234035bc 2019-06-01 stsp struct got_repository *repo;
1962 234035bc 2019-06-01 stsp static const struct got_error *
1963 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1965 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1966 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1967 234035bc 2019-06-01 stsp unsigned char status;
1968 234035bc 2019-06-01 stsp struct stat sb;
1969 234035bc 2019-06-01 stsp char *ondisk_path;
1971 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1972 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1973 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1974 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1976 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1978 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1980 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1981 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1983 234035bc 2019-06-01 stsp return err;
1984 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1985 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1987 234035bc 2019-06-01 stsp return NULL;
1990 818c7501 2019-07-11 stsp static const struct got_error *
1991 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1992 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
1993 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
1994 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1995 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1997 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
1998 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1999 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2000 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2002 03415a1a 2019-06-02 stsp if (commit_id1) {
2003 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2004 03415a1a 2019-06-02 stsp worktree->path_prefix);
2006 03415a1a 2019-06-02 stsp goto done;
2008 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2010 03415a1a 2019-06-02 stsp goto done;
2013 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2014 234035bc 2019-06-01 stsp worktree->path_prefix);
2016 234035bc 2019-06-01 stsp goto done;
2018 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2020 234035bc 2019-06-01 stsp goto done;
2022 234035bc 2019-06-01 stsp arg.worktree = worktree;
2023 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2024 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2025 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2026 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2027 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2028 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2029 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2030 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2031 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2032 af12c6b9 2019-06-04 stsp err = sync_err;
2034 234035bc 2019-06-01 stsp if (tree1)
2035 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2036 234035bc 2019-06-01 stsp if (tree2)
2037 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2038 818c7501 2019-07-11 stsp return err;
2041 818c7501 2019-07-11 stsp const struct got_error *
2042 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2043 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2044 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2045 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2047 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2048 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2049 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2050 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2052 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2054 818c7501 2019-07-11 stsp return err;
2056 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2058 818c7501 2019-07-11 stsp goto done;
2060 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2061 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2062 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2063 818c7501 2019-07-11 stsp &mok_arg);
2065 818c7501 2019-07-11 stsp goto done;
2067 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2068 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2070 818c7501 2019-07-11 stsp if (fileindex)
2071 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2072 818c7501 2019-07-11 stsp free(fileindex_path);
2073 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2074 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2075 234035bc 2019-06-01 stsp err = unlockerr;
2076 234035bc 2019-06-01 stsp return err;
2079 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2080 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2081 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2082 927df6b7 2019-02-10 stsp const char *status_path;
2083 927df6b7 2019-02-10 stsp size_t status_path_len;
2084 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2085 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2086 f8d1f275 2019-02-04 stsp void *status_arg;
2087 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2088 f8d1f275 2019-02-04 stsp void *cancel_arg;
2091 f8d1f275 2019-02-04 stsp static const struct got_error *
2092 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2093 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2094 927df6b7 2019-02-10 stsp struct got_repository *repo)
2096 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2097 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2098 927df6b7 2019-02-10 stsp struct stat sb;
2099 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2101 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2102 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2103 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2104 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2105 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2106 016a88dd 2019-05-13 stsp &commit_id);
2108 927df6b7 2019-02-10 stsp return err;
2111 927df6b7 2019-02-10 stsp static const struct got_error *
2112 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2113 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2115 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2116 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2117 f8d1f275 2019-02-04 stsp char *abspath;
2119 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2122 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
2123 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2124 927df6b7 2019-02-10 stsp return NULL;
2126 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2127 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2128 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2129 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2131 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2132 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2133 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2136 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2138 f8d1f275 2019-02-04 stsp free(abspath);
2139 9d31a1d8 2018-03-11 stsp return err;
2142 f8d1f275 2019-02-04 stsp static const struct got_error *
2143 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2145 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2146 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2147 2ec1f75b 2019-03-26 stsp unsigned char status;
2149 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2150 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2152 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2153 927df6b7 2019-02-10 stsp return NULL;
2155 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2156 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2157 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2158 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2160 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2161 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2162 016a88dd 2019-05-13 stsp &commit_id);
2165 f8d1f275 2019-02-04 stsp static const struct got_error *
2166 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2168 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2169 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2170 f8d1f275 2019-02-04 stsp char *path = NULL;
2172 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2175 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2176 2c201a36 2019-02-10 stsp return NULL;
2178 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2179 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2180 f8d1f275 2019-02-04 stsp return NULL;
2182 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2183 927df6b7 2019-02-10 stsp return NULL;
2185 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2186 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2187 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2189 f8d1f275 2019-02-04 stsp path = de->d_name;
2192 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2193 016a88dd 2019-05-13 stsp NULL, NULL);
2194 f8d1f275 2019-02-04 stsp if (parent_path[0])
2195 f8d1f275 2019-02-04 stsp free(path);
2196 b72f483a 2019-02-05 stsp return err;
2199 347d1d3e 2019-07-12 stsp static const struct got_error *
2200 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2201 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2202 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2203 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2205 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2206 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2207 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2208 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2209 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2211 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2212 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2213 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2214 c513d110 2019-02-05 stsp goto done;
2216 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2217 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2218 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2219 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2220 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2221 927df6b7 2019-02-10 stsp if (ie == NULL) {
2222 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2223 927df6b7 2019-02-10 stsp goto done;
2225 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2226 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2227 927df6b7 2019-02-10 stsp goto done;
2229 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2230 927df6b7 2019-02-10 stsp goto done;
2233 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2234 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2235 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2236 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2237 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2238 927df6b7 2019-02-10 stsp arg.status_path = path;
2239 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2240 f8d1f275 2019-02-04 stsp arg.repo = repo;
2241 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2242 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2243 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2244 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2245 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2246 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2248 f8d1f275 2019-02-04 stsp if (workdir)
2249 f8d1f275 2019-02-04 stsp closedir(workdir);
2250 927df6b7 2019-02-10 stsp free(ondisk_path);
2251 347d1d3e 2019-07-12 stsp return err;
2254 347d1d3e 2019-07-12 stsp const struct got_error *
2255 347d1d3e 2019-07-12 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2256 347d1d3e 2019-07-12 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2257 347d1d3e 2019-07-12 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2259 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2260 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2261 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2263 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2265 347d1d3e 2019-07-12 stsp return err;
2267 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, path, fileindex, repo,
2268 347d1d3e 2019-07-12 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2269 f8d1f275 2019-02-04 stsp free(fileindex_path);
2270 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2271 f8d1f275 2019-02-04 stsp return err;
2274 6c7ab921 2019-03-18 stsp const struct got_error *
2275 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2276 6c7ab921 2019-03-18 stsp const char *arg)
2278 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2279 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2280 6c7ab921 2019-03-18 stsp size_t len;
2282 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2284 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2285 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2286 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2287 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2288 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2289 d0710d08 2019-07-22 stsp if (cwd == NULL)
2290 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2291 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2292 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2293 d0710d08 2019-07-22 stsp goto done;
2297 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2298 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2299 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2300 6c7ab921 2019-03-18 stsp goto done;
2303 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2304 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2305 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2307 f6d88e1a 2019-05-29 stsp goto done;
2309 f6d88e1a 2019-05-29 stsp path = strdup("");
2310 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2311 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2312 f6d88e1a 2019-05-29 stsp goto done;
2316 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2317 6c7ab921 2019-03-18 stsp len = strlen(path);
2318 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2319 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2323 6c7ab921 2019-03-18 stsp free(resolved);
2324 d0710d08 2019-07-22 stsp free(cwd);
2325 6c7ab921 2019-03-18 stsp if (err == NULL)
2326 6c7ab921 2019-03-18 stsp *wt_path = path;
2328 6c7ab921 2019-03-18 stsp free(path);
2329 d00136be 2019-03-26 stsp return err;
2332 07f5b47a 2019-06-02 stsp static const struct got_error *
2333 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2334 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2335 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2337 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2338 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2340 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2341 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2342 07f5b47a 2019-06-02 stsp return NULL;
2344 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2346 07f5b47a 2019-06-02 stsp return err;
2348 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2349 07f5b47a 2019-06-02 stsp if (err) {
2350 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2351 07f5b47a 2019-06-02 stsp return err;
2354 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2357 d00136be 2019-03-26 stsp const struct got_error *
2358 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2359 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2360 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2361 031a5338 2019-03-26 stsp struct got_repository *repo)
2363 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2364 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2365 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2366 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2368 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2370 d00136be 2019-03-26 stsp return err;
2372 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2374 d00136be 2019-03-26 stsp goto done;
2376 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2377 1dd54920 2019-05-11 stsp char *relpath;
2378 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2379 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2382 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2383 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2384 1dd54920 2019-05-11 stsp free(relpath);
2388 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2389 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2390 af12c6b9 2019-06-04 stsp err = sync_err;
2392 fb399478 2019-07-12 stsp free(fileindex_path);
2393 d00136be 2019-03-26 stsp if (fileindex)
2394 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2395 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2396 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2397 d00136be 2019-03-26 stsp err = unlockerr;
2398 6c7ab921 2019-03-18 stsp return err;
2401 17ed4618 2019-06-02 stsp static const struct got_error *
2402 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2403 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2404 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2405 17ed4618 2019-06-02 stsp struct got_repository *repo)
2407 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2408 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2409 17ed4618 2019-06-02 stsp unsigned char status;
2410 17ed4618 2019-06-02 stsp struct stat sb;
2412 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2413 17ed4618 2019-06-02 stsp if (ie == NULL)
2414 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2416 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2418 17ed4618 2019-06-02 stsp return err;
2420 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2421 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2422 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2423 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2424 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2425 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2426 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2429 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2430 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2432 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2433 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2436 2ec1f75b 2019-03-26 stsp const struct got_error *
2437 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2438 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2439 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2440 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2442 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2443 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2444 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2445 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2447 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2449 2ec1f75b 2019-03-26 stsp return err;
2451 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2453 2ec1f75b 2019-03-26 stsp goto done;
2455 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2456 17ed4618 2019-06-02 stsp char *relpath;
2457 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2458 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2461 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2462 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2463 17ed4618 2019-06-02 stsp free(relpath);
2467 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2468 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2469 af12c6b9 2019-06-04 stsp err = sync_err;
2471 fb399478 2019-07-12 stsp free(fileindex_path);
2472 2ec1f75b 2019-03-26 stsp if (fileindex)
2473 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2474 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2475 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2476 2ec1f75b 2019-03-26 stsp err = unlockerr;
2477 2ec1f75b 2019-03-26 stsp return err;
2480 e20a8b6f 2019-06-04 stsp static const struct got_error *
2481 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2482 a129376b 2019-03-28 stsp const char *ondisk_path,
2483 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2484 a129376b 2019-03-28 stsp struct got_repository *repo)
2486 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2487 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2488 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2489 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2490 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2491 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2492 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2493 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2494 a129376b 2019-03-28 stsp unsigned char status;
2495 a129376b 2019-03-28 stsp struct stat sb;
2497 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2498 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2500 a129376b 2019-03-28 stsp goto done;
2502 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2503 a129376b 2019-03-28 stsp if (ie == NULL) {
2504 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2505 a129376b 2019-03-28 stsp goto done;
2508 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2509 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2510 a129376b 2019-03-28 stsp if (err) {
2511 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2512 a129376b 2019-03-28 stsp goto done;
2513 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2514 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2515 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2516 e20a8b6f 2019-06-04 stsp goto done;
2519 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2520 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2521 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2522 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2523 a129376b 2019-03-28 stsp goto done;
2526 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2527 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2528 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2529 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2530 a129376b 2019-03-28 stsp goto done;
2533 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2534 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2535 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2536 a129376b 2019-03-28 stsp goto done;
2541 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2542 a129376b 2019-03-28 stsp tree_path);
2544 a129376b 2019-03-28 stsp goto done;
2546 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2548 a129376b 2019-03-28 stsp goto done;
2550 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2551 a129376b 2019-03-28 stsp if (te_name == NULL) {
2552 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2553 a129376b 2019-03-28 stsp goto done;
2556 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2558 a129376b 2019-03-28 stsp goto done;
2560 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2561 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2562 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2563 a129376b 2019-03-28 stsp goto done;
2566 a129376b 2019-03-28 stsp switch (status) {
2567 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2568 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2570 1ee397ad 2019-07-12 stsp goto done;
2571 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2573 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2574 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2575 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2576 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2577 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2578 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2579 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2581 a129376b 2019-03-28 stsp goto done;
2582 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2583 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2584 a129376b 2019-03-28 stsp progress_arg);
2586 a129376b 2019-03-28 stsp goto done;
2587 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2588 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2589 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2591 a129376b 2019-03-28 stsp goto done;
2596 a129376b 2019-03-28 stsp goto done;
2599 a129376b 2019-03-28 stsp free(relpath);
2600 e20a8b6f 2019-06-04 stsp free(parent_path);
2601 a129376b 2019-03-28 stsp free(tree_path);
2603 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2605 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2606 a129376b 2019-03-28 stsp free(tree_id);
2607 e20a8b6f 2019-06-04 stsp return err;
2610 e20a8b6f 2019-06-04 stsp const struct got_error *
2611 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2612 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2613 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2614 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2616 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2617 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2618 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2619 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2620 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2622 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2624 e20a8b6f 2019-06-04 stsp return err;
2626 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2628 e20a8b6f 2019-06-04 stsp goto done;
2630 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2631 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2632 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2636 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2637 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2638 e20a8b6f 2019-06-04 stsp err = sync_err;
2640 fb399478 2019-07-12 stsp free(fileindex_path);
2641 a129376b 2019-03-28 stsp if (fileindex)
2642 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2643 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2644 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2645 a129376b 2019-03-28 stsp err = unlockerr;
2646 c4296144 2019-05-09 stsp return err;
2649 cf066bf8 2019-05-09 stsp static void
2650 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2652 24519714 2019-05-09 stsp free(ct->path);
2653 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2654 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2655 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2656 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2657 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2661 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2662 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2663 24519714 2019-05-09 stsp struct got_repository *repo;
2664 24519714 2019-05-09 stsp struct got_worktree *worktree;
2667 c4296144 2019-05-09 stsp static const struct got_error *
2668 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2669 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2671 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2672 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2673 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2674 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2675 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2676 768aea60 2019-05-09 stsp struct stat sb;
2678 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2679 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2681 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2682 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2683 c4296144 2019-05-09 stsp return NULL;
2685 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2686 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2687 036813ee 2019-05-09 stsp goto done;
2689 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2690 036813ee 2019-05-09 stsp parent_path = strdup("");
2691 69960a46 2019-05-09 stsp if (parent_path == NULL)
2692 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2694 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2696 69960a46 2019-05-09 stsp return err;
2699 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2700 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2701 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2702 c4296144 2019-05-09 stsp goto done;
2705 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2706 768aea60 2019-05-09 stsp relpath) == -1) {
2707 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2708 768aea60 2019-05-09 stsp goto done;
2710 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2711 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2713 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2714 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2715 768aea60 2019-05-09 stsp goto done;
2717 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2720 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2721 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2722 44d03001 2019-05-09 stsp relpath) == -1) {
2723 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2724 44d03001 2019-05-09 stsp goto done;
2727 cf066bf8 2019-05-09 stsp ct->status = status;
2728 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2729 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2730 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2731 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2732 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2733 b416585c 2019-05-13 stsp goto done;
2735 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2736 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2737 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2738 036813ee 2019-05-09 stsp goto done;
2741 24519714 2019-05-09 stsp ct->path = strdup(path);
2742 24519714 2019-05-09 stsp if (ct->path == NULL) {
2743 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2744 24519714 2019-05-09 stsp goto done;
2746 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2748 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2749 ed175427 2019-05-09 stsp free_commitable(ct);
2750 24519714 2019-05-09 stsp free(parent_path);
2751 036813ee 2019-05-09 stsp free(path);
2752 a129376b 2019-03-28 stsp return err;
2755 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2756 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2757 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2758 036813ee 2019-05-09 stsp struct got_repository *);
2760 ed175427 2019-05-09 stsp static const struct got_error *
2761 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2762 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2763 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2764 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2765 afa376bf 2019-05-09 stsp struct got_repository *repo)
2767 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2768 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2769 036813ee 2019-05-09 stsp char *subpath;
2771 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2772 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2773 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2775 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2777 ed175427 2019-05-09 stsp return err;
2779 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2780 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2781 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2782 036813ee 2019-05-09 stsp free(subpath);
2783 036813ee 2019-05-09 stsp return err;
2786 036813ee 2019-05-09 stsp static const struct got_error *
2787 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2789 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2790 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2792 036813ee 2019-05-09 stsp *match = 0;
2794 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2795 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2796 0f63689d 2019-05-10 stsp return NULL;
2799 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2801 0f63689d 2019-05-10 stsp return err;
2802 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2803 036813ee 2019-05-09 stsp free(ct_parent_path);
2804 036813ee 2019-05-09 stsp return err;
2807 768aea60 2019-05-09 stsp static mode_t
2808 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2810 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2813 036813ee 2019-05-09 stsp static const struct got_error *
2814 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2815 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2817 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2819 036813ee 2019-05-09 stsp *new_te = NULL;
2821 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2823 036813ee 2019-05-09 stsp goto done;
2825 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2827 036813ee 2019-05-09 stsp free((*new_te)->id);
2828 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2829 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2830 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2831 036813ee 2019-05-09 stsp goto done;
2834 036813ee 2019-05-09 stsp if (err && *new_te) {
2835 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2836 036813ee 2019-05-09 stsp *new_te = NULL;
2838 036813ee 2019-05-09 stsp return err;
2841 036813ee 2019-05-09 stsp static const struct got_error *
2842 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2843 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2845 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2846 036813ee 2019-05-09 stsp char *ct_name;
2848 036813ee 2019-05-09 stsp *new_te = NULL;
2850 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2851 036813ee 2019-05-09 stsp if (*new_te == NULL)
2852 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2854 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2855 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2856 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2857 036813ee 2019-05-09 stsp goto done;
2859 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2860 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2861 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2862 036813ee 2019-05-09 stsp goto done;
2865 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2867 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2868 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2869 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2870 036813ee 2019-05-09 stsp goto done;
2873 036813ee 2019-05-09 stsp if (err && *new_te) {
2874 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2875 036813ee 2019-05-09 stsp *new_te = NULL;
2877 036813ee 2019-05-09 stsp return err;
2880 036813ee 2019-05-09 stsp static const struct got_error *
2881 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2882 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2884 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2885 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2887 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2889 036813ee 2019-05-09 stsp return err;
2890 036813ee 2019-05-09 stsp if (new_pe == NULL)
2891 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2892 036813ee 2019-05-09 stsp return NULL;
2895 afa376bf 2019-05-09 stsp static const struct got_error *
2896 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2897 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2899 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2900 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2901 afa376bf 2019-05-09 stsp ct_path++;
2902 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2905 036813ee 2019-05-09 stsp static const struct got_error *
2906 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2907 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2909 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2910 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2911 44d03001 2019-05-09 stsp char *te_path;
2913 44d03001 2019-05-09 stsp *modified = 0;
2915 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2916 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2917 44d03001 2019-05-09 stsp te->name) == -1)
2918 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2920 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2921 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2922 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2923 44d03001 2019-05-09 stsp strlen(te_path));
2924 44d03001 2019-05-09 stsp if (*modified)
2928 44d03001 2019-05-09 stsp free(te_path);
2929 44d03001 2019-05-09 stsp return err;
2932 44d03001 2019-05-09 stsp static const struct got_error *
2933 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2934 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2935 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2937 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2938 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2940 036813ee 2019-05-09 stsp *ctp = NULL;
2942 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2943 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2944 036813ee 2019-05-09 stsp char *ct_name = NULL;
2945 036813ee 2019-05-09 stsp int path_matches;
2947 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2948 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2951 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2954 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2956 036813ee 2019-05-09 stsp return err;
2957 036813ee 2019-05-09 stsp if (!path_matches)
2960 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2961 036813ee 2019-05-09 stsp if (ct_name == NULL)
2962 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2964 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2967 036813ee 2019-05-09 stsp *ctp = ct;
2971 2b496619 2019-07-10 stsp return err;
2974 2b496619 2019-07-10 stsp static const struct got_error *
2975 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2976 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
2977 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
2978 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2979 2b496619 2019-07-10 stsp struct got_repository *repo)
2981 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
2982 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
2983 2b496619 2019-07-10 stsp char *subtree_path;
2985 2b496619 2019-07-10 stsp *new_tep = NULL;
2987 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2988 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2989 2b496619 2019-07-10 stsp child_path) == -1)
2990 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
2992 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
2993 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
2994 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
2995 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
2996 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
2997 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
2998 2b496619 2019-07-10 stsp goto done;
3000 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3001 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3002 2b496619 2019-07-10 stsp if (err) {
3003 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3004 2b496619 2019-07-10 stsp goto done;
3007 2b496619 2019-07-10 stsp free(subtree_path);
3008 2b496619 2019-07-10 stsp if (err == NULL)
3009 2b496619 2019-07-10 stsp *new_tep = new_te;
3010 036813ee 2019-05-09 stsp return err;
3013 036813ee 2019-05-09 stsp static const struct got_error *
3014 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3015 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3016 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3017 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3018 036813ee 2019-05-09 stsp struct got_repository *repo)
3020 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3021 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3022 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3023 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3024 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3025 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3027 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3028 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3029 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3031 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3032 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3033 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3034 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3036 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
3037 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3040 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3041 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3044 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3045 036813ee 2019-05-09 stsp pe->path);
3047 036813ee 2019-05-09 stsp goto done;
3049 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3050 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3051 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3053 036813ee 2019-05-09 stsp goto done;
3054 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3056 afa376bf 2019-05-09 stsp goto done;
3057 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3058 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3060 2f51b5b3 2019-05-09 stsp goto done;
3062 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3063 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3064 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3065 2b496619 2019-07-10 stsp == NULL) {
3066 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3067 2b496619 2019-07-10 stsp child_path, path_base_tree,
3068 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3071 2b496619 2019-07-10 stsp goto done;
3072 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3074 2b496619 2019-07-10 stsp goto done;
3079 2f51b5b3 2019-05-09 stsp if (base_tree) {
3080 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3081 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3082 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3083 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3085 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3086 44d03001 2019-05-09 stsp int modified;
3087 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3089 036813ee 2019-05-09 stsp goto done;
3090 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3091 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3093 2f51b5b3 2019-05-09 stsp goto done;
3094 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3095 44d03001 2019-05-09 stsp if (modified) {
3096 44d03001 2019-05-09 stsp free(new_te->id);
3097 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3098 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3099 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3101 44d03001 2019-05-09 stsp goto done;
3103 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3105 036813ee 2019-05-09 stsp goto done;
3109 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3110 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3112 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3113 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
3114 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3115 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3117 2f51b5b3 2019-05-09 stsp goto done;
3118 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3120 2f51b5b3 2019-05-09 stsp goto done;
3122 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3123 b416585c 2019-05-13 stsp status_arg);
3125 afa376bf 2019-05-09 stsp goto done;
3127 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3128 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3130 2f51b5b3 2019-05-09 stsp goto done;
3131 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3133 2f51b5b3 2019-05-09 stsp goto done;
3138 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3139 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3140 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3141 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3142 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3144 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3146 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3147 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3148 ed175427 2019-05-09 stsp return err;
3151 ebf99748 2019-05-09 stsp static const struct got_error *
3152 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3153 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3155 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3156 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3158 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3159 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3162 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3164 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3165 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3166 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3168 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3169 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3170 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3172 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3173 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3174 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3177 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3182 d56d26ce 2019-05-10 stsp return err;
3185 d56d26ce 2019-05-10 stsp static const struct got_error *
3186 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3187 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3189 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3190 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3191 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3192 1a36436d 2019-06-10 stsp char *path = NULL;
3193 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3195 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3196 1a36436d 2019-06-10 stsp ct_path++;
3199 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3200 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3202 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3203 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3204 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3205 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3207 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3208 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3210 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3211 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3212 1a36436d 2019-06-10 stsp char *slash;
3213 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3215 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3216 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3217 1a36436d 2019-06-10 stsp return NULL;
3219 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3220 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3221 1a36436d 2019-06-10 stsp if (path == NULL) {
3222 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3223 1a36436d 2019-06-10 stsp goto done;
3225 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3226 1a36436d 2019-06-10 stsp if (slash)
3227 1a36436d 2019-06-10 stsp *slash = '\0';
3229 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3231 1a36436d 2019-06-10 stsp goto done;
3233 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3234 1a36436d 2019-06-10 stsp head_commit_id, path);
3236 1a36436d 2019-06-10 stsp goto done;
3238 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3239 1a36436d 2019-06-10 stsp while (pid) {
3240 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3242 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3243 1a36436d 2019-06-10 stsp if (err) {
3244 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3245 1a36436d 2019-06-10 stsp goto done;
3246 1a36436d 2019-06-10 stsp err = NULL;
3250 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3252 1a36436d 2019-06-10 stsp goto done;
3254 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3255 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3259 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3260 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3262 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3265 1a36436d 2019-06-10 stsp goto done;
3267 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3268 1a36436d 2019-06-10 stsp commit = pcommit;
3269 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3273 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3274 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3276 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3277 1a36436d 2019-06-10 stsp goto done;
3278 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3281 1a36436d 2019-06-10 stsp if (commit)
3282 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3283 d56d26ce 2019-05-10 stsp free(id_in_head);
3285 1a36436d 2019-06-10 stsp free(path);
3286 1a36436d 2019-06-10 stsp return err;
3289 c4296144 2019-05-09 stsp const struct got_error *
3290 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3291 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3292 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3293 39cd0ff6 2019-07-12 stsp const char *ondisk_path, const char *author, const char *committer,
3294 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3295 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3296 afa376bf 2019-05-09 stsp struct got_repository *repo)
3298 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3299 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3300 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3301 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3302 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3303 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3304 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3305 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3306 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3307 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3308 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3310 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3312 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3314 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3316 588edf97 2019-05-10 stsp goto done;
3318 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3320 588edf97 2019-05-10 stsp goto done;
3322 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3323 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3328 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3329 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3333 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3334 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3335 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3336 cf066bf8 2019-05-09 stsp char *ondisk_path;
3338 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3339 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3342 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3343 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3344 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3345 cf066bf8 2019-05-09 stsp goto done;
3347 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3348 a7055788 2019-05-09 stsp free(ondisk_path);
3350 cf066bf8 2019-05-09 stsp goto done;
3353 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3354 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3355 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3357 036813ee 2019-05-09 stsp goto done;
3359 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3361 de18fc63 2019-05-09 stsp goto done;
3362 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3363 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3364 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3365 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3366 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3367 33ad4cbe 2019-05-12 jcs free(logmsg);
3369 9d40349a 2019-05-09 stsp goto done;
3371 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3372 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3373 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3374 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3375 09f5bd90 2019-05-09 stsp goto done;
3377 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3378 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3380 09f5bd90 2019-05-09 stsp goto done;
3381 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3383 ebf99748 2019-05-09 stsp goto done;
3384 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3385 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3386 09f5bd90 2019-05-09 stsp goto done;
3388 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3389 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3391 f2c16586 2019-05-09 stsp goto done;
3392 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3394 f2c16586 2019-05-09 stsp goto done;
3396 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3398 09f5bd90 2019-05-09 stsp goto done;
3400 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3402 ebf99748 2019-05-09 stsp goto done;
3404 588edf97 2019-05-10 stsp if (head_tree)
3405 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3406 588edf97 2019-05-10 stsp if (head_commit)
3407 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3408 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3409 2f17228e 2019-05-12 stsp if (head_ref2) {
3410 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3411 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3412 2f17228e 2019-05-12 stsp err = unlockerr;
3413 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3415 39cd0ff6 2019-07-12 stsp return err;
3418 39cd0ff6 2019-07-12 stsp const struct got_error *
3419 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3420 39cd0ff6 2019-07-12 stsp struct got_worktree *worktree, const char *ondisk_path,
3421 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
3422 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3423 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3424 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
3426 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3427 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3428 39cd0ff6 2019-07-12 stsp char *fileindex_path = NULL, *relpath = NULL;
3429 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3430 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3431 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
3432 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
3433 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3435 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
3437 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3439 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
3441 39cd0ff6 2019-07-12 stsp goto done;
3443 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3445 39cd0ff6 2019-07-12 stsp goto done;
3447 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3449 39cd0ff6 2019-07-12 stsp goto done;
3451 39cd0ff6 2019-07-12 stsp if (ondisk_path) {
3452 39cd0ff6 2019-07-12 stsp err = got_path_skip_common_ancestor(&relpath,
3453 39cd0ff6 2019-07-12 stsp worktree->root_path, ondisk_path);
3455 39cd0ff6 2019-07-12 stsp return err;
3458 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3460 39cd0ff6 2019-07-12 stsp goto done;
3462 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3463 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
3464 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
3465 39cd0ff6 2019-07-12 stsp err = worktree_status(worktree, relpath ? relpath : "",
3466 39cd0ff6 2019-07-12 stsp fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3468 39cd0ff6 2019-07-12 stsp goto done;
3470 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3471 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3472 39cd0ff6 2019-07-12 stsp goto done;
3475 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3476 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
3477 b50cabdf 2019-07-12 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3479 b50cabdf 2019-07-12 stsp goto done;
3482 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
3483 39cd0ff6 2019-07-12 stsp head_commit_id, worktree, ondisk_path, author, committer,
3484 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3486 39cd0ff6 2019-07-12 stsp goto done;
3488 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3489 93ec1b5f 2019-07-12 stsp fileindex);
3490 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3491 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
3492 39cd0ff6 2019-07-12 stsp err = sync_err;
3494 39cd0ff6 2019-07-12 stsp if (fileindex)
3495 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
3496 39cd0ff6 2019-07-12 stsp free(fileindex_path);
3497 39cd0ff6 2019-07-12 stsp free(relpath);
3498 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3499 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
3500 39cd0ff6 2019-07-12 stsp err = unlockerr;
3501 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3502 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
3503 39cd0ff6 2019-07-12 stsp free_commitable(ct);
3505 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
3506 c4296144 2019-05-09 stsp return err;
3509 8656d6c4 2019-05-20 stsp const char *
3510 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3512 8656d6c4 2019-05-20 stsp return ct->path;
3515 8656d6c4 2019-05-20 stsp unsigned int
3516 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3518 8656d6c4 2019-05-20 stsp return ct->status;
3521 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3522 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3523 818c7501 2019-07-11 stsp struct got_repository *repo;
3524 818c7501 2019-07-11 stsp int rebase_in_progress;
3527 818c7501 2019-07-11 stsp static const struct got_error *
3528 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3530 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3531 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3532 818c7501 2019-07-11 stsp unsigned char status;
3533 818c7501 2019-07-11 stsp struct stat sb;
3534 818c7501 2019-07-11 stsp char *ondisk_path;
3536 818c7501 2019-07-11 stsp if (!a->rebase_in_progress) {
3537 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with mixed base commits. */
3538 818c7501 2019-07-11 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3539 818c7501 2019-07-11 stsp SHA1_DIGEST_LENGTH))
3540 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3543 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3545 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3547 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with modified or conflicted files. */
3548 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3549 818c7501 2019-07-11 stsp free(ondisk_path);
3551 818c7501 2019-07-11 stsp return err;
3553 818c7501 2019-07-11 stsp if (a->rebase_in_progress) {
3554 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT)
3555 818c7501 2019-07-11 stsp return got_error(GOT_ERR_CONFLICTS);
3556 818c7501 2019-07-11 stsp } else if (status != GOT_STATUS_NO_CHANGE)
3557 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3559 818c7501 2019-07-11 stsp return NULL;
3562 818c7501 2019-07-11 stsp const struct got_error *
3563 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3564 818c7501 2019-07-11 stsp struct got_reference **tmp_branch, struct got_worktree *worktree,
3565 818c7501 2019-07-11 stsp struct got_reference *branch, struct got_repository *repo)
3567 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3568 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3569 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3570 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3571 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3572 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3573 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3575 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3576 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3578 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3580 818c7501 2019-07-11 stsp return err;
3582 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3584 818c7501 2019-07-11 stsp goto done;
3586 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3587 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3588 818c7501 2019-07-11 stsp ok_arg.rebase_in_progress = 0;
3589 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3592 818c7501 2019-07-11 stsp goto done;
3594 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3596 818c7501 2019-07-11 stsp goto done;
3598 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3600 818c7501 2019-07-11 stsp goto done;
3602 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3604 818c7501 2019-07-11 stsp goto done;
3606 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3609 818c7501 2019-07-11 stsp goto done;
3611 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3612 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3614 818c7501 2019-07-11 stsp goto done;
3615 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3617 818c7501 2019-07-11 stsp goto done;
3619 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3621 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3623 818c7501 2019-07-11 stsp goto done;
3625 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3627 818c7501 2019-07-11 stsp goto done;
3629 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3630 818c7501 2019-07-11 stsp worktree->base_commit_id);
3632 818c7501 2019-07-11 stsp goto done;
3633 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3635 818c7501 2019-07-11 stsp goto done;
3637 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3639 818c7501 2019-07-11 stsp goto done;
3641 818c7501 2019-07-11 stsp free(fileindex_path);
3642 818c7501 2019-07-11 stsp if (fileindex)
3643 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3644 818c7501 2019-07-11 stsp free(tmp_branch_name);
3645 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3646 818c7501 2019-07-11 stsp free(branch_ref_name);
3647 818c7501 2019-07-11 stsp if (branch_ref)
3648 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3649 818c7501 2019-07-11 stsp if (wt_branch)
3650 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3651 818c7501 2019-07-11 stsp if (err) {
3652 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3653 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3654 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3656 818c7501 2019-07-11 stsp if (*tmp_branch) {
3657 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3658 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3660 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3662 818c7501 2019-07-11 stsp return err;
3665 818c7501 2019-07-11 stsp const struct got_error *
3666 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3667 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3668 818c7501 2019-07-11 stsp struct got_reference **branch, struct got_worktree *worktree,
3669 818c7501 2019-07-11 stsp struct got_repository *repo)
3671 818c7501 2019-07-11 stsp const struct got_error *err;
3672 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3673 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3674 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3676 818c7501 2019-07-11 stsp *commit_id = NULL;
3678 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3680 818c7501 2019-07-11 stsp return err;
3682 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3684 818c7501 2019-07-11 stsp goto done;
3686 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3688 818c7501 2019-07-11 stsp goto done;
3690 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3692 818c7501 2019-07-11 stsp goto done;
3694 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3696 818c7501 2019-07-11 stsp goto done;
3698 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3699 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3701 818c7501 2019-07-11 stsp goto done;
3703 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3705 818c7501 2019-07-11 stsp goto done;
3707 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
3709 818c7501 2019-07-11 stsp goto done;
3711 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
3712 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
3714 818c7501 2019-07-11 stsp goto done;
3716 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3718 818c7501 2019-07-11 stsp goto done;
3720 818c7501 2019-07-11 stsp free(commit_ref_name);
3721 818c7501 2019-07-11 stsp free(branch_ref_name);
3722 818c7501 2019-07-11 stsp if (commit_ref)
3723 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3724 818c7501 2019-07-11 stsp if (branch_ref)
3725 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3726 818c7501 2019-07-11 stsp if (err) {
3727 818c7501 2019-07-11 stsp free(*commit_id);
3728 818c7501 2019-07-11 stsp *commit_id = NULL;
3729 818c7501 2019-07-11 stsp if (*tmp_branch) {
3730 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3731 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3733 818c7501 2019-07-11 stsp if (*new_base_branch) {
3734 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
3735 818c7501 2019-07-11 stsp *new_base_branch = NULL;
3737 818c7501 2019-07-11 stsp if (*branch) {
3738 818c7501 2019-07-11 stsp got_ref_close(*branch);
3739 818c7501 2019-07-11 stsp *branch = NULL;
3742 818c7501 2019-07-11 stsp return err;
3745 818c7501 2019-07-11 stsp const struct got_error *
3746 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3748 818c7501 2019-07-11 stsp const struct got_error *err;
3749 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
3751 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3753 818c7501 2019-07-11 stsp return err;
3755 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3756 818c7501 2019-07-11 stsp free(tmp_branch_name);
3757 818c7501 2019-07-11 stsp return NULL;
3760 818c7501 2019-07-11 stsp static const struct got_error *
3761 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3762 818c7501 2019-07-11 stsp char **logmsg, void *arg)
3764 818c7501 2019-07-11 stsp struct got_commit_object *commit = arg;
3766 818c7501 2019-07-11 stsp *logmsg = strdup(got_object_commit_get_logmsg(commit));
3767 818c7501 2019-07-11 stsp if (*logmsg == NULL)
3768 818c7501 2019-07-11 stsp return got_error_from_errno("strdup");
3770 818c7501 2019-07-11 stsp return NULL;
3773 818c7501 2019-07-11 stsp static const struct got_error *
3774 818c7501 2019-07-11 stsp rebase_status(void *arg, unsigned char status, const char *path,
3775 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3777 818c7501 2019-07-11 stsp return NULL;
3780 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
3781 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
3782 01757395 2019-07-12 stsp void *progress_arg;
3783 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
3786 01757395 2019-07-12 stsp static const struct got_error *
3787 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
3789 01757395 2019-07-12 stsp const struct got_error *err;
3790 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
3792 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
3794 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
3796 01757395 2019-07-12 stsp return err;
3798 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
3799 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
3800 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
3801 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
3802 01757395 2019-07-12 stsp return NULL;
3804 01757395 2019-07-12 stsp p = strdup(path);
3805 01757395 2019-07-12 stsp if (p == NULL)
3806 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
3808 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3809 01757395 2019-07-12 stsp if (err || new == NULL)
3811 01757395 2019-07-12 stsp return err;
3815 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3817 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
3819 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
3820 01757395 2019-07-12 stsp free((char *)pe->path);
3822 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
3825 818c7501 2019-07-11 stsp const struct got_error *
3826 01757395 2019-07-12 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3827 01757395 2019-07-12 stsp struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3828 01757395 2019-07-12 stsp struct got_object_id *commit_id, struct got_repository *repo,
3829 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3830 01757395 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3832 818c7501 2019-07-11 stsp const struct got_error *err;
3833 818c7501 2019-07-11 stsp struct got_fileindex *fileindex;
3834 818c7501 2019-07-11 stsp char *fileindex_path, *commit_ref_name = NULL;
3835 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3836 01757395 2019-07-12 stsp struct collect_merged_paths_arg cmp_arg;
3838 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
3840 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3842 818c7501 2019-07-11 stsp return err;
3844 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3846 818c7501 2019-07-11 stsp goto done;
3847 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3848 818c7501 2019-07-11 stsp if (err) {
3849 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
3850 818c7501 2019-07-11 stsp goto done;
3851 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3853 818c7501 2019-07-11 stsp goto done;
3854 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
3856 818c7501 2019-07-11 stsp goto done;
3858 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
3861 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
3863 818c7501 2019-07-11 stsp goto done;
3864 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
3865 818c7501 2019-07-11 stsp free(stored_id);
3866 818c7501 2019-07-11 stsp if (cmp != 0) {
3867 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3868 818c7501 2019-07-11 stsp goto done;
3872 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
3873 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
3874 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
3875 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
3876 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
3877 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
3879 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3880 818c7501 2019-07-11 stsp free(fileindex_path);
3881 818c7501 2019-07-11 stsp if (commit_ref)
3882 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3883 818c7501 2019-07-11 stsp return err;
3886 818c7501 2019-07-11 stsp const struct got_error *
3887 818c7501 2019-07-11 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3888 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
3889 01757395 2019-07-12 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
3890 818c7501 2019-07-11 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
3892 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
3893 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3894 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3895 a0e95631 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3896 a0e95631 2019-07-12 stsp char *fileindex_path = NULL, *commit_ref_name = NULL;
3897 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
3898 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3899 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3900 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
3902 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3903 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
3905 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
3907 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3909 a0e95631 2019-07-12 stsp return err;
3910 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3912 818c7501 2019-07-11 stsp goto done;
3913 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
3915 818c7501 2019-07-11 stsp goto done;
3916 818c7501 2019-07-11 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3917 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3918 818c7501 2019-07-11 stsp goto done;
3921 a0e95631 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3923 818c7501 2019-07-11 stsp goto done;
3925 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3926 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
3927 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
3929 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
3930 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
3931 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
3932 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
3934 01757395 2019-07-12 stsp if (merged_paths) {
3935 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
3936 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
3937 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
3938 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3940 01757395 2019-07-12 stsp goto done;
3943 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
3944 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
3946 01757395 2019-07-12 stsp goto done;
3949 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3950 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
3951 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
3953 a0e95631 2019-07-12 stsp goto done;
3954 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3955 a0e95631 2019-07-12 stsp goto done;
3958 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3960 edd02c5e 2019-07-11 stsp goto done;
3962 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3964 818c7501 2019-07-11 stsp goto done;
3966 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
3967 a0e95631 2019-07-12 stsp worktree, NULL, got_object_commit_get_author(orig_commit),
3968 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
3969 a0e95631 2019-07-12 stsp collect_rebase_commit_msg, orig_commit,
3970 a0e95631 2019-07-12 stsp rebase_status, NULL, repo);
3972 a0e95631 2019-07-12 stsp goto done;
3974 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
3976 a0e95631 2019-07-12 stsp goto done;
3978 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
3980 a0e95631 2019-07-12 stsp goto done;
3982 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3983 93ec1b5f 2019-07-12 stsp fileindex);
3984 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3985 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
3986 a0e95631 2019-07-12 stsp err = sync_err;
3988 a0e95631 2019-07-12 stsp if (fileindex)
3989 a0e95631 2019-07-12 stsp got_fileindex_free(fileindex);
3990 a0e95631 2019-07-12 stsp free(fileindex_path);
3991 818c7501 2019-07-11 stsp free(commit_ref_name);
3992 818c7501 2019-07-11 stsp if (commit_ref)
3993 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3994 a0e95631 2019-07-12 stsp free(head_commit_id);
3995 a0e95631 2019-07-12 stsp if (head_ref)
3996 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
3997 818c7501 2019-07-11 stsp if (err) {
3998 818c7501 2019-07-11 stsp free(*new_commit_id);
3999 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4001 818c7501 2019-07-11 stsp return err;
4004 818c7501 2019-07-11 stsp const struct got_error *
4005 818c7501 2019-07-11 stsp got_worktree_rebase_postpone(struct got_worktree *worktree)
4007 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4010 69844fba 2019-07-11 stsp static const struct got_error *
4011 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4013 69844fba 2019-07-11 stsp const struct got_error *err;
4014 69844fba 2019-07-11 stsp struct got_reference *ref;
4016 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4017 69844fba 2019-07-11 stsp if (err) {
4018 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4019 69844fba 2019-07-11 stsp return NULL;
4020 69844fba 2019-07-11 stsp return err;
4023 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4024 69844fba 2019-07-11 stsp got_ref_close(ref);
4025 69844fba 2019-07-11 stsp return err;
4028 69844fba 2019-07-11 stsp static const struct got_error *
4029 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4031 69844fba 2019-07-11 stsp const struct got_error *err;
4032 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4033 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4035 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4037 69844fba 2019-07-11 stsp goto done;
4038 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4040 69844fba 2019-07-11 stsp goto done;
4042 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4044 69844fba 2019-07-11 stsp goto done;
4045 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4047 69844fba 2019-07-11 stsp goto done;
4049 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4051 69844fba 2019-07-11 stsp goto done;
4052 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4054 69844fba 2019-07-11 stsp goto done;
4056 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4058 69844fba 2019-07-11 stsp goto done;
4059 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4061 69844fba 2019-07-11 stsp goto done;
4064 69844fba 2019-07-11 stsp free(tmp_branch_name);
4065 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4066 69844fba 2019-07-11 stsp free(branch_ref_name);
4067 69844fba 2019-07-11 stsp free(commit_ref_name);
4068 69844fba 2019-07-11 stsp return err;
4071 818c7501 2019-07-11 stsp const struct got_error *
4072 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4073 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
4074 818c7501 2019-07-11 stsp struct got_reference *rebased_branch,
4075 818c7501 2019-07-11 stsp struct got_repository *repo)
4077 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4078 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4080 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4082 818c7501 2019-07-11 stsp return err;
4084 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4086 818c7501 2019-07-11 stsp goto done;
4088 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4090 818c7501 2019-07-11 stsp goto done;
4092 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4094 818c7501 2019-07-11 stsp goto done;
4096 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4098 818c7501 2019-07-11 stsp free(new_head_commit_id);
4099 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4100 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4101 818c7501 2019-07-11 stsp err = unlockerr;
4102 818c7501 2019-07-11 stsp return err;
4105 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
4106 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
4107 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4110 818c7501 2019-07-11 stsp static const struct got_error *
4111 818c7501 2019-07-11 stsp collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4112 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
4114 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
4115 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4116 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
4117 818c7501 2019-07-11 stsp char *path = NULL;
4119 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
4120 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
4121 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
4122 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
4123 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
4124 818c7501 2019-07-11 stsp return NULL;
4126 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4127 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4129 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4130 818c7501 2019-07-11 stsp if (err || new == NULL)
4131 818c7501 2019-07-11 stsp free(path);
4132 818c7501 2019-07-11 stsp return err;
4135 818c7501 2019-07-11 stsp const struct got_error *
4136 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4137 818c7501 2019-07-11 stsp struct got_repository *repo, struct got_reference *new_base_branch,
4138 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4140 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4141 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4142 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4143 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
4144 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4145 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
4146 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
4147 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
4148 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4150 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
4152 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4154 818c7501 2019-07-11 stsp return err;
4156 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4157 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4159 818c7501 2019-07-11 stsp goto done;
4161 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4163 818c7501 2019-07-11 stsp goto done;
4166 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4167 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4168 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4170 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4172 818c7501 2019-07-11 stsp goto done;
4174 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4176 818c7501 2019-07-11 stsp goto done;
4178 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4179 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4181 ca355955 2019-07-12 stsp goto done;
4183 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4185 ca355955 2019-07-12 stsp goto done;
4187 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4189 818c7501 2019-07-11 stsp goto done;
4191 347d1d3e 2019-07-12 stsp crp_arg.revertible_paths = &revertible_paths;
4192 347d1d3e 2019-07-12 stsp crp_arg.worktree = worktree;
4193 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4194 347d1d3e 2019-07-12 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4196 55bd499d 2019-07-12 stsp goto done;
4198 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4199 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
4200 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
4202 ca355955 2019-07-12 stsp goto sync;
4205 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4206 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4208 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4209 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4210 ca355955 2019-07-12 stsp err = sync_err;
4212 818c7501 2019-07-11 stsp got_ref_close(resolved);
4213 a3a2faf2 2019-07-12 stsp free(tree_id);
4214 818c7501 2019-07-11 stsp free(commit_id);
4215 5ade8233 2019-07-12 stsp if (fileindex)
4216 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
4217 818c7501 2019-07-11 stsp free(fileindex_path);
4218 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4219 818c7501 2019-07-11 stsp free((char *)pe->path);
4220 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
4222 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4223 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4224 818c7501 2019-07-11 stsp err = unlockerr;
4225 818c7501 2019-07-11 stsp return err;