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 a2e6d162 2019-07-27 stsp err = got_error_path(path, 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 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
146 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", 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 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 a2e6d162 2019-07-27 stsp err = got_error_path(path, 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 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
359 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
362 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
367 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
368 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
372 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
374 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
375 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
376 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
379 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
380 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
384 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
389 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
390 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
394 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
397 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
399 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
403 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
407 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 eaccb85f 2018-12-25 stsp base_commit_id_str);
412 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
416 eaccb85f 2018-12-25 stsp got_repo_close(repo);
417 7ac97322 2018-03-11 stsp free(path_got);
418 056e7441 2018-03-11 stsp free(path_lock);
419 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
420 c442a90d 2019-03-10 stsp free(uuidstr);
421 bd165944 2019-03-10 stsp free(formatstr);
423 6d9d28c3 2018-03-11 stsp if (fd != -1)
425 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
426 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
427 6d9d28c3 2018-03-11 stsp *worktree = NULL;
429 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
431 6d9d28c3 2018-03-11 stsp return err;
434 247140b2 2019-02-05 stsp const struct got_error *
435 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
437 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
440 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
441 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 247140b2 2019-02-05 stsp return err;
443 247140b2 2019-02-05 stsp if (*worktree)
444 247140b2 2019-02-05 stsp return NULL;
445 247140b2 2019-02-05 stsp path = dirname(path);
446 d1542a27 2019-02-05 stsp if (path == NULL)
447 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
448 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
453 3a6ce05a 2019-02-11 stsp const struct got_error *
454 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
456 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
457 c88eb298 2018-03-11 stsp free(worktree->root_path);
458 cde76477 2018-03-11 stsp free(worktree->repo_path);
459 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
460 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
461 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
462 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
463 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
464 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
465 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
466 6d9d28c3 2018-03-11 stsp free(worktree);
467 3a6ce05a 2019-02-11 stsp return err;
470 2fbdb5ae 2018-12-29 stsp const char *
471 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
473 c7f4312f 2019-02-05 stsp return worktree->root_path;
476 c7f4312f 2019-02-05 stsp const char *
477 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
479 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
482 49520a32 2018-12-29 stsp const char *
483 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
485 f609be2e 2018-12-29 stsp return worktree->path_prefix;
488 e5dc7198 2018-12-29 stsp const struct got_error *
489 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 e5dc7198 2018-12-29 stsp const char *path_prefix)
492 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
494 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
495 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
498 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
499 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
500 e5dc7198 2018-12-29 stsp free(absprefix);
501 e5dc7198 2018-12-29 stsp return NULL;
504 bc70eb79 2019-05-09 stsp const char *
505 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 36a38700 2019-05-10 stsp return worktree->head_ref_name;
510 024e9686 2019-05-14 stsp const struct got_error *
511 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
512 024e9686 2019-05-14 stsp struct got_reference *head_ref)
514 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
515 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
517 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
519 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
520 024e9686 2019-05-14 stsp path_got = NULL;
524 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
525 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
526 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
530 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
534 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
535 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
537 024e9686 2019-05-14 stsp free(path_got);
539 024e9686 2019-05-14 stsp free(head_ref_name);
540 024e9686 2019-05-14 stsp return err;
543 b72f483a 2019-02-05 stsp struct got_object_id *
544 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
549 507dc3bb 2018-12-29 stsp const struct got_error *
550 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
553 507dc3bb 2018-12-29 stsp const struct got_error *err;
554 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
555 507dc3bb 2018-12-29 stsp char *id_str = NULL;
556 507dc3bb 2018-12-29 stsp char *path_got = NULL;
558 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
561 507dc3bb 2018-12-29 stsp path_got = NULL;
565 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
567 507dc3bb 2018-12-29 stsp return err;
569 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
574 507dc3bb 2018-12-29 stsp /* Record our base commit. */
575 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
578 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
582 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
583 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
584 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
585 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
590 507dc3bb 2018-12-29 stsp got_object_close(obj);
591 507dc3bb 2018-12-29 stsp free(id_str);
592 507dc3bb 2018-12-29 stsp free(path_got);
593 507dc3bb 2018-12-29 stsp return err;
596 9d31a1d8 2018-03-11 stsp static const struct got_error *
597 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
599 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
602 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
603 86c3caaf 2018-03-09 stsp return NULL;
606 21908da4 2019-01-13 stsp static const struct got_error *
607 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
610 4a1ddfc2 2019-01-12 stsp char *abspath;
612 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
615 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
616 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 ddcd8544 2019-03-11 stsp struct stat sb;
618 ddcd8544 2019-03-11 stsp err = NULL;
619 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
620 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
621 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
622 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
623 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
626 4a1ddfc2 2019-01-12 stsp free(abspath);
627 68c76935 2019-02-19 stsp return err;
630 68c76935 2019-02-19 stsp static const struct got_error *
631 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
634 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
635 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
636 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
641 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
643 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
646 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
651 68c76935 2019-02-19 stsp if (flen1 == 0) {
652 68c76935 2019-02-19 stsp if (flen2 != 0)
655 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
656 68c76935 2019-02-19 stsp if (flen1 != 0)
659 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
660 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
670 68c76935 2019-02-19 stsp return err;
673 68c76935 2019-02-19 stsp static const struct got_error *
674 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
677 68c76935 2019-02-19 stsp struct stat sb;
678 68c76935 2019-02-19 stsp size_t size1, size2;
679 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
683 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
684 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
687 68c76935 2019-02-19 stsp size1 = sb.st_size;
689 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
693 68c76935 2019-02-19 stsp size2 = sb.st_size;
695 68c76935 2019-02-19 stsp if (size1 != size2) {
697 68c76935 2019-02-19 stsp return NULL;
700 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
701 68c76935 2019-02-19 stsp if (f1 == NULL)
702 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
704 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
705 68c76935 2019-02-19 stsp if (f2 == NULL) {
706 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
710 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
712 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
713 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
714 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
715 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
717 6353ad76 2019-02-08 stsp return err;
721 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
723 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
725 6353ad76 2019-02-08 stsp static const struct got_error *
726 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
727 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
728 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
729 14c901f1 2019-08-08 stsp const char *label_deriv, struct got_repository *repo,
730 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, 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 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
735 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
736 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
737 234035bc 2019-06-01 stsp int overlapcnt = 0;
738 af54ae4a 2019-02-19 stsp char *parent;
740 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
742 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
743 af54ae4a 2019-02-19 stsp if (parent == NULL)
744 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
746 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
747 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
749 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
753 af54ae4a 2019-02-19 stsp free(base_path);
754 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
755 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
756 af54ae4a 2019-02-19 stsp base_path = NULL;
760 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
763 46b6ee73 2019-06-04 stsp if (blob_orig) {
764 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
765 46b6ee73 2019-06-04 stsp blob_orig);
770 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
771 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
772 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
776 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
777 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
781 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
782 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
786 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
787 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
791 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
792 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
793 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
794 68c76935 2019-02-19 stsp merged_path);
799 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
800 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
804 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
805 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
806 230a42bd 2019-05-11 jcs ondisk_path);
807 2a57020b 2019-02-20 stsp unlink(merged_path);
812 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
813 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
814 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
815 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
816 6353ad76 2019-02-08 stsp free(merged_path);
817 af54ae4a 2019-02-19 stsp free(base_path);
818 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
819 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
820 46b6ee73 2019-06-04 stsp free(blob_orig_path);
822 14c901f1 2019-08-08 stsp return err;
826 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
827 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
828 14c901f1 2019-08-08 stsp * acts as the second derived version.
830 14c901f1 2019-08-08 stsp static const struct got_error *
831 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
832 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
833 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
834 14c901f1 2019-08-08 stsp struct got_object_id *deriv_base_commit_id,
835 14c901f1 2019-08-08 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
836 14c901f1 2019-08-08 stsp void *progress_arg)
838 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
839 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
840 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
841 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
843 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
845 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
846 14c901f1 2019-08-08 stsp if (parent == NULL)
847 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
849 14c901f1 2019-08-08 stsp free(base_path);
850 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
851 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
852 14c901f1 2019-08-08 stsp base_path = NULL;
856 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
859 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
860 14c901f1 2019-08-08 stsp blob_deriv);
864 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
867 14c901f1 2019-08-08 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
868 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
872 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
873 14c901f1 2019-08-08 stsp ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
874 14c901f1 2019-08-08 stsp repo, progress_cb, progress_arg);
876 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
877 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
878 14c901f1 2019-08-08 stsp free(base_path);
879 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
880 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
881 14c901f1 2019-08-08 stsp free(blob_deriv_path);
883 6353ad76 2019-02-08 stsp free(id_str);
884 818c7501 2019-07-11 stsp free(label_deriv);
885 4a1ddfc2 2019-01-12 stsp return err;
888 4a1ddfc2 2019-01-12 stsp static const struct got_error *
889 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
890 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
891 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
892 13d9040b 2019-03-27 stsp int update_timestamps)
894 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
896 13d9040b 2019-03-27 stsp if (ie == NULL)
897 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
899 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
900 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
901 13d9040b 2019-03-27 stsp update_timestamps);
903 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
904 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
905 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
907 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
909 13d9040b 2019-03-27 stsp return err;
912 13d9040b 2019-03-27 stsp static const struct got_error *
913 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
914 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
915 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
916 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
917 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
919 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
920 507dc3bb 2018-12-29 stsp int fd = -1;
921 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
922 507dc3bb 2018-12-29 stsp int update = 0;
923 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
925 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
926 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
927 9d31a1d8 2018-03-11 stsp if (fd == -1) {
928 21908da4 2019-01-13 stsp if (errno == ENOENT) {
929 21908da4 2019-01-13 stsp char *parent = dirname(path);
930 21908da4 2019-01-13 stsp if (parent == NULL)
931 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
932 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
934 21908da4 2019-01-13 stsp return err;
935 21908da4 2019-01-13 stsp fd = open(ondisk_path,
936 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
937 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
938 21908da4 2019-01-13 stsp if (fd == -1)
939 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
940 230a42bd 2019-05-11 jcs ondisk_path);
941 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
942 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
943 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
944 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
947 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
948 507dc3bb 2018-12-29 stsp ondisk_path);
951 507dc3bb 2018-12-29 stsp update = 1;
954 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
957 a378724f 2019-02-10 stsp if (restoring_missing_file)
958 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
959 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
960 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
962 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
963 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
967 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
969 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
970 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
973 9d31a1d8 2018-03-11 stsp if (len > 0) {
974 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
975 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
976 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
977 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
979 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
980 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
983 61d6eaa3 2018-12-24 stsp hdrlen = 0;
985 9d31a1d8 2018-03-11 stsp } while (len != 0);
987 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
988 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
992 507dc3bb 2018-12-29 stsp if (update) {
993 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
994 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
995 230a42bd 2019-05-11 jcs ondisk_path);
996 2a57020b 2019-02-20 stsp unlink(tmppath);
1001 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
1002 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1003 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1004 507dc3bb 2018-12-29 stsp goto done;
1007 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1008 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1009 68ed9ba5 2019-02-10 stsp goto done;
1014 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1015 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1016 507dc3bb 2018-12-29 stsp free(tmppath);
1017 6353ad76 2019-02-08 stsp return err;
1020 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1021 6353ad76 2019-02-08 stsp static const struct got_error *
1022 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1024 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1025 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1026 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1027 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1028 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1030 7154f6ce 2019-03-27 stsp int i = 0;
1031 7154f6ce 2019-03-27 stsp char *line;
1032 7154f6ce 2019-03-27 stsp size_t len;
1033 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1035 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1036 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1037 7154f6ce 2019-03-27 stsp if (line == NULL) {
1038 7154f6ce 2019-03-27 stsp if (feof(f))
1040 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1044 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1045 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1047 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1053 7154f6ce 2019-03-27 stsp return err;
1056 e2b1e152 2019-07-13 stsp static int
1057 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1059 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1060 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1061 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1062 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1063 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1066 c363b2c1 2019-08-03 stsp static unsigned char
1067 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1069 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1070 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1071 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1072 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1073 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1074 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1075 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1077 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1081 7154f6ce 2019-03-27 stsp static const struct got_error *
1082 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1083 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1084 b8f41171 2019-02-10 stsp struct got_repository *repo)
1086 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1087 6353ad76 2019-02-08 stsp struct got_object_id id;
1088 6353ad76 2019-02-08 stsp size_t hdrlen;
1089 6353ad76 2019-02-08 stsp FILE *f = NULL;
1090 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1091 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1092 6353ad76 2019-02-08 stsp size_t flen, blen;
1093 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1095 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1097 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1098 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1099 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1100 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1102 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1103 a378724f 2019-02-10 stsp return NULL;
1105 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1108 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1109 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1110 339c298e 2019-07-27 stsp return NULL;
1113 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1114 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1115 339c298e 2019-07-27 stsp return NULL;
1116 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1117 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1118 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1119 339c298e 2019-07-27 stsp return NULL;
1122 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1123 339c298e 2019-07-27 stsp return NULL;
1125 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1126 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1127 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1129 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1131 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1133 339c298e 2019-07-27 stsp return err;
1135 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1136 6353ad76 2019-02-08 stsp if (f == NULL) {
1137 339c298e 2019-07-27 stsp err = got_error_from_errno2("fopen", abspath);
1138 6353ad76 2019-02-08 stsp goto done;
1140 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1142 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1143 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1145 7154f6ce 2019-03-27 stsp goto done;
1146 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1147 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1148 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1149 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1150 7154f6ce 2019-03-27 stsp goto done;
1152 6353ad76 2019-02-08 stsp if (blen == 0) {
1153 6353ad76 2019-02-08 stsp if (flen != 0)
1154 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1156 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1157 6353ad76 2019-02-08 stsp if (blen != 0)
1158 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1160 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1161 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1162 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1163 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1167 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1170 6353ad76 2019-02-08 stsp hdrlen = 0;
1173 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1174 7154f6ce 2019-03-27 stsp rewind(f);
1175 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1179 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1181 6353ad76 2019-02-08 stsp fclose(f);
1182 9d31a1d8 2018-03-11 stsp return err;
1186 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1187 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1189 e2b1e152 2019-07-13 stsp static const struct got_error *
1190 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1191 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1193 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1194 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1195 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1197 e2b1e152 2019-07-13 stsp return NULL;
1200 9d31a1d8 2018-03-11 stsp static const struct got_error *
1201 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1202 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1203 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1204 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1205 0584f854 2019-04-06 stsp void *progress_arg)
1207 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1208 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1209 6353ad76 2019-02-08 stsp char *ondisk_path;
1210 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1211 b8f41171 2019-02-10 stsp struct stat sb;
1213 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1214 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1217 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1218 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1219 a76c42e6 2019-08-03 stsp goto done;
1221 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1223 abb4604f 2019-07-27 stsp goto done;
1224 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1225 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1227 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1229 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1230 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1231 b8f41171 2019-02-10 stsp goto done;
1234 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1235 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1236 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1237 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1238 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1240 e2b1e152 2019-07-13 stsp goto done;
1241 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1243 6353ad76 2019-02-08 stsp goto done;
1245 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1246 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1247 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1248 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1249 b8f41171 2019-02-10 stsp goto done;
1253 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1255 6353ad76 2019-02-08 stsp goto done;
1257 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1258 234035bc 2019-06-01 stsp int update_timestamps;
1259 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1260 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1261 234035bc 2019-06-01 stsp struct got_object_id id2;
1262 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1263 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1265 234035bc 2019-06-01 stsp goto done;
1267 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1268 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1269 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1270 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1271 234035bc 2019-06-01 stsp if (blob2)
1272 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1274 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1275 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1276 234035bc 2019-06-01 stsp * unmodified files again.
1278 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1279 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1280 234035bc 2019-06-01 stsp update_timestamps);
1281 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1282 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1284 1ee397ad 2019-07-12 stsp goto done;
1285 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1286 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1288 13d9040b 2019-03-27 stsp goto done;
1290 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1291 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1292 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1294 13d9040b 2019-03-27 stsp goto done;
1295 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1296 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1298 13d9040b 2019-03-27 stsp goto done;
1300 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1302 6353ad76 2019-02-08 stsp free(ondisk_path);
1303 8da9e5f4 2019-01-12 stsp return err;
1306 90285c3b 2019-01-08 stsp static const struct got_error *
1307 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1309 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1310 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1312 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1313 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1315 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1316 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1317 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1319 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1320 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1321 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1322 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1323 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1327 90285c3b 2019-01-08 stsp parent = dirname(parent);
1330 90285c3b 2019-01-08 stsp free(ondisk_path);
1331 90285c3b 2019-01-08 stsp return err;
1334 708d8e67 2019-03-27 stsp static const struct got_error *
1335 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1336 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1337 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1339 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1340 708d8e67 2019-03-27 stsp unsigned char status;
1341 708d8e67 2019-03-27 stsp struct stat sb;
1342 708d8e67 2019-03-27 stsp char *ondisk_path;
1344 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1345 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1347 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1349 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1351 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1353 708d8e67 2019-03-27 stsp return err;
1355 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1356 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1357 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1359 1ee397ad 2019-07-12 stsp return err;
1361 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1362 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1364 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1367 708d8e67 2019-03-27 stsp return err;
1369 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1371 1ee397ad 2019-07-12 stsp return err;
1372 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1373 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1375 fc6346c4 2019-03-27 stsp return err;
1377 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1380 fc6346c4 2019-03-27 stsp return err;
1383 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1384 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1385 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1386 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1387 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1388 8da9e5f4 2019-01-12 stsp void *progress_arg;
1389 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1390 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1393 512f0d0e 2019-01-02 stsp static const struct got_error *
1394 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1395 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1397 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1399 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1400 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1402 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1403 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1406 8da9e5f4 2019-01-12 stsp static const struct got_error *
1407 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1409 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1411 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1412 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1414 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1415 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1418 9d31a1d8 2018-03-11 stsp static const struct got_error *
1419 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1421 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1422 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1423 8da9e5f4 2019-01-12 stsp char *path;
1425 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1426 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1428 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1429 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1431 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1433 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1434 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1436 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1437 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1439 8da9e5f4 2019-01-12 stsp free(path);
1440 0cd1c46a 2019-03-11 stsp return err;
1443 818c7501 2019-07-11 stsp static const struct got_error *
1444 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1446 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1447 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1448 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1450 517bab73 2019-03-11 stsp *refname = NULL;
1452 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1453 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1454 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1456 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1458 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1459 0647c563 2019-03-11 stsp *refname = NULL;
1461 517bab73 2019-03-11 stsp free(uuidstr);
1462 517bab73 2019-03-11 stsp return err;
1465 818c7501 2019-07-11 stsp const struct got_error *
1466 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1468 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1471 818c7501 2019-07-11 stsp static const struct got_error *
1472 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1474 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1475 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1478 818c7501 2019-07-11 stsp static const struct got_error *
1479 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1481 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1484 818c7501 2019-07-11 stsp static const struct got_error *
1485 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1487 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1488 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1491 818c7501 2019-07-11 stsp static const struct got_error *
1492 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1494 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1495 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1498 0ebf8283 2019-07-24 stsp static const struct got_error *
1499 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1501 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1502 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1505 0ebf8283 2019-07-24 stsp static const struct got_error *
1506 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1508 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1509 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1512 0ebf8283 2019-07-24 stsp static const struct got_error *
1513 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1515 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1516 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1519 0ebf8283 2019-07-24 stsp static const struct got_error *
1520 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1522 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1523 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1526 0ebf8283 2019-07-24 stsp const struct got_error *
1527 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1528 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1530 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1531 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1532 0ebf8283 2019-07-24 stsp *path = NULL;
1533 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1535 0ebf8283 2019-07-24 stsp return NULL;
1539 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1540 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1542 517bab73 2019-03-11 stsp static const struct got_error *
1543 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1545 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1546 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1547 517bab73 2019-03-11 stsp char *refname;
1549 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1551 517bab73 2019-03-11 stsp return err;
1553 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1555 0cd1c46a 2019-03-11 stsp goto done;
1557 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1559 0cd1c46a 2019-03-11 stsp free(refname);
1561 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1562 3e3a69f1 2019-07-25 stsp return err;
1565 3e3a69f1 2019-07-25 stsp static const struct got_error *
1566 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1568 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1570 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1571 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1572 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1573 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1575 9d31a1d8 2018-03-11 stsp return err;
1579 ebf99748 2019-05-09 stsp static const struct got_error *
1580 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1581 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1583 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1584 ebf99748 2019-05-09 stsp FILE *index = NULL;
1586 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1587 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1588 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1589 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1591 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1593 ebf99748 2019-05-09 stsp goto done;
1595 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1596 ebf99748 2019-05-09 stsp if (index == NULL) {
1597 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1598 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1600 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1601 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1602 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1605 ebf99748 2019-05-09 stsp if (err) {
1606 ebf99748 2019-05-09 stsp free(*fileindex_path);
1607 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1608 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1609 ebf99748 2019-05-09 stsp *fileindex = NULL;
1611 ebf99748 2019-05-09 stsp return err;
1614 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1615 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1616 c932eeeb 2019-05-22 stsp const char *path;
1617 c932eeeb 2019-05-22 stsp size_t path_len;
1618 c932eeeb 2019-05-22 stsp const char *entry_name;
1619 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1620 a484d721 2019-06-10 stsp void *progress_arg;
1623 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1624 c932eeeb 2019-05-22 stsp static const struct got_error *
1625 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1627 1ee397ad 2019-07-12 stsp const struct got_error *err;
1628 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1630 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1631 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1632 c932eeeb 2019-05-22 stsp return NULL;
1633 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1634 102ff934 2019-06-11 stsp return NULL;
1636 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1637 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1638 c932eeeb 2019-05-22 stsp return NULL;
1640 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1641 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1642 edd02c5e 2019-07-11 stsp ie->path);
1644 1ee397ad 2019-07-12 stsp return err;
1646 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1647 c932eeeb 2019-05-22 stsp return NULL;
1650 9c6338c4 2019-06-02 stsp static const struct got_error *
1651 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1653 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1654 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1655 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1657 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1658 9c6338c4 2019-06-02 stsp fileindex_path);
1660 9c6338c4 2019-06-02 stsp goto done;
1662 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1664 9c6338c4 2019-06-02 stsp goto done;
1666 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1667 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1668 9c6338c4 2019-06-02 stsp fileindex_path);
1669 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1672 9c6338c4 2019-06-02 stsp if (new_index)
1673 9c6338c4 2019-06-02 stsp fclose(new_index);
1674 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1675 9c6338c4 2019-06-02 stsp return err;
1678 6ced4176 2019-07-12 stsp static const struct got_error *
1679 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1680 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1681 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1683 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1684 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1685 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1686 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1688 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1689 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1690 6ced4176 2019-07-12 stsp *tree_id = NULL;
1692 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1693 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1694 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1695 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1696 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1698 6ced4176 2019-07-12 stsp goto done;
1700 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1701 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1703 6ced4176 2019-07-12 stsp goto done;
1704 6ced4176 2019-07-12 stsp return NULL;
1707 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1709 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1710 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1711 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1712 6ced4176 2019-07-12 stsp goto done;
1715 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1716 6ced4176 2019-07-12 stsp in_repo_path);
1718 6ced4176 2019-07-12 stsp goto done;
1720 6ced4176 2019-07-12 stsp free(in_repo_path);
1721 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1723 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1725 6ced4176 2019-07-12 stsp goto done;
1727 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1728 6ced4176 2019-07-12 stsp /* Check out a single file. */
1729 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1730 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1731 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1732 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1733 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1734 6ced4176 2019-07-12 stsp goto done;
1736 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1737 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1738 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1739 6ced4176 2019-07-12 stsp goto done;
1742 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1743 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1745 6ced4176 2019-07-12 stsp return err;
1746 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1747 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1748 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1749 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1750 6ced4176 2019-07-12 stsp goto done;
1753 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1754 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1756 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1757 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1758 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1759 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1760 6ced4176 2019-07-12 stsp goto done;
1762 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1763 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1764 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1765 6ced4176 2019-07-12 stsp goto done;
1770 6ced4176 2019-07-12 stsp free(in_repo_path);
1771 6ced4176 2019-07-12 stsp if (err) {
1772 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1773 6ced4176 2019-07-12 stsp free(*tree_relpath);
1774 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1775 6ced4176 2019-07-12 stsp free(*tree_id);
1776 6ced4176 2019-07-12 stsp *tree_id = NULL;
1778 07ed472d 2019-07-12 stsp return err;
1781 07ed472d 2019-07-12 stsp static const struct got_error *
1782 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1783 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1784 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1785 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1787 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1788 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1789 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1790 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1791 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1793 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1795 07ed472d 2019-07-12 stsp goto done;
1797 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1798 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1800 07ed472d 2019-07-12 stsp goto done;
1802 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1804 07ed472d 2019-07-12 stsp goto done;
1806 07ed472d 2019-07-12 stsp if (entry_name &&
1807 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1808 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1809 07ed472d 2019-07-12 stsp goto done;
1812 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1813 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1814 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1815 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1816 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1817 07ed472d 2019-07-12 stsp arg.repo = repo;
1818 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1819 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1820 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1821 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1822 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1823 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1826 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1827 07ed472d 2019-07-12 stsp if (commit)
1828 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1829 6ced4176 2019-07-12 stsp return err;
1832 9d31a1d8 2018-03-11 stsp const struct got_error *
1833 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1834 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1835 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1836 f2ea84fa 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1838 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1839 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1840 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1841 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1842 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1843 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1844 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1845 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1846 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1847 f2ea84fa 2019-07-27 stsp int entry_type;
1848 f2ea84fa 2019-07-27 stsp char *relpath;
1849 f2ea84fa 2019-07-27 stsp char *entry_name;
1850 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1851 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1853 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1855 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1857 9d31a1d8 2018-03-11 stsp return err;
1859 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1860 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1861 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1862 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1863 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1864 f2ea84fa 2019-07-27 stsp goto done;
1867 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1868 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1869 f2ea84fa 2019-07-27 stsp if (err) {
1870 f2ea84fa 2019-07-27 stsp free(tpd);
1871 6ced4176 2019-07-12 stsp goto done;
1874 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1875 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1876 f2ea84fa 2019-07-27 stsp if (err) {
1877 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1878 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1879 f2ea84fa 2019-07-27 stsp free(tpd);
1880 f2ea84fa 2019-07-27 stsp goto done;
1883 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1885 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1889 51514078 2018-12-25 stsp * Read the file index.
1890 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1891 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1893 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1895 c4cdcb68 2019-04-03 stsp goto done;
1897 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1898 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1899 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1901 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1902 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1903 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1907 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1908 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1909 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1910 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
1911 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1912 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1913 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1914 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1918 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1920 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1921 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1922 af12c6b9 2019-06-04 stsp err = sync_err;
1924 9c6338c4 2019-06-02 stsp free(fileindex_path);
1926 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1927 9d31a1d8 2018-03-11 stsp if (commit)
1928 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1929 5ade8233 2019-07-12 stsp if (fileindex)
1930 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1931 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1932 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1933 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1934 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1935 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1936 f2ea84fa 2019-07-27 stsp free(tpd);
1938 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1939 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1940 9d31a1d8 2018-03-11 stsp err = unlockerr;
1941 f8d1f275 2019-02-04 stsp return err;
1944 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1945 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1946 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1947 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1948 234035bc 2019-06-01 stsp void *progress_arg;
1949 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1950 234035bc 2019-06-01 stsp void *cancel_arg;
1951 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1954 234035bc 2019-06-01 stsp static const struct got_error *
1955 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1956 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1957 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1958 234035bc 2019-06-01 stsp struct got_repository *repo)
1960 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1961 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1962 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1963 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1964 234035bc 2019-06-01 stsp struct stat sb;
1965 234035bc 2019-06-01 stsp unsigned char status;
1966 234035bc 2019-06-01 stsp int local_changes_subsumed;
1968 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1969 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1970 d6c87207 2019-08-02 stsp strlen(path2));
1971 1ee397ad 2019-07-12 stsp if (ie == NULL)
1972 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1973 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1975 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1976 234035bc 2019-06-01 stsp path2) == -1)
1977 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1979 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1981 234035bc 2019-06-01 stsp goto done;
1983 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1984 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1985 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1986 234035bc 2019-06-01 stsp goto done;
1988 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1989 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1990 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1991 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1992 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1993 234035bc 2019-06-01 stsp goto done;
1996 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1997 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1998 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1999 234035bc 2019-06-01 stsp } else if (blob1) {
2000 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2001 d6c87207 2019-08-02 stsp strlen(path1));
2002 1ee397ad 2019-07-12 stsp if (ie == NULL)
2003 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2004 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2006 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2007 2b92fad7 2019-06-02 stsp path1) == -1)
2008 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2010 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2012 2b92fad7 2019-06-02 stsp goto done;
2014 2b92fad7 2019-06-02 stsp switch (status) {
2015 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2016 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2017 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2019 1ee397ad 2019-07-12 stsp goto done;
2020 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2022 2b92fad7 2019-06-02 stsp goto done;
2024 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2026 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2027 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2028 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2029 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2031 1ee397ad 2019-07-12 stsp goto done;
2033 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2035 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2036 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2037 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2039 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2041 1ee397ad 2019-07-12 stsp goto done;
2043 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2044 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2046 1ee397ad 2019-07-12 stsp goto done;
2051 234035bc 2019-06-01 stsp } else if (blob2) {
2052 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2053 234035bc 2019-06-01 stsp path2) == -1)
2054 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2055 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2056 d6c87207 2019-08-02 stsp strlen(path2));
2058 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2061 234035bc 2019-06-01 stsp goto done;
2062 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2063 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2064 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2065 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2066 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2067 1ee397ad 2019-07-12 stsp status, path2);
2068 234035bc 2019-06-01 stsp goto done;
2070 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2071 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2072 818c7501 2019-07-11 stsp a->commit_id2, repo,
2073 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2074 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2075 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2076 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2077 234035bc 2019-06-01 stsp blob2, 0);
2079 234035bc 2019-06-01 stsp goto done;
2082 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2083 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2084 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2085 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2086 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2087 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2089 234035bc 2019-06-01 stsp goto done;
2090 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2091 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2093 234035bc 2019-06-01 stsp goto done;
2094 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2095 2b92fad7 2019-06-02 stsp if (err) {
2096 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2097 2b92fad7 2019-06-02 stsp goto done;
2102 234035bc 2019-06-01 stsp free(ondisk_path);
2103 234035bc 2019-06-01 stsp return err;
2106 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2107 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2108 234035bc 2019-06-01 stsp struct got_repository *repo;
2111 234035bc 2019-06-01 stsp static const struct got_error *
2112 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2114 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2115 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2116 234035bc 2019-06-01 stsp unsigned char status;
2117 234035bc 2019-06-01 stsp struct stat sb;
2118 234035bc 2019-06-01 stsp char *ondisk_path;
2120 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2121 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2122 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2123 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2125 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2127 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2129 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2130 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2132 234035bc 2019-06-01 stsp return err;
2133 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2134 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2136 234035bc 2019-06-01 stsp return NULL;
2139 818c7501 2019-07-11 stsp static const struct got_error *
2140 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2142 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2143 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2144 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2146 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2147 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2148 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2149 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2151 03415a1a 2019-06-02 stsp if (commit_id1) {
2152 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2153 03415a1a 2019-06-02 stsp worktree->path_prefix);
2155 03415a1a 2019-06-02 stsp goto done;
2157 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2159 03415a1a 2019-06-02 stsp goto done;
2162 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2163 234035bc 2019-06-01 stsp worktree->path_prefix);
2165 234035bc 2019-06-01 stsp goto done;
2167 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2169 234035bc 2019-06-01 stsp goto done;
2171 234035bc 2019-06-01 stsp arg.worktree = worktree;
2172 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2173 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2174 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2175 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2176 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2177 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2178 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2179 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2180 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2181 af12c6b9 2019-06-04 stsp err = sync_err;
2183 234035bc 2019-06-01 stsp if (tree1)
2184 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2185 234035bc 2019-06-01 stsp if (tree2)
2186 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2187 818c7501 2019-07-11 stsp return err;
2190 818c7501 2019-07-11 stsp const struct got_error *
2191 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2192 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2193 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2194 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2196 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2197 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2198 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2199 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2201 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2203 818c7501 2019-07-11 stsp return err;
2205 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2207 818c7501 2019-07-11 stsp goto done;
2209 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2210 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2211 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2212 818c7501 2019-07-11 stsp &mok_arg);
2214 818c7501 2019-07-11 stsp goto done;
2216 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2217 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2219 818c7501 2019-07-11 stsp if (fileindex)
2220 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2221 818c7501 2019-07-11 stsp free(fileindex_path);
2222 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2223 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2224 234035bc 2019-06-01 stsp err = unlockerr;
2225 234035bc 2019-06-01 stsp return err;
2228 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2229 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2230 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2231 927df6b7 2019-02-10 stsp const char *status_path;
2232 927df6b7 2019-02-10 stsp size_t status_path_len;
2233 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2234 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2235 f8d1f275 2019-02-04 stsp void *status_arg;
2236 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2237 f8d1f275 2019-02-04 stsp void *cancel_arg;
2240 f8d1f275 2019-02-04 stsp static const struct got_error *
2241 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2242 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2243 927df6b7 2019-02-10 stsp struct got_repository *repo)
2245 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2246 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2247 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2248 927df6b7 2019-02-10 stsp struct stat sb;
2249 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2250 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2251 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2253 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2255 98eaaa12 2019-08-03 stsp return err;
2257 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2258 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_NO_CHANGE)
2259 98eaaa12 2019-08-03 stsp return NULL;
2261 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2262 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2263 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2265 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2266 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2267 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2269 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2270 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2271 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2272 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2273 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2276 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2277 98eaaa12 2019-08-03 stsp ie->path, blob_idp, staged_blob_idp, commit_idp);
2280 927df6b7 2019-02-10 stsp static const struct got_error *
2281 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2282 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2284 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2285 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2286 f8d1f275 2019-02-04 stsp char *abspath;
2288 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2289 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2291 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2292 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2293 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2294 927df6b7 2019-02-10 stsp return NULL;
2296 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2297 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2298 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2299 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2301 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2302 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2303 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2306 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2308 f8d1f275 2019-02-04 stsp free(abspath);
2309 9d31a1d8 2018-03-11 stsp return err;
2312 f8d1f275 2019-02-04 stsp static const struct got_error *
2313 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2315 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2316 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2317 2ec1f75b 2019-03-26 stsp unsigned char status;
2319 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2320 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2322 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2323 927df6b7 2019-02-10 stsp return NULL;
2325 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2326 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2327 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2328 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2330 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2331 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2332 537ac44b 2019-08-03 stsp ie->path, &blob_id, NULL, &commit_id);
2335 f8d1f275 2019-02-04 stsp static const struct got_error *
2336 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2338 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2339 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2340 f8d1f275 2019-02-04 stsp char *path = NULL;
2342 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2343 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2345 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2346 2c201a36 2019-02-10 stsp return NULL;
2348 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2349 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2350 927df6b7 2019-02-10 stsp return NULL;
2352 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2353 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2354 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2356 f8d1f275 2019-02-04 stsp path = de->d_name;
2359 c577a9ce 2019-07-27 stsp if (got_path_is_child(path, a->status_path, a->status_path_len))
2360 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2361 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2362 f8d1f275 2019-02-04 stsp if (parent_path[0])
2363 f8d1f275 2019-02-04 stsp free(path);
2364 b72f483a 2019-02-05 stsp return err;
2367 347d1d3e 2019-07-12 stsp static const struct got_error *
2368 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2369 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2370 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2372 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2373 abb4604f 2019-07-27 stsp struct stat sb;
2375 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2377 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2378 abb4604f 2019-07-27 stsp status_arg, repo);
2380 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2381 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2382 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2383 abb4604f 2019-07-27 stsp return NULL;
2386 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2387 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2388 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2390 abb4604f 2019-07-27 stsp return NULL;
2393 abb4604f 2019-07-27 stsp static const struct got_error *
2394 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2395 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2396 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2397 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2399 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2400 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2401 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2402 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2403 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2405 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2406 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2407 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2409 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2410 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2411 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2412 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2414 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2415 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2417 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2418 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2419 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2420 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2421 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2422 abb4604f 2019-07-27 stsp arg.status_path = path;
2423 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2424 abb4604f 2019-07-27 stsp arg.repo = repo;
2425 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2426 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2427 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2428 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2429 abb4604f 2019-07-27 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2430 abb4604f 2019-07-27 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2433 f8d1f275 2019-02-04 stsp if (workdir)
2434 f8d1f275 2019-02-04 stsp closedir(workdir);
2435 927df6b7 2019-02-10 stsp free(ondisk_path);
2436 347d1d3e 2019-07-12 stsp return err;
2439 347d1d3e 2019-07-12 stsp const struct got_error *
2440 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2441 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2442 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2443 72ea6654 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2445 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2446 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2447 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2448 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2450 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2452 347d1d3e 2019-07-12 stsp return err;
2454 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2455 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2456 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2460 f8d1f275 2019-02-04 stsp free(fileindex_path);
2461 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2462 f8d1f275 2019-02-04 stsp return err;
2465 6c7ab921 2019-03-18 stsp const struct got_error *
2466 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2467 6c7ab921 2019-03-18 stsp const char *arg)
2469 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2470 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2471 6c7ab921 2019-03-18 stsp size_t len;
2473 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2475 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2476 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2477 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2478 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2479 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2480 d0710d08 2019-07-22 stsp if (cwd == NULL)
2481 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2482 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2483 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2484 d0710d08 2019-07-22 stsp goto done;
2488 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2489 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2490 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2491 6c7ab921 2019-03-18 stsp goto done;
2494 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2495 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2496 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2498 f6d88e1a 2019-05-29 stsp goto done;
2500 f6d88e1a 2019-05-29 stsp path = strdup("");
2501 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2502 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2503 f6d88e1a 2019-05-29 stsp goto done;
2507 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2508 6c7ab921 2019-03-18 stsp len = strlen(path);
2509 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2510 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2514 6c7ab921 2019-03-18 stsp free(resolved);
2515 d0710d08 2019-07-22 stsp free(cwd);
2516 6c7ab921 2019-03-18 stsp if (err == NULL)
2517 6c7ab921 2019-03-18 stsp *wt_path = path;
2519 6c7ab921 2019-03-18 stsp free(path);
2520 d00136be 2019-03-26 stsp return err;
2523 07f5b47a 2019-06-02 stsp static const struct got_error *
2524 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2525 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2526 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2528 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2529 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2530 6d022e97 2019-08-04 stsp unsigned char status;
2531 6d022e97 2019-08-04 stsp struct stat sb;
2533 6d022e97 2019-08-04 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2535 6d022e97 2019-08-04 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2537 6d022e97 2019-08-04 stsp return err;
2538 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
2539 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
2540 6d022e97 2019-08-04 stsp return NULL;
2541 6d022e97 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2544 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2546 07f5b47a 2019-06-02 stsp return err;
2548 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2549 07f5b47a 2019-06-02 stsp if (err) {
2550 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2551 07f5b47a 2019-06-02 stsp return err;
2554 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2557 d00136be 2019-03-26 stsp const struct got_error *
2558 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2559 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
2560 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2561 031a5338 2019-03-26 stsp struct got_repository *repo)
2563 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2564 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2565 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2566 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2568 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2570 d00136be 2019-03-26 stsp return err;
2572 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2574 d00136be 2019-03-26 stsp goto done;
2576 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2577 6d022e97 2019-08-04 stsp char *ondisk_path;
2578 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2579 6d022e97 2019-08-04 stsp pe->path) == -1)
2580 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2581 6d022e97 2019-08-04 stsp err = schedule_addition(ondisk_path, fileindex, pe->path,
2582 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2583 6d022e97 2019-08-04 stsp free(ondisk_path);
2587 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2588 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2589 af12c6b9 2019-06-04 stsp err = sync_err;
2591 fb399478 2019-07-12 stsp free(fileindex_path);
2592 d00136be 2019-03-26 stsp if (fileindex)
2593 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2594 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2595 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2596 d00136be 2019-03-26 stsp err = unlockerr;
2597 6c7ab921 2019-03-18 stsp return err;
2600 17ed4618 2019-06-02 stsp static const struct got_error *
2601 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2602 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2603 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2604 17ed4618 2019-06-02 stsp struct got_repository *repo)
2606 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2607 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2608 9acbc4fa 2019-08-03 stsp unsigned char status, staged_status;
2609 17ed4618 2019-06-02 stsp struct stat sb;
2611 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2612 17ed4618 2019-06-02 stsp if (ie == NULL)
2613 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2615 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
2616 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
2617 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2618 9acbc4fa 2019-08-03 stsp return NULL;
2619 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2622 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2624 17ed4618 2019-06-02 stsp return err;
2626 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2627 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2628 de426ea6 2019-08-03 stsp return NULL;
2629 f0b0c0ce 2019-08-04 stsp if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2630 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2631 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MODIFY &&
2632 f0b0c0ce 2019-08-04 stsp status != GOT_STATUS_MISSING)
2633 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2636 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2637 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2639 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2640 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2643 2ec1f75b 2019-03-26 stsp const struct got_error *
2644 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2645 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
2646 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2647 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2649 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2650 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2651 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2652 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2654 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2656 2ec1f75b 2019-03-26 stsp return err;
2658 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2660 2ec1f75b 2019-03-26 stsp goto done;
2662 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2663 6d022e97 2019-08-04 stsp char *ondisk_path;
2664 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2665 6d022e97 2019-08-04 stsp pe->path) == -1)
2666 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2667 6d022e97 2019-08-04 stsp err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2668 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2669 6d022e97 2019-08-04 stsp free(ondisk_path);
2673 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2674 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2675 af12c6b9 2019-06-04 stsp err = sync_err;
2677 fb399478 2019-07-12 stsp free(fileindex_path);
2678 2ec1f75b 2019-03-26 stsp if (fileindex)
2679 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2680 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2681 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2682 2ec1f75b 2019-03-26 stsp err = unlockerr;
2683 2ec1f75b 2019-03-26 stsp return err;
2686 e20a8b6f 2019-06-04 stsp static const struct got_error *
2687 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2688 a129376b 2019-03-28 stsp const char *ondisk_path,
2689 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2690 a129376b 2019-03-28 stsp struct got_repository *repo)
2692 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2693 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2694 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2695 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2696 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2697 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
2698 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2699 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2700 24278f30 2019-08-03 stsp unsigned char status, staged_status;
2701 a129376b 2019-03-28 stsp struct stat sb;
2703 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2704 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2706 a129376b 2019-03-28 stsp goto done;
2708 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2709 a129376b 2019-03-28 stsp if (ie == NULL) {
2710 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2711 a129376b 2019-03-28 stsp goto done;
2714 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2715 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2716 a129376b 2019-03-28 stsp if (err) {
2717 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2718 a129376b 2019-03-28 stsp goto done;
2719 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2720 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2721 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2722 e20a8b6f 2019-06-04 stsp goto done;
2725 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2726 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2727 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2728 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2729 a129376b 2019-03-28 stsp goto done;
2732 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2733 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2734 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2735 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2736 a129376b 2019-03-28 stsp goto done;
2739 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2740 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2741 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2742 a129376b 2019-03-28 stsp goto done;
2747 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2749 a129376b 2019-03-28 stsp goto done;
2750 7fa0d182 2019-08-03 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2751 7fa0d182 2019-08-03 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
2753 24278f30 2019-08-03 stsp staged_status = get_staged_status(ie);
2754 24278f30 2019-08-03 stsp if (status == GOT_STATUS_DELETE &&
2755 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_NO_CHANGE) {
2756 24278f30 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2757 24278f30 2019-08-03 stsp goto done;
2760 a9fa2909 2019-07-27 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2761 a9fa2909 2019-07-27 stsp tree_path);
2762 a9fa2909 2019-07-27 stsp if (err) {
2763 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2764 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
2765 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
2766 a9fa2909 2019-07-27 stsp goto done;
2768 a9fa2909 2019-07-27 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2770 a9fa2909 2019-07-27 stsp goto done;
2772 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
2773 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
2774 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
2775 a9fa2909 2019-07-27 stsp goto done;
2778 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
2779 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
2780 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
2781 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2782 a9fa2909 2019-07-27 stsp goto done;
2786 a129376b 2019-03-28 stsp switch (status) {
2787 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2788 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2790 1ee397ad 2019-07-12 stsp goto done;
2791 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2793 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2794 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2795 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2796 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2797 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2798 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2799 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2800 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
2801 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2803 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
2804 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2805 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2807 a129376b 2019-03-28 stsp goto done;
2808 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2809 24278f30 2019-08-03 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE, sb.st_mode,
2810 24278f30 2019-08-03 stsp blob, 0, 1, repo, progress_cb, progress_arg);
2812 a129376b 2019-03-28 stsp goto done;
2813 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2814 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2815 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2817 a129376b 2019-03-28 stsp goto done;
2822 a129376b 2019-03-28 stsp goto done;
2825 a129376b 2019-03-28 stsp free(relpath);
2826 e20a8b6f 2019-06-04 stsp free(parent_path);
2827 a129376b 2019-03-28 stsp free(tree_path);
2829 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2831 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2832 a129376b 2019-03-28 stsp free(tree_id);
2833 e20a8b6f 2019-06-04 stsp return err;
2836 e20a8b6f 2019-06-04 stsp const struct got_error *
2837 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2838 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2839 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2840 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2842 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2843 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2844 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2845 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2846 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2848 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2850 e20a8b6f 2019-06-04 stsp return err;
2852 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2854 e20a8b6f 2019-06-04 stsp goto done;
2856 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2857 6d022e97 2019-08-04 stsp char *ondisk_path;
2858 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2859 6d022e97 2019-08-04 stsp pe->path) == -1)
2860 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2861 6d022e97 2019-08-04 stsp err = revert_file(worktree, fileindex, ondisk_path,
2862 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2863 6d022e97 2019-08-04 stsp free(ondisk_path);
2867 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2868 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2869 e20a8b6f 2019-06-04 stsp err = sync_err;
2871 fb399478 2019-07-12 stsp free(fileindex_path);
2872 a129376b 2019-03-28 stsp if (fileindex)
2873 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2874 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2875 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2876 a129376b 2019-03-28 stsp err = unlockerr;
2877 c4296144 2019-05-09 stsp return err;
2880 cf066bf8 2019-05-09 stsp static void
2881 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2883 24519714 2019-05-09 stsp free(ct->path);
2884 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2885 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2886 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2887 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2888 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
2889 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2893 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2894 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2895 24519714 2019-05-09 stsp struct got_repository *repo;
2896 24519714 2019-05-09 stsp struct got_worktree *worktree;
2897 5f8a88c6 2019-08-03 stsp int have_staged_files;
2900 c4296144 2019-05-09 stsp static const struct got_error *
2901 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
2902 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
2903 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2904 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
2906 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2907 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2908 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2909 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2910 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2911 768aea60 2019-05-09 stsp struct stat sb;
2913 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
2914 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2915 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2916 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2917 5f8a88c6 2019-08-03 stsp return NULL;
2919 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
2920 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2922 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2923 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
2924 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
2925 5f8a88c6 2019-08-03 stsp return NULL;
2928 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2929 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2930 036813ee 2019-05-09 stsp goto done;
2932 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2933 036813ee 2019-05-09 stsp parent_path = strdup("");
2934 69960a46 2019-05-09 stsp if (parent_path == NULL)
2935 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2937 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2939 69960a46 2019-05-09 stsp return err;
2942 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2943 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2944 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2945 c4296144 2019-05-09 stsp goto done;
2948 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2949 768aea60 2019-05-09 stsp relpath) == -1) {
2950 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2951 768aea60 2019-05-09 stsp goto done;
2953 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
2954 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2956 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2957 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2958 768aea60 2019-05-09 stsp goto done;
2960 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2963 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2964 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2965 44d03001 2019-05-09 stsp relpath) == -1) {
2966 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2967 44d03001 2019-05-09 stsp goto done;
2970 cf066bf8 2019-05-09 stsp ct->status = status;
2971 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
2972 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2973 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
2974 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
2975 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2976 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2977 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2978 b416585c 2019-05-13 stsp goto done;
2980 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2981 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2982 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
2983 f0b75401 2019-08-03 stsp goto done;
2986 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
2987 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
2988 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
2989 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
2990 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2991 036813ee 2019-05-09 stsp goto done;
2994 24519714 2019-05-09 stsp ct->path = strdup(path);
2995 24519714 2019-05-09 stsp if (ct->path == NULL) {
2996 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2997 24519714 2019-05-09 stsp goto done;
2999 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3001 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
3002 ed175427 2019-05-09 stsp free_commitable(ct);
3003 24519714 2019-05-09 stsp free(parent_path);
3004 036813ee 2019-05-09 stsp free(path);
3005 a129376b 2019-03-28 stsp return err;
3008 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
3009 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
3010 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3011 036813ee 2019-05-09 stsp struct got_repository *);
3013 ed175427 2019-05-09 stsp static const struct got_error *
3014 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
3015 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
3016 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3017 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3018 afa376bf 2019-05-09 stsp struct got_repository *repo)
3020 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
3021 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
3022 036813ee 2019-05-09 stsp char *subpath;
3024 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
3025 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3026 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3028 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
3030 ed175427 2019-05-09 stsp return err;
3032 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3033 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3034 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
3035 036813ee 2019-05-09 stsp free(subpath);
3036 036813ee 2019-05-09 stsp return err;
3039 036813ee 2019-05-09 stsp static const struct got_error *
3040 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3042 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3043 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
3045 036813ee 2019-05-09 stsp *match = 0;
3047 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
3048 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
3049 0f63689d 2019-05-10 stsp return NULL;
3052 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3054 0f63689d 2019-05-10 stsp return err;
3055 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
3056 036813ee 2019-05-09 stsp free(ct_parent_path);
3057 036813ee 2019-05-09 stsp return err;
3060 768aea60 2019-05-09 stsp static mode_t
3061 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
3063 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3066 036813ee 2019-05-09 stsp static const struct got_error *
3067 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3068 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3070 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3072 036813ee 2019-05-09 stsp *new_te = NULL;
3074 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3076 036813ee 2019-05-09 stsp goto done;
3078 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3080 036813ee 2019-05-09 stsp free((*new_te)->id);
3081 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
3082 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3084 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3085 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3086 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3087 036813ee 2019-05-09 stsp goto done;
3090 036813ee 2019-05-09 stsp if (err && *new_te) {
3091 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3092 036813ee 2019-05-09 stsp *new_te = NULL;
3094 036813ee 2019-05-09 stsp return err;
3097 036813ee 2019-05-09 stsp static const struct got_error *
3098 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3099 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3101 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3102 036813ee 2019-05-09 stsp char *ct_name;
3104 036813ee 2019-05-09 stsp *new_te = NULL;
3106 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3107 036813ee 2019-05-09 stsp if (*new_te == NULL)
3108 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3110 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3111 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3112 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3113 036813ee 2019-05-09 stsp goto done;
3115 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
3116 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
3117 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3118 036813ee 2019-05-09 stsp goto done;
3121 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3123 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
3124 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3126 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3127 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3128 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3129 036813ee 2019-05-09 stsp goto done;
3132 036813ee 2019-05-09 stsp if (err && *new_te) {
3133 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3134 036813ee 2019-05-09 stsp *new_te = NULL;
3136 036813ee 2019-05-09 stsp return err;
3139 036813ee 2019-05-09 stsp static const struct got_error *
3140 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3141 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3143 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3144 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3146 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3148 036813ee 2019-05-09 stsp return err;
3149 036813ee 2019-05-09 stsp if (new_pe == NULL)
3150 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3151 036813ee 2019-05-09 stsp return NULL;
3154 afa376bf 2019-05-09 stsp static const struct got_error *
3155 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3156 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3158 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3159 5f8a88c6 2019-08-03 stsp unsigned char status;
3161 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3162 afa376bf 2019-05-09 stsp ct_path++;
3164 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3165 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
3167 5f8a88c6 2019-08-03 stsp status = ct->status;
3169 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3170 537ac44b 2019-08-03 stsp ct_path, ct->blob_id, NULL, NULL);
3173 036813ee 2019-05-09 stsp static const struct got_error *
3174 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3175 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3177 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3178 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3179 44d03001 2019-05-09 stsp char *te_path;
3181 44d03001 2019-05-09 stsp *modified = 0;
3183 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3184 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3185 44d03001 2019-05-09 stsp te->name) == -1)
3186 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3188 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3189 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3190 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3191 44d03001 2019-05-09 stsp strlen(te_path));
3192 44d03001 2019-05-09 stsp if (*modified)
3196 44d03001 2019-05-09 stsp free(te_path);
3197 44d03001 2019-05-09 stsp return err;
3200 44d03001 2019-05-09 stsp static const struct got_error *
3201 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3202 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3203 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3205 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3206 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3208 036813ee 2019-05-09 stsp *ctp = NULL;
3210 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3211 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3212 036813ee 2019-05-09 stsp char *ct_name = NULL;
3213 036813ee 2019-05-09 stsp int path_matches;
3215 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3216 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
3217 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
3220 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
3221 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
3225 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3228 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3230 036813ee 2019-05-09 stsp return err;
3231 036813ee 2019-05-09 stsp if (!path_matches)
3234 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3235 036813ee 2019-05-09 stsp if (ct_name == NULL)
3236 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3238 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3241 036813ee 2019-05-09 stsp *ctp = ct;
3245 2b496619 2019-07-10 stsp return err;
3248 2b496619 2019-07-10 stsp static const struct got_error *
3249 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3250 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3251 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3252 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3253 2b496619 2019-07-10 stsp struct got_repository *repo)
3255 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3256 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3257 2b496619 2019-07-10 stsp char *subtree_path;
3259 2b496619 2019-07-10 stsp *new_tep = NULL;
3261 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3262 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3263 2b496619 2019-07-10 stsp child_path) == -1)
3264 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3266 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3267 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3268 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3269 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3270 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3271 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3272 2b496619 2019-07-10 stsp goto done;
3274 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3275 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3276 2b496619 2019-07-10 stsp if (err) {
3277 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3278 2b496619 2019-07-10 stsp goto done;
3281 2b496619 2019-07-10 stsp free(subtree_path);
3282 2b496619 2019-07-10 stsp if (err == NULL)
3283 2b496619 2019-07-10 stsp *new_tep = new_te;
3284 036813ee 2019-05-09 stsp return err;
3287 036813ee 2019-05-09 stsp static const struct got_error *
3288 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3289 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3290 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3291 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3292 036813ee 2019-05-09 stsp struct got_repository *repo)
3294 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3295 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3296 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3297 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3298 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3299 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3301 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3302 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3303 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3305 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3306 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3307 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3308 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3310 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
3311 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
3312 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3315 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3316 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3319 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3320 036813ee 2019-05-09 stsp pe->path);
3322 036813ee 2019-05-09 stsp goto done;
3324 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3325 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3326 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3328 036813ee 2019-05-09 stsp goto done;
3329 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3331 afa376bf 2019-05-09 stsp goto done;
3332 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3333 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3335 2f51b5b3 2019-05-09 stsp goto done;
3337 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3338 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3339 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3340 2b496619 2019-07-10 stsp == NULL) {
3341 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3342 2b496619 2019-07-10 stsp child_path, path_base_tree,
3343 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3346 2b496619 2019-07-10 stsp goto done;
3347 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3349 2b496619 2019-07-10 stsp goto done;
3354 2f51b5b3 2019-05-09 stsp if (base_tree) {
3355 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3356 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3357 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3358 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3360 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3361 44d03001 2019-05-09 stsp int modified;
3362 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3364 036813ee 2019-05-09 stsp goto done;
3365 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3366 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3368 2f51b5b3 2019-05-09 stsp goto done;
3369 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3370 44d03001 2019-05-09 stsp if (modified) {
3371 44d03001 2019-05-09 stsp free(new_te->id);
3372 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3373 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3374 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3376 44d03001 2019-05-09 stsp goto done;
3378 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3380 036813ee 2019-05-09 stsp goto done;
3384 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3385 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3387 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3388 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
3389 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3390 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3391 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3393 2f51b5b3 2019-05-09 stsp goto done;
3394 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3396 2f51b5b3 2019-05-09 stsp goto done;
3398 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3399 b416585c 2019-05-13 stsp status_arg);
3401 afa376bf 2019-05-09 stsp goto done;
3403 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3404 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3406 2f51b5b3 2019-05-09 stsp goto done;
3407 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3409 2f51b5b3 2019-05-09 stsp goto done;
3414 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3415 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3416 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3417 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3418 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3420 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3422 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3423 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3424 ed175427 2019-05-09 stsp return err;
3427 ebf99748 2019-05-09 stsp static const struct got_error *
3428 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3429 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3431 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3432 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3434 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3435 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3436 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3438 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3440 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
3441 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
3442 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3443 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3444 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
3445 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3446 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
3447 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
3448 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
3449 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
3450 5f8a88c6 2019-08-03 stsp new_base_commit_id->sha1, 1);
3452 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3453 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3454 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3456 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3457 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3458 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3461 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3466 d56d26ce 2019-05-10 stsp return err;
3470 d56d26ce 2019-05-10 stsp static const struct got_error *
3471 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
3472 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
3473 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
3474 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
3475 735ef5ac 2019-08-03 stsp int ood_errcode)
3477 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3478 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
3480 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3481 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3482 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3483 1a36436d 2019-06-10 stsp return NULL;
3485 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
3486 9bead371 2019-07-28 stsp * on matches file content in the branch head.
3488 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3489 735ef5ac 2019-08-03 stsp in_repo_path);
3490 9bead371 2019-07-28 stsp if (err) {
3491 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
3492 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
3493 1a36436d 2019-06-10 stsp goto done;
3494 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
3495 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
3497 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3498 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3499 735ef5ac 2019-08-03 stsp in_repo_path);
3500 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3501 1a36436d 2019-06-10 stsp goto done;
3502 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
3506 1a36436d 2019-06-10 stsp return err;
3509 c4296144 2019-05-09 stsp const struct got_error *
3510 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3511 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3512 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3513 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
3514 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3515 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3516 afa376bf 2019-05-09 stsp struct got_repository *repo)
3518 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3519 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3520 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3521 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3522 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3523 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3524 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3525 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3526 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3527 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3528 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3530 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3532 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3534 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3536 588edf97 2019-05-10 stsp goto done;
3538 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3540 588edf97 2019-05-10 stsp goto done;
3542 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3543 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3548 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3549 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3553 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3554 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3555 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3556 cf066bf8 2019-05-09 stsp char *ondisk_path;
3558 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
3559 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3560 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
3563 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3564 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3567 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3568 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3570 cf066bf8 2019-05-09 stsp goto done;
3572 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3573 a7055788 2019-05-09 stsp free(ondisk_path);
3575 cf066bf8 2019-05-09 stsp goto done;
3578 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3579 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3580 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3582 036813ee 2019-05-09 stsp goto done;
3584 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3586 de18fc63 2019-05-09 stsp goto done;
3587 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3588 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3589 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3590 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3591 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3592 33ad4cbe 2019-05-12 jcs free(logmsg);
3594 9d40349a 2019-05-09 stsp goto done;
3596 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3597 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3598 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3599 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3600 09f5bd90 2019-05-09 stsp goto done;
3602 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3603 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3605 09f5bd90 2019-05-09 stsp goto done;
3606 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3608 ebf99748 2019-05-09 stsp goto done;
3609 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3610 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3611 09f5bd90 2019-05-09 stsp goto done;
3613 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3614 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3616 f2c16586 2019-05-09 stsp goto done;
3617 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3619 f2c16586 2019-05-09 stsp goto done;
3621 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3623 09f5bd90 2019-05-09 stsp goto done;
3625 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3627 ebf99748 2019-05-09 stsp goto done;
3629 588edf97 2019-05-10 stsp if (head_tree)
3630 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3631 588edf97 2019-05-10 stsp if (head_commit)
3632 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3633 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3634 2f17228e 2019-05-12 stsp if (head_ref2) {
3635 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3636 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3637 2f17228e 2019-05-12 stsp err = unlockerr;
3638 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3640 39cd0ff6 2019-07-12 stsp return err;
3643 5c1e53bc 2019-07-28 stsp static const struct got_error *
3644 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
3645 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
3647 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
3648 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
3650 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
3651 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
3652 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
3654 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
3655 5c1e53bc 2019-07-28 stsp ct_path++;
3657 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
3658 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
3662 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
3663 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
3665 5c1e53bc 2019-07-28 stsp return NULL;
3668 f0b75401 2019-08-03 stsp static const struct got_error *
3669 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
3671 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
3673 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
3674 f0b75401 2019-08-03 stsp *have_staged_files = 1;
3675 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
3678 f0b75401 2019-08-03 stsp return NULL;
3681 5f8a88c6 2019-08-03 stsp static const struct got_error *
3682 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
3683 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
3685 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
3686 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
3688 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
3689 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
3691 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3692 5f8a88c6 2019-08-03 stsp if (ie == NULL)
3693 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
3694 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
3695 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
3696 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
3699 5f8a88c6 2019-08-03 stsp return NULL;
3702 39cd0ff6 2019-07-12 stsp const struct got_error *
3703 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3704 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
3705 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
3706 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3707 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3708 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
3710 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3711 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3712 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
3713 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3714 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3715 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
3716 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
3717 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3718 f0b75401 2019-08-03 stsp int have_staged_files = 0;
3720 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
3722 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3724 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
3726 39cd0ff6 2019-07-12 stsp goto done;
3728 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3730 39cd0ff6 2019-07-12 stsp goto done;
3732 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3734 39cd0ff6 2019-07-12 stsp goto done;
3736 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3738 39cd0ff6 2019-07-12 stsp goto done;
3740 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
3741 5f8a88c6 2019-08-03 stsp &have_staged_files);
3742 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
3743 5f8a88c6 2019-08-03 stsp goto done;
3744 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
3745 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
3747 5f8a88c6 2019-08-03 stsp goto done;
3750 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3751 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
3752 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
3753 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
3754 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3755 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3756 5c1e53bc 2019-07-28 stsp collect_commitables, &cc_arg, NULL, NULL);
3758 5c1e53bc 2019-07-28 stsp goto done;
3761 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3762 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3763 39cd0ff6 2019-07-12 stsp goto done;
3766 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3767 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
3769 5c1e53bc 2019-07-28 stsp goto done;
3772 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3773 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
3774 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
3776 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
3777 f0b75401 2019-08-03 stsp ct_path++;
3778 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
3779 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3780 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3782 b50cabdf 2019-07-12 stsp goto done;
3786 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
3787 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
3788 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3790 39cd0ff6 2019-07-12 stsp goto done;
3792 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3793 93ec1b5f 2019-07-12 stsp fileindex);
3794 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3795 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
3796 39cd0ff6 2019-07-12 stsp err = sync_err;
3798 39cd0ff6 2019-07-12 stsp if (fileindex)
3799 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
3800 39cd0ff6 2019-07-12 stsp free(fileindex_path);
3801 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3802 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
3803 39cd0ff6 2019-07-12 stsp err = unlockerr;
3804 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3805 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
3806 39cd0ff6 2019-07-12 stsp free_commitable(ct);
3808 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
3809 c4296144 2019-05-09 stsp return err;
3812 8656d6c4 2019-05-20 stsp const char *
3813 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3815 8656d6c4 2019-05-20 stsp return ct->path;
3818 8656d6c4 2019-05-20 stsp unsigned int
3819 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3821 8656d6c4 2019-05-20 stsp return ct->status;
3824 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3825 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3826 818c7501 2019-07-11 stsp struct got_repository *repo;
3829 818c7501 2019-07-11 stsp static const struct got_error *
3830 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3832 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3833 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3834 818c7501 2019-07-11 stsp unsigned char status;
3835 818c7501 2019-07-11 stsp struct stat sb;
3836 818c7501 2019-07-11 stsp char *ondisk_path;
3838 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
3839 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3840 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
3841 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3843 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3845 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3847 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
3848 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3849 818c7501 2019-07-11 stsp free(ondisk_path);
3851 818c7501 2019-07-11 stsp return err;
3853 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
3854 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3855 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
3856 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
3858 818c7501 2019-07-11 stsp return NULL;
3861 818c7501 2019-07-11 stsp const struct got_error *
3862 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3863 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3864 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
3865 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
3867 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3868 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3869 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3870 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3871 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3872 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3874 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3875 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3876 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3878 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3880 818c7501 2019-07-11 stsp return err;
3882 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3884 818c7501 2019-07-11 stsp goto done;
3886 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3887 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3888 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3891 818c7501 2019-07-11 stsp goto done;
3893 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3895 818c7501 2019-07-11 stsp goto done;
3897 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3899 818c7501 2019-07-11 stsp goto done;
3901 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3903 818c7501 2019-07-11 stsp goto done;
3905 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3908 818c7501 2019-07-11 stsp goto done;
3910 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3911 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3913 818c7501 2019-07-11 stsp goto done;
3914 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3916 818c7501 2019-07-11 stsp goto done;
3918 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3920 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3922 818c7501 2019-07-11 stsp goto done;
3924 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3926 818c7501 2019-07-11 stsp goto done;
3928 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3929 818c7501 2019-07-11 stsp worktree->base_commit_id);
3931 818c7501 2019-07-11 stsp goto done;
3932 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3934 818c7501 2019-07-11 stsp goto done;
3936 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3938 818c7501 2019-07-11 stsp goto done;
3940 818c7501 2019-07-11 stsp free(fileindex_path);
3941 818c7501 2019-07-11 stsp free(tmp_branch_name);
3942 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3943 818c7501 2019-07-11 stsp free(branch_ref_name);
3944 818c7501 2019-07-11 stsp if (branch_ref)
3945 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3946 818c7501 2019-07-11 stsp if (wt_branch)
3947 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3948 818c7501 2019-07-11 stsp if (err) {
3949 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3950 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3951 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3953 818c7501 2019-07-11 stsp if (*tmp_branch) {
3954 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3955 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3957 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3958 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3959 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3961 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3963 818c7501 2019-07-11 stsp return err;
3966 818c7501 2019-07-11 stsp const struct got_error *
3967 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3968 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3969 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
3970 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
3972 818c7501 2019-07-11 stsp const struct got_error *err;
3973 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3974 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3975 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3976 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
3977 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
3979 818c7501 2019-07-11 stsp *commit_id = NULL;
3980 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
3981 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
3982 3e3a69f1 2019-07-25 stsp *branch = NULL;
3983 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3985 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
3987 3e3a69f1 2019-07-25 stsp return err;
3989 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3991 f032f1f7 2019-08-04 stsp goto done;
3993 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
3994 f032f1f7 2019-08-04 stsp &have_staged_files);
3995 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
3996 f032f1f7 2019-08-04 stsp goto done;
3997 f032f1f7 2019-08-04 stsp if (have_staged_files) {
3998 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
3999 3e3a69f1 2019-07-25 stsp goto done;
4002 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4004 f032f1f7 2019-08-04 stsp goto done;
4006 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4008 818c7501 2019-07-11 stsp goto done;
4010 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4012 818c7501 2019-07-11 stsp goto done;
4014 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4016 818c7501 2019-07-11 stsp goto done;
4018 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4020 818c7501 2019-07-11 stsp goto done;
4022 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
4023 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
4025 818c7501 2019-07-11 stsp goto done;
4027 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4029 818c7501 2019-07-11 stsp goto done;
4031 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4033 818c7501 2019-07-11 stsp goto done;
4035 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
4036 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
4038 818c7501 2019-07-11 stsp goto done;
4040 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4042 818c7501 2019-07-11 stsp goto done;
4044 818c7501 2019-07-11 stsp free(commit_ref_name);
4045 818c7501 2019-07-11 stsp free(branch_ref_name);
4046 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4047 818c7501 2019-07-11 stsp if (commit_ref)
4048 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4049 818c7501 2019-07-11 stsp if (branch_ref)
4050 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4051 818c7501 2019-07-11 stsp if (err) {
4052 818c7501 2019-07-11 stsp free(*commit_id);
4053 818c7501 2019-07-11 stsp *commit_id = NULL;
4054 818c7501 2019-07-11 stsp if (*tmp_branch) {
4055 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4056 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4058 818c7501 2019-07-11 stsp if (*new_base_branch) {
4059 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
4060 818c7501 2019-07-11 stsp *new_base_branch = NULL;
4062 818c7501 2019-07-11 stsp if (*branch) {
4063 818c7501 2019-07-11 stsp got_ref_close(*branch);
4064 818c7501 2019-07-11 stsp *branch = NULL;
4066 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4067 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4068 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4070 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
4072 818c7501 2019-07-11 stsp return err;
4075 818c7501 2019-07-11 stsp const struct got_error *
4076 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4078 818c7501 2019-07-11 stsp const struct got_error *err;
4079 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
4081 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4083 818c7501 2019-07-11 stsp return err;
4085 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4086 818c7501 2019-07-11 stsp free(tmp_branch_name);
4087 818c7501 2019-07-11 stsp return NULL;
4090 818c7501 2019-07-11 stsp static const struct got_error *
4091 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4092 818c7501 2019-07-11 stsp char **logmsg, void *arg)
4094 0ebf8283 2019-07-24 stsp *logmsg = arg;
4095 818c7501 2019-07-11 stsp return NULL;
4098 818c7501 2019-07-11 stsp static const struct got_error *
4099 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4100 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4101 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4103 818c7501 2019-07-11 stsp return NULL;
4106 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
4107 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
4108 01757395 2019-07-12 stsp void *progress_arg;
4109 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
4112 01757395 2019-07-12 stsp static const struct got_error *
4113 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
4115 01757395 2019-07-12 stsp const struct got_error *err;
4116 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
4118 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
4120 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
4122 01757395 2019-07-12 stsp return err;
4124 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
4125 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
4126 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
4127 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
4128 01757395 2019-07-12 stsp return NULL;
4130 01757395 2019-07-12 stsp p = strdup(path);
4131 01757395 2019-07-12 stsp if (p == NULL)
4132 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
4134 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4135 01757395 2019-07-12 stsp if (err || new == NULL)
4137 01757395 2019-07-12 stsp return err;
4141 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4143 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4145 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
4146 01757395 2019-07-12 stsp free((char *)pe->path);
4148 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
4151 0ebf8283 2019-07-24 stsp static const struct got_error *
4152 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4153 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4155 818c7501 2019-07-11 stsp const struct got_error *err;
4156 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
4158 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4159 818c7501 2019-07-11 stsp if (err) {
4160 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
4161 818c7501 2019-07-11 stsp goto done;
4162 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4164 818c7501 2019-07-11 stsp goto done;
4165 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
4167 818c7501 2019-07-11 stsp goto done;
4169 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
4172 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
4174 818c7501 2019-07-11 stsp goto done;
4175 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
4176 818c7501 2019-07-11 stsp free(stored_id);
4177 818c7501 2019-07-11 stsp if (cmp != 0) {
4178 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4179 818c7501 2019-07-11 stsp goto done;
4183 0ebf8283 2019-07-24 stsp if (commit_ref)
4184 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4185 0ebf8283 2019-07-24 stsp return err;
4188 0ebf8283 2019-07-24 stsp static const struct got_error *
4189 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
4190 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
4191 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4192 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
4193 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4194 3e3a69f1 2019-07-25 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4196 0ebf8283 2019-07-24 stsp const struct got_error *err;
4197 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4198 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
4199 3e3a69f1 2019-07-25 stsp char *fileindex_path;
4201 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4203 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4205 0ebf8283 2019-07-24 stsp return err;
4207 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
4208 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
4209 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
4210 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
4211 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
4212 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
4213 818c7501 2019-07-11 stsp if (commit_ref)
4214 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4215 818c7501 2019-07-11 stsp return err;
4218 818c7501 2019-07-11 stsp const struct got_error *
4219 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4220 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4221 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4222 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4223 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4224 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4226 0ebf8283 2019-07-24 stsp const struct got_error *err;
4227 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4229 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4231 0ebf8283 2019-07-24 stsp return err;
4233 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4235 0ebf8283 2019-07-24 stsp goto done;
4237 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4238 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4239 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4241 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4242 0ebf8283 2019-07-24 stsp return err;
4245 0ebf8283 2019-07-24 stsp const struct got_error *
4246 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4247 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4248 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4249 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4250 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4251 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4253 0ebf8283 2019-07-24 stsp const struct got_error *err;
4254 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4256 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4258 0ebf8283 2019-07-24 stsp return err;
4260 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4262 0ebf8283 2019-07-24 stsp goto done;
4264 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4265 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4266 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4268 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4269 0ebf8283 2019-07-24 stsp return err;
4272 0ebf8283 2019-07-24 stsp static const struct got_error *
4273 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4274 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4275 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4276 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4277 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4279 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4280 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4281 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4282 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4283 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4284 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4285 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4287 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4288 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4290 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4292 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4294 3e3a69f1 2019-07-25 stsp return err;
4296 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4297 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4298 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4299 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
4301 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4302 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4303 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4304 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4306 01757395 2019-07-12 stsp if (merged_paths) {
4307 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4308 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4309 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4310 8f8646e5 2019-07-25 stsp goto done;
4312 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4313 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4314 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4316 01757395 2019-07-12 stsp goto done;
4319 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4320 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4322 01757395 2019-07-12 stsp goto done;
4325 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4326 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4327 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4329 a0e95631 2019-07-12 stsp goto done;
4330 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4331 a0e95631 2019-07-12 stsp goto done;
4334 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4336 edd02c5e 2019-07-11 stsp goto done;
4338 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4340 818c7501 2019-07-11 stsp goto done;
4342 0ebf8283 2019-07-24 stsp if (new_logmsg)
4343 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4345 0ebf8283 2019-07-24 stsp logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4346 0ebf8283 2019-07-24 stsp if (logmsg == NULL)
4347 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
4349 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4350 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
4351 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4352 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4354 a0e95631 2019-07-12 stsp goto done;
4356 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4358 a0e95631 2019-07-12 stsp goto done;
4360 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4362 a0e95631 2019-07-12 stsp goto done;
4364 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4365 93ec1b5f 2019-07-12 stsp fileindex);
4366 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4367 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4368 a0e95631 2019-07-12 stsp err = sync_err;
4370 a0e95631 2019-07-12 stsp free(fileindex_path);
4371 a0e95631 2019-07-12 stsp free(head_commit_id);
4372 a0e95631 2019-07-12 stsp if (head_ref)
4373 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4374 818c7501 2019-07-11 stsp if (err) {
4375 818c7501 2019-07-11 stsp free(*new_commit_id);
4376 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4378 818c7501 2019-07-11 stsp return err;
4381 818c7501 2019-07-11 stsp const struct got_error *
4382 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4383 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4384 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4385 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4386 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4388 0ebf8283 2019-07-24 stsp const struct got_error *err;
4389 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4390 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4391 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4393 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4395 0ebf8283 2019-07-24 stsp return err;
4397 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4399 0ebf8283 2019-07-24 stsp goto done;
4400 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4402 0ebf8283 2019-07-24 stsp goto done;
4403 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4404 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4405 0ebf8283 2019-07-24 stsp goto done;
4408 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4409 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4411 0ebf8283 2019-07-24 stsp if (commit_ref)
4412 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4413 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4414 0ebf8283 2019-07-24 stsp free(commit_id);
4415 0ebf8283 2019-07-24 stsp return err;
4418 0ebf8283 2019-07-24 stsp const struct got_error *
4419 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4420 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4421 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4422 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4423 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4424 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4426 0ebf8283 2019-07-24 stsp const struct got_error *err;
4427 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4428 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4429 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4431 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4433 0ebf8283 2019-07-24 stsp return err;
4435 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4437 0ebf8283 2019-07-24 stsp goto done;
4438 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4440 0ebf8283 2019-07-24 stsp goto done;
4441 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4442 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4443 0ebf8283 2019-07-24 stsp goto done;
4446 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4447 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4449 0ebf8283 2019-07-24 stsp if (commit_ref)
4450 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4451 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4452 0ebf8283 2019-07-24 stsp free(commit_id);
4453 0ebf8283 2019-07-24 stsp return err;
4456 0ebf8283 2019-07-24 stsp const struct got_error *
4457 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4458 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4460 3e3a69f1 2019-07-25 stsp if (fileindex)
4461 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4462 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4465 69844fba 2019-07-11 stsp static const struct got_error *
4466 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4468 69844fba 2019-07-11 stsp const struct got_error *err;
4469 69844fba 2019-07-11 stsp struct got_reference *ref;
4471 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4472 69844fba 2019-07-11 stsp if (err) {
4473 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4474 69844fba 2019-07-11 stsp return NULL;
4475 69844fba 2019-07-11 stsp return err;
4478 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4479 69844fba 2019-07-11 stsp got_ref_close(ref);
4480 69844fba 2019-07-11 stsp return err;
4483 69844fba 2019-07-11 stsp static const struct got_error *
4484 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4486 69844fba 2019-07-11 stsp const struct got_error *err;
4487 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4488 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4490 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4492 69844fba 2019-07-11 stsp goto done;
4493 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4495 69844fba 2019-07-11 stsp goto done;
4497 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4499 69844fba 2019-07-11 stsp goto done;
4500 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4502 69844fba 2019-07-11 stsp goto done;
4504 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4506 69844fba 2019-07-11 stsp goto done;
4507 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4509 69844fba 2019-07-11 stsp goto done;
4511 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4513 69844fba 2019-07-11 stsp goto done;
4514 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4516 69844fba 2019-07-11 stsp goto done;
4519 69844fba 2019-07-11 stsp free(tmp_branch_name);
4520 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4521 69844fba 2019-07-11 stsp free(branch_ref_name);
4522 69844fba 2019-07-11 stsp free(commit_ref_name);
4523 69844fba 2019-07-11 stsp return err;
4526 818c7501 2019-07-11 stsp const struct got_error *
4527 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4528 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4529 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4530 818c7501 2019-07-11 stsp struct got_repository *repo)
4532 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4533 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4535 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4537 818c7501 2019-07-11 stsp return err;
4539 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4541 818c7501 2019-07-11 stsp goto done;
4543 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4545 818c7501 2019-07-11 stsp goto done;
4547 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4549 818c7501 2019-07-11 stsp goto done;
4551 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4553 3e3a69f1 2019-07-25 stsp if (fileindex)
4554 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4555 818c7501 2019-07-11 stsp free(new_head_commit_id);
4556 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4557 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4558 818c7501 2019-07-11 stsp err = unlockerr;
4559 818c7501 2019-07-11 stsp return err;
4562 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
4563 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
4564 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4567 818c7501 2019-07-11 stsp static const struct got_error *
4568 88d0e355 2019-08-03 stsp collect_revertible_paths(void *arg, unsigned char status,
4569 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4570 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4571 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
4573 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
4574 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4575 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
4576 818c7501 2019-07-11 stsp char *path = NULL;
4578 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
4579 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
4580 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
4581 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
4582 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
4583 818c7501 2019-07-11 stsp return NULL;
4585 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4586 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4588 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4589 818c7501 2019-07-11 stsp if (err || new == NULL)
4590 818c7501 2019-07-11 stsp free(path);
4591 818c7501 2019-07-11 stsp return err;
4594 818c7501 2019-07-11 stsp const struct got_error *
4595 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4596 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4597 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
4598 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4600 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4601 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4602 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4603 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4604 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
4605 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
4606 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
4607 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4609 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
4611 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4613 818c7501 2019-07-11 stsp return err;
4615 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4616 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4618 818c7501 2019-07-11 stsp goto done;
4620 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4622 818c7501 2019-07-11 stsp goto done;
4625 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4626 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4627 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4629 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4631 818c7501 2019-07-11 stsp goto done;
4633 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4635 818c7501 2019-07-11 stsp goto done;
4637 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4638 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4640 ca355955 2019-07-12 stsp goto done;
4642 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4644 ca355955 2019-07-12 stsp goto done;
4646 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4648 818c7501 2019-07-11 stsp goto done;
4650 347d1d3e 2019-07-12 stsp crp_arg.revertible_paths = &revertible_paths;
4651 347d1d3e 2019-07-12 stsp crp_arg.worktree = worktree;
4652 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4653 347d1d3e 2019-07-12 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4655 55bd499d 2019-07-12 stsp goto done;
4657 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4658 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
4659 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
4661 ca355955 2019-07-12 stsp goto sync;
4664 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4665 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4667 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4668 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4669 ca355955 2019-07-12 stsp err = sync_err;
4671 818c7501 2019-07-11 stsp got_ref_close(resolved);
4672 a3a2faf2 2019-07-12 stsp free(tree_id);
4673 818c7501 2019-07-11 stsp free(commit_id);
4674 0ebf8283 2019-07-24 stsp if (fileindex)
4675 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
4676 0ebf8283 2019-07-24 stsp free(fileindex_path);
4677 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4678 0ebf8283 2019-07-24 stsp free((char *)pe->path);
4679 0ebf8283 2019-07-24 stsp got_pathlist_free(&revertible_paths);
4681 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4682 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4683 0ebf8283 2019-07-24 stsp err = unlockerr;
4684 0ebf8283 2019-07-24 stsp return err;
4687 0ebf8283 2019-07-24 stsp const struct got_error *
4688 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4689 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4690 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
4691 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4693 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4694 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4695 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
4696 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
4697 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4698 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
4699 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
4700 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4702 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4703 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4704 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4705 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4707 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4709 0ebf8283 2019-07-24 stsp return err;
4711 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4713 0ebf8283 2019-07-24 stsp goto done;
4715 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
4716 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
4717 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4720 0ebf8283 2019-07-24 stsp goto done;
4722 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4724 0ebf8283 2019-07-24 stsp goto done;
4726 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4728 0ebf8283 2019-07-24 stsp goto done;
4730 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4731 0ebf8283 2019-07-24 stsp worktree);
4733 0ebf8283 2019-07-24 stsp goto done;
4735 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4738 0ebf8283 2019-07-24 stsp goto done;
4740 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4742 0ebf8283 2019-07-24 stsp goto done;
4744 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
4746 0ebf8283 2019-07-24 stsp goto done;
4748 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4749 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4751 0ebf8283 2019-07-24 stsp goto done;
4752 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
4754 0ebf8283 2019-07-24 stsp goto done;
4755 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4756 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
4757 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
4758 0ebf8283 2019-07-24 stsp goto done;
4761 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4762 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4764 0ebf8283 2019-07-24 stsp goto done;
4765 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
4767 0ebf8283 2019-07-24 stsp goto done;
4769 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4771 0ebf8283 2019-07-24 stsp goto done;
4773 0ebf8283 2019-07-24 stsp free(fileindex_path);
4774 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4775 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4776 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4777 0ebf8283 2019-07-24 stsp if (wt_branch)
4778 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
4779 0ebf8283 2019-07-24 stsp if (err) {
4780 0ebf8283 2019-07-24 stsp if (*branch_ref) {
4781 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
4782 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4784 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4785 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4786 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4788 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4789 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4790 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4791 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4793 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
4795 0ebf8283 2019-07-24 stsp return err;
4798 0ebf8283 2019-07-24 stsp const struct got_error *
4799 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
4800 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4802 3e3a69f1 2019-07-25 stsp if (fileindex)
4803 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4804 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
4807 0ebf8283 2019-07-24 stsp const struct got_error *
4808 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
4809 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
4811 0ebf8283 2019-07-24 stsp const struct got_error *err;
4812 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4814 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4816 0ebf8283 2019-07-24 stsp return err;
4818 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4819 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4820 0ebf8283 2019-07-24 stsp return NULL;
4823 0ebf8283 2019-07-24 stsp const struct got_error *
4824 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
4825 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
4826 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4827 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4829 0ebf8283 2019-07-24 stsp const struct got_error *err;
4830 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4831 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4832 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4833 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4834 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4835 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
4837 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4838 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4839 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4840 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4842 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4844 3e3a69f1 2019-07-25 stsp return err;
4846 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4848 f032f1f7 2019-08-04 stsp goto done;
4850 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4851 f032f1f7 2019-08-04 stsp &have_staged_files);
4852 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
4853 f032f1f7 2019-08-04 stsp goto done;
4854 f032f1f7 2019-08-04 stsp if (have_staged_files) {
4855 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
4856 3e3a69f1 2019-07-25 stsp goto done;
4859 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4861 f032f1f7 2019-08-04 stsp goto done;
4863 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4865 0ebf8283 2019-07-24 stsp goto done;
4867 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4869 0ebf8283 2019-07-24 stsp goto done;
4871 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4872 0ebf8283 2019-07-24 stsp worktree);
4874 0ebf8283 2019-07-24 stsp goto done;
4876 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4878 0ebf8283 2019-07-24 stsp goto done;
4880 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4882 0ebf8283 2019-07-24 stsp goto done;
4883 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4885 0ebf8283 2019-07-24 stsp goto done;
4887 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4889 0ebf8283 2019-07-24 stsp goto done;
4890 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4892 0ebf8283 2019-07-24 stsp goto done;
4894 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4896 0ebf8283 2019-07-24 stsp goto done;
4898 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4899 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4900 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4901 0ebf8283 2019-07-24 stsp if (commit_ref)
4902 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4903 0ebf8283 2019-07-24 stsp if (base_commit_ref)
4904 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
4905 0ebf8283 2019-07-24 stsp if (err) {
4906 0ebf8283 2019-07-24 stsp free(*commit_id);
4907 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4908 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4909 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4910 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4911 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4912 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4914 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4915 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4916 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4918 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
4920 0ebf8283 2019-07-24 stsp return err;
4923 0ebf8283 2019-07-24 stsp static const struct got_error *
4924 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4926 0ebf8283 2019-07-24 stsp const struct got_error *err;
4927 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4928 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4930 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4932 0ebf8283 2019-07-24 stsp goto done;
4933 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
4935 0ebf8283 2019-07-24 stsp goto done;
4937 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4938 0ebf8283 2019-07-24 stsp worktree);
4940 0ebf8283 2019-07-24 stsp goto done;
4941 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
4943 0ebf8283 2019-07-24 stsp goto done;
4945 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4947 0ebf8283 2019-07-24 stsp goto done;
4948 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
4950 0ebf8283 2019-07-24 stsp goto done;
4952 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4954 0ebf8283 2019-07-24 stsp goto done;
4955 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4957 0ebf8283 2019-07-24 stsp goto done;
4959 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4960 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4961 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4962 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4963 0ebf8283 2019-07-24 stsp return err;
4966 0ebf8283 2019-07-24 stsp const struct got_error *
4967 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
4968 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4969 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
4970 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4972 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
4973 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4974 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4975 0ebf8283 2019-07-24 stsp struct got_pathlist_head revertible_paths;
4976 0ebf8283 2019-07-24 stsp struct got_pathlist_entry *pe;
4977 0ebf8283 2019-07-24 stsp struct collect_revertible_paths_arg crp_arg;
4978 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
4980 0ebf8283 2019-07-24 stsp TAILQ_INIT(&revertible_paths);
4982 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4984 0ebf8283 2019-07-24 stsp return err;
4986 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4987 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
4989 0ebf8283 2019-07-24 stsp goto done;
4991 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4993 0ebf8283 2019-07-24 stsp goto done;
4995 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4997 0ebf8283 2019-07-24 stsp goto done;
4999 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5000 0ebf8283 2019-07-24 stsp worktree->path_prefix);
5002 0ebf8283 2019-07-24 stsp goto done;
5004 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5006 0ebf8283 2019-07-24 stsp goto done;
5008 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5010 0ebf8283 2019-07-24 stsp goto done;
5012 0ebf8283 2019-07-24 stsp crp_arg.revertible_paths = &revertible_paths;
5013 0ebf8283 2019-07-24 stsp crp_arg.worktree = worktree;
5014 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
5015 0ebf8283 2019-07-24 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
5017 0ebf8283 2019-07-24 stsp goto done;
5019 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
5020 0ebf8283 2019-07-24 stsp err = revert_file(worktree, fileindex, pe->path,
5021 0ebf8283 2019-07-24 stsp progress_cb, progress_arg, repo);
5023 0ebf8283 2019-07-24 stsp goto sync;
5026 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5027 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
5029 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5030 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
5031 0ebf8283 2019-07-24 stsp err = sync_err;
5033 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
5034 0ebf8283 2019-07-24 stsp free(tree_id);
5035 818c7501 2019-07-11 stsp free(fileindex_path);
5036 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
5037 818c7501 2019-07-11 stsp free((char *)pe->path);
5038 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
5040 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5041 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5042 818c7501 2019-07-11 stsp err = unlockerr;
5043 818c7501 2019-07-11 stsp return err;
5046 0ebf8283 2019-07-24 stsp const struct got_error *
5047 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
5048 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5049 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
5051 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
5052 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
5053 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5055 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5057 0ebf8283 2019-07-24 stsp return err;
5059 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5060 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
5062 0ebf8283 2019-07-24 stsp goto done;
5064 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
5066 0ebf8283 2019-07-24 stsp goto done;
5068 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
5070 0ebf8283 2019-07-24 stsp goto done;
5072 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5074 0ebf8283 2019-07-24 stsp goto done;
5076 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5078 3e3a69f1 2019-07-25 stsp if (fileindex)
5079 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5080 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
5081 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5082 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5083 0ebf8283 2019-07-24 stsp err = unlockerr;
5084 0ebf8283 2019-07-24 stsp return err;
5087 0ebf8283 2019-07-24 stsp const struct got_error *
5088 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5089 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5091 0ebf8283 2019-07-24 stsp const struct got_error *err;
5092 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5094 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5096 0ebf8283 2019-07-24 stsp return err;
5098 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5100 0ebf8283 2019-07-24 stsp goto done;
5102 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5104 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5105 0cb83759 2019-08-03 stsp return err;
5108 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
5109 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
5110 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5111 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5112 2db2652d 2019-08-07 stsp struct got_repository *repo;
5113 2db2652d 2019-08-07 stsp int have_changes;
5116 2db2652d 2019-08-07 stsp const struct got_error *
5117 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
5118 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5119 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5120 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5122 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
5123 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
5124 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
5125 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
5126 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
5127 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
5129 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
5130 8b13ce36 2019-08-08 stsp return NULL;
5132 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5133 735ef5ac 2019-08-03 stsp if (ie == NULL)
5134 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5136 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5137 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5138 735ef5ac 2019-08-03 stsp relpath) == -1)
5139 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
5141 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
5142 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
5143 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5144 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
5147 3aa5969e 2019-08-06 stsp if (status == GOT_STATUS_NO_CHANGE) {
5148 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5149 3aa5969e 2019-08-06 stsp goto done;
5150 3aa5969e 2019-08-06 stsp } else if (status == GOT_STATUS_CONFLICT) {
5151 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5152 3aa5969e 2019-08-06 stsp goto done;
5153 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
5154 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
5155 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
5156 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5157 735ef5ac 2019-08-03 stsp goto done;
5160 2db2652d 2019-08-07 stsp a->have_changes = 1;
5162 735ef5ac 2019-08-03 stsp p = in_repo_path;
5163 735ef5ac 2019-08-03 stsp while (p[0] == '/')
5165 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
5166 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
5167 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
5169 735ef5ac 2019-08-03 stsp free(in_repo_path);
5170 735ef5ac 2019-08-03 stsp return err;
5173 dc424a06 2019-08-07 stsp static const struct got_error *
5174 2e1f37b0 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
5176 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
5177 dc424a06 2019-08-07 stsp char *line = NULL;
5178 dc424a06 2019-08-07 stsp size_t linesize = 0, n;
5179 dc424a06 2019-08-07 stsp ssize_t linelen;
5181 dc424a06 2019-08-07 stsp linelen = getline(&line, &linesize, infile);
5182 dc424a06 2019-08-07 stsp if (linelen == -1) {
5183 dc424a06 2019-08-07 stsp if (ferror(infile)) {
5184 dc424a06 2019-08-07 stsp err = got_error_from_errno("getline");
5185 dc424a06 2019-08-07 stsp goto done;
5187 dc424a06 2019-08-07 stsp return NULL;
5189 2e1f37b0 2019-08-08 stsp if (outfile) {
5190 2e1f37b0 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
5191 2e1f37b0 2019-08-08 stsp if (n != linelen) {
5192 2e1f37b0 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
5193 2e1f37b0 2019-08-08 stsp goto done;
5196 2e1f37b0 2019-08-08 stsp if (rejectfile) {
5197 2e1f37b0 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
5198 2e1f37b0 2019-08-08 stsp if (n != linelen)
5199 2e1f37b0 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
5202 dc424a06 2019-08-07 stsp free(line);
5203 dc424a06 2019-08-07 stsp return err;
5206 dc424a06 2019-08-07 stsp static const struct got_error *
5207 dc424a06 2019-08-07 stsp skip_one_line(FILE *f)
5209 dc424a06 2019-08-07 stsp char *line = NULL;
5210 dc424a06 2019-08-07 stsp size_t linesize = 0;
5211 dc424a06 2019-08-07 stsp ssize_t linelen;
5213 dc424a06 2019-08-07 stsp linelen = getline(&line, &linesize, f);
5214 dc424a06 2019-08-07 stsp free(line);
5215 dc424a06 2019-08-07 stsp if (linelen == -1 && ferror(f))
5216 dc424a06 2019-08-07 stsp return got_error_from_errno("getline");
5217 dc424a06 2019-08-07 stsp return NULL;
5220 735ef5ac 2019-08-03 stsp static const struct got_error *
5221 2e1f37b0 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
5222 2e1f37b0 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
5223 2e1f37b0 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
5225 2e1f37b0 2019-08-08 stsp const struct got_error *err;
5227 2e1f37b0 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
5228 2e1f37b0 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
5229 2e1f37b0 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
5231 2e1f37b0 2019-08-08 stsp return err;
5232 2e1f37b0 2019-08-08 stsp (*line_cur1)++;
5234 2e1f37b0 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
5235 2e1f37b0 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
5236 2e1f37b0 2019-08-08 stsp if (rejectfile)
5237 2e1f37b0 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
5239 2e1f37b0 2019-08-08 stsp err = skip_one_line(f2);
5241 2e1f37b0 2019-08-08 stsp return err;
5242 2e1f37b0 2019-08-08 stsp (*line_cur2)++;
5244 2e1f37b0 2019-08-08 stsp /* Copy patched lines. */
5245 2e1f37b0 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
5246 2e1f37b0 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
5248 2e1f37b0 2019-08-08 stsp return err;
5249 2e1f37b0 2019-08-08 stsp (*line_cur2)++;
5251 2e1f37b0 2019-08-08 stsp /* Skip over old file's replaced lines. */
5252 2e1f37b0 2019-08-08 stsp while (!feof(f1) && *line_cur1 <= end_new) {
5253 2e1f37b0 2019-08-08 stsp if (rejectfile)
5254 2e1f37b0 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
5256 2e1f37b0 2019-08-08 stsp err = skip_one_line(f1);
5258 2e1f37b0 2019-08-08 stsp return err;
5259 2e1f37b0 2019-08-08 stsp (*line_cur1)++;
5261 2e1f37b0 2019-08-08 stsp /* Copy old file's lines after patch. */
5262 2e1f37b0 2019-08-08 stsp while (!feof(f1) && *line_cur1 <= end_old) {
5263 2e1f37b0 2019-08-08 stsp err = copy_one_line(f1, outfile, rejectfile);
5265 2e1f37b0 2019-08-08 stsp return err;
5266 2e1f37b0 2019-08-08 stsp (*line_cur1)++;
5269 2e1f37b0 2019-08-08 stsp return NULL;
5272 2e1f37b0 2019-08-08 stsp static const struct got_error *
5273 a7c9878d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
5274 a7c9878d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
5275 a7c9878d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
5276 2e1f37b0 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
5277 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
5279 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
5280 dc424a06 2019-08-07 stsp int start_old = change->cv.a;
5281 dc424a06 2019-08-07 stsp int end_old = change->cv.b;
5282 dc424a06 2019-08-07 stsp int start_new = change->cv.c;
5283 dc424a06 2019-08-07 stsp int end_new = change->cv.d;
5284 dc424a06 2019-08-07 stsp long pos1, pos2;
5285 dc424a06 2019-08-07 stsp FILE *hunkfile;
5287 dc424a06 2019-08-07 stsp *choice = GOT_PATCH_CHOICE_NONE;
5289 dc424a06 2019-08-07 stsp hunkfile = got_opentemp();
5290 dc424a06 2019-08-07 stsp if (hunkfile == NULL)
5291 dc424a06 2019-08-07 stsp return got_error_from_errno("got_opentemp");
5293 dc424a06 2019-08-07 stsp pos1 = ftell(f1);
5294 dc424a06 2019-08-07 stsp pos2 = ftell(f2);
5296 dc424a06 2019-08-07 stsp /* XXX TODO needs error checking */
5297 dc424a06 2019-08-07 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
5299 dc424a06 2019-08-07 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
5300 dc424a06 2019-08-07 stsp err = got_ferror(f1, GOT_ERR_IO);
5301 dc424a06 2019-08-07 stsp goto done;
5303 dc424a06 2019-08-07 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
5304 dc424a06 2019-08-07 stsp err = got_ferror(f1, GOT_ERR_IO);
5305 dc424a06 2019-08-07 stsp goto done;
5307 dc424a06 2019-08-07 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
5308 dc424a06 2019-08-07 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
5309 dc424a06 2019-08-07 stsp goto done;
5312 dc424a06 2019-08-07 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
5313 a7c9878d 2019-08-08 stsp hunkfile, n, nchanges);
5315 dc424a06 2019-08-07 stsp goto done;
5317 dc424a06 2019-08-07 stsp switch (*choice) {
5318 dc424a06 2019-08-07 stsp case GOT_PATCH_CHOICE_YES:
5319 2e1f37b0 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
5320 2e1f37b0 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
5322 dc424a06 2019-08-07 stsp case GOT_PATCH_CHOICE_NO:
5323 2e1f37b0 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
5324 2e1f37b0 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
5326 b353a198 2019-08-07 stsp case GOT_PATCH_CHOICE_QUIT:
5327 19e4b907 2019-08-08 stsp if (outfile) {
5328 19e4b907 2019-08-08 stsp /* Copy old file's lines until EOF. */
5329 19e4b907 2019-08-08 stsp while (!feof(f1)) {
5330 19e4b907 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
5332 19e4b907 2019-08-08 stsp goto done;
5333 19e4b907 2019-08-08 stsp (*line_cur1)++;
5336 19e4b907 2019-08-08 stsp if (rejectfile) {
5337 19e4b907 2019-08-08 stsp /* Copy new file's lines until EOF. */
5338 19e4b907 2019-08-08 stsp while (!feof(f2)) {
5339 19e4b907 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
5341 19e4b907 2019-08-08 stsp goto done;
5342 19e4b907 2019-08-08 stsp (*line_cur2)++;
5347 dc424a06 2019-08-07 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5351 dc424a06 2019-08-07 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
5352 dc424a06 2019-08-07 stsp err = got_error_from_errno("fclose");
5353 dc424a06 2019-08-07 stsp return err;
5356 dc424a06 2019-08-07 stsp static const struct got_error *
5357 dc424a06 2019-08-07 stsp create_staged_content(char **path_outfile, struct got_object_id *blob_id,
5358 dc424a06 2019-08-07 stsp const char *path2, const char *relpath, struct got_repository *repo,
5359 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
5361 dc424a06 2019-08-07 stsp const struct got_error *err;
5362 dc424a06 2019-08-07 stsp struct got_blob_object *blob = NULL;
5363 dc424a06 2019-08-07 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5364 dc424a06 2019-08-07 stsp char *path1 = NULL, *id_str = NULL;
5365 dc424a06 2019-08-07 stsp struct stat sb1, sb2;
5366 dc424a06 2019-08-07 stsp struct got_diff_changes *changes = NULL;
5367 dc424a06 2019-08-07 stsp struct got_diff_state *ds = NULL;
5368 dc424a06 2019-08-07 stsp struct got_diff_args *args = NULL;
5369 dc424a06 2019-08-07 stsp struct got_diff_change *change;
5370 dc424a06 2019-08-07 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
5371 a7c9878d 2019-08-08 stsp int n = 0;
5373 dc424a06 2019-08-07 stsp *path_outfile = NULL;
5375 dc424a06 2019-08-07 stsp err = got_object_id_str(&id_str, blob_id);
5377 dc424a06 2019-08-07 stsp return err;
5379 dc424a06 2019-08-07 stsp f2 = fopen(path2, "r");
5380 dc424a06 2019-08-07 stsp if (f2 == NULL) {
5381 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fopen", path2);
5382 dc424a06 2019-08-07 stsp goto done;
5385 dc424a06 2019-08-07 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5387 dc424a06 2019-08-07 stsp goto done;
5389 dc424a06 2019-08-07 stsp err = got_opentemp_named(&path1, &f1, "got-stage-blob");
5391 dc424a06 2019-08-07 stsp goto done;
5393 dc424a06 2019-08-07 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5395 dc424a06 2019-08-07 stsp goto done;
5397 dc424a06 2019-08-07 stsp if (stat(path1, &sb1) == -1) {
5398 dc424a06 2019-08-07 stsp err = got_error_from_errno2("stat", path1);
5399 dc424a06 2019-08-07 stsp goto done;
5401 dc424a06 2019-08-07 stsp if (stat(path2, &sb2) == -1) {
5402 dc424a06 2019-08-07 stsp err = got_error_from_errno2("stat", path2);
5403 dc424a06 2019-08-07 stsp goto done;
5406 dc424a06 2019-08-07 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
5407 dc424a06 2019-08-07 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
5409 dc424a06 2019-08-07 stsp goto done;
5411 dc424a06 2019-08-07 stsp err = got_opentemp_named(path_outfile, &outfile, "got-stage-content");
5413 dc424a06 2019-08-07 stsp goto done;
5415 dc424a06 2019-08-07 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5416 dc424a06 2019-08-07 stsp return got_ferror(f1, GOT_ERR_IO);
5417 dc424a06 2019-08-07 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5418 dc424a06 2019-08-07 stsp return got_ferror(f2, GOT_ERR_IO);
5419 dc424a06 2019-08-07 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5420 dc424a06 2019-08-07 stsp int choice;
5421 a7c9878d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
5422 a7c9878d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
5423 a7c9878d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
5424 2e1f37b0 2019-08-08 stsp outfile, NULL, patch_cb, patch_arg);
5426 dc424a06 2019-08-07 stsp goto done;
5427 dc424a06 2019-08-07 stsp if (choice == GOT_PATCH_CHOICE_YES)
5428 dc424a06 2019-08-07 stsp have_content = 1;
5429 b353a198 2019-08-07 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5433 dc424a06 2019-08-07 stsp free(id_str);
5435 dc424a06 2019-08-07 stsp got_object_blob_close(blob);
5436 dc424a06 2019-08-07 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5437 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fclose", path1);
5438 dc424a06 2019-08-07 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5439 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fclose", path2);
5440 dc424a06 2019-08-07 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5441 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fclose", *path_outfile);
5442 dc424a06 2019-08-07 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5443 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path1);
5444 dc424a06 2019-08-07 stsp if (err || !have_content) {
5445 dc424a06 2019-08-07 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5446 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", *path_outfile);
5447 dc424a06 2019-08-07 stsp free(*path_outfile);
5448 dc424a06 2019-08-07 stsp *path_outfile = NULL;
5450 dc424a06 2019-08-07 stsp free(args);
5452 dc424a06 2019-08-07 stsp got_diff_state_free(ds);
5455 dc424a06 2019-08-07 stsp if (changes)
5456 dc424a06 2019-08-07 stsp got_diff_free_changes(changes);
5457 dc424a06 2019-08-07 stsp free(path1);
5458 dc424a06 2019-08-07 stsp return err;
5461 2db2652d 2019-08-07 stsp struct stage_path_arg {
5462 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5463 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5464 2db2652d 2019-08-07 stsp struct got_repository *repo;
5465 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
5466 2db2652d 2019-08-07 stsp void *status_arg;
5467 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
5468 2db2652d 2019-08-07 stsp void *patch_arg;
5471 2db2652d 2019-08-07 stsp static const struct got_error *
5472 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
5473 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5474 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5475 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5477 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
5478 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
5479 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
5480 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
5481 0cb83759 2019-08-03 stsp uint32_t stage;
5483 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
5484 8b13ce36 2019-08-08 stsp return NULL;
5486 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5487 d3e7c587 2019-08-03 stsp if (ie == NULL)
5488 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5490 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5491 2db2652d 2019-08-07 stsp relpath)== -1)
5492 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
5494 0cb83759 2019-08-03 stsp switch (status) {
5495 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
5496 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
5497 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5498 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
5499 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5500 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5501 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5504 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
5507 dc424a06 2019-08-07 stsp err = create_staged_content(&path_content,
5508 2db2652d 2019-08-07 stsp blob_id, ondisk_path, ie->path, a->repo,
5509 2db2652d 2019-08-07 stsp a->patch_cb, a->patch_arg);
5510 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
5514 537ac44b 2019-08-03 stsp err = got_object_blob_create(&staged_blob_id,
5515 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
5518 537ac44b 2019-08-03 stsp memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
5519 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5520 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5521 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
5523 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
5524 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5525 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5527 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5528 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
5529 537ac44b 2019-08-03 stsp staged_blob_id, NULL);
5531 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
5532 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
5534 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5535 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5536 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
5537 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
5540 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
5542 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
5543 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5547 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
5548 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5549 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5551 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5552 537ac44b 2019-08-03 stsp get_staged_status(ie), relpath, NULL, NULL, NULL);
5554 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
5555 d3e7c587 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5557 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
5558 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5561 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5565 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
5566 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
5567 dc424a06 2019-08-07 stsp free(path_content);
5568 2db2652d 2019-08-07 stsp free(ondisk_path);
5569 0ebf8283 2019-07-24 stsp return err;
5572 0cb83759 2019-08-03 stsp const struct got_error *
5573 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
5574 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
5575 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
5576 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5577 1e71573e 2019-08-03 stsp struct got_repository *repo)
5579 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5580 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
5581 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5582 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
5583 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
5584 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
5585 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
5586 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
5588 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5590 0cb83759 2019-08-03 stsp return err;
5592 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
5593 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
5595 735ef5ac 2019-08-03 stsp goto done;
5596 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5598 735ef5ac 2019-08-03 stsp goto done;
5599 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5601 0cb83759 2019-08-03 stsp goto done;
5603 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
5604 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
5605 2db2652d 2019-08-07 stsp oka.worktree = worktree;
5606 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
5607 2db2652d 2019-08-07 stsp oka.repo = repo;
5608 2db2652d 2019-08-07 stsp oka.have_changes = 0;
5609 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5610 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5611 2db2652d 2019-08-07 stsp check_stage_ok, &oka, NULL, NULL);
5613 735ef5ac 2019-08-03 stsp goto done;
5615 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
5616 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5617 2db2652d 2019-08-07 stsp goto done;
5620 2db2652d 2019-08-07 stsp spa.worktree = worktree;
5621 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
5622 2db2652d 2019-08-07 stsp spa.repo = repo;
5623 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
5624 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
5625 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
5626 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
5627 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5628 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5629 2db2652d 2019-08-07 stsp stage_path, &spa, NULL, NULL);
5631 2db2652d 2019-08-07 stsp goto done;
5634 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5635 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
5636 0cb83759 2019-08-03 stsp err = sync_err;
5638 735ef5ac 2019-08-03 stsp if (head_ref)
5639 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
5640 735ef5ac 2019-08-03 stsp free(head_commit_id);
5641 0cb83759 2019-08-03 stsp free(fileindex_path);
5642 0cb83759 2019-08-03 stsp if (fileindex)
5643 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
5644 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5645 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
5646 0cb83759 2019-08-03 stsp err = unlockerr;
5647 0cb83759 2019-08-03 stsp return err;
5650 ad493afc 2019-08-03 stsp struct unstage_path_arg {
5651 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
5652 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
5653 ad493afc 2019-08-03 stsp struct got_repository *repo;
5654 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
5655 ad493afc 2019-08-03 stsp void *progress_arg;
5656 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
5657 2e1f37b0 2019-08-08 stsp void *patch_arg;
5660 2e1f37b0 2019-08-08 stsp static const struct got_error *
5661 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
5662 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
5663 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
5664 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
5665 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
5667 2e1f37b0 2019-08-08 stsp const struct got_error *err;
5668 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
5669 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5670 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5671 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
5672 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
5673 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
5674 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
5675 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
5676 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5677 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
5679 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
5680 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
5682 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
5684 2e1f37b0 2019-08-08 stsp return err;
5685 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5687 2e1f37b0 2019-08-08 stsp goto done;
5689 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5691 2e1f37b0 2019-08-08 stsp goto done;
5693 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5695 2e1f37b0 2019-08-08 stsp goto done;
5697 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5699 2e1f37b0 2019-08-08 stsp goto done;
5701 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5703 2e1f37b0 2019-08-08 stsp goto done;
5705 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5707 2e1f37b0 2019-08-08 stsp goto done;
5709 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
5710 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
5711 2e1f37b0 2019-08-08 stsp goto done;
5714 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
5715 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
5716 2e1f37b0 2019-08-08 stsp goto done;
5719 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
5720 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5722 2e1f37b0 2019-08-08 stsp goto done;
5724 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
5725 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
5727 2e1f37b0 2019-08-08 stsp goto done;
5728 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
5729 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
5731 2e1f37b0 2019-08-08 stsp goto done;
5733 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
5734 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
5735 2e1f37b0 2019-08-08 stsp goto done;
5737 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
5738 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
5739 2e1f37b0 2019-08-08 stsp goto done;
5741 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5742 2e1f37b0 2019-08-08 stsp int choice;
5743 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
5744 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
5745 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
5746 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
5748 2e1f37b0 2019-08-08 stsp goto done;
5749 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5750 2e1f37b0 2019-08-08 stsp have_content = 1;
5752 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
5753 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
5757 2e1f37b0 2019-08-08 stsp free(label1);
5759 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
5760 2e1f37b0 2019-08-08 stsp if (staged_blob)
5761 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
5762 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5763 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5764 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5765 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5766 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5767 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
5768 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5769 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
5770 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5771 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5772 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
5773 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
5774 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
5775 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
5776 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
5777 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
5778 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
5779 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
5780 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
5782 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
5783 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
5784 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
5785 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
5786 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
5787 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
5788 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
5790 2e1f37b0 2019-08-08 stsp free(args);
5792 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
5795 2e1f37b0 2019-08-08 stsp if (changes)
5796 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
5797 2e1f37b0 2019-08-08 stsp free(path1);
5798 2e1f37b0 2019-08-08 stsp free(path2);
5799 2e1f37b0 2019-08-08 stsp return err;
5802 ad493afc 2019-08-03 stsp static const struct got_error *
5803 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
5804 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
5805 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5806 ad493afc 2019-08-03 stsp struct got_object_id *commit_id)
5808 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
5809 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
5810 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
5811 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5812 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
5813 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
5814 ad493afc 2019-08-03 stsp int local_changes_subsumed;
5815 9bc94a15 2019-08-03 stsp struct stat sb;
5817 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
5818 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
5819 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
5820 2e1f37b0 2019-08-08 stsp return NULL;
5822 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5823 ad493afc 2019-08-03 stsp if (ie == NULL)
5824 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5826 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5828 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
5830 ad493afc 2019-08-03 stsp switch (staged_status) {
5831 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
5832 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
5833 ad493afc 2019-08-03 stsp blob_id, 8192);
5836 ad493afc 2019-08-03 stsp /* fall through */
5837 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
5838 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
5839 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
5840 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5841 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5842 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
5845 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5848 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
5849 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
5850 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
5851 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
5852 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
5853 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
5855 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
5856 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
5857 2e1f37b0 2019-08-08 stsp &staged_blob_id,
5858 2e1f37b0 2019-08-08 stsp path_new_staged_content,
5862 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
5863 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
5864 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
5866 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
5867 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
5868 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
5869 2e1f37b0 2019-08-08 stsp path_unstaged_content, "unstaged",
5870 2e1f37b0 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
5871 2e1f37b0 2019-08-08 stsp if (err == NULL &&
5872 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
5873 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
5874 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
5875 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
5878 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
5879 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
5882 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
5883 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
5884 ad493afc 2019-08-03 stsp got_fileindex_perms_to_st(ie), blob_staged,
5885 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
5886 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
5887 ad493afc 2019-08-03 stsp if (err == NULL)
5888 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5889 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5891 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
5892 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
5893 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5894 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5895 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
5898 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
5900 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
5901 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5905 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
5906 9bc94a15 2019-08-03 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
5909 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
5913 ad493afc 2019-08-03 stsp free(ondisk_path);
5914 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
5915 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
5916 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
5917 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
5918 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
5919 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
5920 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
5921 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
5922 ad493afc 2019-08-03 stsp if (blob_base)
5923 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
5924 ad493afc 2019-08-03 stsp if (blob_staged)
5925 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
5926 ad493afc 2019-08-03 stsp return err;
5929 ad493afc 2019-08-03 stsp const struct got_error *
5930 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
5931 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
5932 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5933 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5934 ad493afc 2019-08-03 stsp struct got_repository *repo)
5936 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5937 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5938 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5939 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
5940 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
5942 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5944 ad493afc 2019-08-03 stsp return err;
5946 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5948 ad493afc 2019-08-03 stsp goto done;
5950 ad493afc 2019-08-03 stsp upa.worktree = worktree;
5951 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
5952 ad493afc 2019-08-03 stsp upa.repo = repo;
5953 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
5954 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
5955 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
5956 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
5957 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5958 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5959 ad493afc 2019-08-03 stsp unstage_path, &upa, NULL, NULL);
5961 ad493afc 2019-08-03 stsp goto done;
5964 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5965 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
5966 ad493afc 2019-08-03 stsp err = sync_err;
5968 ad493afc 2019-08-03 stsp free(fileindex_path);
5969 ad493afc 2019-08-03 stsp if (fileindex)
5970 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
5971 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5972 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
5973 ad493afc 2019-08-03 stsp err = unlockerr;
5974 ad493afc 2019-08-03 stsp return err;