2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
121 09fe317a 2018-03-11 stsp struct stat sb;
123 09fe317a 2018-03-11 stsp *content = NULL;
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
145 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
175 09fe317a 2018-03-11 stsp return err;
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
198 86c3caaf 2018-03-09 stsp const struct got_error *
199 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
200 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
202 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
203 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
204 ec22038e 2019-03-10 stsp uuid_t uuid;
205 ec22038e 2019-03-10 stsp uint32_t uuid_status;
206 65596e15 2018-12-24 stsp int obj_type;
207 7ac97322 2018-03-11 stsp char *path_got = NULL;
208 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
361 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
366 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
367 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
368 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
371 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
374 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
378 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
379 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
383 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
388 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
389 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
393 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
398 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
402 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
406 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 eaccb85f 2018-12-25 stsp base_commit_id_str);
411 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
415 eaccb85f 2018-12-25 stsp got_repo_close(repo);
416 7ac97322 2018-03-11 stsp free(path_got);
417 056e7441 2018-03-11 stsp free(path_lock);
418 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
419 c442a90d 2019-03-10 stsp free(uuidstr);
420 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
425 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
426 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp return err;
433 247140b2 2019-02-05 stsp const struct got_error *
434 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
439 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
440 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 247140b2 2019-02-05 stsp return err;
442 247140b2 2019-02-05 stsp if (*worktree)
443 247140b2 2019-02-05 stsp return NULL;
444 247140b2 2019-02-05 stsp path = dirname(path);
445 d1542a27 2019-02-05 stsp if (path == NULL)
446 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
447 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
452 3a6ce05a 2019-02-11 stsp const struct got_error *
453 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
456 c88eb298 2018-03-11 stsp free(worktree->root_path);
457 cde76477 2018-03-11 stsp free(worktree->repo_path);
458 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
459 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
460 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
461 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
462 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
463 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
464 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
465 6d9d28c3 2018-03-11 stsp free(worktree);
466 3a6ce05a 2019-02-11 stsp return err;
469 2fbdb5ae 2018-12-29 stsp const char *
470 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp return worktree->root_path;
475 c7f4312f 2019-02-05 stsp const char *
476 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
481 49520a32 2018-12-29 stsp const char *
482 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 f609be2e 2018-12-29 stsp return worktree->path_prefix;
487 e5dc7198 2018-12-29 stsp const struct got_error *
488 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
494 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
498 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
499 e5dc7198 2018-12-29 stsp free(absprefix);
500 e5dc7198 2018-12-29 stsp return NULL;
503 bc70eb79 2019-05-09 stsp const char *
504 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 36a38700 2019-05-10 stsp return worktree->head_ref_name;
509 024e9686 2019-05-14 stsp const struct got_error *
510 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
511 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
514 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
518 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
519 024e9686 2019-05-14 stsp path_got = NULL;
523 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
524 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
525 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
529 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
533 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
534 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp free(head_ref_name);
539 024e9686 2019-05-14 stsp return err;
542 b72f483a 2019-02-05 stsp struct got_object_id *
543 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
548 507dc3bb 2018-12-29 stsp const struct got_error *
549 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp const struct got_error *err;
553 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
554 507dc3bb 2018-12-29 stsp char *id_str = NULL;
555 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
560 507dc3bb 2018-12-29 stsp path_got = NULL;
564 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
573 507dc3bb 2018-12-29 stsp /* Record our base commit. */
574 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
577 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
582 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
583 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
589 507dc3bb 2018-12-29 stsp got_object_close(obj);
590 507dc3bb 2018-12-29 stsp free(id_str);
591 507dc3bb 2018-12-29 stsp free(path_got);
592 507dc3bb 2018-12-29 stsp return err;
595 9d31a1d8 2018-03-11 stsp static const struct got_error *
596 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
601 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
602 86c3caaf 2018-03-09 stsp return NULL;
605 21908da4 2019-01-13 stsp static const struct got_error *
606 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
609 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
615 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 ddcd8544 2019-03-11 stsp struct stat sb;
617 ddcd8544 2019-03-11 stsp err = NULL;
618 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
619 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
620 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
621 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
622 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 4a1ddfc2 2019-01-12 stsp free(abspath);
626 68c76935 2019-02-19 stsp return err;
629 68c76935 2019-02-19 stsp static const struct got_error *
630 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
633 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
634 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
635 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
640 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
642 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
645 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
647 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
650 68c76935 2019-02-19 stsp if (flen1 == 0) {
651 68c76935 2019-02-19 stsp if (flen2 != 0)
654 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
655 68c76935 2019-02-19 stsp if (flen1 != 0)
658 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
659 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
669 68c76935 2019-02-19 stsp return err;
672 68c76935 2019-02-19 stsp static const struct got_error *
673 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
676 68c76935 2019-02-19 stsp struct stat sb;
677 68c76935 2019-02-19 stsp size_t size1, size2;
678 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
682 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
686 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
692 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp return NULL;
699 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
700 68c76935 2019-02-19 stsp if (f1 == NULL)
701 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
704 68c76935 2019-02-19 stsp if (f2 == NULL) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
709 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
712 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
713 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 6353ad76 2019-02-08 stsp return err;
720 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
722 234035bc 2019-06-01 stsp * acts as the second derived version.
724 6353ad76 2019-02-08 stsp static const struct got_error *
725 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
727 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
729 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 234035bc 2019-06-01 stsp void *progress_arg)
732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
733 6353ad76 2019-02-08 stsp int merged_fd = -1;
734 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
735 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
737 6353ad76 2019-02-08 stsp char *id_str = NULL;
738 818c7501 2019-07-11 stsp char *label_deriv = NULL;
739 234035bc 2019-06-01 stsp int overlapcnt = 0;
740 af54ae4a 2019-02-19 stsp char *parent;
742 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
744 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
745 af54ae4a 2019-02-19 stsp if (parent == NULL)
746 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
748 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
751 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 af54ae4a 2019-02-19 stsp free(base_path);
756 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
758 af54ae4a 2019-02-19 stsp base_path = NULL;
762 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
765 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 6c4c42e0 2019-06-24 stsp blob_deriv);
770 af54ae4a 2019-02-19 stsp free(base_path);
771 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
773 af54ae4a 2019-02-19 stsp base_path = NULL;
777 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
780 46b6ee73 2019-06-04 stsp if (blob_orig) {
781 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 46b6ee73 2019-06-04 stsp blob_orig);
787 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
788 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
789 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
793 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
796 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
801 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
806 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
807 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
811 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
812 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
816 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
817 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
818 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
819 68c76935 2019-02-19 stsp merged_path);
824 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
825 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
829 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
830 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
831 230a42bd 2019-05-11 jcs ondisk_path);
832 2a57020b 2019-02-20 stsp unlink(merged_path);
837 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
838 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
839 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
840 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
841 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
842 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
843 6353ad76 2019-02-08 stsp free(merged_path);
844 af54ae4a 2019-02-19 stsp free(base_path);
845 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
846 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
847 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
849 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
850 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
851 46b6ee73 2019-06-04 stsp free(blob_orig_path);
853 6353ad76 2019-02-08 stsp free(id_str);
854 818c7501 2019-07-11 stsp free(label_deriv);
855 4a1ddfc2 2019-01-12 stsp return err;
858 4a1ddfc2 2019-01-12 stsp static const struct got_error *
859 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
860 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
861 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
862 13d9040b 2019-03-27 stsp int update_timestamps)
864 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
866 13d9040b 2019-03-27 stsp if (ie == NULL)
867 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
869 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
870 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
871 13d9040b 2019-03-27 stsp update_timestamps);
873 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
874 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
875 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
877 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
879 13d9040b 2019-03-27 stsp return err;
882 13d9040b 2019-03-27 stsp static const struct got_error *
883 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
884 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
885 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
886 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
887 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
889 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
890 507dc3bb 2018-12-29 stsp int fd = -1;
891 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
892 507dc3bb 2018-12-29 stsp int update = 0;
893 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
895 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
897 9d31a1d8 2018-03-11 stsp if (fd == -1) {
898 21908da4 2019-01-13 stsp if (errno == ENOENT) {
899 21908da4 2019-01-13 stsp char *parent = dirname(path);
900 21908da4 2019-01-13 stsp if (parent == NULL)
901 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
902 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
904 21908da4 2019-01-13 stsp return err;
905 21908da4 2019-01-13 stsp fd = open(ondisk_path,
906 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
907 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
908 21908da4 2019-01-13 stsp if (fd == -1)
909 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
910 230a42bd 2019-05-11 jcs ondisk_path);
911 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
912 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
913 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
914 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
917 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
918 507dc3bb 2018-12-29 stsp ondisk_path);
921 507dc3bb 2018-12-29 stsp update = 1;
924 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
927 a378724f 2019-02-10 stsp if (restoring_missing_file)
928 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
929 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
930 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
933 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
937 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
939 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
940 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
943 9d31a1d8 2018-03-11 stsp if (len > 0) {
944 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
945 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
946 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
947 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
949 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
950 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
953 61d6eaa3 2018-12-24 stsp hdrlen = 0;
955 9d31a1d8 2018-03-11 stsp } while (len != 0);
957 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
958 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
962 507dc3bb 2018-12-29 stsp if (update) {
963 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
964 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
965 230a42bd 2019-05-11 jcs ondisk_path);
966 2a57020b 2019-02-20 stsp unlink(tmppath);
971 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
972 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
973 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
977 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
978 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
984 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
985 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
986 507dc3bb 2018-12-29 stsp free(tmppath);
987 6353ad76 2019-02-08 stsp return err;
990 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
991 6353ad76 2019-02-08 stsp static const struct got_error *
992 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
994 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
995 7154f6ce 2019-03-27 stsp const char *markers[3] = {
996 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
997 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
998 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1000 7154f6ce 2019-03-27 stsp int i = 0;
1001 7154f6ce 2019-03-27 stsp char *line;
1002 7154f6ce 2019-03-27 stsp size_t len;
1003 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1005 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1006 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1007 7154f6ce 2019-03-27 stsp if (line == NULL) {
1008 7154f6ce 2019-03-27 stsp if (feof(f))
1010 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1014 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1015 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1023 7154f6ce 2019-03-27 stsp return err;
1026 e2b1e152 2019-07-13 stsp static int
1027 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1030 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1031 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1032 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1033 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1036 7154f6ce 2019-03-27 stsp static const struct got_error *
1037 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1038 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1039 b8f41171 2019-02-10 stsp struct got_repository *repo)
1041 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1042 6353ad76 2019-02-08 stsp struct got_object_id id;
1043 6353ad76 2019-02-08 stsp size_t hdrlen;
1044 6353ad76 2019-02-08 stsp FILE *f = NULL;
1045 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1046 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1047 6353ad76 2019-02-08 stsp size_t flen, blen;
1049 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1051 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1052 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1053 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1054 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1056 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1057 a378724f 2019-02-10 stsp return NULL;
1059 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1062 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1063 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1064 339c298e 2019-07-27 stsp return NULL;
1067 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1068 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1069 339c298e 2019-07-27 stsp return NULL;
1070 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1071 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1072 339c298e 2019-07-27 stsp return NULL;
1075 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1076 339c298e 2019-07-27 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 339c298e 2019-07-27 stsp return err;
1083 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1084 6353ad76 2019-02-08 stsp if (f == NULL) {
1085 339c298e 2019-07-27 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;
1134 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1135 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1137 e2b1e152 2019-07-13 stsp static const struct got_error *
1138 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1139 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1141 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1142 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1143 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1145 e2b1e152 2019-07-13 stsp return NULL;
1148 9d31a1d8 2018-03-11 stsp static const struct got_error *
1149 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1150 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1151 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1152 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1153 0584f854 2019-04-06 stsp void *progress_arg)
1155 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1156 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1157 6353ad76 2019-02-08 stsp char *ondisk_path;
1158 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1159 b8f41171 2019-02-10 stsp struct stat sb;
1161 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1162 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1165 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1167 abb4604f 2019-07-27 stsp goto done;
1168 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1169 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1171 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1173 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1174 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1175 b8f41171 2019-02-10 stsp goto done;
1178 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1179 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1180 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1181 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1182 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1184 e2b1e152 2019-07-13 stsp goto done;
1185 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1187 6353ad76 2019-02-08 stsp goto done;
1189 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1190 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1191 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1192 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1193 b8f41171 2019-02-10 stsp goto done;
1197 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1199 6353ad76 2019-02-08 stsp goto done;
1201 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1202 234035bc 2019-06-01 stsp int update_timestamps;
1203 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1204 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1205 234035bc 2019-06-01 stsp struct got_object_id id2;
1206 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1207 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1209 234035bc 2019-06-01 stsp goto done;
1211 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1212 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1213 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1214 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1215 234035bc 2019-06-01 stsp if (blob2)
1216 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1218 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1219 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1220 234035bc 2019-06-01 stsp * unmodified files again.
1222 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1223 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1224 234035bc 2019-06-01 stsp update_timestamps);
1225 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1226 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1228 1ee397ad 2019-07-12 stsp goto done;
1229 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1230 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1232 13d9040b 2019-03-27 stsp goto done;
1234 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1235 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1236 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1238 13d9040b 2019-03-27 stsp goto done;
1239 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1240 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1242 13d9040b 2019-03-27 stsp goto done;
1244 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1246 6353ad76 2019-02-08 stsp free(ondisk_path);
1247 8da9e5f4 2019-01-12 stsp return err;
1250 90285c3b 2019-01-08 stsp static const struct got_error *
1251 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1253 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1254 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1256 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1257 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1259 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1260 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1261 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1263 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1264 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1265 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1266 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1267 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1271 90285c3b 2019-01-08 stsp parent = dirname(parent);
1274 90285c3b 2019-01-08 stsp free(ondisk_path);
1275 90285c3b 2019-01-08 stsp return err;
1278 708d8e67 2019-03-27 stsp static const struct got_error *
1279 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1280 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1281 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1283 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1284 708d8e67 2019-03-27 stsp unsigned char status;
1285 708d8e67 2019-03-27 stsp struct stat sb;
1286 708d8e67 2019-03-27 stsp char *ondisk_path;
1288 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1290 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1292 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1294 708d8e67 2019-03-27 stsp return err;
1296 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1297 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1298 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1300 1ee397ad 2019-07-12 stsp return err;
1302 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1303 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1305 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1308 708d8e67 2019-03-27 stsp return err;
1310 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1312 1ee397ad 2019-07-12 stsp return err;
1313 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1314 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1316 fc6346c4 2019-03-27 stsp return err;
1318 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1321 fc6346c4 2019-03-27 stsp return err;
1324 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1325 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1326 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1327 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1328 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1329 8da9e5f4 2019-01-12 stsp void *progress_arg;
1330 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1331 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1334 512f0d0e 2019-01-02 stsp static const struct got_error *
1335 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1336 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1338 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1340 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1341 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1343 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1344 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1347 8da9e5f4 2019-01-12 stsp static const struct got_error *
1348 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1350 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1352 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1353 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1355 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1356 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1359 9d31a1d8 2018-03-11 stsp static const struct got_error *
1360 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1362 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1363 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1364 8da9e5f4 2019-01-12 stsp char *path;
1366 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1367 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1369 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1370 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1372 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1374 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1375 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1377 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1378 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1380 8da9e5f4 2019-01-12 stsp free(path);
1381 0cd1c46a 2019-03-11 stsp return err;
1384 818c7501 2019-07-11 stsp static const struct got_error *
1385 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1387 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1388 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1389 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1391 517bab73 2019-03-11 stsp *refname = NULL;
1393 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1394 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1395 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1397 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1399 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1400 0647c563 2019-03-11 stsp *refname = NULL;
1402 517bab73 2019-03-11 stsp free(uuidstr);
1403 517bab73 2019-03-11 stsp return err;
1406 818c7501 2019-07-11 stsp const struct got_error *
1407 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1409 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1412 818c7501 2019-07-11 stsp static const struct got_error *
1413 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1415 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1416 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1419 818c7501 2019-07-11 stsp static const struct got_error *
1420 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1422 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1425 818c7501 2019-07-11 stsp static const struct got_error *
1426 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1428 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1429 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1432 818c7501 2019-07-11 stsp static const struct got_error *
1433 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1435 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1436 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1439 0ebf8283 2019-07-24 stsp static const struct got_error *
1440 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1442 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1443 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1446 0ebf8283 2019-07-24 stsp static const struct got_error *
1447 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1449 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1450 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1453 0ebf8283 2019-07-24 stsp static const struct got_error *
1454 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1456 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1457 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1460 0ebf8283 2019-07-24 stsp static const struct got_error *
1461 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1463 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1464 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1467 0ebf8283 2019-07-24 stsp const struct got_error *
1468 0ebf8283 2019-07-24 stsp got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1470 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1471 0ebf8283 2019-07-24 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1472 0ebf8283 2019-07-24 stsp *path = NULL;
1473 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1475 0ebf8283 2019-07-24 stsp return NULL;
1479 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1480 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1482 517bab73 2019-03-11 stsp static const struct got_error *
1483 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1485 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1486 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1487 517bab73 2019-03-11 stsp char *refname;
1489 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1491 517bab73 2019-03-11 stsp return err;
1493 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1495 0cd1c46a 2019-03-11 stsp goto done;
1497 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1499 0cd1c46a 2019-03-11 stsp free(refname);
1501 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1502 3e3a69f1 2019-07-25 stsp return err;
1505 3e3a69f1 2019-07-25 stsp static const struct got_error *
1506 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1508 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1510 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1511 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1512 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1513 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1515 9d31a1d8 2018-03-11 stsp return err;
1519 ebf99748 2019-05-09 stsp static const struct got_error *
1520 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1521 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1523 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1524 ebf99748 2019-05-09 stsp FILE *index = NULL;
1526 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1527 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1528 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1529 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1531 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1533 ebf99748 2019-05-09 stsp goto done;
1535 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1536 ebf99748 2019-05-09 stsp if (index == NULL) {
1537 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1538 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1540 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1541 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1542 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1545 ebf99748 2019-05-09 stsp if (err) {
1546 ebf99748 2019-05-09 stsp free(*fileindex_path);
1547 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1548 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1549 ebf99748 2019-05-09 stsp *fileindex = NULL;
1551 ebf99748 2019-05-09 stsp return err;
1554 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1555 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1556 c932eeeb 2019-05-22 stsp const char *path;
1557 c932eeeb 2019-05-22 stsp size_t path_len;
1558 c932eeeb 2019-05-22 stsp const char *entry_name;
1559 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1560 a484d721 2019-06-10 stsp void *progress_arg;
1563 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1564 c932eeeb 2019-05-22 stsp static const struct got_error *
1565 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1567 1ee397ad 2019-07-12 stsp const struct got_error *err;
1568 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1570 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1571 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1572 c932eeeb 2019-05-22 stsp return NULL;
1573 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1574 102ff934 2019-06-11 stsp return NULL;
1576 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1577 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1578 c932eeeb 2019-05-22 stsp return NULL;
1580 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1581 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1582 edd02c5e 2019-07-11 stsp ie->path);
1584 1ee397ad 2019-07-12 stsp return err;
1586 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1587 c932eeeb 2019-05-22 stsp return NULL;
1590 9c6338c4 2019-06-02 stsp static const struct got_error *
1591 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1593 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1594 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1595 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1597 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1598 9c6338c4 2019-06-02 stsp fileindex_path);
1600 9c6338c4 2019-06-02 stsp goto done;
1602 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1604 9c6338c4 2019-06-02 stsp goto done;
1606 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1607 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1608 9c6338c4 2019-06-02 stsp fileindex_path);
1609 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1612 9c6338c4 2019-06-02 stsp if (new_index)
1613 9c6338c4 2019-06-02 stsp fclose(new_index);
1614 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1615 9c6338c4 2019-06-02 stsp return err;
1618 6ced4176 2019-07-12 stsp static const struct got_error *
1619 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1620 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1621 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1623 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1624 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1625 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1626 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1628 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1629 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1630 6ced4176 2019-07-12 stsp *tree_id = NULL;
1632 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1633 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1634 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1635 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1636 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1637 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1638 6ced4176 2019-07-12 stsp goto done;
1640 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1641 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1643 6ced4176 2019-07-12 stsp goto done;
1644 6ced4176 2019-07-12 stsp return NULL;
1647 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1649 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1650 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1651 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1652 6ced4176 2019-07-12 stsp goto done;
1655 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1656 6ced4176 2019-07-12 stsp in_repo_path);
1658 6ced4176 2019-07-12 stsp goto done;
1660 6ced4176 2019-07-12 stsp free(in_repo_path);
1661 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1663 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1665 6ced4176 2019-07-12 stsp goto done;
1667 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1668 6ced4176 2019-07-12 stsp /* Check out a single file. */
1669 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1670 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1671 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1672 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1673 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1674 6ced4176 2019-07-12 stsp goto done;
1676 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1677 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1678 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1679 6ced4176 2019-07-12 stsp goto done;
1682 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1683 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1685 6ced4176 2019-07-12 stsp return err;
1686 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1687 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1688 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1689 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1690 6ced4176 2019-07-12 stsp goto done;
1693 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1694 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1696 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1697 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1698 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1699 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1700 6ced4176 2019-07-12 stsp goto done;
1702 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1703 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1704 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1705 6ced4176 2019-07-12 stsp goto done;
1710 6ced4176 2019-07-12 stsp free(in_repo_path);
1711 6ced4176 2019-07-12 stsp if (err) {
1712 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1713 6ced4176 2019-07-12 stsp free(*tree_relpath);
1714 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1715 6ced4176 2019-07-12 stsp free(*tree_id);
1716 6ced4176 2019-07-12 stsp *tree_id = NULL;
1718 07ed472d 2019-07-12 stsp return err;
1721 07ed472d 2019-07-12 stsp static const struct got_error *
1722 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1723 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1724 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1725 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1727 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1728 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1729 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1730 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1731 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1733 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1735 07ed472d 2019-07-12 stsp goto done;
1737 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1738 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1740 07ed472d 2019-07-12 stsp goto done;
1742 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1744 07ed472d 2019-07-12 stsp goto done;
1746 07ed472d 2019-07-12 stsp if (entry_name &&
1747 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1748 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1749 07ed472d 2019-07-12 stsp goto done;
1752 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1753 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1754 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1755 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1756 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1757 07ed472d 2019-07-12 stsp arg.repo = repo;
1758 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1759 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1760 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1761 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1762 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1763 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1766 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1767 07ed472d 2019-07-12 stsp if (commit)
1768 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1769 6ced4176 2019-07-12 stsp return err;
1772 9d31a1d8 2018-03-11 stsp const struct got_error *
1773 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1774 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1775 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1776 f2ea84fa 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1778 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1779 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1780 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1781 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1782 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1783 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1784 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1785 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1786 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1787 f2ea84fa 2019-07-27 stsp int entry_type;
1788 f2ea84fa 2019-07-27 stsp char *relpath;
1789 f2ea84fa 2019-07-27 stsp char *entry_name;
1790 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1791 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1793 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1795 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1797 9d31a1d8 2018-03-11 stsp return err;
1799 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1800 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1801 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1802 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1803 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1804 f2ea84fa 2019-07-27 stsp goto done;
1807 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1808 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1809 f2ea84fa 2019-07-27 stsp if (err) {
1810 f2ea84fa 2019-07-27 stsp free(tpd);
1811 6ced4176 2019-07-12 stsp goto done;
1814 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1815 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1816 f2ea84fa 2019-07-27 stsp if (err) {
1817 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1818 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1819 f2ea84fa 2019-07-27 stsp free(tpd);
1820 f2ea84fa 2019-07-27 stsp goto done;
1823 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1825 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1829 51514078 2018-12-25 stsp * Read the file index.
1830 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1831 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1833 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1835 c4cdcb68 2019-04-03 stsp goto done;
1837 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1838 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1839 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1841 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1842 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1843 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1847 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1848 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1849 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1850 f2ea84fa 2019-07-27 stsp bbc_arg.path_len = strlen(pe->path);
1851 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1852 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1853 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1854 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1858 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1860 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1861 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1862 af12c6b9 2019-06-04 stsp err = sync_err;
1864 9c6338c4 2019-06-02 stsp free(fileindex_path);
1866 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1867 9d31a1d8 2018-03-11 stsp if (commit)
1868 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1869 5ade8233 2019-07-12 stsp if (fileindex)
1870 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1871 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1872 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1873 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1874 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1875 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1876 f2ea84fa 2019-07-27 stsp free(tpd);
1878 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1879 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1880 9d31a1d8 2018-03-11 stsp err = unlockerr;
1881 f8d1f275 2019-02-04 stsp return err;
1884 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1885 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1886 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1887 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1888 234035bc 2019-06-01 stsp void *progress_arg;
1889 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1890 234035bc 2019-06-01 stsp void *cancel_arg;
1891 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1894 234035bc 2019-06-01 stsp static const struct got_error *
1895 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1896 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1897 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1898 234035bc 2019-06-01 stsp struct got_repository *repo)
1900 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1901 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1902 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1903 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1904 234035bc 2019-06-01 stsp struct stat sb;
1905 234035bc 2019-06-01 stsp unsigned char status;
1906 234035bc 2019-06-01 stsp int local_changes_subsumed;
1908 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1909 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1910 1ee397ad 2019-07-12 stsp if (ie == NULL)
1911 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1912 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1914 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1915 234035bc 2019-06-01 stsp path2) == -1)
1916 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1918 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1920 234035bc 2019-06-01 stsp goto done;
1922 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1923 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1924 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1925 234035bc 2019-06-01 stsp goto done;
1927 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1928 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1929 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1930 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1931 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1932 234035bc 2019-06-01 stsp goto done;
1935 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1936 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1937 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1938 234035bc 2019-06-01 stsp } else if (blob1) {
1939 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1940 1ee397ad 2019-07-12 stsp if (ie == NULL)
1941 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1942 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1944 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1945 2b92fad7 2019-06-02 stsp path1) == -1)
1946 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1948 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1950 2b92fad7 2019-06-02 stsp goto done;
1952 2b92fad7 2019-06-02 stsp switch (status) {
1953 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1954 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1955 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1957 1ee397ad 2019-07-12 stsp goto done;
1958 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1960 2b92fad7 2019-06-02 stsp goto done;
1962 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1964 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1965 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1966 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1967 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1969 1ee397ad 2019-07-12 stsp goto done;
1971 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1973 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1974 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1975 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1976 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1977 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1979 1ee397ad 2019-07-12 stsp goto done;
1981 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1982 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
1984 1ee397ad 2019-07-12 stsp goto done;
1989 234035bc 2019-06-01 stsp } else if (blob2) {
1990 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1991 234035bc 2019-06-01 stsp path2) == -1)
1992 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1993 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1995 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1998 234035bc 2019-06-01 stsp goto done;
1999 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2000 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2001 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2002 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2003 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2004 1ee397ad 2019-07-12 stsp status, path2);
2005 234035bc 2019-06-01 stsp goto done;
2007 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2008 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2009 818c7501 2019-07-11 stsp a->commit_id2, repo,
2010 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2011 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2012 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2013 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2014 234035bc 2019-06-01 stsp blob2, 0);
2016 234035bc 2019-06-01 stsp goto done;
2019 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2020 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2021 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2022 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2023 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2024 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2026 234035bc 2019-06-01 stsp goto done;
2027 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2028 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2030 234035bc 2019-06-01 stsp goto done;
2031 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2032 2b92fad7 2019-06-02 stsp if (err) {
2033 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2034 2b92fad7 2019-06-02 stsp goto done;
2039 234035bc 2019-06-01 stsp free(ondisk_path);
2040 234035bc 2019-06-01 stsp return err;
2043 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2044 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2045 234035bc 2019-06-01 stsp struct got_repository *repo;
2048 234035bc 2019-06-01 stsp static const struct got_error *
2049 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2051 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2052 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2053 234035bc 2019-06-01 stsp unsigned char status;
2054 234035bc 2019-06-01 stsp struct stat sb;
2055 234035bc 2019-06-01 stsp char *ondisk_path;
2057 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2058 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2059 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2060 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2062 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2064 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2066 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2067 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2069 234035bc 2019-06-01 stsp return err;
2070 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2071 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2073 234035bc 2019-06-01 stsp return NULL;
2076 818c7501 2019-07-11 stsp static const struct got_error *
2077 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2078 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2079 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2080 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2081 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2083 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2084 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2085 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2086 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2088 03415a1a 2019-06-02 stsp if (commit_id1) {
2089 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2090 03415a1a 2019-06-02 stsp worktree->path_prefix);
2092 03415a1a 2019-06-02 stsp goto done;
2094 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2096 03415a1a 2019-06-02 stsp goto done;
2099 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2100 234035bc 2019-06-01 stsp worktree->path_prefix);
2102 234035bc 2019-06-01 stsp goto done;
2104 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2106 234035bc 2019-06-01 stsp goto done;
2108 234035bc 2019-06-01 stsp arg.worktree = worktree;
2109 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2110 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2111 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2112 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2113 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2114 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2115 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2116 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2117 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2118 af12c6b9 2019-06-04 stsp err = sync_err;
2120 234035bc 2019-06-01 stsp if (tree1)
2121 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2122 234035bc 2019-06-01 stsp if (tree2)
2123 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2124 818c7501 2019-07-11 stsp return err;
2127 818c7501 2019-07-11 stsp const struct got_error *
2128 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2129 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2130 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2131 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2133 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2134 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2135 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2136 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2138 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2140 818c7501 2019-07-11 stsp return err;
2142 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2144 818c7501 2019-07-11 stsp goto done;
2146 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2147 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2148 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2149 818c7501 2019-07-11 stsp &mok_arg);
2151 818c7501 2019-07-11 stsp goto done;
2153 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2154 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2156 818c7501 2019-07-11 stsp if (fileindex)
2157 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2158 818c7501 2019-07-11 stsp free(fileindex_path);
2159 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2160 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2161 234035bc 2019-06-01 stsp err = unlockerr;
2162 234035bc 2019-06-01 stsp return err;
2165 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2166 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2167 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2168 927df6b7 2019-02-10 stsp const char *status_path;
2169 927df6b7 2019-02-10 stsp size_t status_path_len;
2170 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2171 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2172 f8d1f275 2019-02-04 stsp void *status_arg;
2173 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2174 f8d1f275 2019-02-04 stsp void *cancel_arg;
2177 f8d1f275 2019-02-04 stsp static const struct got_error *
2178 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2179 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2180 927df6b7 2019-02-10 stsp struct got_repository *repo)
2182 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2183 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2184 927df6b7 2019-02-10 stsp struct stat sb;
2185 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2187 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2188 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2189 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2190 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2191 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2192 016a88dd 2019-05-13 stsp &commit_id);
2194 927df6b7 2019-02-10 stsp return err;
2197 927df6b7 2019-02-10 stsp static const struct got_error *
2198 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2199 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2201 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2202 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2203 f8d1f275 2019-02-04 stsp char *abspath;
2205 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2206 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2208 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
2209 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2210 927df6b7 2019-02-10 stsp return NULL;
2212 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2213 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2214 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2215 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2217 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2218 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2219 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2222 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2224 f8d1f275 2019-02-04 stsp free(abspath);
2225 9d31a1d8 2018-03-11 stsp return err;
2228 f8d1f275 2019-02-04 stsp static const struct got_error *
2229 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2231 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2232 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2233 2ec1f75b 2019-03-26 stsp unsigned char status;
2235 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2236 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2238 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2239 927df6b7 2019-02-10 stsp return NULL;
2241 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2242 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2243 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2244 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2246 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2247 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2248 016a88dd 2019-05-13 stsp &commit_id);
2251 f8d1f275 2019-02-04 stsp static const struct got_error *
2252 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2254 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2255 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2256 f8d1f275 2019-02-04 stsp char *path = NULL;
2258 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2259 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2261 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2262 2c201a36 2019-02-10 stsp return NULL;
2264 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2265 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2266 927df6b7 2019-02-10 stsp return NULL;
2268 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2269 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2270 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2272 f8d1f275 2019-02-04 stsp path = de->d_name;
2275 c577a9ce 2019-07-27 stsp if (got_path_is_child(path, a->status_path, a->status_path_len))
2276 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2277 c577a9ce 2019-07-27 stsp path, NULL, NULL);
2278 f8d1f275 2019-02-04 stsp if (parent_path[0])
2279 f8d1f275 2019-02-04 stsp free(path);
2280 b72f483a 2019-02-05 stsp return err;
2283 347d1d3e 2019-07-12 stsp static const struct got_error *
2284 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2285 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2286 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2288 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2289 abb4604f 2019-07-27 stsp struct stat sb;
2291 abb4604f 2019-07-27 stsp ie = got_fileindex_entry_get(fileindex, path);
2293 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2294 abb4604f 2019-07-27 stsp status_arg, repo);
2296 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2297 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2298 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2299 abb4604f 2019-07-27 stsp return NULL;
2302 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2303 abb4604f 2019-07-27 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2304 abb4604f 2019-07-27 stsp NULL, NULL);
2306 abb4604f 2019-07-27 stsp return NULL;
2309 abb4604f 2019-07-27 stsp static const struct got_error *
2310 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2311 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2312 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2313 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2315 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2316 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2317 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2318 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2319 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2321 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2322 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2323 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2324 c513d110 2019-02-05 stsp goto done;
2326 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2327 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2328 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2329 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2331 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2332 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2334 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2335 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2336 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2337 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2338 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2339 abb4604f 2019-07-27 stsp arg.status_path = path;
2340 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2341 abb4604f 2019-07-27 stsp arg.repo = repo;
2342 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2343 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2344 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2345 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2346 abb4604f 2019-07-27 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2347 abb4604f 2019-07-27 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2350 f8d1f275 2019-02-04 stsp if (workdir)
2351 f8d1f275 2019-02-04 stsp closedir(workdir);
2352 927df6b7 2019-02-10 stsp free(ondisk_path);
2353 347d1d3e 2019-07-12 stsp return err;
2356 347d1d3e 2019-07-12 stsp const struct got_error *
2357 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2358 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2359 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2360 72ea6654 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2362 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2363 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2364 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2365 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2367 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2369 347d1d3e 2019-07-12 stsp return err;
2371 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2372 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2373 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2377 f8d1f275 2019-02-04 stsp free(fileindex_path);
2378 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2379 f8d1f275 2019-02-04 stsp return err;
2382 6c7ab921 2019-03-18 stsp const struct got_error *
2383 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2384 6c7ab921 2019-03-18 stsp const char *arg)
2386 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2387 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2388 6c7ab921 2019-03-18 stsp size_t len;
2390 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2392 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2393 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2394 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2395 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2396 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2397 d0710d08 2019-07-22 stsp if (cwd == NULL)
2398 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2399 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2400 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2401 d0710d08 2019-07-22 stsp goto done;
2405 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2406 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2407 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2408 6c7ab921 2019-03-18 stsp goto done;
2411 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2412 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2413 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2415 f6d88e1a 2019-05-29 stsp goto done;
2417 f6d88e1a 2019-05-29 stsp path = strdup("");
2418 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2419 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2420 f6d88e1a 2019-05-29 stsp goto done;
2424 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2425 6c7ab921 2019-03-18 stsp len = strlen(path);
2426 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2427 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2431 6c7ab921 2019-03-18 stsp free(resolved);
2432 d0710d08 2019-07-22 stsp free(cwd);
2433 6c7ab921 2019-03-18 stsp if (err == NULL)
2434 6c7ab921 2019-03-18 stsp *wt_path = path;
2436 6c7ab921 2019-03-18 stsp free(path);
2437 d00136be 2019-03-26 stsp return err;
2440 07f5b47a 2019-06-02 stsp static const struct got_error *
2441 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2442 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2443 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2445 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2446 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2448 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2449 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2450 07f5b47a 2019-06-02 stsp return NULL;
2452 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2454 07f5b47a 2019-06-02 stsp return err;
2456 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2457 07f5b47a 2019-06-02 stsp if (err) {
2458 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2459 07f5b47a 2019-06-02 stsp return err;
2462 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2465 d00136be 2019-03-26 stsp const struct got_error *
2466 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2467 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2468 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2469 031a5338 2019-03-26 stsp struct got_repository *repo)
2471 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2472 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2473 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2474 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2476 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2478 d00136be 2019-03-26 stsp return err;
2480 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2482 d00136be 2019-03-26 stsp goto done;
2484 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2485 1dd54920 2019-05-11 stsp char *relpath;
2486 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2487 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2490 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2491 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2492 1dd54920 2019-05-11 stsp free(relpath);
2496 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2497 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2498 af12c6b9 2019-06-04 stsp err = sync_err;
2500 fb399478 2019-07-12 stsp free(fileindex_path);
2501 d00136be 2019-03-26 stsp if (fileindex)
2502 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2503 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2504 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2505 d00136be 2019-03-26 stsp err = unlockerr;
2506 6c7ab921 2019-03-18 stsp return err;
2509 17ed4618 2019-06-02 stsp static const struct got_error *
2510 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2511 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2512 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2513 17ed4618 2019-06-02 stsp struct got_repository *repo)
2515 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2516 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2517 17ed4618 2019-06-02 stsp unsigned char status;
2518 17ed4618 2019-06-02 stsp struct stat sb;
2520 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2521 17ed4618 2019-06-02 stsp if (ie == NULL)
2522 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2524 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2526 17ed4618 2019-06-02 stsp return err;
2528 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2529 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2530 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2531 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2532 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2533 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2534 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2537 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2538 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2540 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2541 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2544 2ec1f75b 2019-03-26 stsp const struct got_error *
2545 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2546 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2547 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2548 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2550 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2551 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2552 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2553 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2555 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2557 2ec1f75b 2019-03-26 stsp return err;
2559 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2561 2ec1f75b 2019-03-26 stsp goto done;
2563 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2564 17ed4618 2019-06-02 stsp char *relpath;
2565 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2566 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2569 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2570 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2571 17ed4618 2019-06-02 stsp free(relpath);
2575 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2576 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2577 af12c6b9 2019-06-04 stsp err = sync_err;
2579 fb399478 2019-07-12 stsp free(fileindex_path);
2580 2ec1f75b 2019-03-26 stsp if (fileindex)
2581 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2582 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2583 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2584 2ec1f75b 2019-03-26 stsp err = unlockerr;
2585 2ec1f75b 2019-03-26 stsp return err;
2588 e20a8b6f 2019-06-04 stsp static const struct got_error *
2589 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2590 a129376b 2019-03-28 stsp const char *ondisk_path,
2591 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2592 a129376b 2019-03-28 stsp struct got_repository *repo)
2594 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2595 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2596 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2597 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2598 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2599 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2600 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2601 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2602 a129376b 2019-03-28 stsp unsigned char status;
2603 a129376b 2019-03-28 stsp struct stat sb;
2605 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2606 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2608 a129376b 2019-03-28 stsp goto done;
2610 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2611 a129376b 2019-03-28 stsp if (ie == NULL) {
2612 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2613 a129376b 2019-03-28 stsp goto done;
2616 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2617 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2618 a129376b 2019-03-28 stsp if (err) {
2619 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2620 a129376b 2019-03-28 stsp goto done;
2621 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2622 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2623 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2624 e20a8b6f 2019-06-04 stsp goto done;
2627 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2628 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2629 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2630 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2631 a129376b 2019-03-28 stsp goto done;
2634 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2635 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2636 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2637 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2638 a129376b 2019-03-28 stsp goto done;
2641 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2642 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2643 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2644 a129376b 2019-03-28 stsp goto done;
2649 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2650 a129376b 2019-03-28 stsp tree_path);
2652 a129376b 2019-03-28 stsp goto done;
2654 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2656 a129376b 2019-03-28 stsp goto done;
2658 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2659 a129376b 2019-03-28 stsp if (te_name == NULL) {
2660 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2661 a129376b 2019-03-28 stsp goto done;
2664 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2666 a129376b 2019-03-28 stsp goto done;
2668 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2669 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2670 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2671 a129376b 2019-03-28 stsp goto done;
2674 a129376b 2019-03-28 stsp switch (status) {
2675 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2676 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2678 1ee397ad 2019-07-12 stsp goto done;
2679 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2681 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2682 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2683 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2684 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2685 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2686 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2687 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2689 a129376b 2019-03-28 stsp goto done;
2690 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2691 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2692 a129376b 2019-03-28 stsp progress_arg);
2694 a129376b 2019-03-28 stsp goto done;
2695 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2696 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2697 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2699 a129376b 2019-03-28 stsp goto done;
2704 a129376b 2019-03-28 stsp goto done;
2707 a129376b 2019-03-28 stsp free(relpath);
2708 e20a8b6f 2019-06-04 stsp free(parent_path);
2709 a129376b 2019-03-28 stsp free(tree_path);
2711 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2713 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2714 a129376b 2019-03-28 stsp free(tree_id);
2715 e20a8b6f 2019-06-04 stsp return err;
2718 e20a8b6f 2019-06-04 stsp const struct got_error *
2719 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2720 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2721 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2722 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2724 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2725 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2726 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2727 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2728 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2730 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2732 e20a8b6f 2019-06-04 stsp return err;
2734 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2736 e20a8b6f 2019-06-04 stsp goto done;
2738 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2739 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2740 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2744 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2745 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2746 e20a8b6f 2019-06-04 stsp err = sync_err;
2748 fb399478 2019-07-12 stsp free(fileindex_path);
2749 a129376b 2019-03-28 stsp if (fileindex)
2750 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2751 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2752 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2753 a129376b 2019-03-28 stsp err = unlockerr;
2754 c4296144 2019-05-09 stsp return err;
2757 cf066bf8 2019-05-09 stsp static void
2758 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2760 24519714 2019-05-09 stsp free(ct->path);
2761 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2762 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2763 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2764 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2765 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2769 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2770 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2771 24519714 2019-05-09 stsp struct got_repository *repo;
2772 24519714 2019-05-09 stsp struct got_worktree *worktree;
2775 c4296144 2019-05-09 stsp static const struct got_error *
2776 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2777 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2779 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2780 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2781 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2782 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2783 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2784 768aea60 2019-05-09 stsp struct stat sb;
2786 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2787 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2789 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2790 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2791 c4296144 2019-05-09 stsp return NULL;
2793 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2794 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2795 036813ee 2019-05-09 stsp goto done;
2797 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2798 036813ee 2019-05-09 stsp parent_path = strdup("");
2799 69960a46 2019-05-09 stsp if (parent_path == NULL)
2800 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2802 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2804 69960a46 2019-05-09 stsp return err;
2807 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2808 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2809 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2810 c4296144 2019-05-09 stsp goto done;
2813 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2814 768aea60 2019-05-09 stsp relpath) == -1) {
2815 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2816 768aea60 2019-05-09 stsp goto done;
2818 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2819 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2821 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2822 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2823 768aea60 2019-05-09 stsp goto done;
2825 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2828 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2829 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2830 44d03001 2019-05-09 stsp relpath) == -1) {
2831 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2832 44d03001 2019-05-09 stsp goto done;
2835 cf066bf8 2019-05-09 stsp ct->status = status;
2836 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2837 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2838 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2839 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2840 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2841 b416585c 2019-05-13 stsp goto done;
2843 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2844 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2845 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2846 036813ee 2019-05-09 stsp goto done;
2849 24519714 2019-05-09 stsp ct->path = strdup(path);
2850 24519714 2019-05-09 stsp if (ct->path == NULL) {
2851 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2852 24519714 2019-05-09 stsp goto done;
2854 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2856 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2857 ed175427 2019-05-09 stsp free_commitable(ct);
2858 24519714 2019-05-09 stsp free(parent_path);
2859 036813ee 2019-05-09 stsp free(path);
2860 a129376b 2019-03-28 stsp return err;
2863 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2864 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2865 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2866 036813ee 2019-05-09 stsp struct got_repository *);
2868 ed175427 2019-05-09 stsp static const struct got_error *
2869 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2870 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2871 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2872 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2873 afa376bf 2019-05-09 stsp struct got_repository *repo)
2875 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2876 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2877 036813ee 2019-05-09 stsp char *subpath;
2879 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2880 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2881 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2883 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2885 ed175427 2019-05-09 stsp return err;
2887 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2888 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2889 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2890 036813ee 2019-05-09 stsp free(subpath);
2891 036813ee 2019-05-09 stsp return err;
2894 036813ee 2019-05-09 stsp static const struct got_error *
2895 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2897 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2898 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2900 036813ee 2019-05-09 stsp *match = 0;
2902 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
2903 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2904 0f63689d 2019-05-10 stsp return NULL;
2907 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2909 0f63689d 2019-05-10 stsp return err;
2910 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2911 036813ee 2019-05-09 stsp free(ct_parent_path);
2912 036813ee 2019-05-09 stsp return err;
2915 768aea60 2019-05-09 stsp static mode_t
2916 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2918 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2921 036813ee 2019-05-09 stsp static const struct got_error *
2922 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2923 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2925 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2927 036813ee 2019-05-09 stsp *new_te = NULL;
2929 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2931 036813ee 2019-05-09 stsp goto done;
2933 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2935 036813ee 2019-05-09 stsp free((*new_te)->id);
2936 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2937 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2938 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2939 036813ee 2019-05-09 stsp goto done;
2942 036813ee 2019-05-09 stsp if (err && *new_te) {
2943 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2944 036813ee 2019-05-09 stsp *new_te = NULL;
2946 036813ee 2019-05-09 stsp return err;
2949 036813ee 2019-05-09 stsp static const struct got_error *
2950 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2951 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2953 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2954 036813ee 2019-05-09 stsp char *ct_name;
2956 036813ee 2019-05-09 stsp *new_te = NULL;
2958 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2959 036813ee 2019-05-09 stsp if (*new_te == NULL)
2960 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2962 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2963 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2964 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2965 036813ee 2019-05-09 stsp goto done;
2967 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2968 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2969 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2970 036813ee 2019-05-09 stsp goto done;
2973 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2975 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2976 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2977 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2978 036813ee 2019-05-09 stsp goto done;
2981 036813ee 2019-05-09 stsp if (err && *new_te) {
2982 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2983 036813ee 2019-05-09 stsp *new_te = NULL;
2985 036813ee 2019-05-09 stsp return err;
2988 036813ee 2019-05-09 stsp static const struct got_error *
2989 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2990 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2992 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2993 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2995 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2997 036813ee 2019-05-09 stsp return err;
2998 036813ee 2019-05-09 stsp if (new_pe == NULL)
2999 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3000 036813ee 2019-05-09 stsp return NULL;
3003 afa376bf 2019-05-09 stsp static const struct got_error *
3004 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3005 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3007 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3008 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3009 afa376bf 2019-05-09 stsp ct_path++;
3010 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3013 036813ee 2019-05-09 stsp static const struct got_error *
3014 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3015 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3017 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3018 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3019 44d03001 2019-05-09 stsp char *te_path;
3021 44d03001 2019-05-09 stsp *modified = 0;
3023 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3024 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3025 44d03001 2019-05-09 stsp te->name) == -1)
3026 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3028 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3029 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3030 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3031 44d03001 2019-05-09 stsp strlen(te_path));
3032 44d03001 2019-05-09 stsp if (*modified)
3036 44d03001 2019-05-09 stsp free(te_path);
3037 44d03001 2019-05-09 stsp return err;
3040 44d03001 2019-05-09 stsp static const struct got_error *
3041 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3042 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3043 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3045 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3046 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3048 036813ee 2019-05-09 stsp *ctp = NULL;
3050 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3051 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3052 036813ee 2019-05-09 stsp char *ct_name = NULL;
3053 036813ee 2019-05-09 stsp int path_matches;
3055 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
3056 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
3059 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3062 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3064 036813ee 2019-05-09 stsp return err;
3065 036813ee 2019-05-09 stsp if (!path_matches)
3068 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3069 036813ee 2019-05-09 stsp if (ct_name == NULL)
3070 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3072 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3075 036813ee 2019-05-09 stsp *ctp = ct;
3079 2b496619 2019-07-10 stsp return err;
3082 2b496619 2019-07-10 stsp static const struct got_error *
3083 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3084 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3085 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3086 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3087 2b496619 2019-07-10 stsp struct got_repository *repo)
3089 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3090 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3091 2b496619 2019-07-10 stsp char *subtree_path;
3093 2b496619 2019-07-10 stsp *new_tep = NULL;
3095 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3096 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3097 2b496619 2019-07-10 stsp child_path) == -1)
3098 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3100 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3101 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3102 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3103 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3104 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3105 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3106 2b496619 2019-07-10 stsp goto done;
3108 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3109 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3110 2b496619 2019-07-10 stsp if (err) {
3111 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3112 2b496619 2019-07-10 stsp goto done;
3115 2b496619 2019-07-10 stsp free(subtree_path);
3116 2b496619 2019-07-10 stsp if (err == NULL)
3117 2b496619 2019-07-10 stsp *new_tep = new_te;
3118 036813ee 2019-05-09 stsp return err;
3121 036813ee 2019-05-09 stsp static const struct got_error *
3122 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3123 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3124 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3125 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3126 036813ee 2019-05-09 stsp struct got_repository *repo)
3128 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3129 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3130 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3131 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3132 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3133 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3135 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3136 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3137 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3139 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3140 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3141 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3142 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3144 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
3145 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3148 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3149 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3152 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3153 036813ee 2019-05-09 stsp pe->path);
3155 036813ee 2019-05-09 stsp goto done;
3157 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3158 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3159 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3161 036813ee 2019-05-09 stsp goto done;
3162 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3164 afa376bf 2019-05-09 stsp goto done;
3165 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3166 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3168 2f51b5b3 2019-05-09 stsp goto done;
3170 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3171 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3172 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3173 2b496619 2019-07-10 stsp == NULL) {
3174 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3175 2b496619 2019-07-10 stsp child_path, path_base_tree,
3176 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3179 2b496619 2019-07-10 stsp goto done;
3180 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3182 2b496619 2019-07-10 stsp goto done;
3187 2f51b5b3 2019-05-09 stsp if (base_tree) {
3188 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3189 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3190 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3191 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3193 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3194 44d03001 2019-05-09 stsp int modified;
3195 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3197 036813ee 2019-05-09 stsp goto done;
3198 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3199 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3201 2f51b5b3 2019-05-09 stsp goto done;
3202 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3203 44d03001 2019-05-09 stsp if (modified) {
3204 44d03001 2019-05-09 stsp free(new_te->id);
3205 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3206 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3207 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3209 44d03001 2019-05-09 stsp goto done;
3211 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3213 036813ee 2019-05-09 stsp goto done;
3217 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3218 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3220 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3221 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
3222 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3223 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3225 2f51b5b3 2019-05-09 stsp goto done;
3226 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3228 2f51b5b3 2019-05-09 stsp goto done;
3230 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3231 b416585c 2019-05-13 stsp status_arg);
3233 afa376bf 2019-05-09 stsp goto done;
3235 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3236 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3238 2f51b5b3 2019-05-09 stsp goto done;
3239 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3241 2f51b5b3 2019-05-09 stsp goto done;
3246 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3247 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3248 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3249 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3250 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3252 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3254 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3255 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3256 ed175427 2019-05-09 stsp return err;
3259 ebf99748 2019-05-09 stsp static const struct got_error *
3260 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3261 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3263 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3264 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3266 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3267 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3268 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3270 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3272 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3273 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3274 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3276 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3277 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3278 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3280 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3281 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3282 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3285 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3290 d56d26ce 2019-05-10 stsp return err;
3293 d56d26ce 2019-05-10 stsp static const struct got_error *
3294 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3295 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3297 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3298 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3299 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3300 1a36436d 2019-06-10 stsp char *path = NULL;
3301 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3303 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3304 1a36436d 2019-06-10 stsp ct_path++;
3307 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3308 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3310 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3311 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3312 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3313 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3315 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3316 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3318 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3319 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3320 1a36436d 2019-06-10 stsp char *slash;
3321 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3323 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3324 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3325 1a36436d 2019-06-10 stsp return NULL;
3327 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3328 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3329 1a36436d 2019-06-10 stsp if (path == NULL) {
3330 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3331 1a36436d 2019-06-10 stsp goto done;
3333 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3334 1a36436d 2019-06-10 stsp if (slash)
3335 1a36436d 2019-06-10 stsp *slash = '\0';
3337 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3339 1a36436d 2019-06-10 stsp goto done;
3341 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3342 1a36436d 2019-06-10 stsp head_commit_id, path);
3344 1a36436d 2019-06-10 stsp goto done;
3346 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3347 1a36436d 2019-06-10 stsp while (pid) {
3348 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3350 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3351 1a36436d 2019-06-10 stsp if (err) {
3352 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3353 1a36436d 2019-06-10 stsp goto done;
3354 1a36436d 2019-06-10 stsp err = NULL;
3358 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3360 1a36436d 2019-06-10 stsp goto done;
3362 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3363 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3367 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3368 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3370 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3373 1a36436d 2019-06-10 stsp goto done;
3375 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3376 1a36436d 2019-06-10 stsp commit = pcommit;
3377 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3381 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3382 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3384 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3385 1a36436d 2019-06-10 stsp goto done;
3386 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3389 1a36436d 2019-06-10 stsp if (commit)
3390 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3391 d56d26ce 2019-05-10 stsp free(id_in_head);
3393 1a36436d 2019-06-10 stsp free(path);
3394 1a36436d 2019-06-10 stsp return err;
3397 c4296144 2019-05-09 stsp const struct got_error *
3398 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3399 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3400 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3401 39cd0ff6 2019-07-12 stsp const char *ondisk_path, const char *author, const char *committer,
3402 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3403 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3404 afa376bf 2019-05-09 stsp struct got_repository *repo)
3406 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3407 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3408 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3409 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3410 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3411 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3412 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3413 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3414 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3415 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3416 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3418 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3420 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3422 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3424 588edf97 2019-05-10 stsp goto done;
3426 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3428 588edf97 2019-05-10 stsp goto done;
3430 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3431 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3436 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3437 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3441 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3442 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3443 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3444 cf066bf8 2019-05-09 stsp char *ondisk_path;
3446 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3447 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3450 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3451 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3452 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3453 cf066bf8 2019-05-09 stsp goto done;
3455 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3456 a7055788 2019-05-09 stsp free(ondisk_path);
3458 cf066bf8 2019-05-09 stsp goto done;
3461 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3462 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3463 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3465 036813ee 2019-05-09 stsp goto done;
3467 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3469 de18fc63 2019-05-09 stsp goto done;
3470 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3471 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3472 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3473 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3474 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3475 33ad4cbe 2019-05-12 jcs free(logmsg);
3477 9d40349a 2019-05-09 stsp goto done;
3479 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3480 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3481 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3482 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3483 09f5bd90 2019-05-09 stsp goto done;
3485 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3486 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3488 09f5bd90 2019-05-09 stsp goto done;
3489 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3491 ebf99748 2019-05-09 stsp goto done;
3492 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3493 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3494 09f5bd90 2019-05-09 stsp goto done;
3496 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3497 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3499 f2c16586 2019-05-09 stsp goto done;
3500 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3502 f2c16586 2019-05-09 stsp goto done;
3504 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3506 09f5bd90 2019-05-09 stsp goto done;
3508 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3510 ebf99748 2019-05-09 stsp goto done;
3512 588edf97 2019-05-10 stsp if (head_tree)
3513 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3514 588edf97 2019-05-10 stsp if (head_commit)
3515 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3516 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3517 2f17228e 2019-05-12 stsp if (head_ref2) {
3518 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3519 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3520 2f17228e 2019-05-12 stsp err = unlockerr;
3521 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3523 39cd0ff6 2019-07-12 stsp return err;
3526 39cd0ff6 2019-07-12 stsp const struct got_error *
3527 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3528 39cd0ff6 2019-07-12 stsp struct got_worktree *worktree, const char *ondisk_path,
3529 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
3530 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3531 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3532 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
3534 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3535 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3536 39cd0ff6 2019-07-12 stsp char *fileindex_path = NULL, *relpath = NULL;
3537 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3538 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3539 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
3540 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
3541 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3543 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
3545 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3547 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
3549 39cd0ff6 2019-07-12 stsp goto done;
3551 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3553 39cd0ff6 2019-07-12 stsp goto done;
3555 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3557 39cd0ff6 2019-07-12 stsp goto done;
3559 39cd0ff6 2019-07-12 stsp if (ondisk_path) {
3560 90e8619e 2019-07-25 stsp if (strcmp(ondisk_path, worktree->root_path) == 0) {
3561 90e8619e 2019-07-25 stsp relpath = strdup("");
3562 90e8619e 2019-07-25 stsp if (relpath == NULL) {
3563 90e8619e 2019-07-25 stsp err = got_error_from_errno("strdup");
3564 90e8619e 2019-07-25 stsp goto done;
3567 90e8619e 2019-07-25 stsp err = got_path_skip_common_ancestor(&relpath,
3568 90e8619e 2019-07-25 stsp worktree->root_path, ondisk_path);
3570 90e8619e 2019-07-25 stsp return err;
3574 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3576 39cd0ff6 2019-07-12 stsp goto done;
3578 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3579 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
3580 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
3581 39cd0ff6 2019-07-12 stsp err = worktree_status(worktree, relpath ? relpath : "",
3582 39cd0ff6 2019-07-12 stsp fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3584 39cd0ff6 2019-07-12 stsp goto done;
3586 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3587 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3588 39cd0ff6 2019-07-12 stsp goto done;
3591 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3592 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
3593 b50cabdf 2019-07-12 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3595 b50cabdf 2019-07-12 stsp goto done;
3598 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
3599 39cd0ff6 2019-07-12 stsp head_commit_id, worktree, ondisk_path, author, committer,
3600 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3602 39cd0ff6 2019-07-12 stsp goto done;
3604 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3605 93ec1b5f 2019-07-12 stsp fileindex);
3606 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3607 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
3608 39cd0ff6 2019-07-12 stsp err = sync_err;
3610 39cd0ff6 2019-07-12 stsp if (fileindex)
3611 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
3612 39cd0ff6 2019-07-12 stsp free(fileindex_path);
3613 39cd0ff6 2019-07-12 stsp free(relpath);
3614 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3615 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
3616 39cd0ff6 2019-07-12 stsp err = unlockerr;
3617 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3618 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
3619 39cd0ff6 2019-07-12 stsp free_commitable(ct);
3621 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
3622 c4296144 2019-05-09 stsp return err;
3625 8656d6c4 2019-05-20 stsp const char *
3626 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3628 8656d6c4 2019-05-20 stsp return ct->path;
3631 8656d6c4 2019-05-20 stsp unsigned int
3632 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3634 8656d6c4 2019-05-20 stsp return ct->status;
3637 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3638 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3639 818c7501 2019-07-11 stsp struct got_repository *repo;
3642 818c7501 2019-07-11 stsp static const struct got_error *
3643 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3645 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3646 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3647 818c7501 2019-07-11 stsp unsigned char status;
3648 818c7501 2019-07-11 stsp struct stat sb;
3649 818c7501 2019-07-11 stsp char *ondisk_path;
3651 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
3652 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3653 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
3654 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3656 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3658 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3660 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with modified or conflicted files. */
3661 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3662 818c7501 2019-07-11 stsp free(ondisk_path);
3664 818c7501 2019-07-11 stsp return err;
3666 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
3667 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3669 818c7501 2019-07-11 stsp return NULL;
3672 818c7501 2019-07-11 stsp const struct got_error *
3673 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3674 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3675 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
3676 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
3678 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3679 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3680 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3681 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3682 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3683 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3685 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3686 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3687 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3689 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3691 818c7501 2019-07-11 stsp return err;
3693 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3695 818c7501 2019-07-11 stsp goto done;
3697 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3698 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3699 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3702 818c7501 2019-07-11 stsp goto done;
3704 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3706 818c7501 2019-07-11 stsp goto done;
3708 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3710 818c7501 2019-07-11 stsp goto done;
3712 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3714 818c7501 2019-07-11 stsp goto done;
3716 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3719 818c7501 2019-07-11 stsp goto done;
3721 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3722 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3724 818c7501 2019-07-11 stsp goto done;
3725 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3727 818c7501 2019-07-11 stsp goto done;
3729 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3731 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3733 818c7501 2019-07-11 stsp goto done;
3735 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3737 818c7501 2019-07-11 stsp goto done;
3739 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3740 818c7501 2019-07-11 stsp worktree->base_commit_id);
3742 818c7501 2019-07-11 stsp goto done;
3743 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3745 818c7501 2019-07-11 stsp goto done;
3747 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3749 818c7501 2019-07-11 stsp goto done;
3751 818c7501 2019-07-11 stsp free(fileindex_path);
3752 818c7501 2019-07-11 stsp free(tmp_branch_name);
3753 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3754 818c7501 2019-07-11 stsp free(branch_ref_name);
3755 818c7501 2019-07-11 stsp if (branch_ref)
3756 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3757 818c7501 2019-07-11 stsp if (wt_branch)
3758 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3759 818c7501 2019-07-11 stsp if (err) {
3760 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3761 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3762 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3764 818c7501 2019-07-11 stsp if (*tmp_branch) {
3765 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3766 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3768 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3769 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3770 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3772 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3774 818c7501 2019-07-11 stsp return err;
3777 818c7501 2019-07-11 stsp const struct got_error *
3778 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3779 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3780 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
3781 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
3783 818c7501 2019-07-11 stsp const struct got_error *err;
3784 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3785 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3786 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3787 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
3789 818c7501 2019-07-11 stsp *commit_id = NULL;
3790 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
3791 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
3792 3e3a69f1 2019-07-25 stsp *branch = NULL;
3793 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3795 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
3797 3e3a69f1 2019-07-25 stsp return err;
3799 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3801 3e3a69f1 2019-07-25 stsp goto done;
3803 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3805 818c7501 2019-07-11 stsp return err;
3807 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3809 818c7501 2019-07-11 stsp goto done;
3811 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3813 818c7501 2019-07-11 stsp goto done;
3815 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3817 818c7501 2019-07-11 stsp goto done;
3819 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3821 818c7501 2019-07-11 stsp goto done;
3823 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3824 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3826 818c7501 2019-07-11 stsp goto done;
3828 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3830 818c7501 2019-07-11 stsp goto done;
3832 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
3834 818c7501 2019-07-11 stsp goto done;
3836 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
3837 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
3839 818c7501 2019-07-11 stsp goto done;
3841 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3843 818c7501 2019-07-11 stsp goto done;
3845 818c7501 2019-07-11 stsp free(commit_ref_name);
3846 818c7501 2019-07-11 stsp free(branch_ref_name);
3847 3e3a69f1 2019-07-25 stsp free(fileindex_path);
3848 818c7501 2019-07-11 stsp if (commit_ref)
3849 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3850 818c7501 2019-07-11 stsp if (branch_ref)
3851 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3852 818c7501 2019-07-11 stsp if (err) {
3853 818c7501 2019-07-11 stsp free(*commit_id);
3854 818c7501 2019-07-11 stsp *commit_id = NULL;
3855 818c7501 2019-07-11 stsp if (*tmp_branch) {
3856 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3857 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3859 818c7501 2019-07-11 stsp if (*new_base_branch) {
3860 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
3861 818c7501 2019-07-11 stsp *new_base_branch = NULL;
3863 818c7501 2019-07-11 stsp if (*branch) {
3864 818c7501 2019-07-11 stsp got_ref_close(*branch);
3865 818c7501 2019-07-11 stsp *branch = NULL;
3867 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3868 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3869 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3871 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
3873 818c7501 2019-07-11 stsp return err;
3876 818c7501 2019-07-11 stsp const struct got_error *
3877 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3879 818c7501 2019-07-11 stsp const struct got_error *err;
3880 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
3882 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3884 818c7501 2019-07-11 stsp return err;
3886 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3887 818c7501 2019-07-11 stsp free(tmp_branch_name);
3888 818c7501 2019-07-11 stsp return NULL;
3891 818c7501 2019-07-11 stsp static const struct got_error *
3892 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3893 818c7501 2019-07-11 stsp char **logmsg, void *arg)
3895 0ebf8283 2019-07-24 stsp *logmsg = arg;
3896 818c7501 2019-07-11 stsp return NULL;
3899 818c7501 2019-07-11 stsp static const struct got_error *
3900 818c7501 2019-07-11 stsp rebase_status(void *arg, unsigned char status, const char *path,
3901 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3903 818c7501 2019-07-11 stsp return NULL;
3906 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
3907 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
3908 01757395 2019-07-12 stsp void *progress_arg;
3909 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
3912 01757395 2019-07-12 stsp static const struct got_error *
3913 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
3915 01757395 2019-07-12 stsp const struct got_error *err;
3916 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
3918 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
3920 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
3922 01757395 2019-07-12 stsp return err;
3924 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
3925 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
3926 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
3927 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
3928 01757395 2019-07-12 stsp return NULL;
3930 01757395 2019-07-12 stsp p = strdup(path);
3931 01757395 2019-07-12 stsp if (p == NULL)
3932 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
3934 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3935 01757395 2019-07-12 stsp if (err || new == NULL)
3937 01757395 2019-07-12 stsp return err;
3941 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3943 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
3945 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
3946 01757395 2019-07-12 stsp free((char *)pe->path);
3948 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
3951 0ebf8283 2019-07-24 stsp static const struct got_error *
3952 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3953 0ebf8283 2019-07-24 stsp struct got_repository *repo)
3955 818c7501 2019-07-11 stsp const struct got_error *err;
3956 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3958 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3959 818c7501 2019-07-11 stsp if (err) {
3960 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
3961 818c7501 2019-07-11 stsp goto done;
3962 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3964 818c7501 2019-07-11 stsp goto done;
3965 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
3967 818c7501 2019-07-11 stsp goto done;
3969 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
3972 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
3974 818c7501 2019-07-11 stsp goto done;
3975 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
3976 818c7501 2019-07-11 stsp free(stored_id);
3977 818c7501 2019-07-11 stsp if (cmp != 0) {
3978 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3979 818c7501 2019-07-11 stsp goto done;
3983 0ebf8283 2019-07-24 stsp if (commit_ref)
3984 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
3985 0ebf8283 2019-07-24 stsp return err;
3988 0ebf8283 2019-07-24 stsp static const struct got_error *
3989 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
3990 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
3991 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3992 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
3993 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3994 3e3a69f1 2019-07-25 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3996 0ebf8283 2019-07-24 stsp const struct got_error *err;
3997 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
3998 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
3999 3e3a69f1 2019-07-25 stsp char *fileindex_path;
4001 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4003 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4005 0ebf8283 2019-07-24 stsp return err;
4007 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
4008 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
4009 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
4010 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
4011 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
4012 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
4013 818c7501 2019-07-11 stsp if (commit_ref)
4014 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4015 818c7501 2019-07-11 stsp return err;
4018 818c7501 2019-07-11 stsp const struct got_error *
4019 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4020 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4021 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4022 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4023 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4024 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4026 0ebf8283 2019-07-24 stsp const struct got_error *err;
4027 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4029 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4031 0ebf8283 2019-07-24 stsp return err;
4033 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4035 0ebf8283 2019-07-24 stsp goto done;
4037 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4038 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4039 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4041 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4042 0ebf8283 2019-07-24 stsp return err;
4045 0ebf8283 2019-07-24 stsp const struct got_error *
4046 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4047 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4048 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4049 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4050 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4051 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4053 0ebf8283 2019-07-24 stsp const struct got_error *err;
4054 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4056 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4058 0ebf8283 2019-07-24 stsp return err;
4060 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4062 0ebf8283 2019-07-24 stsp goto done;
4064 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4065 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4066 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4068 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4069 0ebf8283 2019-07-24 stsp return err;
4072 0ebf8283 2019-07-24 stsp static const struct got_error *
4073 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4074 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4075 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4077 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4079 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4080 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4081 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4082 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4083 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4084 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4085 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4087 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4088 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4090 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4092 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4094 3e3a69f1 2019-07-25 stsp return err;
4096 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4097 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4098 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4100 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4101 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4102 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4103 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4105 01757395 2019-07-12 stsp if (merged_paths) {
4106 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4107 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4108 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4109 8f8646e5 2019-07-25 stsp goto done;
4111 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4112 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4113 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4115 01757395 2019-07-12 stsp goto done;
4118 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4119 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4121 01757395 2019-07-12 stsp goto done;
4124 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4125 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4126 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4128 a0e95631 2019-07-12 stsp goto done;
4129 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4130 a0e95631 2019-07-12 stsp goto done;
4133 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4135 edd02c5e 2019-07-11 stsp goto done;
4137 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4139 818c7501 2019-07-11 stsp goto done;
4141 0ebf8283 2019-07-24 stsp if (new_logmsg)
4142 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4144 0ebf8283 2019-07-24 stsp logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4145 0ebf8283 2019-07-24 stsp if (logmsg == NULL)
4146 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
4148 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4149 a0e95631 2019-07-12 stsp worktree, NULL, got_object_commit_get_author(orig_commit),
4150 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4151 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4153 a0e95631 2019-07-12 stsp goto done;
4155 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4157 a0e95631 2019-07-12 stsp goto done;
4159 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4161 a0e95631 2019-07-12 stsp goto done;
4163 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4164 93ec1b5f 2019-07-12 stsp fileindex);
4165 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4166 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4167 a0e95631 2019-07-12 stsp err = sync_err;
4169 a0e95631 2019-07-12 stsp free(fileindex_path);
4170 a0e95631 2019-07-12 stsp free(head_commit_id);
4171 a0e95631 2019-07-12 stsp if (head_ref)
4172 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4173 818c7501 2019-07-11 stsp if (err) {
4174 818c7501 2019-07-11 stsp free(*new_commit_id);
4175 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4177 818c7501 2019-07-11 stsp return err;
4180 818c7501 2019-07-11 stsp const struct got_error *
4181 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4182 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4183 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4184 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4185 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4187 0ebf8283 2019-07-24 stsp const struct got_error *err;
4188 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4189 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4190 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4192 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4194 0ebf8283 2019-07-24 stsp return err;
4196 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4198 0ebf8283 2019-07-24 stsp goto done;
4199 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4201 0ebf8283 2019-07-24 stsp goto done;
4202 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4203 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4204 0ebf8283 2019-07-24 stsp goto done;
4207 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4208 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4210 0ebf8283 2019-07-24 stsp if (commit_ref)
4211 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4212 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4213 0ebf8283 2019-07-24 stsp free(commit_id);
4214 0ebf8283 2019-07-24 stsp return err;
4217 0ebf8283 2019-07-24 stsp const struct got_error *
4218 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4219 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4220 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4221 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4222 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4223 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4225 0ebf8283 2019-07-24 stsp const struct got_error *err;
4226 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4227 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4228 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4230 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4232 0ebf8283 2019-07-24 stsp return err;
4234 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4236 0ebf8283 2019-07-24 stsp goto done;
4237 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4239 0ebf8283 2019-07-24 stsp goto done;
4240 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4241 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4242 0ebf8283 2019-07-24 stsp goto done;
4245 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4246 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4248 0ebf8283 2019-07-24 stsp if (commit_ref)
4249 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4250 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4251 0ebf8283 2019-07-24 stsp free(commit_id);
4252 0ebf8283 2019-07-24 stsp return err;
4255 0ebf8283 2019-07-24 stsp const struct got_error *
4256 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4257 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4259 3e3a69f1 2019-07-25 stsp if (fileindex)
4260 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4261 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4264 69844fba 2019-07-11 stsp static const struct got_error *
4265 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4267 69844fba 2019-07-11 stsp const struct got_error *err;
4268 69844fba 2019-07-11 stsp struct got_reference *ref;
4270 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4271 69844fba 2019-07-11 stsp if (err) {
4272 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4273 69844fba 2019-07-11 stsp return NULL;
4274 69844fba 2019-07-11 stsp return err;
4277 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4278 69844fba 2019-07-11 stsp got_ref_close(ref);
4279 69844fba 2019-07-11 stsp return err;
4282 69844fba 2019-07-11 stsp static const struct got_error *
4283 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4285 69844fba 2019-07-11 stsp const struct got_error *err;
4286 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4287 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4289 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4291 69844fba 2019-07-11 stsp goto done;
4292 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4294 69844fba 2019-07-11 stsp goto done;
4296 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4298 69844fba 2019-07-11 stsp goto done;
4299 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4301 69844fba 2019-07-11 stsp goto done;
4303 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4305 69844fba 2019-07-11 stsp goto done;
4306 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4308 69844fba 2019-07-11 stsp goto done;
4310 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4312 69844fba 2019-07-11 stsp goto done;
4313 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4315 69844fba 2019-07-11 stsp goto done;
4318 69844fba 2019-07-11 stsp free(tmp_branch_name);
4319 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4320 69844fba 2019-07-11 stsp free(branch_ref_name);
4321 69844fba 2019-07-11 stsp free(commit_ref_name);
4322 69844fba 2019-07-11 stsp return err;
4325 818c7501 2019-07-11 stsp const struct got_error *
4326 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4327 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4328 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4329 818c7501 2019-07-11 stsp struct got_repository *repo)
4331 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4332 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4334 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4336 818c7501 2019-07-11 stsp return err;
4338 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4340 818c7501 2019-07-11 stsp goto done;
4342 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4344 818c7501 2019-07-11 stsp goto done;
4346 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4348 818c7501 2019-07-11 stsp goto done;
4350 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4352 3e3a69f1 2019-07-25 stsp if (fileindex)
4353 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4354 818c7501 2019-07-11 stsp free(new_head_commit_id);
4355 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4356 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4357 818c7501 2019-07-11 stsp err = unlockerr;
4358 818c7501 2019-07-11 stsp return err;
4361 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
4362 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
4363 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4366 818c7501 2019-07-11 stsp static const struct got_error *
4367 818c7501 2019-07-11 stsp collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4368 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
4370 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
4371 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4372 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
4373 818c7501 2019-07-11 stsp char *path = NULL;
4375 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
4376 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
4377 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
4378 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
4379 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
4380 818c7501 2019-07-11 stsp return NULL;
4382 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4383 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4385 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4386 818c7501 2019-07-11 stsp if (err || new == NULL)
4387 818c7501 2019-07-11 stsp free(path);
4388 818c7501 2019-07-11 stsp return err;
4391 818c7501 2019-07-11 stsp const struct got_error *
4392 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4393 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4394 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
4395 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4397 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4398 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4399 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4400 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4401 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
4402 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
4403 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
4404 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4406 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
4408 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4410 818c7501 2019-07-11 stsp return err;
4412 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4413 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4415 818c7501 2019-07-11 stsp goto done;
4417 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4419 818c7501 2019-07-11 stsp goto done;
4422 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4423 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4424 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4426 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4428 818c7501 2019-07-11 stsp goto done;
4430 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4432 818c7501 2019-07-11 stsp goto done;
4434 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4435 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4437 ca355955 2019-07-12 stsp goto done;
4439 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4441 ca355955 2019-07-12 stsp goto done;
4443 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4445 818c7501 2019-07-11 stsp goto done;
4447 347d1d3e 2019-07-12 stsp crp_arg.revertible_paths = &revertible_paths;
4448 347d1d3e 2019-07-12 stsp crp_arg.worktree = worktree;
4449 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4450 347d1d3e 2019-07-12 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4452 55bd499d 2019-07-12 stsp goto done;
4454 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4455 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
4456 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
4458 ca355955 2019-07-12 stsp goto sync;
4461 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4462 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4464 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4465 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4466 ca355955 2019-07-12 stsp err = sync_err;
4468 818c7501 2019-07-11 stsp got_ref_close(resolved);
4469 a3a2faf2 2019-07-12 stsp free(tree_id);
4470 818c7501 2019-07-11 stsp free(commit_id);
4471 0ebf8283 2019-07-24 stsp if (fileindex)
4472 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
4473 0ebf8283 2019-07-24 stsp free(fileindex_path);
4474 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4475 0ebf8283 2019-07-24 stsp free((char *)pe->path);
4476 0ebf8283 2019-07-24 stsp got_pathlist_free(&revertible_paths);
4478 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4479 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4480 0ebf8283 2019-07-24 stsp err = unlockerr;
4481 0ebf8283 2019-07-24 stsp return err;
4484 0ebf8283 2019-07-24 stsp const struct got_error *
4485 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4486 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4487 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
4488 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4490 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4491 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4492 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
4493 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
4494 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4495 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
4496 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
4497 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4499 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4500 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4501 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4502 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4504 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4506 0ebf8283 2019-07-24 stsp return err;
4508 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4510 0ebf8283 2019-07-24 stsp goto done;
4512 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
4513 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
4514 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4517 0ebf8283 2019-07-24 stsp goto done;
4519 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4521 0ebf8283 2019-07-24 stsp goto done;
4523 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4525 0ebf8283 2019-07-24 stsp goto done;
4527 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4528 0ebf8283 2019-07-24 stsp worktree);
4530 0ebf8283 2019-07-24 stsp goto done;
4532 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4535 0ebf8283 2019-07-24 stsp goto done;
4537 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4539 0ebf8283 2019-07-24 stsp goto done;
4541 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
4543 0ebf8283 2019-07-24 stsp goto done;
4545 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4546 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4548 0ebf8283 2019-07-24 stsp goto done;
4549 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
4551 0ebf8283 2019-07-24 stsp goto done;
4552 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4553 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
4554 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
4555 0ebf8283 2019-07-24 stsp goto done;
4558 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4559 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4561 0ebf8283 2019-07-24 stsp goto done;
4562 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
4564 0ebf8283 2019-07-24 stsp goto done;
4566 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4568 0ebf8283 2019-07-24 stsp goto done;
4570 0ebf8283 2019-07-24 stsp free(fileindex_path);
4571 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4572 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4573 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4574 0ebf8283 2019-07-24 stsp if (wt_branch)
4575 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
4576 0ebf8283 2019-07-24 stsp if (err) {
4577 0ebf8283 2019-07-24 stsp if (*branch_ref) {
4578 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
4579 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4581 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4582 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4583 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4585 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4586 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4587 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4588 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4590 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
4592 0ebf8283 2019-07-24 stsp return err;
4595 0ebf8283 2019-07-24 stsp const struct got_error *
4596 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
4597 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4599 3e3a69f1 2019-07-25 stsp if (fileindex)
4600 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4601 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
4604 0ebf8283 2019-07-24 stsp const struct got_error *
4605 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
4606 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
4608 0ebf8283 2019-07-24 stsp const struct got_error *err;
4609 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4611 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4613 0ebf8283 2019-07-24 stsp return err;
4615 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4616 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4617 0ebf8283 2019-07-24 stsp return NULL;
4620 0ebf8283 2019-07-24 stsp const struct got_error *
4621 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
4622 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
4623 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4624 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4626 0ebf8283 2019-07-24 stsp const struct got_error *err;
4627 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4628 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4629 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4630 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4631 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4633 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4634 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4635 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4636 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4638 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4640 3e3a69f1 2019-07-25 stsp return err;
4642 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4644 3e3a69f1 2019-07-25 stsp goto done;
4646 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4648 0ebf8283 2019-07-24 stsp return err;
4650 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4652 0ebf8283 2019-07-24 stsp goto done;
4654 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4656 0ebf8283 2019-07-24 stsp goto done;
4658 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4659 0ebf8283 2019-07-24 stsp worktree);
4661 0ebf8283 2019-07-24 stsp goto done;
4663 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4665 0ebf8283 2019-07-24 stsp goto done;
4667 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4669 0ebf8283 2019-07-24 stsp goto done;
4670 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4672 0ebf8283 2019-07-24 stsp goto done;
4674 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4676 0ebf8283 2019-07-24 stsp goto done;
4677 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4679 0ebf8283 2019-07-24 stsp goto done;
4681 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4683 0ebf8283 2019-07-24 stsp goto done;
4685 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4686 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4687 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4688 0ebf8283 2019-07-24 stsp if (commit_ref)
4689 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4690 0ebf8283 2019-07-24 stsp if (base_commit_ref)
4691 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
4692 0ebf8283 2019-07-24 stsp if (err) {
4693 0ebf8283 2019-07-24 stsp free(*commit_id);
4694 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4695 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4696 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4697 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4698 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4699 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4701 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4702 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4703 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4705 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
4707 0ebf8283 2019-07-24 stsp return err;
4710 0ebf8283 2019-07-24 stsp static const struct got_error *
4711 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4713 0ebf8283 2019-07-24 stsp const struct got_error *err;
4714 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4715 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4717 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4719 0ebf8283 2019-07-24 stsp goto done;
4720 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
4722 0ebf8283 2019-07-24 stsp goto done;
4724 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4725 0ebf8283 2019-07-24 stsp worktree);
4727 0ebf8283 2019-07-24 stsp goto done;
4728 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
4730 0ebf8283 2019-07-24 stsp goto done;
4732 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4734 0ebf8283 2019-07-24 stsp goto done;
4735 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
4737 0ebf8283 2019-07-24 stsp goto done;
4739 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4741 0ebf8283 2019-07-24 stsp goto done;
4742 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4744 0ebf8283 2019-07-24 stsp goto done;
4746 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4747 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4748 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4749 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4750 0ebf8283 2019-07-24 stsp return err;
4753 0ebf8283 2019-07-24 stsp const struct got_error *
4754 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
4755 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4756 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
4757 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4759 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
4760 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4761 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4762 0ebf8283 2019-07-24 stsp struct got_pathlist_head revertible_paths;
4763 0ebf8283 2019-07-24 stsp struct got_pathlist_entry *pe;
4764 0ebf8283 2019-07-24 stsp struct collect_revertible_paths_arg crp_arg;
4765 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
4767 0ebf8283 2019-07-24 stsp TAILQ_INIT(&revertible_paths);
4769 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4771 0ebf8283 2019-07-24 stsp return err;
4773 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4774 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
4776 0ebf8283 2019-07-24 stsp goto done;
4778 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4780 0ebf8283 2019-07-24 stsp goto done;
4782 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4784 0ebf8283 2019-07-24 stsp goto done;
4786 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4787 0ebf8283 2019-07-24 stsp worktree->path_prefix);
4789 0ebf8283 2019-07-24 stsp goto done;
4791 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4793 0ebf8283 2019-07-24 stsp goto done;
4795 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4797 0ebf8283 2019-07-24 stsp goto done;
4799 0ebf8283 2019-07-24 stsp crp_arg.revertible_paths = &revertible_paths;
4800 0ebf8283 2019-07-24 stsp crp_arg.worktree = worktree;
4801 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
4802 0ebf8283 2019-07-24 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4804 0ebf8283 2019-07-24 stsp goto done;
4806 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4807 0ebf8283 2019-07-24 stsp err = revert_file(worktree, fileindex, pe->path,
4808 0ebf8283 2019-07-24 stsp progress_cb, progress_arg, repo);
4810 0ebf8283 2019-07-24 stsp goto sync;
4813 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4814 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
4816 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4817 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
4818 0ebf8283 2019-07-24 stsp err = sync_err;
4820 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
4821 0ebf8283 2019-07-24 stsp free(tree_id);
4822 818c7501 2019-07-11 stsp free(fileindex_path);
4823 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4824 818c7501 2019-07-11 stsp free((char *)pe->path);
4825 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
4827 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4828 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4829 818c7501 2019-07-11 stsp err = unlockerr;
4830 818c7501 2019-07-11 stsp return err;
4833 0ebf8283 2019-07-24 stsp const struct got_error *
4834 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
4835 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4836 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
4838 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
4839 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
4840 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4842 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4844 0ebf8283 2019-07-24 stsp return err;
4846 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4847 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
4849 0ebf8283 2019-07-24 stsp goto done;
4851 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
4853 0ebf8283 2019-07-24 stsp goto done;
4855 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
4857 0ebf8283 2019-07-24 stsp goto done;
4859 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4861 0ebf8283 2019-07-24 stsp goto done;
4863 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4865 3e3a69f1 2019-07-25 stsp if (fileindex)
4866 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4867 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
4868 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4869 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4870 0ebf8283 2019-07-24 stsp err = unlockerr;
4871 0ebf8283 2019-07-24 stsp return err;
4874 0ebf8283 2019-07-24 stsp const struct got_error *
4875 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4876 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4878 0ebf8283 2019-07-24 stsp const struct got_error *err;
4879 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4881 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4883 0ebf8283 2019-07-24 stsp return err;
4885 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4887 0ebf8283 2019-07-24 stsp goto done;
4889 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4891 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4892 0ebf8283 2019-07-24 stsp return err;