2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
121 09fe317a 2018-03-11 stsp struct stat sb;
123 09fe317a 2018-03-11 stsp *content = NULL;
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
145 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
175 09fe317a 2018-03-11 stsp return err;
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
198 86c3caaf 2018-03-09 stsp const struct got_error *
199 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
200 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
202 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
203 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
204 ec22038e 2019-03-10 stsp uuid_t uuid;
205 ec22038e 2019-03-10 stsp uint32_t uuid_status;
206 65596e15 2018-12-24 stsp int obj_type;
207 7ac97322 2018-03-11 stsp char *path_got = NULL;
208 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
361 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
366 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
367 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
368 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
371 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
374 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
378 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
379 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
383 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
388 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
389 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
393 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
398 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
402 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
406 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 eaccb85f 2018-12-25 stsp base_commit_id_str);
411 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
415 eaccb85f 2018-12-25 stsp got_repo_close(repo);
416 7ac97322 2018-03-11 stsp free(path_got);
417 056e7441 2018-03-11 stsp free(path_lock);
418 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
419 c442a90d 2019-03-10 stsp free(uuidstr);
420 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
425 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
426 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp return err;
433 247140b2 2019-02-05 stsp const struct got_error *
434 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
439 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
440 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 247140b2 2019-02-05 stsp return err;
442 247140b2 2019-02-05 stsp if (*worktree)
443 247140b2 2019-02-05 stsp return NULL;
444 247140b2 2019-02-05 stsp path = dirname(path);
445 d1542a27 2019-02-05 stsp if (path == NULL)
446 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
447 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
452 3a6ce05a 2019-02-11 stsp const struct got_error *
453 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
456 c88eb298 2018-03-11 stsp free(worktree->root_path);
457 cde76477 2018-03-11 stsp free(worktree->repo_path);
458 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
459 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
460 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
461 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
462 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
463 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
464 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
465 6d9d28c3 2018-03-11 stsp free(worktree);
466 3a6ce05a 2019-02-11 stsp return err;
469 2fbdb5ae 2018-12-29 stsp const char *
470 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp return worktree->root_path;
475 c7f4312f 2019-02-05 stsp const char *
476 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
481 49520a32 2018-12-29 stsp const char *
482 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 f609be2e 2018-12-29 stsp return worktree->path_prefix;
487 e5dc7198 2018-12-29 stsp const struct got_error *
488 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
494 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
498 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
499 e5dc7198 2018-12-29 stsp free(absprefix);
500 e5dc7198 2018-12-29 stsp return NULL;
503 bc70eb79 2019-05-09 stsp const char *
504 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 36a38700 2019-05-10 stsp return worktree->head_ref_name;
509 024e9686 2019-05-14 stsp const struct got_error *
510 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
511 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
514 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
518 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
519 024e9686 2019-05-14 stsp path_got = NULL;
523 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
524 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
525 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
529 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
533 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
534 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp free(head_ref_name);
539 024e9686 2019-05-14 stsp return err;
542 b72f483a 2019-02-05 stsp struct got_object_id *
543 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
548 507dc3bb 2018-12-29 stsp const struct got_error *
549 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp const struct got_error *err;
553 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
554 507dc3bb 2018-12-29 stsp char *id_str = NULL;
555 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
560 507dc3bb 2018-12-29 stsp path_got = NULL;
564 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
573 507dc3bb 2018-12-29 stsp /* Record our base commit. */
574 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
577 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
582 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
583 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
589 507dc3bb 2018-12-29 stsp got_object_close(obj);
590 507dc3bb 2018-12-29 stsp free(id_str);
591 507dc3bb 2018-12-29 stsp free(path_got);
592 507dc3bb 2018-12-29 stsp return err;
595 9d31a1d8 2018-03-11 stsp static const struct got_error *
596 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
601 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
602 86c3caaf 2018-03-09 stsp return NULL;
605 21908da4 2019-01-13 stsp static const struct got_error *
606 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
609 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
615 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 ddcd8544 2019-03-11 stsp struct stat sb;
617 ddcd8544 2019-03-11 stsp err = NULL;
618 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
619 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
620 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
621 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
622 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 4a1ddfc2 2019-01-12 stsp free(abspath);
626 68c76935 2019-02-19 stsp return err;
629 68c76935 2019-02-19 stsp static const struct got_error *
630 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
633 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
634 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
635 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
640 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
642 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
645 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
647 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
650 68c76935 2019-02-19 stsp if (flen1 == 0) {
651 68c76935 2019-02-19 stsp if (flen2 != 0)
654 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
655 68c76935 2019-02-19 stsp if (flen1 != 0)
658 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
659 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
669 68c76935 2019-02-19 stsp return err;
672 68c76935 2019-02-19 stsp static const struct got_error *
673 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
676 68c76935 2019-02-19 stsp struct stat sb;
677 68c76935 2019-02-19 stsp size_t size1, size2;
678 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
682 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
686 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
692 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp return NULL;
699 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
700 68c76935 2019-02-19 stsp if (f1 == NULL)
701 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
704 68c76935 2019-02-19 stsp if (f2 == NULL) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
709 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
712 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
713 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 6353ad76 2019-02-08 stsp return err;
720 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
722 234035bc 2019-06-01 stsp * acts as the second derived version.
724 6353ad76 2019-02-08 stsp static const struct got_error *
725 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
727 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
729 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 234035bc 2019-06-01 stsp void *progress_arg)
732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
733 6353ad76 2019-02-08 stsp int merged_fd = -1;
734 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
735 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
737 6353ad76 2019-02-08 stsp char *id_str = NULL;
738 818c7501 2019-07-11 stsp char *label_deriv = NULL;
739 234035bc 2019-06-01 stsp int overlapcnt = 0;
740 af54ae4a 2019-02-19 stsp char *parent;
742 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
744 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
745 af54ae4a 2019-02-19 stsp if (parent == NULL)
746 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
748 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
751 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 af54ae4a 2019-02-19 stsp free(base_path);
756 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
758 af54ae4a 2019-02-19 stsp base_path = NULL;
762 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
765 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 6c4c42e0 2019-06-24 stsp blob_deriv);
770 af54ae4a 2019-02-19 stsp free(base_path);
771 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
773 af54ae4a 2019-02-19 stsp base_path = NULL;
777 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
780 46b6ee73 2019-06-04 stsp if (blob_orig) {
781 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 46b6ee73 2019-06-04 stsp blob_orig);
787 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
788 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
789 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
793 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
796 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
801 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
806 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
807 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
814 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
815 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
816 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
817 68c76935 2019-02-19 stsp merged_path);
822 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
823 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
827 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
828 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
829 230a42bd 2019-05-11 jcs ondisk_path);
830 2a57020b 2019-02-20 stsp unlink(merged_path);
835 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
836 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
837 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
838 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
839 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
840 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
841 6353ad76 2019-02-08 stsp free(merged_path);
842 af54ae4a 2019-02-19 stsp free(base_path);
843 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
844 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
845 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
847 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
848 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
849 46b6ee73 2019-06-04 stsp free(blob_orig_path);
851 6353ad76 2019-02-08 stsp free(id_str);
852 818c7501 2019-07-11 stsp free(label_deriv);
853 4a1ddfc2 2019-01-12 stsp return err;
856 4a1ddfc2 2019-01-12 stsp static const struct got_error *
857 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
858 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
859 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
860 13d9040b 2019-03-27 stsp int update_timestamps)
862 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
864 13d9040b 2019-03-27 stsp if (ie == NULL)
865 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
867 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
868 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
869 13d9040b 2019-03-27 stsp update_timestamps);
871 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
872 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
873 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
875 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
877 13d9040b 2019-03-27 stsp return err;
880 13d9040b 2019-03-27 stsp static const struct got_error *
881 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
882 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
883 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
884 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
885 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
887 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
888 507dc3bb 2018-12-29 stsp int fd = -1;
889 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
890 507dc3bb 2018-12-29 stsp int update = 0;
891 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
893 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
894 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
895 9d31a1d8 2018-03-11 stsp if (fd == -1) {
896 21908da4 2019-01-13 stsp if (errno == ENOENT) {
897 21908da4 2019-01-13 stsp char *parent = dirname(path);
898 21908da4 2019-01-13 stsp if (parent == NULL)
899 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
900 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
902 21908da4 2019-01-13 stsp return err;
903 21908da4 2019-01-13 stsp fd = open(ondisk_path,
904 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
905 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
906 21908da4 2019-01-13 stsp if (fd == -1)
907 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
908 230a42bd 2019-05-11 jcs ondisk_path);
909 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
910 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
911 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
912 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
915 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
916 507dc3bb 2018-12-29 stsp ondisk_path);
919 507dc3bb 2018-12-29 stsp update = 1;
922 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
925 a378724f 2019-02-10 stsp if (restoring_missing_file)
926 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
927 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
928 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
930 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
931 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
933 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
935 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
936 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
939 9d31a1d8 2018-03-11 stsp if (len > 0) {
940 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
941 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
942 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
943 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
945 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
946 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
949 61d6eaa3 2018-12-24 stsp hdrlen = 0;
951 9d31a1d8 2018-03-11 stsp } while (len != 0);
953 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
954 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
958 507dc3bb 2018-12-29 stsp if (update) {
959 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
960 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
961 230a42bd 2019-05-11 jcs ondisk_path);
962 2a57020b 2019-02-20 stsp unlink(tmppath);
967 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
968 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
969 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
973 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
980 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
981 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
982 507dc3bb 2018-12-29 stsp free(tmppath);
983 6353ad76 2019-02-08 stsp return err;
986 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
987 6353ad76 2019-02-08 stsp static const struct got_error *
988 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
990 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
991 7154f6ce 2019-03-27 stsp const char *markers[3] = {
992 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
993 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
994 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
997 7154f6ce 2019-03-27 stsp char *line;
998 7154f6ce 2019-03-27 stsp size_t len;
999 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1001 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1002 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1003 7154f6ce 2019-03-27 stsp if (line == NULL) {
1004 7154f6ce 2019-03-27 stsp if (feof(f))
1006 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1010 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1011 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1013 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1019 7154f6ce 2019-03-27 stsp return err;
1022 7154f6ce 2019-03-27 stsp static const struct got_error *
1023 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1024 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1025 b8f41171 2019-02-10 stsp struct got_repository *repo)
1027 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1028 6353ad76 2019-02-08 stsp struct got_object_id id;
1029 6353ad76 2019-02-08 stsp size_t hdrlen;
1030 6353ad76 2019-02-08 stsp FILE *f = NULL;
1031 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1032 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1033 6353ad76 2019-02-08 stsp size_t flen, blen;
1035 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1037 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1038 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1040 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1041 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1043 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1044 b8f41171 2019-02-10 stsp sb->st_mode =
1045 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1046 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1048 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1049 a378724f 2019-02-10 stsp return NULL;
1051 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1054 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1055 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1056 6353ad76 2019-02-08 stsp return NULL;
1059 b8f41171 2019-02-10 stsp if (ie == NULL)
1060 d00136be 2019-03-26 stsp return NULL;
1062 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1063 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1064 2ec1f75b 2019-03-26 stsp return NULL;
1065 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1066 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1067 b8f41171 2019-02-10 stsp return NULL;
1070 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1071 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1072 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1073 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1074 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1075 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1076 6353ad76 2019-02-08 stsp return NULL;
1078 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1079 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1081 6353ad76 2019-02-08 stsp return err;
1083 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1084 6353ad76 2019-02-08 stsp if (f == NULL) {
1085 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1086 6353ad76 2019-02-08 stsp goto done;
1088 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1090 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1091 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1093 7154f6ce 2019-03-27 stsp goto done;
1094 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1095 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1096 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1097 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1098 7154f6ce 2019-03-27 stsp goto done;
1100 6353ad76 2019-02-08 stsp if (blen == 0) {
1101 6353ad76 2019-02-08 stsp if (flen != 0)
1102 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1104 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1105 6353ad76 2019-02-08 stsp if (blen != 0)
1106 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1108 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1109 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1110 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1111 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1115 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1118 6353ad76 2019-02-08 stsp hdrlen = 0;
1121 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1122 7154f6ce 2019-03-27 stsp rewind(f);
1123 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1127 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1129 6353ad76 2019-02-08 stsp fclose(f);
1130 9d31a1d8 2018-03-11 stsp return err;
1133 9d31a1d8 2018-03-11 stsp static const struct got_error *
1134 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1135 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1136 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1137 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1138 0584f854 2019-04-06 stsp void *progress_arg)
1140 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1141 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1142 6353ad76 2019-02-08 stsp char *ondisk_path;
1143 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1144 b8f41171 2019-02-10 stsp struct stat sb;
1146 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1147 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1149 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1151 b8f41171 2019-02-10 stsp goto done;
1153 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1154 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1155 b8f41171 2019-02-10 stsp goto done;
1158 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1159 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1160 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1161 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1162 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1164 6353ad76 2019-02-08 stsp goto done;
1166 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1167 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1168 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1169 b8f41171 2019-02-10 stsp goto done;
1172 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1174 6353ad76 2019-02-08 stsp goto done;
1176 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1177 234035bc 2019-06-01 stsp int update_timestamps;
1178 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1179 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1180 234035bc 2019-06-01 stsp struct got_object_id id2;
1181 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1182 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1184 234035bc 2019-06-01 stsp goto done;
1186 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1187 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1188 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1189 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1190 234035bc 2019-06-01 stsp if (blob2)
1191 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1193 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1194 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1195 234035bc 2019-06-01 stsp * unmodified files again.
1197 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1198 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1199 234035bc 2019-06-01 stsp update_timestamps);
1200 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1201 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1202 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1203 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1205 13d9040b 2019-03-27 stsp goto done;
1207 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1208 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1209 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1211 13d9040b 2019-03-27 stsp goto done;
1212 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1213 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1215 13d9040b 2019-03-27 stsp goto done;
1217 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1219 6353ad76 2019-02-08 stsp free(ondisk_path);
1220 8da9e5f4 2019-01-12 stsp return err;
1223 90285c3b 2019-01-08 stsp static const struct got_error *
1224 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1226 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1227 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1229 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1230 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1232 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1233 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1234 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1236 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1237 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1238 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1239 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1240 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1244 90285c3b 2019-01-08 stsp parent = dirname(parent);
1247 90285c3b 2019-01-08 stsp free(ondisk_path);
1248 90285c3b 2019-01-08 stsp return err;
1251 708d8e67 2019-03-27 stsp static const struct got_error *
1252 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1253 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1254 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1257 708d8e67 2019-03-27 stsp unsigned char status;
1258 708d8e67 2019-03-27 stsp struct stat sb;
1259 708d8e67 2019-03-27 stsp char *ondisk_path;
1261 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1263 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1265 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1267 708d8e67 2019-03-27 stsp return err;
1269 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1270 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1271 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1273 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1274 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1276 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1279 708d8e67 2019-03-27 stsp return err;
1281 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1282 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1283 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1285 fc6346c4 2019-03-27 stsp return err;
1287 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1290 fc6346c4 2019-03-27 stsp return err;
1293 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1294 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1295 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1296 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1297 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1298 8da9e5f4 2019-01-12 stsp void *progress_arg;
1299 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1300 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1303 512f0d0e 2019-01-02 stsp static const struct got_error *
1304 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1305 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1307 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1309 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1310 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1312 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1313 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1316 8da9e5f4 2019-01-12 stsp static const struct got_error *
1317 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1319 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1321 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1322 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1324 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1325 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1328 9d31a1d8 2018-03-11 stsp static const struct got_error *
1329 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1331 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1332 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1333 8da9e5f4 2019-01-12 stsp char *path;
1335 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1336 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1338 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1339 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1341 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1343 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1344 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1346 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1347 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1349 8da9e5f4 2019-01-12 stsp free(path);
1350 0cd1c46a 2019-03-11 stsp return err;
1353 818c7501 2019-07-11 stsp static const struct got_error *
1354 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1356 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1357 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1358 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1360 517bab73 2019-03-11 stsp *refname = NULL;
1362 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1363 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1364 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1366 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1368 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1369 0647c563 2019-03-11 stsp *refname = NULL;
1371 517bab73 2019-03-11 stsp free(uuidstr);
1372 517bab73 2019-03-11 stsp return err;
1375 818c7501 2019-07-11 stsp const struct got_error *
1376 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1378 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1381 818c7501 2019-07-11 stsp static const struct got_error *
1382 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1384 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1385 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1388 818c7501 2019-07-11 stsp static const struct got_error *
1389 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1391 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1394 818c7501 2019-07-11 stsp static const struct got_error *
1395 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1397 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1398 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1401 818c7501 2019-07-11 stsp static const struct got_error *
1402 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1404 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1405 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1410 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1411 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1413 517bab73 2019-03-11 stsp static const struct got_error *
1414 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1416 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1417 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1418 517bab73 2019-03-11 stsp char *refname;
1420 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1422 517bab73 2019-03-11 stsp return err;
1424 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1426 0cd1c46a 2019-03-11 stsp goto done;
1428 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1430 0cd1c46a 2019-03-11 stsp free(refname);
1432 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1433 9d31a1d8 2018-03-11 stsp return err;
1436 ebf99748 2019-05-09 stsp static const struct got_error *
1437 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1438 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1440 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1441 ebf99748 2019-05-09 stsp FILE *index = NULL;
1443 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1444 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1445 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1446 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1448 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1449 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1450 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1451 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1452 ebf99748 2019-05-09 stsp goto done;
1455 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1456 ebf99748 2019-05-09 stsp if (index == NULL) {
1457 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1458 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1460 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1461 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1462 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1465 ebf99748 2019-05-09 stsp if (err) {
1466 ebf99748 2019-05-09 stsp free(*fileindex_path);
1467 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1468 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1469 ebf99748 2019-05-09 stsp *fileindex = NULL;
1471 ebf99748 2019-05-09 stsp return err;
1474 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1475 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1476 c932eeeb 2019-05-22 stsp const char *path;
1477 c932eeeb 2019-05-22 stsp size_t path_len;
1478 c932eeeb 2019-05-22 stsp const char *entry_name;
1479 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1480 a484d721 2019-06-10 stsp void *progress_arg;
1483 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1484 c932eeeb 2019-05-22 stsp static const struct got_error *
1485 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1487 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1489 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1490 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1491 c932eeeb 2019-05-22 stsp return NULL;
1492 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1493 102ff934 2019-06-11 stsp return NULL;
1495 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1496 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1497 c932eeeb 2019-05-22 stsp return NULL;
1499 edd02c5e 2019-07-11 stsp if (a->progress_cb)
1500 edd02c5e 2019-07-11 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1501 edd02c5e 2019-07-11 stsp ie->path);
1502 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1503 c932eeeb 2019-05-22 stsp return NULL;
1506 9c6338c4 2019-06-02 stsp static const struct got_error *
1507 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1509 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1510 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1511 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1513 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1514 9c6338c4 2019-06-02 stsp fileindex_path);
1516 9c6338c4 2019-06-02 stsp goto done;
1518 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1520 9c6338c4 2019-06-02 stsp goto done;
1522 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1523 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1524 9c6338c4 2019-06-02 stsp fileindex_path);
1525 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1528 9c6338c4 2019-06-02 stsp if (new_index)
1529 9c6338c4 2019-06-02 stsp fclose(new_index);
1530 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1531 9c6338c4 2019-06-02 stsp return err;
1534 9d31a1d8 2018-03-11 stsp const struct got_error *
1535 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1536 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1537 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1539 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1540 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1541 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1542 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1543 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1544 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1545 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1546 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1547 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1548 af12c6b9 2019-06-04 stsp struct bump_base_commit_id_arg bbc_arg;
1550 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1552 9d31a1d8 2018-03-11 stsp return err;
1555 51514078 2018-12-25 stsp * Read the file index.
1556 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1557 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1559 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1561 ebf99748 2019-05-09 stsp goto done;
1563 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1565 0cd1c46a 2019-03-11 stsp goto done;
1567 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1568 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1570 9d31a1d8 2018-03-11 stsp goto done;
1572 c4cdcb68 2019-04-03 stsp if (path[0]) {
1573 c4cdcb68 2019-04-03 stsp char *tree_path;
1574 c4cdcb68 2019-04-03 stsp int obj_type;
1575 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1576 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1577 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1578 c4cdcb68 2019-04-03 stsp goto done;
1580 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1581 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1582 c4cdcb68 2019-04-03 stsp path) == -1) {
1583 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1584 c4cdcb68 2019-04-03 stsp goto done;
1586 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1587 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1588 c4cdcb68 2019-04-03 stsp free(tree_path);
1590 c4cdcb68 2019-04-03 stsp goto done;
1591 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1593 c4cdcb68 2019-04-03 stsp goto done;
1594 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1595 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1596 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1597 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1598 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1599 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1600 c4cdcb68 2019-04-03 stsp goto done;
1602 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1603 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1604 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1605 c4cdcb68 2019-04-03 stsp goto done;
1608 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1610 c4cdcb68 2019-04-03 stsp goto done;
1611 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1612 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1613 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1614 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1615 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1616 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1617 c4cdcb68 2019-04-03 stsp goto done;
1620 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1621 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1622 c4cdcb68 2019-04-03 stsp free(tree_path);
1624 c4cdcb68 2019-04-03 stsp goto done;
1625 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1626 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1627 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1628 c4cdcb68 2019-04-03 stsp goto done;
1632 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1633 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1634 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1635 c4cdcb68 2019-04-03 stsp goto done;
1637 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1638 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1640 c4cdcb68 2019-04-03 stsp goto done;
1643 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1645 c4cdcb68 2019-04-03 stsp goto done;
1647 c4cdcb68 2019-04-03 stsp if (entry_name &&
1648 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1649 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1650 8da9e5f4 2019-01-12 stsp goto done;
1653 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1654 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1655 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1656 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1657 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1658 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1659 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1660 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1661 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1662 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1663 af12c6b9 2019-06-04 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1664 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1666 af12c6b9 2019-06-04 stsp goto sync;
1668 af12c6b9 2019-06-04 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1669 af12c6b9 2019-06-04 stsp bbc_arg.entry_name = entry_name;
1670 af12c6b9 2019-06-04 stsp bbc_arg.path = path;
1671 af12c6b9 2019-06-04 stsp bbc_arg.path_len = strlen(path);
1672 a484d721 2019-06-10 stsp bbc_arg.progress_cb = progress_cb;
1673 a484d721 2019-06-10 stsp bbc_arg.progress_arg = progress_arg;
1674 af12c6b9 2019-06-04 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1675 af12c6b9 2019-06-04 stsp bump_base_commit_id, &bbc_arg);
1677 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1678 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1679 af12c6b9 2019-06-04 stsp err = sync_err;
1681 9c6338c4 2019-06-02 stsp free(fileindex_path);
1682 c4cdcb68 2019-04-03 stsp free(relpath);
1684 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1685 9d31a1d8 2018-03-11 stsp if (commit)
1686 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1687 5ade8233 2019-07-12 stsp if (fileindex)
1688 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1689 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1690 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1691 9d31a1d8 2018-03-11 stsp err = unlockerr;
1692 f8d1f275 2019-02-04 stsp return err;
1695 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1696 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1697 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1698 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1699 234035bc 2019-06-01 stsp void *progress_arg;
1700 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1701 234035bc 2019-06-01 stsp void *cancel_arg;
1702 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1705 234035bc 2019-06-01 stsp static const struct got_error *
1706 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1707 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1708 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1709 234035bc 2019-06-01 stsp struct got_repository *repo)
1711 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1712 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1713 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1714 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1715 234035bc 2019-06-01 stsp struct stat sb;
1716 234035bc 2019-06-01 stsp unsigned char status;
1717 234035bc 2019-06-01 stsp int local_changes_subsumed;
1719 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1720 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1721 234035bc 2019-06-01 stsp if (ie == NULL) {
1722 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1724 234035bc 2019-06-01 stsp return NULL;
1727 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1728 234035bc 2019-06-01 stsp path2) == -1)
1729 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1731 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1733 234035bc 2019-06-01 stsp goto done;
1735 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1736 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1738 234035bc 2019-06-01 stsp goto done;
1740 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1741 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1742 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1743 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1744 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status, path2);
1745 234035bc 2019-06-01 stsp goto done;
1748 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1749 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1750 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1751 234035bc 2019-06-01 stsp } else if (blob1) {
1752 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1753 234035bc 2019-06-01 stsp if (ie == NULL) {
1754 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1756 234035bc 2019-06-01 stsp return NULL;
1759 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1760 2b92fad7 2019-06-02 stsp path1) == -1)
1761 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1763 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1765 2b92fad7 2019-06-02 stsp goto done;
1767 2b92fad7 2019-06-02 stsp switch (status) {
1768 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1769 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1771 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1773 2b92fad7 2019-06-02 stsp goto done;
1775 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1777 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1778 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1779 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1782 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1784 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1785 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1786 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1787 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg,
1788 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1790 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1791 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, status, path1);
1796 234035bc 2019-06-01 stsp } else if (blob2) {
1797 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1798 234035bc 2019-06-01 stsp path2) == -1)
1799 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1800 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1802 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1805 234035bc 2019-06-01 stsp goto done;
1806 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1807 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1808 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1809 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1810 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status,
1812 234035bc 2019-06-01 stsp goto done;
1814 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1815 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
1816 818c7501 2019-07-11 stsp a->commit_id2, repo,
1817 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1818 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1819 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1820 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1821 234035bc 2019-06-01 stsp blob2, 0);
1823 234035bc 2019-06-01 stsp goto done;
1826 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1827 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1828 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1829 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1830 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1831 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1833 234035bc 2019-06-01 stsp goto done;
1834 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1835 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1837 234035bc 2019-06-01 stsp goto done;
1838 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1839 2b92fad7 2019-06-02 stsp if (err) {
1840 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1841 2b92fad7 2019-06-02 stsp goto done;
1846 234035bc 2019-06-01 stsp free(ondisk_path);
1847 234035bc 2019-06-01 stsp return err;
1850 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1851 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1852 234035bc 2019-06-01 stsp struct got_repository *repo;
1855 234035bc 2019-06-01 stsp static const struct got_error *
1856 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1858 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1859 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1860 234035bc 2019-06-01 stsp unsigned char status;
1861 234035bc 2019-06-01 stsp struct stat sb;
1862 234035bc 2019-06-01 stsp char *ondisk_path;
1864 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1865 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1866 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1867 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1869 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1871 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1873 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1874 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1876 234035bc 2019-06-01 stsp return err;
1877 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1878 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1880 234035bc 2019-06-01 stsp return NULL;
1883 818c7501 2019-07-11 stsp static const struct got_error *
1884 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1885 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
1886 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
1887 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1888 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1890 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
1891 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1892 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1893 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
1895 03415a1a 2019-06-02 stsp if (commit_id1) {
1896 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1897 03415a1a 2019-06-02 stsp worktree->path_prefix);
1899 03415a1a 2019-06-02 stsp goto done;
1901 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1903 03415a1a 2019-06-02 stsp goto done;
1906 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1907 234035bc 2019-06-01 stsp worktree->path_prefix);
1909 234035bc 2019-06-01 stsp goto done;
1911 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1913 234035bc 2019-06-01 stsp goto done;
1915 234035bc 2019-06-01 stsp arg.worktree = worktree;
1916 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
1917 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
1918 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
1919 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
1920 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
1921 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
1922 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1923 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1924 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1925 af12c6b9 2019-06-04 stsp err = sync_err;
1927 234035bc 2019-06-01 stsp if (tree1)
1928 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
1929 234035bc 2019-06-01 stsp if (tree2)
1930 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
1931 818c7501 2019-07-11 stsp return err;
1934 818c7501 2019-07-11 stsp const struct got_error *
1935 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
1936 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1937 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1938 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1940 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
1941 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
1942 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
1943 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
1945 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
1947 818c7501 2019-07-11 stsp return err;
1949 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1951 818c7501 2019-07-11 stsp goto done;
1953 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
1954 818c7501 2019-07-11 stsp mok_arg.repo = repo;
1955 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1956 818c7501 2019-07-11 stsp &mok_arg);
1958 818c7501 2019-07-11 stsp goto done;
1960 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
1961 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1963 818c7501 2019-07-11 stsp if (fileindex)
1964 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
1965 818c7501 2019-07-11 stsp free(fileindex_path);
1966 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1967 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
1968 234035bc 2019-06-01 stsp err = unlockerr;
1969 234035bc 2019-06-01 stsp return err;
1972 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1973 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1974 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1975 927df6b7 2019-02-10 stsp const char *status_path;
1976 927df6b7 2019-02-10 stsp size_t status_path_len;
1977 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1978 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1979 f8d1f275 2019-02-04 stsp void *status_arg;
1980 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1981 f8d1f275 2019-02-04 stsp void *cancel_arg;
1984 f8d1f275 2019-02-04 stsp static const struct got_error *
1985 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1986 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1987 927df6b7 2019-02-10 stsp struct got_repository *repo)
1989 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1990 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1991 927df6b7 2019-02-10 stsp struct stat sb;
1992 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1994 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1995 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1996 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1997 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1998 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1999 016a88dd 2019-05-13 stsp &commit_id);
2001 927df6b7 2019-02-10 stsp return err;
2004 927df6b7 2019-02-10 stsp static const struct got_error *
2005 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2006 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2008 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2009 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2010 f8d1f275 2019-02-04 stsp char *abspath;
2012 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2013 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2015 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
2016 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2017 927df6b7 2019-02-10 stsp return NULL;
2019 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2020 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2021 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2022 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2024 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2025 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2026 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2029 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2031 f8d1f275 2019-02-04 stsp free(abspath);
2032 9d31a1d8 2018-03-11 stsp return err;
2035 f8d1f275 2019-02-04 stsp static const struct got_error *
2036 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2038 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2039 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2040 2ec1f75b 2019-03-26 stsp unsigned char status;
2042 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2043 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2045 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2046 927df6b7 2019-02-10 stsp return NULL;
2048 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2049 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2050 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2051 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2053 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2054 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2055 016a88dd 2019-05-13 stsp &commit_id);
2058 f8d1f275 2019-02-04 stsp static const struct got_error *
2059 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2061 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2062 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2063 f8d1f275 2019-02-04 stsp char *path = NULL;
2065 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2066 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2068 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2069 2c201a36 2019-02-10 stsp return NULL;
2071 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2072 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2073 f8d1f275 2019-02-04 stsp return NULL;
2075 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2076 927df6b7 2019-02-10 stsp return NULL;
2078 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2079 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2080 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2082 f8d1f275 2019-02-04 stsp path = de->d_name;
2085 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2086 016a88dd 2019-05-13 stsp NULL, NULL);
2087 f8d1f275 2019-02-04 stsp if (parent_path[0])
2088 f8d1f275 2019-02-04 stsp free(path);
2089 b72f483a 2019-02-05 stsp return err;
2092 f8d1f275 2019-02-04 stsp const struct got_error *
2093 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2094 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2095 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2097 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2098 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2099 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
2100 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
2101 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2102 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2103 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2105 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2107 3605a814 2019-07-12 stsp return err;
2109 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2110 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2111 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2112 c513d110 2019-02-05 stsp goto done;
2114 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2115 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2116 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2117 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2118 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2119 927df6b7 2019-02-10 stsp if (ie == NULL) {
2120 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2121 927df6b7 2019-02-10 stsp goto done;
2123 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2124 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2125 927df6b7 2019-02-10 stsp goto done;
2127 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2128 927df6b7 2019-02-10 stsp goto done;
2131 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2132 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2133 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2134 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2135 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2136 927df6b7 2019-02-10 stsp arg.status_path = path;
2137 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2138 f8d1f275 2019-02-04 stsp arg.repo = repo;
2139 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2140 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2141 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2142 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2143 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2144 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2146 f8d1f275 2019-02-04 stsp if (workdir)
2147 f8d1f275 2019-02-04 stsp closedir(workdir);
2148 927df6b7 2019-02-10 stsp free(ondisk_path);
2149 f8d1f275 2019-02-04 stsp free(fileindex_path);
2150 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2151 f8d1f275 2019-02-04 stsp return err;
2154 6c7ab921 2019-03-18 stsp const struct got_error *
2155 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2156 6c7ab921 2019-03-18 stsp const char *arg)
2158 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2159 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
2160 6c7ab921 2019-03-18 stsp size_t len;
2162 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2164 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2165 6c7ab921 2019-03-18 stsp if (resolved == NULL)
2166 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
2168 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2169 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2170 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2171 6c7ab921 2019-03-18 stsp goto done;
2174 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2175 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2176 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2178 f6d88e1a 2019-05-29 stsp goto done;
2180 f6d88e1a 2019-05-29 stsp path = strdup("");
2181 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2182 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2183 f6d88e1a 2019-05-29 stsp goto done;
2187 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2188 6c7ab921 2019-03-18 stsp len = strlen(path);
2189 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2190 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2194 6c7ab921 2019-03-18 stsp free(resolved);
2195 6c7ab921 2019-03-18 stsp if (err == NULL)
2196 6c7ab921 2019-03-18 stsp *wt_path = path;
2198 6c7ab921 2019-03-18 stsp free(path);
2199 d00136be 2019-03-26 stsp return err;
2202 07f5b47a 2019-06-02 stsp static const struct got_error *
2203 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2204 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2205 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2207 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2208 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2210 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2211 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2212 07f5b47a 2019-06-02 stsp return NULL;
2214 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2216 07f5b47a 2019-06-02 stsp return err;
2218 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2219 07f5b47a 2019-06-02 stsp if (err) {
2220 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2221 07f5b47a 2019-06-02 stsp return err;
2224 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2227 d00136be 2019-03-26 stsp const struct got_error *
2228 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2229 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2230 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2231 031a5338 2019-03-26 stsp struct got_repository *repo)
2233 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2234 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2235 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2236 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2238 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2240 d00136be 2019-03-26 stsp return err;
2242 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2244 d00136be 2019-03-26 stsp goto done;
2246 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2247 1dd54920 2019-05-11 stsp char *relpath;
2248 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2249 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2252 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2253 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2254 1dd54920 2019-05-11 stsp free(relpath);
2258 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2259 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2260 af12c6b9 2019-06-04 stsp err = sync_err;
2262 fb399478 2019-07-12 stsp free(fileindex_path);
2263 d00136be 2019-03-26 stsp if (fileindex)
2264 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2265 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2266 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2267 d00136be 2019-03-26 stsp err = unlockerr;
2268 6c7ab921 2019-03-18 stsp return err;
2271 17ed4618 2019-06-02 stsp static const struct got_error *
2272 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2273 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2274 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2275 17ed4618 2019-06-02 stsp struct got_repository *repo)
2277 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2278 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2279 17ed4618 2019-06-02 stsp unsigned char status;
2280 17ed4618 2019-06-02 stsp struct stat sb;
2282 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2283 17ed4618 2019-06-02 stsp if (ie == NULL)
2284 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2286 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2288 17ed4618 2019-06-02 stsp return err;
2290 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2291 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2292 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2293 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2294 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2295 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2296 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2299 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2300 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2302 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2303 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2306 2ec1f75b 2019-03-26 stsp const struct got_error *
2307 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2308 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2309 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2310 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2312 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2313 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2314 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2315 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2317 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2319 2ec1f75b 2019-03-26 stsp return err;
2321 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2323 2ec1f75b 2019-03-26 stsp goto done;
2325 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2326 17ed4618 2019-06-02 stsp char *relpath;
2327 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2328 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2331 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2332 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2333 17ed4618 2019-06-02 stsp free(relpath);
2337 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2338 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2339 af12c6b9 2019-06-04 stsp err = sync_err;
2341 fb399478 2019-07-12 stsp free(fileindex_path);
2342 2ec1f75b 2019-03-26 stsp if (fileindex)
2343 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2344 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2345 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2346 2ec1f75b 2019-03-26 stsp err = unlockerr;
2347 2ec1f75b 2019-03-26 stsp return err;
2350 e20a8b6f 2019-06-04 stsp static const struct got_error *
2351 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2352 a129376b 2019-03-28 stsp const char *ondisk_path,
2353 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2354 a129376b 2019-03-28 stsp struct got_repository *repo)
2356 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2357 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2358 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2359 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2360 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2361 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2362 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2363 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2364 a129376b 2019-03-28 stsp unsigned char status;
2365 a129376b 2019-03-28 stsp struct stat sb;
2367 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2368 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2370 a129376b 2019-03-28 stsp goto done;
2372 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2373 a129376b 2019-03-28 stsp if (ie == NULL) {
2374 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2375 a129376b 2019-03-28 stsp goto done;
2378 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2379 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2380 a129376b 2019-03-28 stsp if (err) {
2381 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2382 a129376b 2019-03-28 stsp goto done;
2383 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2384 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2385 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2386 e20a8b6f 2019-06-04 stsp goto done;
2389 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2390 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2391 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2392 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2393 a129376b 2019-03-28 stsp goto done;
2396 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2397 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2398 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2399 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2400 a129376b 2019-03-28 stsp goto done;
2403 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2404 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2405 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2406 a129376b 2019-03-28 stsp goto done;
2411 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2412 a129376b 2019-03-28 stsp tree_path);
2414 a129376b 2019-03-28 stsp goto done;
2416 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2418 a129376b 2019-03-28 stsp goto done;
2420 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2421 a129376b 2019-03-28 stsp if (te_name == NULL) {
2422 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2423 a129376b 2019-03-28 stsp goto done;
2426 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2428 a129376b 2019-03-28 stsp goto done;
2430 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2431 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2432 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2433 a129376b 2019-03-28 stsp goto done;
2436 a129376b 2019-03-28 stsp switch (status) {
2437 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2438 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2439 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2441 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2442 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2443 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2444 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2445 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2446 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2447 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2449 a129376b 2019-03-28 stsp goto done;
2450 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2451 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2452 a129376b 2019-03-28 stsp progress_arg);
2454 a129376b 2019-03-28 stsp goto done;
2455 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2456 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2457 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2459 a129376b 2019-03-28 stsp goto done;
2464 a129376b 2019-03-28 stsp goto done;
2467 a129376b 2019-03-28 stsp free(relpath);
2468 e20a8b6f 2019-06-04 stsp free(parent_path);
2469 a129376b 2019-03-28 stsp free(tree_path);
2471 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2473 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2474 a129376b 2019-03-28 stsp free(tree_id);
2475 e20a8b6f 2019-06-04 stsp return err;
2478 e20a8b6f 2019-06-04 stsp const struct got_error *
2479 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2480 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2481 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2482 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2484 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2485 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2486 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2487 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2488 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2490 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2492 e20a8b6f 2019-06-04 stsp return err;
2494 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2496 e20a8b6f 2019-06-04 stsp goto done;
2498 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2499 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2500 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2504 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2505 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2506 e20a8b6f 2019-06-04 stsp err = sync_err;
2508 fb399478 2019-07-12 stsp free(fileindex_path);
2509 a129376b 2019-03-28 stsp if (fileindex)
2510 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2511 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2512 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2513 a129376b 2019-03-28 stsp err = unlockerr;
2514 c4296144 2019-05-09 stsp return err;
2517 cf066bf8 2019-05-09 stsp static void
2518 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2520 24519714 2019-05-09 stsp free(ct->path);
2521 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2522 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2523 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2524 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2525 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2529 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2530 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2531 24519714 2019-05-09 stsp struct got_repository *repo;
2532 24519714 2019-05-09 stsp struct got_worktree *worktree;
2535 c4296144 2019-05-09 stsp static const struct got_error *
2536 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2537 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2539 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2540 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2541 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2542 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2543 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2544 768aea60 2019-05-09 stsp struct stat sb;
2546 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2547 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2549 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2550 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2551 c4296144 2019-05-09 stsp return NULL;
2553 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2554 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2555 036813ee 2019-05-09 stsp goto done;
2557 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2558 036813ee 2019-05-09 stsp parent_path = strdup("");
2559 69960a46 2019-05-09 stsp if (parent_path == NULL)
2560 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2562 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2564 69960a46 2019-05-09 stsp return err;
2567 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2568 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2569 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2570 c4296144 2019-05-09 stsp goto done;
2573 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2574 768aea60 2019-05-09 stsp relpath) == -1) {
2575 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2576 768aea60 2019-05-09 stsp goto done;
2578 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2579 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2581 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2582 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2583 768aea60 2019-05-09 stsp goto done;
2585 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2588 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2589 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2590 44d03001 2019-05-09 stsp relpath) == -1) {
2591 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2592 44d03001 2019-05-09 stsp goto done;
2595 cf066bf8 2019-05-09 stsp ct->status = status;
2596 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2597 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2598 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2599 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2600 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2601 b416585c 2019-05-13 stsp goto done;
2603 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2604 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2605 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2606 036813ee 2019-05-09 stsp goto done;
2609 24519714 2019-05-09 stsp ct->path = strdup(path);
2610 24519714 2019-05-09 stsp if (ct->path == NULL) {
2611 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2612 24519714 2019-05-09 stsp goto done;
2614 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2616 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2617 ed175427 2019-05-09 stsp free_commitable(ct);
2618 24519714 2019-05-09 stsp free(parent_path);
2619 036813ee 2019-05-09 stsp free(path);
2620 a129376b 2019-03-28 stsp return err;
2623 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2624 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2625 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2626 036813ee 2019-05-09 stsp struct got_repository *);
2628 ed175427 2019-05-09 stsp static const struct got_error *
2629 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2630 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2631 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2632 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2633 afa376bf 2019-05-09 stsp struct got_repository *repo)
2635 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2636 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2637 036813ee 2019-05-09 stsp char *subpath;
2639 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2640 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2641 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2643 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2645 ed175427 2019-05-09 stsp return err;
2647 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2648 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2649 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2650 036813ee 2019-05-09 stsp free(subpath);
2651 036813ee 2019-05-09 stsp return err;
2654 036813ee 2019-05-09 stsp static const struct got_error *
2655 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2657 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2658 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2660 036813ee 2019-05-09 stsp *match = 0;
2662 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2663 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2664 0f63689d 2019-05-10 stsp return NULL;
2667 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2669 0f63689d 2019-05-10 stsp return err;
2670 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2671 036813ee 2019-05-09 stsp free(ct_parent_path);
2672 036813ee 2019-05-09 stsp return err;
2675 768aea60 2019-05-09 stsp static mode_t
2676 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2678 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2681 036813ee 2019-05-09 stsp static const struct got_error *
2682 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2683 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2685 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2687 036813ee 2019-05-09 stsp *new_te = NULL;
2689 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2691 036813ee 2019-05-09 stsp goto done;
2693 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2695 036813ee 2019-05-09 stsp free((*new_te)->id);
2696 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2697 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2698 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2699 036813ee 2019-05-09 stsp goto done;
2702 036813ee 2019-05-09 stsp if (err && *new_te) {
2703 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2704 036813ee 2019-05-09 stsp *new_te = NULL;
2706 036813ee 2019-05-09 stsp return err;
2709 036813ee 2019-05-09 stsp static const struct got_error *
2710 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2711 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2713 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2714 036813ee 2019-05-09 stsp char *ct_name;
2716 036813ee 2019-05-09 stsp *new_te = NULL;
2718 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2719 036813ee 2019-05-09 stsp if (*new_te == NULL)
2720 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2722 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2723 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2724 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2725 036813ee 2019-05-09 stsp goto done;
2727 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2728 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2729 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2730 036813ee 2019-05-09 stsp goto done;
2733 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2735 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2736 036813ee 2019-05-09 stsp if ((*new_te)->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 036813ee 2019-05-09 stsp if (err && *new_te) {
2742 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2743 036813ee 2019-05-09 stsp *new_te = NULL;
2745 036813ee 2019-05-09 stsp return err;
2748 036813ee 2019-05-09 stsp static const struct got_error *
2749 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2750 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2752 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2753 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2755 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2757 036813ee 2019-05-09 stsp return err;
2758 036813ee 2019-05-09 stsp if (new_pe == NULL)
2759 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2760 036813ee 2019-05-09 stsp return NULL;
2763 afa376bf 2019-05-09 stsp static const struct got_error *
2764 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2765 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2767 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2768 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2769 afa376bf 2019-05-09 stsp ct_path++;
2770 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2773 036813ee 2019-05-09 stsp static const struct got_error *
2774 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2775 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2777 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2778 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2779 44d03001 2019-05-09 stsp char *te_path;
2781 44d03001 2019-05-09 stsp *modified = 0;
2783 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2784 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2785 44d03001 2019-05-09 stsp te->name) == -1)
2786 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2788 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2789 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2790 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2791 44d03001 2019-05-09 stsp strlen(te_path));
2792 44d03001 2019-05-09 stsp if (*modified)
2796 44d03001 2019-05-09 stsp free(te_path);
2797 44d03001 2019-05-09 stsp return err;
2800 44d03001 2019-05-09 stsp static const struct got_error *
2801 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2802 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2803 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2805 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2806 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2808 036813ee 2019-05-09 stsp *ctp = NULL;
2810 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2811 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2812 036813ee 2019-05-09 stsp char *ct_name = NULL;
2813 036813ee 2019-05-09 stsp int path_matches;
2815 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2816 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2819 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2822 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2824 036813ee 2019-05-09 stsp return err;
2825 036813ee 2019-05-09 stsp if (!path_matches)
2828 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2829 036813ee 2019-05-09 stsp if (ct_name == NULL)
2830 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2832 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2835 036813ee 2019-05-09 stsp *ctp = ct;
2839 2b496619 2019-07-10 stsp return err;
2842 2b496619 2019-07-10 stsp static const struct got_error *
2843 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2844 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
2845 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
2846 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2847 2b496619 2019-07-10 stsp struct got_repository *repo)
2849 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
2850 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
2851 2b496619 2019-07-10 stsp char *subtree_path;
2853 2b496619 2019-07-10 stsp *new_tep = NULL;
2855 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2856 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2857 2b496619 2019-07-10 stsp child_path) == -1)
2858 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
2860 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
2861 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
2862 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
2863 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
2864 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
2865 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
2866 2b496619 2019-07-10 stsp goto done;
2868 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2869 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
2870 2b496619 2019-07-10 stsp if (err) {
2871 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
2872 2b496619 2019-07-10 stsp goto done;
2875 2b496619 2019-07-10 stsp free(subtree_path);
2876 2b496619 2019-07-10 stsp if (err == NULL)
2877 2b496619 2019-07-10 stsp *new_tep = new_te;
2878 036813ee 2019-05-09 stsp return err;
2881 036813ee 2019-05-09 stsp static const struct got_error *
2882 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2883 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2884 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2885 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2886 036813ee 2019-05-09 stsp struct got_repository *repo)
2888 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2889 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2890 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2891 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2892 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2893 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2895 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2896 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2897 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2899 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2900 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2901 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2902 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2904 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2905 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2908 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2909 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2912 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2913 036813ee 2019-05-09 stsp pe->path);
2915 036813ee 2019-05-09 stsp goto done;
2917 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2918 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2919 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2921 036813ee 2019-05-09 stsp goto done;
2922 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2924 afa376bf 2019-05-09 stsp goto done;
2925 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
2926 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
2928 2f51b5b3 2019-05-09 stsp goto done;
2930 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
2931 2b496619 2019-07-10 stsp if (base_tree == NULL ||
2932 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
2933 2b496619 2019-07-10 stsp == NULL) {
2934 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
2935 2b496619 2019-07-10 stsp child_path, path_base_tree,
2936 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
2939 2b496619 2019-07-10 stsp goto done;
2940 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
2942 2b496619 2019-07-10 stsp goto done;
2947 2f51b5b3 2019-05-09 stsp if (base_tree) {
2948 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2949 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2950 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2951 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2953 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2954 44d03001 2019-05-09 stsp int modified;
2955 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2957 036813ee 2019-05-09 stsp goto done;
2958 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2959 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2961 2f51b5b3 2019-05-09 stsp goto done;
2962 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2963 44d03001 2019-05-09 stsp if (modified) {
2964 44d03001 2019-05-09 stsp free(new_te->id);
2965 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2966 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2967 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2969 44d03001 2019-05-09 stsp goto done;
2971 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2973 036813ee 2019-05-09 stsp goto done;
2977 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2978 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2980 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2981 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2982 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2983 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2985 2f51b5b3 2019-05-09 stsp goto done;
2986 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2988 2f51b5b3 2019-05-09 stsp goto done;
2990 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
2991 b416585c 2019-05-13 stsp status_arg);
2993 afa376bf 2019-05-09 stsp goto done;
2995 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
2996 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2998 2f51b5b3 2019-05-09 stsp goto done;
2999 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3001 2f51b5b3 2019-05-09 stsp goto done;
3006 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3007 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3008 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3009 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3010 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3012 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3014 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3015 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3016 ed175427 2019-05-09 stsp return err;
3019 ebf99748 2019-05-09 stsp static const struct got_error *
3020 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3021 8ec7bf54 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3022 8ec7bf54 2019-07-12 stsp struct got_worktree *worktree)
3024 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3025 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3027 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3028 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3029 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3031 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3033 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3034 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3035 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3037 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3038 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3039 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3041 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3042 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3043 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3046 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3051 d56d26ce 2019-05-10 stsp return err;
3054 d56d26ce 2019-05-10 stsp static const struct got_error *
3055 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3056 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3058 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3059 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3060 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3061 1a36436d 2019-06-10 stsp char *path = NULL;
3062 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3064 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3065 1a36436d 2019-06-10 stsp ct_path++;
3068 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3069 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3071 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3072 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3073 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3074 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3076 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3077 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3079 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3080 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3081 1a36436d 2019-06-10 stsp char *slash;
3082 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3084 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3085 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3086 1a36436d 2019-06-10 stsp return NULL;
3088 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3089 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3090 1a36436d 2019-06-10 stsp if (path == NULL) {
3091 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3092 1a36436d 2019-06-10 stsp goto done;
3094 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3095 1a36436d 2019-06-10 stsp if (slash)
3096 1a36436d 2019-06-10 stsp *slash = '\0';
3098 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3100 1a36436d 2019-06-10 stsp goto done;
3102 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3103 1a36436d 2019-06-10 stsp head_commit_id, path);
3105 1a36436d 2019-06-10 stsp goto done;
3107 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3108 1a36436d 2019-06-10 stsp while (pid) {
3109 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3111 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3112 1a36436d 2019-06-10 stsp if (err) {
3113 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3114 1a36436d 2019-06-10 stsp goto done;
3115 1a36436d 2019-06-10 stsp err = NULL;
3119 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3121 1a36436d 2019-06-10 stsp goto done;
3123 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3124 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3128 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3129 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3131 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3134 1a36436d 2019-06-10 stsp goto done;
3136 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3137 1a36436d 2019-06-10 stsp commit = pcommit;
3138 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3142 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3143 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3145 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3146 1a36436d 2019-06-10 stsp goto done;
3147 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3150 1a36436d 2019-06-10 stsp if (commit)
3151 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3152 d56d26ce 2019-05-10 stsp free(id_in_head);
3154 1a36436d 2019-06-10 stsp free(path);
3155 1a36436d 2019-06-10 stsp return err;
3158 c4296144 2019-05-09 stsp const struct got_error *
3159 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3160 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
3161 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
3162 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3163 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3164 afa376bf 2019-05-09 stsp struct got_repository *repo)
3166 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3167 8ec7bf54 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3168 8ec7bf54 2019-07-12 stsp char *fileindex_path = NULL;
3169 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
3170 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
3171 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3172 bc70eb79 2019-05-09 stsp char *relpath = NULL;
3173 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3174 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
3175 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3176 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
3177 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3178 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3179 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3180 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3181 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3182 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3183 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3185 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3187 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
3188 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3190 c4296144 2019-05-09 stsp if (ondisk_path) {
3191 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
3192 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
3194 c4296144 2019-05-09 stsp return err;
3197 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
3199 c4296144 2019-05-09 stsp goto done;
3201 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3203 36a38700 2019-05-10 stsp goto done;
3204 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3206 09f5bd90 2019-05-09 stsp goto done;
3208 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
3209 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
3210 24519714 2019-05-09 stsp cc_arg.repo = repo;
3211 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
3212 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3214 675c7539 2019-05-09 stsp goto done;
3216 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
3217 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3221 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3223 588edf97 2019-05-10 stsp goto done;
3225 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3226 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3227 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3229 819f385b 2019-05-10 stsp goto done;
3232 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3234 588edf97 2019-05-10 stsp goto done;
3236 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3237 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3242 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3243 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3247 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3248 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3249 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3250 cf066bf8 2019-05-09 stsp char *ondisk_path;
3252 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3253 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3256 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3257 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3258 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3259 cf066bf8 2019-05-09 stsp goto done;
3261 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3262 a7055788 2019-05-09 stsp free(ondisk_path);
3264 cf066bf8 2019-05-09 stsp goto done;
3267 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3268 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3269 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3271 036813ee 2019-05-09 stsp goto done;
3273 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3275 de18fc63 2019-05-09 stsp goto done;
3276 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3277 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3278 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3279 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3280 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3281 33ad4cbe 2019-05-12 jcs free(logmsg);
3283 9d40349a 2019-05-09 stsp goto done;
3285 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3286 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3287 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3288 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3289 09f5bd90 2019-05-09 stsp goto done;
3291 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3292 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3294 09f5bd90 2019-05-09 stsp goto done;
3295 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3297 ebf99748 2019-05-09 stsp goto done;
3298 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3299 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3300 09f5bd90 2019-05-09 stsp goto done;
3302 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3303 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3305 f2c16586 2019-05-09 stsp goto done;
3306 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3308 f2c16586 2019-05-09 stsp goto done;
3310 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3312 09f5bd90 2019-05-09 stsp goto done;
3314 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3316 ebf99748 2019-05-09 stsp goto done;
3318 8ec7bf54 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3320 8ec7bf54 2019-07-12 stsp goto done;
3322 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3323 8ec7bf54 2019-07-12 stsp *new_commit_id, fileindex, worktree);
3325 ebf99748 2019-05-09 stsp goto done;
3327 8ec7bf54 2019-07-12 stsp if (fileindex) {
3328 8ec7bf54 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3329 8ec7bf54 2019-07-12 stsp if (sync_err && err == NULL)
3330 8ec7bf54 2019-07-12 stsp err = sync_err;
3331 8ec7bf54 2019-07-12 stsp got_fileindex_free(fileindex);
3333 8ec7bf54 2019-07-12 stsp free(fileindex_path);
3334 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3335 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3336 c4296144 2019-05-09 stsp err = unlockerr;
3337 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3338 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3339 ed175427 2019-05-09 stsp free_commitable(ct);
3341 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3342 588edf97 2019-05-10 stsp if (head_tree)
3343 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3344 588edf97 2019-05-10 stsp if (head_commit)
3345 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3346 c4296144 2019-05-09 stsp free(relpath);
3347 09f5bd90 2019-05-09 stsp free(head_commit_id);
3348 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3349 36a38700 2019-05-10 stsp if (head_ref)
3350 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3351 2f17228e 2019-05-12 stsp if (head_ref2) {
3352 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3353 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3354 2f17228e 2019-05-12 stsp err = unlockerr;
3355 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3357 c4296144 2019-05-09 stsp return err;
3360 8656d6c4 2019-05-20 stsp const char *
3361 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3363 8656d6c4 2019-05-20 stsp return ct->path;
3366 8656d6c4 2019-05-20 stsp unsigned int
3367 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3369 8656d6c4 2019-05-20 stsp return ct->status;
3372 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3373 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3374 818c7501 2019-07-11 stsp struct got_repository *repo;
3375 818c7501 2019-07-11 stsp int rebase_in_progress;
3378 818c7501 2019-07-11 stsp static const struct got_error *
3379 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3381 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3382 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3383 818c7501 2019-07-11 stsp unsigned char status;
3384 818c7501 2019-07-11 stsp struct stat sb;
3385 818c7501 2019-07-11 stsp char *ondisk_path;
3387 818c7501 2019-07-11 stsp if (!a->rebase_in_progress) {
3388 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with mixed base commits. */
3389 818c7501 2019-07-11 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3390 818c7501 2019-07-11 stsp SHA1_DIGEST_LENGTH))
3391 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3394 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3396 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3398 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with modified or conflicted files. */
3399 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3400 818c7501 2019-07-11 stsp free(ondisk_path);
3402 818c7501 2019-07-11 stsp return err;
3404 818c7501 2019-07-11 stsp if (a->rebase_in_progress) {
3405 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT)
3406 818c7501 2019-07-11 stsp return got_error(GOT_ERR_CONFLICTS);
3407 818c7501 2019-07-11 stsp } else if (status != GOT_STATUS_NO_CHANGE)
3408 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3410 818c7501 2019-07-11 stsp return NULL;
3413 818c7501 2019-07-11 stsp const struct got_error *
3414 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3415 818c7501 2019-07-11 stsp struct got_reference **tmp_branch, struct got_worktree *worktree,
3416 818c7501 2019-07-11 stsp struct got_reference *branch, struct got_repository *repo)
3418 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3419 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3420 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3421 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3422 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3423 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3424 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3426 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3427 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3429 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3431 818c7501 2019-07-11 stsp return err;
3433 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3435 818c7501 2019-07-11 stsp goto done;
3437 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3438 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3439 818c7501 2019-07-11 stsp ok_arg.rebase_in_progress = 0;
3440 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3443 818c7501 2019-07-11 stsp goto done;
3445 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3447 818c7501 2019-07-11 stsp goto done;
3449 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3451 818c7501 2019-07-11 stsp goto done;
3453 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3455 818c7501 2019-07-11 stsp goto done;
3457 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3460 818c7501 2019-07-11 stsp goto done;
3462 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3463 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3465 818c7501 2019-07-11 stsp goto done;
3466 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3468 818c7501 2019-07-11 stsp goto done;
3470 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3472 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3474 818c7501 2019-07-11 stsp goto done;
3476 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3478 818c7501 2019-07-11 stsp goto done;
3480 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3481 818c7501 2019-07-11 stsp worktree->base_commit_id);
3483 818c7501 2019-07-11 stsp goto done;
3484 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3486 818c7501 2019-07-11 stsp goto done;
3488 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3490 818c7501 2019-07-11 stsp goto done;
3492 818c7501 2019-07-11 stsp free(fileindex_path);
3493 818c7501 2019-07-11 stsp if (fileindex)
3494 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3495 818c7501 2019-07-11 stsp free(tmp_branch_name);
3496 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3497 818c7501 2019-07-11 stsp free(branch_ref_name);
3498 818c7501 2019-07-11 stsp if (branch_ref)
3499 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3500 818c7501 2019-07-11 stsp if (wt_branch)
3501 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3502 818c7501 2019-07-11 stsp if (err) {
3503 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3504 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3505 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3507 818c7501 2019-07-11 stsp if (*tmp_branch) {
3508 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3509 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3511 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3513 818c7501 2019-07-11 stsp return err;
3516 818c7501 2019-07-11 stsp const struct got_error *
3517 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3518 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3519 818c7501 2019-07-11 stsp struct got_reference **branch, struct got_worktree *worktree,
3520 818c7501 2019-07-11 stsp struct got_repository *repo)
3522 818c7501 2019-07-11 stsp const struct got_error *err;
3523 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3524 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3525 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3527 818c7501 2019-07-11 stsp *commit_id = NULL;
3529 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3531 818c7501 2019-07-11 stsp return err;
3533 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3535 818c7501 2019-07-11 stsp goto done;
3537 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3539 818c7501 2019-07-11 stsp goto done;
3541 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3543 818c7501 2019-07-11 stsp goto done;
3545 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3547 818c7501 2019-07-11 stsp goto done;
3549 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3550 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3552 818c7501 2019-07-11 stsp goto done;
3554 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3556 818c7501 2019-07-11 stsp goto done;
3558 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
3560 818c7501 2019-07-11 stsp goto done;
3562 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
3563 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
3565 818c7501 2019-07-11 stsp goto done;
3567 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3569 818c7501 2019-07-11 stsp goto done;
3571 818c7501 2019-07-11 stsp free(commit_ref_name);
3572 818c7501 2019-07-11 stsp free(branch_ref_name);
3573 818c7501 2019-07-11 stsp if (commit_ref)
3574 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3575 818c7501 2019-07-11 stsp if (branch_ref)
3576 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3577 818c7501 2019-07-11 stsp if (err) {
3578 818c7501 2019-07-11 stsp free(*commit_id);
3579 818c7501 2019-07-11 stsp *commit_id = NULL;
3580 818c7501 2019-07-11 stsp if (*tmp_branch) {
3581 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3582 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3584 818c7501 2019-07-11 stsp if (*new_base_branch) {
3585 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
3586 818c7501 2019-07-11 stsp *new_base_branch = NULL;
3588 818c7501 2019-07-11 stsp if (*branch) {
3589 818c7501 2019-07-11 stsp got_ref_close(*branch);
3590 818c7501 2019-07-11 stsp *branch = NULL;
3593 818c7501 2019-07-11 stsp return err;
3596 818c7501 2019-07-11 stsp const struct got_error *
3597 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3599 818c7501 2019-07-11 stsp const struct got_error *err;
3600 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
3602 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3604 818c7501 2019-07-11 stsp return err;
3606 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3607 818c7501 2019-07-11 stsp free(tmp_branch_name);
3608 818c7501 2019-07-11 stsp return NULL;
3611 818c7501 2019-07-11 stsp static const struct got_error *
3612 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3613 818c7501 2019-07-11 stsp char **logmsg, void *arg)
3615 818c7501 2019-07-11 stsp struct got_commit_object *commit = arg;
3617 818c7501 2019-07-11 stsp *logmsg = strdup(got_object_commit_get_logmsg(commit));
3618 818c7501 2019-07-11 stsp if (*logmsg == NULL)
3619 818c7501 2019-07-11 stsp return got_error_from_errno("strdup");
3621 818c7501 2019-07-11 stsp return NULL;
3624 818c7501 2019-07-11 stsp static const struct got_error *
3625 818c7501 2019-07-11 stsp rebase_status(void *arg, unsigned char status, const char *path,
3626 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3628 818c7501 2019-07-11 stsp return NULL;
3631 818c7501 2019-07-11 stsp const struct got_error *
3632 818c7501 2019-07-11 stsp got_worktree_rebase_merge_files(struct got_worktree *worktree,
3633 818c7501 2019-07-11 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3634 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3635 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3637 818c7501 2019-07-11 stsp const struct got_error *err;
3638 818c7501 2019-07-11 stsp struct got_fileindex *fileindex;
3639 818c7501 2019-07-11 stsp char *fileindex_path, *commit_ref_name = NULL;
3640 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3642 818c7501 2019-07-11 stsp /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3644 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3646 818c7501 2019-07-11 stsp return err;
3648 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3650 818c7501 2019-07-11 stsp goto done;
3651 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3652 818c7501 2019-07-11 stsp if (err) {
3653 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
3654 818c7501 2019-07-11 stsp goto done;
3655 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3657 818c7501 2019-07-11 stsp goto done;
3658 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
3660 818c7501 2019-07-11 stsp goto done;
3662 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
3665 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
3667 818c7501 2019-07-11 stsp goto done;
3668 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
3669 818c7501 2019-07-11 stsp free(stored_id);
3670 818c7501 2019-07-11 stsp if (cmp != 0) {
3671 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3672 818c7501 2019-07-11 stsp goto done;
3676 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
3677 818c7501 2019-07-11 stsp parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3678 818c7501 2019-07-11 stsp cancel_cb, cancel_arg);
3680 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3681 818c7501 2019-07-11 stsp free(fileindex_path);
3682 818c7501 2019-07-11 stsp if (commit_ref)
3683 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3684 edd02c5e 2019-07-11 stsp return err;
3687 edd02c5e 2019-07-11 stsp static const struct got_error *
3688 edd02c5e 2019-07-11 stsp bump_all_base_commit_ids(struct got_worktree *worktree,
3689 edd02c5e 2019-07-11 stsp struct got_object_id *base_commit_id)
3691 edd02c5e 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3692 edd02c5e 2019-07-11 stsp struct got_fileindex *fileindex;
3693 edd02c5e 2019-07-11 stsp char *fileindex_path;
3694 edd02c5e 2019-07-11 stsp struct bump_base_commit_id_arg bbc_arg;
3696 edd02c5e 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3698 edd02c5e 2019-07-11 stsp return err;
3700 edd02c5e 2019-07-11 stsp bbc_arg.base_commit_id = base_commit_id;
3701 edd02c5e 2019-07-11 stsp bbc_arg.entry_name = NULL;
3702 edd02c5e 2019-07-11 stsp bbc_arg.path = "";
3703 edd02c5e 2019-07-11 stsp bbc_arg.path_len = 0;
3704 edd02c5e 2019-07-11 stsp bbc_arg.progress_cb = NULL;
3705 edd02c5e 2019-07-11 stsp bbc_arg.progress_arg = NULL;
3706 edd02c5e 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex,
3707 edd02c5e 2019-07-11 stsp bump_base_commit_id, &bbc_arg);
3709 edd02c5e 2019-07-11 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3710 edd02c5e 2019-07-11 stsp if (sync_err && err == NULL)
3711 edd02c5e 2019-07-11 stsp err = sync_err;
3713 edd02c5e 2019-07-11 stsp got_fileindex_free(fileindex);
3714 edd02c5e 2019-07-11 stsp free(fileindex_path);
3715 818c7501 2019-07-11 stsp return err;
3718 818c7501 2019-07-11 stsp const struct got_error *
3719 818c7501 2019-07-11 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3720 818c7501 2019-07-11 stsp struct got_worktree *worktree, struct got_reference *tmp_branch,
3721 818c7501 2019-07-11 stsp struct got_commit_object *orig_commit,
3722 818c7501 2019-07-11 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
3724 818c7501 2019-07-11 stsp const struct got_error *err;
3725 818c7501 2019-07-11 stsp char *commit_ref_name = NULL;
3726 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3727 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
3729 818c7501 2019-07-11 stsp /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3731 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3733 818c7501 2019-07-11 stsp goto done;
3734 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3736 818c7501 2019-07-11 stsp goto done;
3737 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
3739 818c7501 2019-07-11 stsp goto done;
3740 818c7501 2019-07-11 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3741 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3742 818c7501 2019-07-11 stsp goto done;
3745 818c7501 2019-07-11 stsp err = got_worktree_commit(new_commit_id, worktree, NULL,
3746 818c7501 2019-07-11 stsp got_object_commit_get_author(orig_commit),
3747 818c7501 2019-07-11 stsp got_object_commit_get_committer(orig_commit),
3748 818c7501 2019-07-11 stsp collect_rebase_commit_msg, orig_commit,
3749 818c7501 2019-07-11 stsp rebase_status, NULL, repo);
3750 ff0d2220 2019-07-11 stsp if (err) {
3751 ff0d2220 2019-07-11 stsp if (err->code == GOT_ERR_COMMIT_NO_CHANGES) {
3752 ff0d2220 2019-07-11 stsp /* No-op change; commit will be elided. */
3753 ff0d2220 2019-07-11 stsp err = got_ref_delete(commit_ref, repo);
3755 ff0d2220 2019-07-11 stsp goto done;
3756 ff0d2220 2019-07-11 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3758 818c7501 2019-07-11 stsp goto done;
3761 edd02c5e 2019-07-11 stsp /* Prevent out-of-date errors during rebase of subsequent commits. */
3762 edd02c5e 2019-07-11 stsp err = bump_all_base_commit_ids(worktree, *new_commit_id);
3764 edd02c5e 2019-07-11 stsp goto done;
3766 818c7501 2019-07-11 stsp err = got_ref_delete(commit_ref, repo);
3768 818c7501 2019-07-11 stsp goto done;
3770 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
3772 818c7501 2019-07-11 stsp free(commit_ref_name);
3773 818c7501 2019-07-11 stsp if (commit_ref)
3774 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3775 818c7501 2019-07-11 stsp if (err) {
3776 818c7501 2019-07-11 stsp free(*new_commit_id);
3777 818c7501 2019-07-11 stsp *new_commit_id = NULL;
3779 818c7501 2019-07-11 stsp return err;
3782 818c7501 2019-07-11 stsp const struct got_error *
3783 818c7501 2019-07-11 stsp got_worktree_rebase_postpone(struct got_worktree *worktree)
3785 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
3788 69844fba 2019-07-11 stsp static const struct got_error *
3789 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
3791 69844fba 2019-07-11 stsp const struct got_error *err;
3792 69844fba 2019-07-11 stsp struct got_reference *ref;
3794 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
3795 69844fba 2019-07-11 stsp if (err) {
3796 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
3797 69844fba 2019-07-11 stsp return NULL;
3798 69844fba 2019-07-11 stsp return err;
3801 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
3802 69844fba 2019-07-11 stsp got_ref_close(ref);
3803 69844fba 2019-07-11 stsp return err;
3806 69844fba 2019-07-11 stsp static const struct got_error *
3807 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3809 69844fba 2019-07-11 stsp const struct got_error *err;
3810 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3811 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
3813 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3815 69844fba 2019-07-11 stsp goto done;
3816 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
3818 69844fba 2019-07-11 stsp goto done;
3820 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3822 69844fba 2019-07-11 stsp goto done;
3823 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
3825 69844fba 2019-07-11 stsp goto done;
3827 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3829 69844fba 2019-07-11 stsp goto done;
3830 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
3832 69844fba 2019-07-11 stsp goto done;
3834 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3836 69844fba 2019-07-11 stsp goto done;
3837 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
3839 69844fba 2019-07-11 stsp goto done;
3842 69844fba 2019-07-11 stsp free(tmp_branch_name);
3843 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
3844 69844fba 2019-07-11 stsp free(branch_ref_name);
3845 69844fba 2019-07-11 stsp free(commit_ref_name);
3846 69844fba 2019-07-11 stsp return err;
3849 818c7501 2019-07-11 stsp const struct got_error *
3850 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
3851 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3852 818c7501 2019-07-11 stsp struct got_reference *rebased_branch,
3853 818c7501 2019-07-11 stsp struct got_repository *repo)
3855 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3856 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
3858 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3860 818c7501 2019-07-11 stsp return err;
3862 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3864 818c7501 2019-07-11 stsp goto done;
3866 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
3868 818c7501 2019-07-11 stsp goto done;
3870 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
3872 818c7501 2019-07-11 stsp goto done;
3874 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
3876 818c7501 2019-07-11 stsp free(new_head_commit_id);
3877 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3878 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
3879 818c7501 2019-07-11 stsp err = unlockerr;
3880 818c7501 2019-07-11 stsp return err;
3883 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
3884 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
3885 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3888 818c7501 2019-07-11 stsp static const struct got_error *
3889 818c7501 2019-07-11 stsp collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
3890 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3892 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
3893 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3894 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
3895 818c7501 2019-07-11 stsp char *path = NULL;
3897 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
3898 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
3899 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
3900 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
3901 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
3902 818c7501 2019-07-11 stsp return NULL;
3904 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
3905 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3907 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
3908 818c7501 2019-07-11 stsp if (err || new == NULL)
3909 818c7501 2019-07-11 stsp free(path);
3910 818c7501 2019-07-11 stsp return err;
3913 818c7501 2019-07-11 stsp const struct got_error *
3914 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
3915 818c7501 2019-07-11 stsp struct got_repository *repo, struct got_reference *new_base_branch,
3916 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
3918 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3919 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
3920 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
3921 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3922 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3923 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
3924 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
3925 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
3927 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
3929 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3931 818c7501 2019-07-11 stsp return err;
3933 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3935 818c7501 2019-07-11 stsp goto done;
3937 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
3938 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
3940 818c7501 2019-07-11 stsp goto done;
3942 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
3944 818c7501 2019-07-11 stsp goto done;
3947 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
3948 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
3949 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
3951 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
3953 818c7501 2019-07-11 stsp goto done;
3955 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
3957 818c7501 2019-07-11 stsp goto done;
3959 818c7501 2019-07-11 stsp err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
3960 818c7501 2019-07-11 stsp progress_arg, NULL, NULL);
3962 818c7501 2019-07-11 stsp goto done;
3964 818c7501 2019-07-11 stsp crp_arg.revertible_paths = &revertible_paths;
3965 818c7501 2019-07-11 stsp crp_arg.worktree = worktree;
3966 818c7501 2019-07-11 stsp err = got_worktree_status(worktree, "", repo,
3967 818c7501 2019-07-11 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
3969 818c7501 2019-07-11 stsp goto done;
3971 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
3972 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
3973 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
3975 69844fba 2019-07-11 stsp goto done;
3978 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
3980 818c7501 2019-07-11 stsp got_ref_close(resolved);
3981 818c7501 2019-07-11 stsp free(commit_id);
3982 5ade8233 2019-07-12 stsp if (fileindex)
3983 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
3984 818c7501 2019-07-11 stsp free(fileindex_path);
3985 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
3986 818c7501 2019-07-11 stsp free((char *)pe->path);
3987 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
3989 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3990 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
3991 818c7501 2019-07-11 stsp err = unlockerr;
3992 818c7501 2019-07-11 stsp return err;