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 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
723 234035bc 2019-06-01 stsp * acts as the second derived version.
725 6353ad76 2019-02-08 stsp static const struct got_error *
726 234035bc 2019-06-01 stsp merge_blob(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 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
730 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 234035bc 2019-06-01 stsp void *progress_arg)
733 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
734 6353ad76 2019-02-08 stsp int merged_fd = -1;
735 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
736 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
738 6353ad76 2019-02-08 stsp char *id_str = NULL;
739 818c7501 2019-07-11 stsp char *label_deriv = NULL;
740 234035bc 2019-06-01 stsp int overlapcnt = 0;
741 af54ae4a 2019-02-19 stsp char *parent;
743 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
745 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
746 af54ae4a 2019-02-19 stsp if (parent == NULL)
747 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
749 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
752 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 af54ae4a 2019-02-19 stsp free(base_path);
757 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
759 af54ae4a 2019-02-19 stsp base_path = NULL;
763 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
766 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
767 6c4c42e0 2019-06-24 stsp blob_deriv);
771 af54ae4a 2019-02-19 stsp free(base_path);
772 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
774 af54ae4a 2019-02-19 stsp base_path = NULL;
778 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
781 46b6ee73 2019-06-04 stsp if (blob_orig) {
782 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
783 46b6ee73 2019-06-04 stsp blob_orig);
788 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
789 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
790 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
794 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
797 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
802 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
807 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
808 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
812 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
813 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
817 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
818 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
819 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 68c76935 2019-02-19 stsp merged_path);
825 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
826 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
830 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
831 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
832 230a42bd 2019-05-11 jcs ondisk_path);
833 2a57020b 2019-02-20 stsp unlink(merged_path);
838 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
840 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
842 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
844 6353ad76 2019-02-08 stsp free(merged_path);
845 af54ae4a 2019-02-19 stsp free(base_path);
846 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
847 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
848 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
850 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
851 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
852 46b6ee73 2019-06-04 stsp free(blob_orig_path);
854 6353ad76 2019-02-08 stsp free(id_str);
855 818c7501 2019-07-11 stsp free(label_deriv);
856 4a1ddfc2 2019-01-12 stsp return err;
859 4a1ddfc2 2019-01-12 stsp static const struct got_error *
860 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
861 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 13d9040b 2019-03-27 stsp int update_timestamps)
865 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
867 13d9040b 2019-03-27 stsp if (ie == NULL)
868 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
870 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
871 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
872 13d9040b 2019-03-27 stsp update_timestamps);
874 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
875 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
878 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
880 13d9040b 2019-03-27 stsp return err;
883 13d9040b 2019-03-27 stsp static const struct got_error *
884 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
886 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
887 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
888 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
890 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
891 507dc3bb 2018-12-29 stsp int fd = -1;
892 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
893 507dc3bb 2018-12-29 stsp int update = 0;
894 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
896 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
898 9d31a1d8 2018-03-11 stsp if (fd == -1) {
899 21908da4 2019-01-13 stsp if (errno == ENOENT) {
900 21908da4 2019-01-13 stsp char *parent = dirname(path);
901 21908da4 2019-01-13 stsp if (parent == NULL)
902 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
903 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
905 21908da4 2019-01-13 stsp return err;
906 21908da4 2019-01-13 stsp fd = open(ondisk_path,
907 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
909 21908da4 2019-01-13 stsp if (fd == -1)
910 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
911 230a42bd 2019-05-11 jcs ondisk_path);
912 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
913 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
914 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
915 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
918 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
919 507dc3bb 2018-12-29 stsp ondisk_path);
922 507dc3bb 2018-12-29 stsp update = 1;
925 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
928 a378724f 2019-02-10 stsp if (restoring_missing_file)
929 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
931 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
933 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
934 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
938 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
940 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
944 9d31a1d8 2018-03-11 stsp if (len > 0) {
945 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
946 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
948 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
950 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
951 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
954 61d6eaa3 2018-12-24 stsp hdrlen = 0;
956 9d31a1d8 2018-03-11 stsp } while (len != 0);
958 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
959 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
963 507dc3bb 2018-12-29 stsp if (update) {
964 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
965 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
966 230a42bd 2019-05-11 jcs ondisk_path);
967 2a57020b 2019-02-20 stsp unlink(tmppath);
972 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
973 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
978 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
985 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
986 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
987 507dc3bb 2018-12-29 stsp free(tmppath);
988 6353ad76 2019-02-08 stsp return err;
991 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 6353ad76 2019-02-08 stsp static const struct got_error *
993 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
995 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
996 7154f6ce 2019-03-27 stsp const char *markers[3] = {
997 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
999 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1001 7154f6ce 2019-03-27 stsp int i = 0;
1002 7154f6ce 2019-03-27 stsp char *line;
1003 7154f6ce 2019-03-27 stsp size_t len;
1004 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1006 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1007 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1008 7154f6ce 2019-03-27 stsp if (line == NULL) {
1009 7154f6ce 2019-03-27 stsp if (feof(f))
1011 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1015 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1018 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1024 7154f6ce 2019-03-27 stsp return err;
1027 e2b1e152 2019-07-13 stsp static int
1028 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1031 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1032 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1033 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1034 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1037 c363b2c1 2019-08-03 stsp static unsigned char
1038 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1040 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1041 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1042 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1043 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1044 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1045 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1046 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1048 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1052 7154f6ce 2019-03-27 stsp static const struct got_error *
1053 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1054 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1055 b8f41171 2019-02-10 stsp struct got_repository *repo)
1057 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1058 6353ad76 2019-02-08 stsp struct got_object_id id;
1059 6353ad76 2019-02-08 stsp size_t hdrlen;
1060 6353ad76 2019-02-08 stsp FILE *f = NULL;
1061 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1062 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1063 6353ad76 2019-02-08 stsp size_t flen, blen;
1064 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1066 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1068 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1069 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1070 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1071 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1073 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1074 a378724f 2019-02-10 stsp return NULL;
1076 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1079 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1080 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1081 339c298e 2019-07-27 stsp return NULL;
1084 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1085 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1086 339c298e 2019-07-27 stsp return NULL;
1087 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1088 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1089 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1090 339c298e 2019-07-27 stsp return NULL;
1093 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1094 339c298e 2019-07-27 stsp return NULL;
1096 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1097 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1098 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1100 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1102 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1104 339c298e 2019-07-27 stsp return err;
1106 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1107 6353ad76 2019-02-08 stsp if (f == NULL) {
1108 339c298e 2019-07-27 stsp err = got_error_from_errno2("fopen", abspath);
1109 6353ad76 2019-02-08 stsp goto done;
1111 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1113 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1114 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1116 7154f6ce 2019-03-27 stsp goto done;
1117 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1118 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1119 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1120 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1121 7154f6ce 2019-03-27 stsp goto done;
1123 6353ad76 2019-02-08 stsp if (blen == 0) {
1124 6353ad76 2019-02-08 stsp if (flen != 0)
1125 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1127 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1128 6353ad76 2019-02-08 stsp if (blen != 0)
1129 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1131 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1132 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1133 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1134 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1138 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1141 6353ad76 2019-02-08 stsp hdrlen = 0;
1144 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1145 7154f6ce 2019-03-27 stsp rewind(f);
1146 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1150 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1152 6353ad76 2019-02-08 stsp fclose(f);
1153 9d31a1d8 2018-03-11 stsp return err;
1157 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1158 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1160 e2b1e152 2019-07-13 stsp static const struct got_error *
1161 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1162 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1164 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1165 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1166 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1168 e2b1e152 2019-07-13 stsp return NULL;
1171 9d31a1d8 2018-03-11 stsp static const struct got_error *
1172 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1173 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1174 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1175 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1176 0584f854 2019-04-06 stsp void *progress_arg)
1178 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1179 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1180 6353ad76 2019-02-08 stsp char *ondisk_path;
1181 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1182 b8f41171 2019-02-10 stsp struct stat sb;
1184 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1185 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1188 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1189 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1190 a76c42e6 2019-08-03 stsp goto done;
1192 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1194 abb4604f 2019-07-27 stsp goto done;
1195 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1196 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1198 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1200 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1201 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1202 b8f41171 2019-02-10 stsp goto done;
1205 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1206 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1207 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1208 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1209 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1211 e2b1e152 2019-07-13 stsp goto done;
1212 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1214 6353ad76 2019-02-08 stsp goto done;
1216 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1217 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1218 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1219 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1220 b8f41171 2019-02-10 stsp goto done;
1224 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1226 6353ad76 2019-02-08 stsp goto done;
1228 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1229 234035bc 2019-06-01 stsp int update_timestamps;
1230 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1231 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1232 234035bc 2019-06-01 stsp struct got_object_id id2;
1233 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1234 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1236 234035bc 2019-06-01 stsp goto done;
1238 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1239 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1240 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1241 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1242 234035bc 2019-06-01 stsp if (blob2)
1243 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1245 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1246 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1247 234035bc 2019-06-01 stsp * unmodified files again.
1249 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1250 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1251 234035bc 2019-06-01 stsp update_timestamps);
1252 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1253 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1255 1ee397ad 2019-07-12 stsp goto done;
1256 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1257 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1259 13d9040b 2019-03-27 stsp goto done;
1261 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1262 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1263 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1265 13d9040b 2019-03-27 stsp goto done;
1266 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1267 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1269 13d9040b 2019-03-27 stsp goto done;
1271 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1273 6353ad76 2019-02-08 stsp free(ondisk_path);
1274 8da9e5f4 2019-01-12 stsp return err;
1277 90285c3b 2019-01-08 stsp static const struct got_error *
1278 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1280 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1281 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1283 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1284 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1286 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1287 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1288 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1290 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1291 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1292 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1293 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1294 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1298 90285c3b 2019-01-08 stsp parent = dirname(parent);
1301 90285c3b 2019-01-08 stsp free(ondisk_path);
1302 90285c3b 2019-01-08 stsp return err;
1305 708d8e67 2019-03-27 stsp static const struct got_error *
1306 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1307 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1308 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1310 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1311 708d8e67 2019-03-27 stsp unsigned char status;
1312 708d8e67 2019-03-27 stsp struct stat sb;
1313 708d8e67 2019-03-27 stsp char *ondisk_path;
1315 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1316 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1318 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1320 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1322 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1324 708d8e67 2019-03-27 stsp return err;
1326 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1327 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1328 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1330 1ee397ad 2019-07-12 stsp return err;
1332 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1333 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1335 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1338 708d8e67 2019-03-27 stsp return err;
1340 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1342 1ee397ad 2019-07-12 stsp return err;
1343 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1344 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1346 fc6346c4 2019-03-27 stsp return err;
1348 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1351 fc6346c4 2019-03-27 stsp return err;
1354 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1355 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1356 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1357 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1358 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1359 8da9e5f4 2019-01-12 stsp void *progress_arg;
1360 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1361 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1364 512f0d0e 2019-01-02 stsp static const struct got_error *
1365 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1366 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1368 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1370 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1373 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1374 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1377 8da9e5f4 2019-01-12 stsp static const struct got_error *
1378 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1380 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1382 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1383 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1385 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1386 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1389 9d31a1d8 2018-03-11 stsp static const struct got_error *
1390 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1392 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1393 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1394 8da9e5f4 2019-01-12 stsp char *path;
1396 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1397 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1399 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1400 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1402 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1404 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1405 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1407 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1408 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1410 8da9e5f4 2019-01-12 stsp free(path);
1411 0cd1c46a 2019-03-11 stsp return err;
1414 818c7501 2019-07-11 stsp static const struct got_error *
1415 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1417 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1418 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1419 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1421 517bab73 2019-03-11 stsp *refname = NULL;
1423 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1424 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1425 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1427 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1429 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1430 0647c563 2019-03-11 stsp *refname = NULL;
1432 517bab73 2019-03-11 stsp free(uuidstr);
1433 517bab73 2019-03-11 stsp return err;
1436 818c7501 2019-07-11 stsp const struct got_error *
1437 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1439 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1442 818c7501 2019-07-11 stsp static const struct got_error *
1443 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1445 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1446 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1449 818c7501 2019-07-11 stsp static const struct got_error *
1450 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1452 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1455 818c7501 2019-07-11 stsp static const struct got_error *
1456 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1458 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1459 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1462 818c7501 2019-07-11 stsp static const struct got_error *
1463 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1465 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1466 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1469 0ebf8283 2019-07-24 stsp static const struct got_error *
1470 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1472 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1473 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1476 0ebf8283 2019-07-24 stsp static const struct got_error *
1477 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1479 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1480 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1483 0ebf8283 2019-07-24 stsp static const struct got_error *
1484 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1486 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1487 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1490 0ebf8283 2019-07-24 stsp static const struct got_error *
1491 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1493 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1494 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1497 0ebf8283 2019-07-24 stsp const struct got_error *
1498 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1499 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1501 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1502 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1503 0ebf8283 2019-07-24 stsp *path = NULL;
1504 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1506 0ebf8283 2019-07-24 stsp return NULL;
1510 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1511 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1513 517bab73 2019-03-11 stsp static const struct got_error *
1514 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1516 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1517 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1518 517bab73 2019-03-11 stsp char *refname;
1520 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1522 517bab73 2019-03-11 stsp return err;
1524 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1526 0cd1c46a 2019-03-11 stsp goto done;
1528 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1530 0cd1c46a 2019-03-11 stsp free(refname);
1532 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1533 3e3a69f1 2019-07-25 stsp return err;
1536 3e3a69f1 2019-07-25 stsp static const struct got_error *
1537 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1539 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1541 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1542 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1543 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1544 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1546 9d31a1d8 2018-03-11 stsp return err;
1550 ebf99748 2019-05-09 stsp static const struct got_error *
1551 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1552 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1554 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1555 ebf99748 2019-05-09 stsp FILE *index = NULL;
1557 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1558 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1559 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1560 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1562 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1564 ebf99748 2019-05-09 stsp goto done;
1566 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1567 ebf99748 2019-05-09 stsp if (index == NULL) {
1568 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1569 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1571 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1572 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1573 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1576 ebf99748 2019-05-09 stsp if (err) {
1577 ebf99748 2019-05-09 stsp free(*fileindex_path);
1578 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1579 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1580 ebf99748 2019-05-09 stsp *fileindex = NULL;
1582 ebf99748 2019-05-09 stsp return err;
1585 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1586 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1587 c932eeeb 2019-05-22 stsp const char *path;
1588 c932eeeb 2019-05-22 stsp size_t path_len;
1589 c932eeeb 2019-05-22 stsp const char *entry_name;
1590 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1591 a484d721 2019-06-10 stsp void *progress_arg;
1594 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1595 c932eeeb 2019-05-22 stsp static const struct got_error *
1596 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1598 1ee397ad 2019-07-12 stsp const struct got_error *err;
1599 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1601 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1602 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1603 c932eeeb 2019-05-22 stsp return NULL;
1604 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1605 102ff934 2019-06-11 stsp return NULL;
1607 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1608 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1609 c932eeeb 2019-05-22 stsp return NULL;
1611 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1612 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1613 edd02c5e 2019-07-11 stsp ie->path);
1615 1ee397ad 2019-07-12 stsp return err;
1617 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1618 c932eeeb 2019-05-22 stsp return NULL;
1621 9c6338c4 2019-06-02 stsp static const struct got_error *
1622 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1624 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1625 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1626 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1628 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1629 9c6338c4 2019-06-02 stsp fileindex_path);
1631 9c6338c4 2019-06-02 stsp goto done;
1633 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1635 9c6338c4 2019-06-02 stsp goto done;
1637 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1638 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1639 9c6338c4 2019-06-02 stsp fileindex_path);
1640 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1643 9c6338c4 2019-06-02 stsp if (new_index)
1644 9c6338c4 2019-06-02 stsp fclose(new_index);
1645 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1646 9c6338c4 2019-06-02 stsp return err;
1649 6ced4176 2019-07-12 stsp static const struct got_error *
1650 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1651 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1652 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1654 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1655 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1656 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1657 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1659 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1660 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1661 6ced4176 2019-07-12 stsp *tree_id = NULL;
1663 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1664 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1665 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1666 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1667 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1668 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1669 6ced4176 2019-07-12 stsp goto done;
1671 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1672 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1674 6ced4176 2019-07-12 stsp goto done;
1675 6ced4176 2019-07-12 stsp return NULL;
1678 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1680 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1681 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1682 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1683 6ced4176 2019-07-12 stsp goto done;
1686 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1687 6ced4176 2019-07-12 stsp in_repo_path);
1689 6ced4176 2019-07-12 stsp goto done;
1691 6ced4176 2019-07-12 stsp free(in_repo_path);
1692 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1694 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1696 6ced4176 2019-07-12 stsp goto done;
1698 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1699 6ced4176 2019-07-12 stsp /* Check out a single file. */
1700 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1701 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1702 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1703 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1704 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1705 6ced4176 2019-07-12 stsp goto done;
1707 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1708 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1709 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1710 6ced4176 2019-07-12 stsp goto done;
1713 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1714 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1716 6ced4176 2019-07-12 stsp return err;
1717 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1718 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1719 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1720 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1721 6ced4176 2019-07-12 stsp goto done;
1724 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1725 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1727 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1728 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1729 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1730 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1731 6ced4176 2019-07-12 stsp goto done;
1733 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1734 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1735 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1736 6ced4176 2019-07-12 stsp goto done;
1741 6ced4176 2019-07-12 stsp free(in_repo_path);
1742 6ced4176 2019-07-12 stsp if (err) {
1743 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1744 6ced4176 2019-07-12 stsp free(*tree_relpath);
1745 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1746 6ced4176 2019-07-12 stsp free(*tree_id);
1747 6ced4176 2019-07-12 stsp *tree_id = NULL;
1749 07ed472d 2019-07-12 stsp return err;
1752 07ed472d 2019-07-12 stsp static const struct got_error *
1753 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1754 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1755 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1756 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1758 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1759 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1760 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1761 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1762 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1764 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1766 07ed472d 2019-07-12 stsp goto done;
1768 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1769 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1771 07ed472d 2019-07-12 stsp goto done;
1773 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1775 07ed472d 2019-07-12 stsp goto done;
1777 07ed472d 2019-07-12 stsp if (entry_name &&
1778 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1779 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1780 07ed472d 2019-07-12 stsp goto done;
1783 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1784 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1785 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1786 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1787 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1788 07ed472d 2019-07-12 stsp arg.repo = repo;
1789 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1790 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1791 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1792 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1793 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1794 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1797 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1798 07ed472d 2019-07-12 stsp if (commit)
1799 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1800 6ced4176 2019-07-12 stsp return err;
1803 9d31a1d8 2018-03-11 stsp const struct got_error *
1804 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1805 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1806 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1807 f2ea84fa 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1809 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1810 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1811 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1812 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1813 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1814 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1815 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1816 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1817 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1818 f2ea84fa 2019-07-27 stsp int entry_type;
1819 f2ea84fa 2019-07-27 stsp char *relpath;
1820 f2ea84fa 2019-07-27 stsp char *entry_name;
1821 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1822 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1824 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1826 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1828 9d31a1d8 2018-03-11 stsp return err;
1830 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1831 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1832 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1833 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1834 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1835 f2ea84fa 2019-07-27 stsp goto done;
1838 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1839 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1840 f2ea84fa 2019-07-27 stsp if (err) {
1841 f2ea84fa 2019-07-27 stsp free(tpd);
1842 6ced4176 2019-07-12 stsp goto done;
1845 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1846 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1847 f2ea84fa 2019-07-27 stsp if (err) {
1848 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1849 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1850 f2ea84fa 2019-07-27 stsp free(tpd);
1851 f2ea84fa 2019-07-27 stsp goto done;
1854 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1856 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1860 51514078 2018-12-25 stsp * Read the file index.
1861 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1862 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1864 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1866 c4cdcb68 2019-04-03 stsp goto done;
1868 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1869 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1870 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1872 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1873 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1874 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1878 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1879 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1880 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1881 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
1882 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1883 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1884 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1885 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1889 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1891 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1892 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1893 af12c6b9 2019-06-04 stsp err = sync_err;
1895 9c6338c4 2019-06-02 stsp free(fileindex_path);
1897 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1898 9d31a1d8 2018-03-11 stsp if (commit)
1899 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1900 5ade8233 2019-07-12 stsp if (fileindex)
1901 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1902 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1903 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1904 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1905 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1906 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1907 f2ea84fa 2019-07-27 stsp free(tpd);
1909 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1910 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1911 9d31a1d8 2018-03-11 stsp err = unlockerr;
1912 f8d1f275 2019-02-04 stsp return err;
1915 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1916 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1917 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1918 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1919 234035bc 2019-06-01 stsp void *progress_arg;
1920 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1921 234035bc 2019-06-01 stsp void *cancel_arg;
1922 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1925 234035bc 2019-06-01 stsp static const struct got_error *
1926 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1927 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1928 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1929 234035bc 2019-06-01 stsp struct got_repository *repo)
1931 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1932 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1933 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1934 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1935 234035bc 2019-06-01 stsp struct stat sb;
1936 234035bc 2019-06-01 stsp unsigned char status;
1937 234035bc 2019-06-01 stsp int local_changes_subsumed;
1939 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1940 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1941 d6c87207 2019-08-02 stsp strlen(path2));
1942 1ee397ad 2019-07-12 stsp if (ie == NULL)
1943 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1944 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1946 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1947 234035bc 2019-06-01 stsp path2) == -1)
1948 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1950 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1952 234035bc 2019-06-01 stsp goto done;
1954 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1955 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1956 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1957 234035bc 2019-06-01 stsp goto done;
1959 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1960 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1961 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1962 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1963 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1964 234035bc 2019-06-01 stsp goto done;
1967 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1968 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1969 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1970 234035bc 2019-06-01 stsp } else if (blob1) {
1971 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
1972 d6c87207 2019-08-02 stsp strlen(path1));
1973 1ee397ad 2019-07-12 stsp if (ie == NULL)
1974 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1975 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1977 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1978 2b92fad7 2019-06-02 stsp path1) == -1)
1979 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1981 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1983 2b92fad7 2019-06-02 stsp goto done;
1985 2b92fad7 2019-06-02 stsp switch (status) {
1986 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1987 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1988 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1990 1ee397ad 2019-07-12 stsp goto done;
1991 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1993 2b92fad7 2019-06-02 stsp goto done;
1995 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1997 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1998 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1999 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2000 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2002 1ee397ad 2019-07-12 stsp goto done;
2004 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2006 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2007 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2008 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2009 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2010 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2012 1ee397ad 2019-07-12 stsp goto done;
2014 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2015 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2017 1ee397ad 2019-07-12 stsp goto done;
2022 234035bc 2019-06-01 stsp } else if (blob2) {
2023 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2024 234035bc 2019-06-01 stsp path2) == -1)
2025 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2026 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2027 d6c87207 2019-08-02 stsp strlen(path2));
2029 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2032 234035bc 2019-06-01 stsp goto done;
2033 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2034 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2035 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2036 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2037 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2038 1ee397ad 2019-07-12 stsp status, path2);
2039 234035bc 2019-06-01 stsp goto done;
2041 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2042 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2043 818c7501 2019-07-11 stsp a->commit_id2, repo,
2044 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2045 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2046 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2047 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2048 234035bc 2019-06-01 stsp blob2, 0);
2050 234035bc 2019-06-01 stsp goto done;
2053 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2054 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2055 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2056 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2057 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2058 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2060 234035bc 2019-06-01 stsp goto done;
2061 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2062 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2064 234035bc 2019-06-01 stsp goto done;
2065 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2066 2b92fad7 2019-06-02 stsp if (err) {
2067 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2068 2b92fad7 2019-06-02 stsp goto done;
2073 234035bc 2019-06-01 stsp free(ondisk_path);
2074 234035bc 2019-06-01 stsp return err;
2077 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2078 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2079 234035bc 2019-06-01 stsp struct got_repository *repo;
2082 234035bc 2019-06-01 stsp static const struct got_error *
2083 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2085 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2086 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2087 234035bc 2019-06-01 stsp unsigned char status;
2088 234035bc 2019-06-01 stsp struct stat sb;
2089 234035bc 2019-06-01 stsp char *ondisk_path;
2091 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2092 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2093 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2094 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2096 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2098 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2100 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2101 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2103 234035bc 2019-06-01 stsp return err;
2104 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2105 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2107 234035bc 2019-06-01 stsp return NULL;
2110 818c7501 2019-07-11 stsp static const struct got_error *
2111 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2112 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2113 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2114 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2115 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2117 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2118 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2119 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2120 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2122 03415a1a 2019-06-02 stsp if (commit_id1) {
2123 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2124 03415a1a 2019-06-02 stsp worktree->path_prefix);
2126 03415a1a 2019-06-02 stsp goto done;
2128 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2130 03415a1a 2019-06-02 stsp goto done;
2133 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2134 234035bc 2019-06-01 stsp worktree->path_prefix);
2136 234035bc 2019-06-01 stsp goto done;
2138 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2140 234035bc 2019-06-01 stsp goto done;
2142 234035bc 2019-06-01 stsp arg.worktree = worktree;
2143 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2144 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2145 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2146 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2147 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2148 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2149 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2150 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2151 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2152 af12c6b9 2019-06-04 stsp err = sync_err;
2154 234035bc 2019-06-01 stsp if (tree1)
2155 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2156 234035bc 2019-06-01 stsp if (tree2)
2157 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2158 818c7501 2019-07-11 stsp return err;
2161 818c7501 2019-07-11 stsp const struct got_error *
2162 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2163 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2164 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2165 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2167 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2168 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2169 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2170 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2172 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2174 818c7501 2019-07-11 stsp return err;
2176 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2178 818c7501 2019-07-11 stsp goto done;
2180 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2181 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2182 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2183 818c7501 2019-07-11 stsp &mok_arg);
2185 818c7501 2019-07-11 stsp goto done;
2187 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2188 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2190 818c7501 2019-07-11 stsp if (fileindex)
2191 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2192 818c7501 2019-07-11 stsp free(fileindex_path);
2193 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2194 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2195 234035bc 2019-06-01 stsp err = unlockerr;
2196 234035bc 2019-06-01 stsp return err;
2199 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2200 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2201 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2202 927df6b7 2019-02-10 stsp const char *status_path;
2203 927df6b7 2019-02-10 stsp size_t status_path_len;
2204 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2205 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2206 f8d1f275 2019-02-04 stsp void *status_arg;
2207 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2208 f8d1f275 2019-02-04 stsp void *cancel_arg;
2211 f8d1f275 2019-02-04 stsp static const struct got_error *
2212 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2213 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2214 927df6b7 2019-02-10 stsp struct got_repository *repo)
2216 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2217 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2218 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2219 927df6b7 2019-02-10 stsp struct stat sb;
2220 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2221 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2222 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2224 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2226 98eaaa12 2019-08-03 stsp return err;
2228 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2229 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_NO_CHANGE)
2230 98eaaa12 2019-08-03 stsp return NULL;
2232 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2233 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2234 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2236 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2237 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2238 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2240 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2241 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2242 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2243 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2244 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2247 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2248 98eaaa12 2019-08-03 stsp ie->path, blob_idp, staged_blob_idp, commit_idp);
2251 927df6b7 2019-02-10 stsp static const struct got_error *
2252 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2253 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2255 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2256 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2257 f8d1f275 2019-02-04 stsp char *abspath;
2259 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2262 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2263 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2264 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2265 927df6b7 2019-02-10 stsp return NULL;
2267 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2268 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2269 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2270 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2272 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2273 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2274 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2277 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2279 f8d1f275 2019-02-04 stsp free(abspath);
2280 9d31a1d8 2018-03-11 stsp return err;
2283 f8d1f275 2019-02-04 stsp static const struct got_error *
2284 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2286 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2287 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2288 2ec1f75b 2019-03-26 stsp unsigned char status;
2290 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2293 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2294 927df6b7 2019-02-10 stsp return NULL;
2296 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2297 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2298 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2299 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2301 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2302 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2303 537ac44b 2019-08-03 stsp ie->path, &blob_id, NULL, &commit_id);
2306 f8d1f275 2019-02-04 stsp static const struct got_error *
2307 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2309 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2310 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2311 f8d1f275 2019-02-04 stsp char *path = NULL;
2313 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2314 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2316 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2317 2c201a36 2019-02-10 stsp return NULL;
2319 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2320 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2321 927df6b7 2019-02-10 stsp return NULL;
2323 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2324 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2325 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2327 f8d1f275 2019-02-04 stsp path = de->d_name;
2330 c577a9ce 2019-07-27 stsp if (got_path_is_child(path, a->status_path, a->status_path_len))
2331 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2332 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2333 f8d1f275 2019-02-04 stsp if (parent_path[0])
2334 f8d1f275 2019-02-04 stsp free(path);
2335 b72f483a 2019-02-05 stsp return err;
2338 347d1d3e 2019-07-12 stsp static const struct got_error *
2339 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2340 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2341 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2343 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2344 abb4604f 2019-07-27 stsp struct stat sb;
2346 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2348 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2349 abb4604f 2019-07-27 stsp status_arg, repo);
2351 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2352 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2353 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2354 abb4604f 2019-07-27 stsp return NULL;
2357 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2358 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2359 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2361 abb4604f 2019-07-27 stsp return NULL;
2364 abb4604f 2019-07-27 stsp static const struct got_error *
2365 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2366 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2367 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2368 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2370 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2371 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2372 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2373 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2374 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2376 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2377 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2378 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2380 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2381 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2382 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2383 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2385 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2386 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2388 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2389 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2390 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2391 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2392 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2393 abb4604f 2019-07-27 stsp arg.status_path = path;
2394 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2395 abb4604f 2019-07-27 stsp arg.repo = repo;
2396 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2397 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2398 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2399 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2400 abb4604f 2019-07-27 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2401 abb4604f 2019-07-27 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2404 f8d1f275 2019-02-04 stsp if (workdir)
2405 f8d1f275 2019-02-04 stsp closedir(workdir);
2406 927df6b7 2019-02-10 stsp free(ondisk_path);
2407 347d1d3e 2019-07-12 stsp return err;
2410 347d1d3e 2019-07-12 stsp const struct got_error *
2411 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2412 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2413 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2414 72ea6654 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2416 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2417 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2418 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2419 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2421 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2423 347d1d3e 2019-07-12 stsp return err;
2425 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2426 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2427 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2431 f8d1f275 2019-02-04 stsp free(fileindex_path);
2432 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2433 f8d1f275 2019-02-04 stsp return err;
2436 6c7ab921 2019-03-18 stsp const struct got_error *
2437 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2438 6c7ab921 2019-03-18 stsp const char *arg)
2440 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2441 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2442 6c7ab921 2019-03-18 stsp size_t len;
2444 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2446 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2447 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2448 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2449 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2450 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2451 d0710d08 2019-07-22 stsp if (cwd == NULL)
2452 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2453 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2454 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2455 d0710d08 2019-07-22 stsp goto done;
2459 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2460 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2461 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2462 6c7ab921 2019-03-18 stsp goto done;
2465 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2466 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2467 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2469 f6d88e1a 2019-05-29 stsp goto done;
2471 f6d88e1a 2019-05-29 stsp path = strdup("");
2472 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2473 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2474 f6d88e1a 2019-05-29 stsp goto done;
2478 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2479 6c7ab921 2019-03-18 stsp len = strlen(path);
2480 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2481 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2485 6c7ab921 2019-03-18 stsp free(resolved);
2486 d0710d08 2019-07-22 stsp free(cwd);
2487 6c7ab921 2019-03-18 stsp if (err == NULL)
2488 6c7ab921 2019-03-18 stsp *wt_path = path;
2490 6c7ab921 2019-03-18 stsp free(path);
2491 d00136be 2019-03-26 stsp return err;
2494 07f5b47a 2019-06-02 stsp static const struct got_error *
2495 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2496 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2497 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2499 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2500 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2501 6d022e97 2019-08-04 stsp unsigned char status;
2502 6d022e97 2019-08-04 stsp struct stat sb;
2504 6d022e97 2019-08-04 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2506 6d022e97 2019-08-04 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2508 6d022e97 2019-08-04 stsp return err;
2509 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
2510 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
2511 6d022e97 2019-08-04 stsp return NULL;
2512 6d022e97 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2515 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2517 07f5b47a 2019-06-02 stsp return err;
2519 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2520 07f5b47a 2019-06-02 stsp if (err) {
2521 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2522 07f5b47a 2019-06-02 stsp return err;
2525 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2528 d00136be 2019-03-26 stsp const struct got_error *
2529 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2530 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
2531 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2532 031a5338 2019-03-26 stsp struct got_repository *repo)
2534 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2535 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2536 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2537 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2539 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2541 d00136be 2019-03-26 stsp return err;
2543 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2545 d00136be 2019-03-26 stsp goto done;
2547 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2548 6d022e97 2019-08-04 stsp char *ondisk_path;
2549 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2550 6d022e97 2019-08-04 stsp pe->path) == -1)
2551 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2552 6d022e97 2019-08-04 stsp err = schedule_addition(ondisk_path, fileindex, pe->path,
2553 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2554 6d022e97 2019-08-04 stsp free(ondisk_path);
2558 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2559 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2560 af12c6b9 2019-06-04 stsp err = sync_err;
2562 fb399478 2019-07-12 stsp free(fileindex_path);
2563 d00136be 2019-03-26 stsp if (fileindex)
2564 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2565 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2566 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2567 d00136be 2019-03-26 stsp err = unlockerr;
2568 6c7ab921 2019-03-18 stsp return err;
2571 17ed4618 2019-06-02 stsp static const struct got_error *
2572 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2573 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2574 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2575 17ed4618 2019-06-02 stsp struct got_repository *repo)
2577 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2578 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2579 9acbc4fa 2019-08-03 stsp unsigned char status, staged_status;
2580 17ed4618 2019-06-02 stsp struct stat sb;
2582 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2583 17ed4618 2019-06-02 stsp if (ie == NULL)
2584 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2586 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
2587 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
2588 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2589 9acbc4fa 2019-08-03 stsp return NULL;
2590 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2593 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2595 17ed4618 2019-06-02 stsp return err;
2597 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2598 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2599 de426ea6 2019-08-03 stsp return NULL;
2600 f0b0c0ce 2019-08-04 stsp if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2601 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2602 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MODIFY &&
2603 f0b0c0ce 2019-08-04 stsp status != GOT_STATUS_MISSING)
2604 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2607 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2608 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2610 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2611 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2614 2ec1f75b 2019-03-26 stsp const struct got_error *
2615 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2616 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
2617 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2618 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2620 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2621 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2622 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2623 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2625 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2627 2ec1f75b 2019-03-26 stsp return err;
2629 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2631 2ec1f75b 2019-03-26 stsp goto done;
2633 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2634 6d022e97 2019-08-04 stsp char *ondisk_path;
2635 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2636 6d022e97 2019-08-04 stsp pe->path) == -1)
2637 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2638 6d022e97 2019-08-04 stsp err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2639 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2640 6d022e97 2019-08-04 stsp free(ondisk_path);
2644 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2645 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2646 af12c6b9 2019-06-04 stsp err = sync_err;
2648 fb399478 2019-07-12 stsp free(fileindex_path);
2649 2ec1f75b 2019-03-26 stsp if (fileindex)
2650 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2651 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2652 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2653 2ec1f75b 2019-03-26 stsp err = unlockerr;
2654 2ec1f75b 2019-03-26 stsp return err;
2657 e20a8b6f 2019-06-04 stsp static const struct got_error *
2658 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2659 a129376b 2019-03-28 stsp const char *ondisk_path,
2660 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2661 a129376b 2019-03-28 stsp struct got_repository *repo)
2663 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2664 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2665 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2666 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2667 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2668 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
2669 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2670 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2671 24278f30 2019-08-03 stsp unsigned char status, staged_status;
2672 a129376b 2019-03-28 stsp struct stat sb;
2674 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2675 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2677 a129376b 2019-03-28 stsp goto done;
2679 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2680 a129376b 2019-03-28 stsp if (ie == NULL) {
2681 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2682 a129376b 2019-03-28 stsp goto done;
2685 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2686 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2687 a129376b 2019-03-28 stsp if (err) {
2688 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2689 a129376b 2019-03-28 stsp goto done;
2690 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2691 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2692 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2693 e20a8b6f 2019-06-04 stsp goto done;
2696 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2697 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2698 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2699 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2700 a129376b 2019-03-28 stsp goto done;
2703 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2704 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2705 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2706 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2707 a129376b 2019-03-28 stsp goto done;
2710 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2711 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2712 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2713 a129376b 2019-03-28 stsp goto done;
2718 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2720 a129376b 2019-03-28 stsp goto done;
2721 7fa0d182 2019-08-03 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2722 7fa0d182 2019-08-03 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
2724 24278f30 2019-08-03 stsp staged_status = get_staged_status(ie);
2725 24278f30 2019-08-03 stsp if (status == GOT_STATUS_DELETE &&
2726 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_NO_CHANGE) {
2727 24278f30 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2728 24278f30 2019-08-03 stsp goto done;
2731 a9fa2909 2019-07-27 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2732 a9fa2909 2019-07-27 stsp tree_path);
2733 a9fa2909 2019-07-27 stsp if (err) {
2734 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2735 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
2736 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
2737 a9fa2909 2019-07-27 stsp goto done;
2739 a9fa2909 2019-07-27 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2741 a9fa2909 2019-07-27 stsp goto done;
2743 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
2744 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
2745 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
2746 a9fa2909 2019-07-27 stsp goto done;
2749 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
2750 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
2751 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
2752 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2753 a9fa2909 2019-07-27 stsp goto done;
2757 a129376b 2019-03-28 stsp switch (status) {
2758 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2759 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2761 1ee397ad 2019-07-12 stsp goto done;
2762 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2764 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2765 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2766 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2767 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2768 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2769 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2770 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2771 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
2772 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2774 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
2775 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2776 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2778 a129376b 2019-03-28 stsp goto done;
2779 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2780 24278f30 2019-08-03 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE, sb.st_mode,
2781 24278f30 2019-08-03 stsp blob, 0, 1, repo, progress_cb, progress_arg);
2783 a129376b 2019-03-28 stsp goto done;
2784 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2785 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2786 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2788 a129376b 2019-03-28 stsp goto done;
2793 a129376b 2019-03-28 stsp goto done;
2796 a129376b 2019-03-28 stsp free(relpath);
2797 e20a8b6f 2019-06-04 stsp free(parent_path);
2798 a129376b 2019-03-28 stsp free(tree_path);
2800 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2802 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2803 a129376b 2019-03-28 stsp free(tree_id);
2804 e20a8b6f 2019-06-04 stsp return err;
2807 e20a8b6f 2019-06-04 stsp const struct got_error *
2808 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2809 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2810 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2813 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2814 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2815 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2816 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2817 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2819 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2821 e20a8b6f 2019-06-04 stsp return err;
2823 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2825 e20a8b6f 2019-06-04 stsp goto done;
2827 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2828 6d022e97 2019-08-04 stsp char *ondisk_path;
2829 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2830 6d022e97 2019-08-04 stsp pe->path) == -1)
2831 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2832 6d022e97 2019-08-04 stsp err = revert_file(worktree, fileindex, ondisk_path,
2833 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2834 6d022e97 2019-08-04 stsp free(ondisk_path);
2838 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2839 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2840 e20a8b6f 2019-06-04 stsp err = sync_err;
2842 fb399478 2019-07-12 stsp free(fileindex_path);
2843 a129376b 2019-03-28 stsp if (fileindex)
2844 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2845 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2846 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2847 a129376b 2019-03-28 stsp err = unlockerr;
2848 c4296144 2019-05-09 stsp return err;
2851 cf066bf8 2019-05-09 stsp static void
2852 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2854 24519714 2019-05-09 stsp free(ct->path);
2855 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2856 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2857 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2858 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2859 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
2860 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2864 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2865 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2866 24519714 2019-05-09 stsp struct got_repository *repo;
2867 24519714 2019-05-09 stsp struct got_worktree *worktree;
2868 5f8a88c6 2019-08-03 stsp int have_staged_files;
2871 c4296144 2019-05-09 stsp static const struct got_error *
2872 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
2873 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
2874 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2875 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
2877 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2878 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2879 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2880 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2881 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2882 768aea60 2019-05-09 stsp struct stat sb;
2884 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
2885 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2886 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2887 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2888 5f8a88c6 2019-08-03 stsp return NULL;
2890 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
2891 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2893 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2894 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
2895 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
2896 5f8a88c6 2019-08-03 stsp return NULL;
2899 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2900 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2901 036813ee 2019-05-09 stsp goto done;
2903 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2904 036813ee 2019-05-09 stsp parent_path = strdup("");
2905 69960a46 2019-05-09 stsp if (parent_path == NULL)
2906 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2908 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2910 69960a46 2019-05-09 stsp return err;
2913 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2914 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2915 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2916 c4296144 2019-05-09 stsp goto done;
2919 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2920 768aea60 2019-05-09 stsp relpath) == -1) {
2921 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2922 768aea60 2019-05-09 stsp goto done;
2924 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
2925 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2927 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2928 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2929 768aea60 2019-05-09 stsp goto done;
2931 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2934 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2935 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2936 44d03001 2019-05-09 stsp relpath) == -1) {
2937 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2938 44d03001 2019-05-09 stsp goto done;
2941 cf066bf8 2019-05-09 stsp ct->status = status;
2942 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
2943 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2944 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
2945 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
2946 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2947 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2948 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2949 b416585c 2019-05-13 stsp goto done;
2951 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2952 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2953 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
2954 f0b75401 2019-08-03 stsp goto done;
2957 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
2958 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
2959 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
2960 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
2961 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2962 036813ee 2019-05-09 stsp goto done;
2965 24519714 2019-05-09 stsp ct->path = strdup(path);
2966 24519714 2019-05-09 stsp if (ct->path == NULL) {
2967 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2968 24519714 2019-05-09 stsp goto done;
2970 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2972 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2973 ed175427 2019-05-09 stsp free_commitable(ct);
2974 24519714 2019-05-09 stsp free(parent_path);
2975 036813ee 2019-05-09 stsp free(path);
2976 a129376b 2019-03-28 stsp return err;
2979 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2980 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2981 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2982 036813ee 2019-05-09 stsp struct got_repository *);
2984 ed175427 2019-05-09 stsp static const struct got_error *
2985 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2986 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2987 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2988 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2989 afa376bf 2019-05-09 stsp struct got_repository *repo)
2991 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2992 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2993 036813ee 2019-05-09 stsp char *subpath;
2995 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2996 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2997 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2999 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
3001 ed175427 2019-05-09 stsp return err;
3003 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3004 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3005 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
3006 036813ee 2019-05-09 stsp free(subpath);
3007 036813ee 2019-05-09 stsp return err;
3010 036813ee 2019-05-09 stsp static const struct got_error *
3011 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3013 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3014 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
3016 036813ee 2019-05-09 stsp *match = 0;
3018 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
3019 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
3020 0f63689d 2019-05-10 stsp return NULL;
3023 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3025 0f63689d 2019-05-10 stsp return err;
3026 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
3027 036813ee 2019-05-09 stsp free(ct_parent_path);
3028 036813ee 2019-05-09 stsp return err;
3031 768aea60 2019-05-09 stsp static mode_t
3032 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
3034 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3037 036813ee 2019-05-09 stsp static const struct got_error *
3038 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3039 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3041 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3043 036813ee 2019-05-09 stsp *new_te = NULL;
3045 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3047 036813ee 2019-05-09 stsp goto done;
3049 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3051 036813ee 2019-05-09 stsp free((*new_te)->id);
3052 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
3053 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3055 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3056 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3057 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3058 036813ee 2019-05-09 stsp goto done;
3061 036813ee 2019-05-09 stsp if (err && *new_te) {
3062 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3063 036813ee 2019-05-09 stsp *new_te = NULL;
3065 036813ee 2019-05-09 stsp return err;
3068 036813ee 2019-05-09 stsp static const struct got_error *
3069 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3070 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3072 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3073 036813ee 2019-05-09 stsp char *ct_name;
3075 036813ee 2019-05-09 stsp *new_te = NULL;
3077 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3078 036813ee 2019-05-09 stsp if (*new_te == NULL)
3079 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3081 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3082 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3083 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3084 036813ee 2019-05-09 stsp goto done;
3086 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
3087 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
3088 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3089 036813ee 2019-05-09 stsp goto done;
3092 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3094 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
3095 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3097 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3098 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3099 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3100 036813ee 2019-05-09 stsp goto done;
3103 036813ee 2019-05-09 stsp if (err && *new_te) {
3104 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3105 036813ee 2019-05-09 stsp *new_te = NULL;
3107 036813ee 2019-05-09 stsp return err;
3110 036813ee 2019-05-09 stsp static const struct got_error *
3111 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3112 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3114 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3115 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3117 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3119 036813ee 2019-05-09 stsp return err;
3120 036813ee 2019-05-09 stsp if (new_pe == NULL)
3121 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3122 036813ee 2019-05-09 stsp return NULL;
3125 afa376bf 2019-05-09 stsp static const struct got_error *
3126 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3127 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3129 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3130 5f8a88c6 2019-08-03 stsp unsigned char status;
3132 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3133 afa376bf 2019-05-09 stsp ct_path++;
3135 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3136 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
3138 5f8a88c6 2019-08-03 stsp status = ct->status;
3140 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3141 537ac44b 2019-08-03 stsp ct_path, ct->blob_id, NULL, NULL);
3144 036813ee 2019-05-09 stsp static const struct got_error *
3145 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3146 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3148 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3149 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3150 44d03001 2019-05-09 stsp char *te_path;
3152 44d03001 2019-05-09 stsp *modified = 0;
3154 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3155 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3156 44d03001 2019-05-09 stsp te->name) == -1)
3157 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3159 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3161 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3162 44d03001 2019-05-09 stsp strlen(te_path));
3163 44d03001 2019-05-09 stsp if (*modified)
3167 44d03001 2019-05-09 stsp free(te_path);
3168 44d03001 2019-05-09 stsp return err;
3171 44d03001 2019-05-09 stsp static const struct got_error *
3172 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3173 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3174 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3176 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3177 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3179 036813ee 2019-05-09 stsp *ctp = NULL;
3181 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3182 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3183 036813ee 2019-05-09 stsp char *ct_name = NULL;
3184 036813ee 2019-05-09 stsp int path_matches;
3186 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3187 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
3188 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
3191 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
3192 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
3196 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3199 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3201 036813ee 2019-05-09 stsp return err;
3202 036813ee 2019-05-09 stsp if (!path_matches)
3205 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3206 036813ee 2019-05-09 stsp if (ct_name == NULL)
3207 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3209 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3212 036813ee 2019-05-09 stsp *ctp = ct;
3216 2b496619 2019-07-10 stsp return err;
3219 2b496619 2019-07-10 stsp static const struct got_error *
3220 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3221 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3222 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3223 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3224 2b496619 2019-07-10 stsp struct got_repository *repo)
3226 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3227 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3228 2b496619 2019-07-10 stsp char *subtree_path;
3230 2b496619 2019-07-10 stsp *new_tep = NULL;
3232 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3233 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3234 2b496619 2019-07-10 stsp child_path) == -1)
3235 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3237 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3238 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3239 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3240 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3241 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3242 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3243 2b496619 2019-07-10 stsp goto done;
3245 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3246 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3247 2b496619 2019-07-10 stsp if (err) {
3248 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3249 2b496619 2019-07-10 stsp goto done;
3252 2b496619 2019-07-10 stsp free(subtree_path);
3253 2b496619 2019-07-10 stsp if (err == NULL)
3254 2b496619 2019-07-10 stsp *new_tep = new_te;
3255 036813ee 2019-05-09 stsp return err;
3258 036813ee 2019-05-09 stsp static const struct got_error *
3259 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3260 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3261 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3262 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3263 036813ee 2019-05-09 stsp struct got_repository *repo)
3265 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3266 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3267 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3268 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3269 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3270 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3272 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3273 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3274 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3276 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3277 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3278 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3279 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3281 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
3282 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
3283 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3286 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3287 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3290 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3291 036813ee 2019-05-09 stsp pe->path);
3293 036813ee 2019-05-09 stsp goto done;
3295 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3296 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3297 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3299 036813ee 2019-05-09 stsp goto done;
3300 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3302 afa376bf 2019-05-09 stsp goto done;
3303 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3304 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3306 2f51b5b3 2019-05-09 stsp goto done;
3308 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3309 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3310 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3311 2b496619 2019-07-10 stsp == NULL) {
3312 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3313 2b496619 2019-07-10 stsp child_path, path_base_tree,
3314 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3317 2b496619 2019-07-10 stsp goto done;
3318 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3320 2b496619 2019-07-10 stsp goto done;
3325 2f51b5b3 2019-05-09 stsp if (base_tree) {
3326 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3327 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3328 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3329 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3331 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3332 44d03001 2019-05-09 stsp int modified;
3333 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3335 036813ee 2019-05-09 stsp goto done;
3336 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3337 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3339 2f51b5b3 2019-05-09 stsp goto done;
3340 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3341 44d03001 2019-05-09 stsp if (modified) {
3342 44d03001 2019-05-09 stsp free(new_te->id);
3343 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3344 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3345 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3347 44d03001 2019-05-09 stsp goto done;
3349 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3351 036813ee 2019-05-09 stsp goto done;
3355 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3356 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3358 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3359 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
3360 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3361 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3362 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3364 2f51b5b3 2019-05-09 stsp goto done;
3365 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3367 2f51b5b3 2019-05-09 stsp goto done;
3369 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3370 b416585c 2019-05-13 stsp status_arg);
3372 afa376bf 2019-05-09 stsp goto done;
3374 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3375 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3377 2f51b5b3 2019-05-09 stsp goto done;
3378 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3380 2f51b5b3 2019-05-09 stsp goto done;
3385 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3386 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3387 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3388 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3389 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3391 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3393 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3394 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3395 ed175427 2019-05-09 stsp return err;
3398 ebf99748 2019-05-09 stsp static const struct got_error *
3399 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3400 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3402 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3403 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3405 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3406 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3407 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3409 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3411 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
3412 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
3413 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3414 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3415 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
3416 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3417 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
3418 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
3419 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
3420 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
3421 5f8a88c6 2019-08-03 stsp new_base_commit_id->sha1, 1);
3423 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3424 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3425 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3427 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3428 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3429 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3432 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3437 d56d26ce 2019-05-10 stsp return err;
3441 d56d26ce 2019-05-10 stsp static const struct got_error *
3442 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
3443 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
3444 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
3445 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
3446 735ef5ac 2019-08-03 stsp int ood_errcode)
3448 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3449 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
3451 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3452 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3453 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3454 1a36436d 2019-06-10 stsp return NULL;
3456 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
3457 9bead371 2019-07-28 stsp * on matches file content in the branch head.
3459 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3460 735ef5ac 2019-08-03 stsp in_repo_path);
3461 9bead371 2019-07-28 stsp if (err) {
3462 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
3463 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
3464 1a36436d 2019-06-10 stsp goto done;
3465 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
3466 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
3468 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3469 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3470 735ef5ac 2019-08-03 stsp in_repo_path);
3471 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3472 1a36436d 2019-06-10 stsp goto done;
3473 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
3477 1a36436d 2019-06-10 stsp return err;
3480 c4296144 2019-05-09 stsp const struct got_error *
3481 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3482 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3483 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3484 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
3485 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3486 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3487 afa376bf 2019-05-09 stsp struct got_repository *repo)
3489 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3490 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3491 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3492 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3493 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3494 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3495 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3496 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3497 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3498 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3499 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3501 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3503 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3505 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3507 588edf97 2019-05-10 stsp goto done;
3509 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3511 588edf97 2019-05-10 stsp goto done;
3513 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3514 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3519 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3520 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3524 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3525 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3526 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3527 cf066bf8 2019-05-09 stsp char *ondisk_path;
3529 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
3530 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3531 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
3534 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3535 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3538 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3539 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3540 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3541 cf066bf8 2019-05-09 stsp goto done;
3543 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3544 a7055788 2019-05-09 stsp free(ondisk_path);
3546 cf066bf8 2019-05-09 stsp goto done;
3549 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3550 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3551 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3553 036813ee 2019-05-09 stsp goto done;
3555 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3557 de18fc63 2019-05-09 stsp goto done;
3558 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3559 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3560 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3561 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3562 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3563 33ad4cbe 2019-05-12 jcs free(logmsg);
3565 9d40349a 2019-05-09 stsp goto done;
3567 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3568 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3569 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3570 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3571 09f5bd90 2019-05-09 stsp goto done;
3573 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3574 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3576 09f5bd90 2019-05-09 stsp goto done;
3577 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3579 ebf99748 2019-05-09 stsp goto done;
3580 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3581 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3582 09f5bd90 2019-05-09 stsp goto done;
3584 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3585 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3587 f2c16586 2019-05-09 stsp goto done;
3588 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3590 f2c16586 2019-05-09 stsp goto done;
3592 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3594 09f5bd90 2019-05-09 stsp goto done;
3596 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3598 ebf99748 2019-05-09 stsp goto done;
3600 588edf97 2019-05-10 stsp if (head_tree)
3601 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3602 588edf97 2019-05-10 stsp if (head_commit)
3603 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3604 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3605 2f17228e 2019-05-12 stsp if (head_ref2) {
3606 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3607 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3608 2f17228e 2019-05-12 stsp err = unlockerr;
3609 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3611 39cd0ff6 2019-07-12 stsp return err;
3614 5c1e53bc 2019-07-28 stsp static const struct got_error *
3615 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
3616 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
3618 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
3619 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
3621 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
3622 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
3623 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
3625 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
3626 5c1e53bc 2019-07-28 stsp ct_path++;
3628 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
3629 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
3633 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
3634 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
3636 5c1e53bc 2019-07-28 stsp return NULL;
3639 f0b75401 2019-08-03 stsp static const struct got_error *
3640 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
3642 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
3644 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
3645 f0b75401 2019-08-03 stsp *have_staged_files = 1;
3646 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
3649 f0b75401 2019-08-03 stsp return NULL;
3652 5f8a88c6 2019-08-03 stsp static const struct got_error *
3653 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
3654 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
3656 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
3657 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
3659 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
3660 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
3662 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3663 5f8a88c6 2019-08-03 stsp if (ie == NULL)
3664 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
3665 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
3666 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
3667 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
3670 5f8a88c6 2019-08-03 stsp return NULL;
3673 39cd0ff6 2019-07-12 stsp const struct got_error *
3674 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3675 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
3676 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
3677 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3678 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3679 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
3681 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3682 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3683 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
3684 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3685 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3686 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
3687 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
3688 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3689 f0b75401 2019-08-03 stsp int have_staged_files = 0;
3691 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
3693 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3695 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
3697 39cd0ff6 2019-07-12 stsp goto done;
3699 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3701 39cd0ff6 2019-07-12 stsp goto done;
3703 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3705 39cd0ff6 2019-07-12 stsp goto done;
3707 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3709 39cd0ff6 2019-07-12 stsp goto done;
3711 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
3712 5f8a88c6 2019-08-03 stsp &have_staged_files);
3713 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
3714 5f8a88c6 2019-08-03 stsp goto done;
3715 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
3716 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
3718 5f8a88c6 2019-08-03 stsp goto done;
3721 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3722 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
3723 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
3724 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
3725 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3726 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3727 5c1e53bc 2019-07-28 stsp collect_commitables, &cc_arg, NULL, NULL);
3729 5c1e53bc 2019-07-28 stsp goto done;
3732 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3733 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3734 39cd0ff6 2019-07-12 stsp goto done;
3737 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3738 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
3740 5c1e53bc 2019-07-28 stsp goto done;
3743 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3744 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
3745 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
3747 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
3748 f0b75401 2019-08-03 stsp ct_path++;
3749 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
3750 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3751 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3753 b50cabdf 2019-07-12 stsp goto done;
3757 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
3758 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
3759 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3761 39cd0ff6 2019-07-12 stsp goto done;
3763 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3764 93ec1b5f 2019-07-12 stsp fileindex);
3765 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3766 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
3767 39cd0ff6 2019-07-12 stsp err = sync_err;
3769 39cd0ff6 2019-07-12 stsp if (fileindex)
3770 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
3771 39cd0ff6 2019-07-12 stsp free(fileindex_path);
3772 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3773 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
3774 39cd0ff6 2019-07-12 stsp err = unlockerr;
3775 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3776 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
3777 39cd0ff6 2019-07-12 stsp free_commitable(ct);
3779 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
3780 c4296144 2019-05-09 stsp return err;
3783 8656d6c4 2019-05-20 stsp const char *
3784 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3786 8656d6c4 2019-05-20 stsp return ct->path;
3789 8656d6c4 2019-05-20 stsp unsigned int
3790 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3792 8656d6c4 2019-05-20 stsp return ct->status;
3795 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3796 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3797 818c7501 2019-07-11 stsp struct got_repository *repo;
3800 818c7501 2019-07-11 stsp static const struct got_error *
3801 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3803 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3804 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3805 818c7501 2019-07-11 stsp unsigned char status;
3806 818c7501 2019-07-11 stsp struct stat sb;
3807 818c7501 2019-07-11 stsp char *ondisk_path;
3809 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
3810 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3811 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
3812 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3814 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3816 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3818 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
3819 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3820 818c7501 2019-07-11 stsp free(ondisk_path);
3822 818c7501 2019-07-11 stsp return err;
3824 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
3825 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3826 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
3827 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
3829 818c7501 2019-07-11 stsp return NULL;
3832 818c7501 2019-07-11 stsp const struct got_error *
3833 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3834 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3835 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
3836 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
3838 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3839 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3840 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3841 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3842 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3843 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3845 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3846 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3847 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3849 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3851 818c7501 2019-07-11 stsp return err;
3853 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3855 818c7501 2019-07-11 stsp goto done;
3857 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3858 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3859 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3862 818c7501 2019-07-11 stsp goto done;
3864 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3866 818c7501 2019-07-11 stsp goto done;
3868 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3870 818c7501 2019-07-11 stsp goto done;
3872 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3874 818c7501 2019-07-11 stsp goto done;
3876 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3879 818c7501 2019-07-11 stsp goto done;
3881 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3882 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3884 818c7501 2019-07-11 stsp goto done;
3885 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3887 818c7501 2019-07-11 stsp goto done;
3889 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3891 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3893 818c7501 2019-07-11 stsp goto done;
3895 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3897 818c7501 2019-07-11 stsp goto done;
3899 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3900 818c7501 2019-07-11 stsp worktree->base_commit_id);
3902 818c7501 2019-07-11 stsp goto done;
3903 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3905 818c7501 2019-07-11 stsp goto done;
3907 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3909 818c7501 2019-07-11 stsp goto done;
3911 818c7501 2019-07-11 stsp free(fileindex_path);
3912 818c7501 2019-07-11 stsp free(tmp_branch_name);
3913 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3914 818c7501 2019-07-11 stsp free(branch_ref_name);
3915 818c7501 2019-07-11 stsp if (branch_ref)
3916 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3917 818c7501 2019-07-11 stsp if (wt_branch)
3918 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3919 818c7501 2019-07-11 stsp if (err) {
3920 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3921 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3922 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3924 818c7501 2019-07-11 stsp if (*tmp_branch) {
3925 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3926 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3928 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3929 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3930 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3932 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3934 818c7501 2019-07-11 stsp return err;
3937 818c7501 2019-07-11 stsp const struct got_error *
3938 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3939 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3940 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
3941 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
3943 818c7501 2019-07-11 stsp const struct got_error *err;
3944 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3945 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3946 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3947 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
3948 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
3950 818c7501 2019-07-11 stsp *commit_id = NULL;
3951 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
3952 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
3953 3e3a69f1 2019-07-25 stsp *branch = NULL;
3954 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3956 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
3958 3e3a69f1 2019-07-25 stsp return err;
3960 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3962 f032f1f7 2019-08-04 stsp goto done;
3964 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
3965 f032f1f7 2019-08-04 stsp &have_staged_files);
3966 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
3967 f032f1f7 2019-08-04 stsp goto done;
3968 f032f1f7 2019-08-04 stsp if (have_staged_files) {
3969 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
3970 3e3a69f1 2019-07-25 stsp goto done;
3973 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3975 f032f1f7 2019-08-04 stsp goto done;
3977 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3979 818c7501 2019-07-11 stsp goto done;
3981 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3983 818c7501 2019-07-11 stsp goto done;
3985 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3987 818c7501 2019-07-11 stsp goto done;
3989 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3991 818c7501 2019-07-11 stsp goto done;
3993 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3994 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3996 818c7501 2019-07-11 stsp goto done;
3998 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4000 818c7501 2019-07-11 stsp goto done;
4002 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4004 818c7501 2019-07-11 stsp goto done;
4006 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
4007 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
4009 818c7501 2019-07-11 stsp goto done;
4011 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4013 818c7501 2019-07-11 stsp goto done;
4015 818c7501 2019-07-11 stsp free(commit_ref_name);
4016 818c7501 2019-07-11 stsp free(branch_ref_name);
4017 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4018 818c7501 2019-07-11 stsp if (commit_ref)
4019 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4020 818c7501 2019-07-11 stsp if (branch_ref)
4021 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4022 818c7501 2019-07-11 stsp if (err) {
4023 818c7501 2019-07-11 stsp free(*commit_id);
4024 818c7501 2019-07-11 stsp *commit_id = NULL;
4025 818c7501 2019-07-11 stsp if (*tmp_branch) {
4026 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4027 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4029 818c7501 2019-07-11 stsp if (*new_base_branch) {
4030 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
4031 818c7501 2019-07-11 stsp *new_base_branch = NULL;
4033 818c7501 2019-07-11 stsp if (*branch) {
4034 818c7501 2019-07-11 stsp got_ref_close(*branch);
4035 818c7501 2019-07-11 stsp *branch = NULL;
4037 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4038 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4039 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4041 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
4043 818c7501 2019-07-11 stsp return err;
4046 818c7501 2019-07-11 stsp const struct got_error *
4047 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4049 818c7501 2019-07-11 stsp const struct got_error *err;
4050 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
4052 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4054 818c7501 2019-07-11 stsp return err;
4056 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4057 818c7501 2019-07-11 stsp free(tmp_branch_name);
4058 818c7501 2019-07-11 stsp return NULL;
4061 818c7501 2019-07-11 stsp static const struct got_error *
4062 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4063 818c7501 2019-07-11 stsp char **logmsg, void *arg)
4065 0ebf8283 2019-07-24 stsp *logmsg = arg;
4066 818c7501 2019-07-11 stsp return NULL;
4069 818c7501 2019-07-11 stsp static const struct got_error *
4070 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4071 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4072 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4074 818c7501 2019-07-11 stsp return NULL;
4077 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
4078 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
4079 01757395 2019-07-12 stsp void *progress_arg;
4080 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
4083 01757395 2019-07-12 stsp static const struct got_error *
4084 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
4086 01757395 2019-07-12 stsp const struct got_error *err;
4087 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
4089 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
4091 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
4093 01757395 2019-07-12 stsp return err;
4095 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
4096 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
4097 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
4098 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
4099 01757395 2019-07-12 stsp return NULL;
4101 01757395 2019-07-12 stsp p = strdup(path);
4102 01757395 2019-07-12 stsp if (p == NULL)
4103 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
4105 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4106 01757395 2019-07-12 stsp if (err || new == NULL)
4108 01757395 2019-07-12 stsp return err;
4112 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4114 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4116 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
4117 01757395 2019-07-12 stsp free((char *)pe->path);
4119 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
4122 0ebf8283 2019-07-24 stsp static const struct got_error *
4123 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4124 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4126 818c7501 2019-07-11 stsp const struct got_error *err;
4127 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
4129 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4130 818c7501 2019-07-11 stsp if (err) {
4131 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
4132 818c7501 2019-07-11 stsp goto done;
4133 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4135 818c7501 2019-07-11 stsp goto done;
4136 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
4138 818c7501 2019-07-11 stsp goto done;
4140 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
4143 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
4145 818c7501 2019-07-11 stsp goto done;
4146 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
4147 818c7501 2019-07-11 stsp free(stored_id);
4148 818c7501 2019-07-11 stsp if (cmp != 0) {
4149 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4150 818c7501 2019-07-11 stsp goto done;
4154 0ebf8283 2019-07-24 stsp if (commit_ref)
4155 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4156 0ebf8283 2019-07-24 stsp return err;
4159 0ebf8283 2019-07-24 stsp static const struct got_error *
4160 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
4161 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
4162 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4163 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
4164 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4165 3e3a69f1 2019-07-25 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4167 0ebf8283 2019-07-24 stsp const struct got_error *err;
4168 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4169 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
4170 3e3a69f1 2019-07-25 stsp char *fileindex_path;
4172 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4174 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4176 0ebf8283 2019-07-24 stsp return err;
4178 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
4179 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
4180 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
4181 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
4182 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
4183 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
4184 818c7501 2019-07-11 stsp if (commit_ref)
4185 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4186 818c7501 2019-07-11 stsp return err;
4189 818c7501 2019-07-11 stsp const struct got_error *
4190 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4191 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4192 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4193 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4194 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4195 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4197 0ebf8283 2019-07-24 stsp const struct got_error *err;
4198 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4200 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4202 0ebf8283 2019-07-24 stsp return err;
4204 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4206 0ebf8283 2019-07-24 stsp goto done;
4208 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4209 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4210 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4212 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4213 0ebf8283 2019-07-24 stsp return err;
4216 0ebf8283 2019-07-24 stsp const struct got_error *
4217 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4218 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4219 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4220 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4221 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4222 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4224 0ebf8283 2019-07-24 stsp const struct got_error *err;
4225 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4227 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4229 0ebf8283 2019-07-24 stsp return err;
4231 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4233 0ebf8283 2019-07-24 stsp goto done;
4235 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4236 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4237 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4239 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4240 0ebf8283 2019-07-24 stsp return err;
4243 0ebf8283 2019-07-24 stsp static const struct got_error *
4244 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4245 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4246 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4247 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4248 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4250 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4251 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4252 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4253 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4254 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4255 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4256 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4258 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4259 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4261 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4263 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4265 3e3a69f1 2019-07-25 stsp return err;
4267 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4268 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4269 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4270 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
4272 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4273 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4274 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4275 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4277 01757395 2019-07-12 stsp if (merged_paths) {
4278 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4279 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4280 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4281 8f8646e5 2019-07-25 stsp goto done;
4283 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4284 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4285 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4287 01757395 2019-07-12 stsp goto done;
4290 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4291 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4293 01757395 2019-07-12 stsp goto done;
4296 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4297 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4298 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4300 a0e95631 2019-07-12 stsp goto done;
4301 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4302 a0e95631 2019-07-12 stsp goto done;
4305 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4307 edd02c5e 2019-07-11 stsp goto done;
4309 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4311 818c7501 2019-07-11 stsp goto done;
4313 0ebf8283 2019-07-24 stsp if (new_logmsg)
4314 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4316 0ebf8283 2019-07-24 stsp logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4317 0ebf8283 2019-07-24 stsp if (logmsg == NULL)
4318 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
4320 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4321 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
4322 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4323 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4325 a0e95631 2019-07-12 stsp goto done;
4327 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4329 a0e95631 2019-07-12 stsp goto done;
4331 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4333 a0e95631 2019-07-12 stsp goto done;
4335 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4336 93ec1b5f 2019-07-12 stsp fileindex);
4337 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4338 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4339 a0e95631 2019-07-12 stsp err = sync_err;
4341 a0e95631 2019-07-12 stsp free(fileindex_path);
4342 a0e95631 2019-07-12 stsp free(head_commit_id);
4343 a0e95631 2019-07-12 stsp if (head_ref)
4344 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4345 818c7501 2019-07-11 stsp if (err) {
4346 818c7501 2019-07-11 stsp free(*new_commit_id);
4347 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4349 818c7501 2019-07-11 stsp return err;
4352 818c7501 2019-07-11 stsp const struct got_error *
4353 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4354 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4355 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4356 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4357 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4359 0ebf8283 2019-07-24 stsp const struct got_error *err;
4360 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4361 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4362 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4364 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4366 0ebf8283 2019-07-24 stsp return err;
4368 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4370 0ebf8283 2019-07-24 stsp goto done;
4371 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4373 0ebf8283 2019-07-24 stsp goto done;
4374 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4375 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4376 0ebf8283 2019-07-24 stsp goto done;
4379 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4380 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4382 0ebf8283 2019-07-24 stsp if (commit_ref)
4383 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4384 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4385 0ebf8283 2019-07-24 stsp free(commit_id);
4386 0ebf8283 2019-07-24 stsp return err;
4389 0ebf8283 2019-07-24 stsp const struct got_error *
4390 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4391 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4392 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4393 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4394 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4395 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4397 0ebf8283 2019-07-24 stsp const struct got_error *err;
4398 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4399 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4400 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4402 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4404 0ebf8283 2019-07-24 stsp return err;
4406 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4408 0ebf8283 2019-07-24 stsp goto done;
4409 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4411 0ebf8283 2019-07-24 stsp goto done;
4412 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4413 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4414 0ebf8283 2019-07-24 stsp goto done;
4417 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4418 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4420 0ebf8283 2019-07-24 stsp if (commit_ref)
4421 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4422 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4423 0ebf8283 2019-07-24 stsp free(commit_id);
4424 0ebf8283 2019-07-24 stsp return err;
4427 0ebf8283 2019-07-24 stsp const struct got_error *
4428 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4429 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4431 3e3a69f1 2019-07-25 stsp if (fileindex)
4432 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4433 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4436 69844fba 2019-07-11 stsp static const struct got_error *
4437 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4439 69844fba 2019-07-11 stsp const struct got_error *err;
4440 69844fba 2019-07-11 stsp struct got_reference *ref;
4442 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4443 69844fba 2019-07-11 stsp if (err) {
4444 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4445 69844fba 2019-07-11 stsp return NULL;
4446 69844fba 2019-07-11 stsp return err;
4449 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4450 69844fba 2019-07-11 stsp got_ref_close(ref);
4451 69844fba 2019-07-11 stsp return err;
4454 69844fba 2019-07-11 stsp static const struct got_error *
4455 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4457 69844fba 2019-07-11 stsp const struct got_error *err;
4458 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4459 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4461 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4463 69844fba 2019-07-11 stsp goto done;
4464 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4466 69844fba 2019-07-11 stsp goto done;
4468 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4470 69844fba 2019-07-11 stsp goto done;
4471 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4473 69844fba 2019-07-11 stsp goto done;
4475 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4477 69844fba 2019-07-11 stsp goto done;
4478 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4480 69844fba 2019-07-11 stsp goto done;
4482 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4484 69844fba 2019-07-11 stsp goto done;
4485 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4487 69844fba 2019-07-11 stsp goto done;
4490 69844fba 2019-07-11 stsp free(tmp_branch_name);
4491 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4492 69844fba 2019-07-11 stsp free(branch_ref_name);
4493 69844fba 2019-07-11 stsp free(commit_ref_name);
4494 69844fba 2019-07-11 stsp return err;
4497 818c7501 2019-07-11 stsp const struct got_error *
4498 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4499 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4500 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4501 818c7501 2019-07-11 stsp struct got_repository *repo)
4503 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4504 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4506 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4508 818c7501 2019-07-11 stsp return err;
4510 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4512 818c7501 2019-07-11 stsp goto done;
4514 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4516 818c7501 2019-07-11 stsp goto done;
4518 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4520 818c7501 2019-07-11 stsp goto done;
4522 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4524 3e3a69f1 2019-07-25 stsp if (fileindex)
4525 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4526 818c7501 2019-07-11 stsp free(new_head_commit_id);
4527 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4528 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4529 818c7501 2019-07-11 stsp err = unlockerr;
4530 818c7501 2019-07-11 stsp return err;
4533 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
4534 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
4535 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4538 818c7501 2019-07-11 stsp static const struct got_error *
4539 88d0e355 2019-08-03 stsp collect_revertible_paths(void *arg, unsigned char status,
4540 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4541 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4542 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
4544 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
4545 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4546 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
4547 818c7501 2019-07-11 stsp char *path = NULL;
4549 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
4550 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
4551 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
4552 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
4553 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
4554 818c7501 2019-07-11 stsp return NULL;
4556 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4557 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4559 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4560 818c7501 2019-07-11 stsp if (err || new == NULL)
4561 818c7501 2019-07-11 stsp free(path);
4562 818c7501 2019-07-11 stsp return err;
4565 818c7501 2019-07-11 stsp const struct got_error *
4566 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4567 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4568 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
4569 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4571 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4572 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4573 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4574 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4575 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
4576 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
4577 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
4578 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4580 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
4582 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4584 818c7501 2019-07-11 stsp return err;
4586 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4587 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4589 818c7501 2019-07-11 stsp goto done;
4591 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4593 818c7501 2019-07-11 stsp goto done;
4596 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4597 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4598 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4600 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4602 818c7501 2019-07-11 stsp goto done;
4604 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4606 818c7501 2019-07-11 stsp goto done;
4608 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4609 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4611 ca355955 2019-07-12 stsp goto done;
4613 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4615 ca355955 2019-07-12 stsp goto done;
4617 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4619 818c7501 2019-07-11 stsp goto done;
4621 347d1d3e 2019-07-12 stsp crp_arg.revertible_paths = &revertible_paths;
4622 347d1d3e 2019-07-12 stsp crp_arg.worktree = worktree;
4623 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4624 347d1d3e 2019-07-12 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4626 55bd499d 2019-07-12 stsp goto done;
4628 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4629 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
4630 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
4632 ca355955 2019-07-12 stsp goto sync;
4635 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4636 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4638 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4639 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4640 ca355955 2019-07-12 stsp err = sync_err;
4642 818c7501 2019-07-11 stsp got_ref_close(resolved);
4643 a3a2faf2 2019-07-12 stsp free(tree_id);
4644 818c7501 2019-07-11 stsp free(commit_id);
4645 0ebf8283 2019-07-24 stsp if (fileindex)
4646 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
4647 0ebf8283 2019-07-24 stsp free(fileindex_path);
4648 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4649 0ebf8283 2019-07-24 stsp free((char *)pe->path);
4650 0ebf8283 2019-07-24 stsp got_pathlist_free(&revertible_paths);
4652 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4653 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4654 0ebf8283 2019-07-24 stsp err = unlockerr;
4655 0ebf8283 2019-07-24 stsp return err;
4658 0ebf8283 2019-07-24 stsp const struct got_error *
4659 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4660 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4661 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
4662 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4664 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4665 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4666 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
4667 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
4668 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4669 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
4670 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
4671 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4673 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4674 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4675 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4676 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4678 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4680 0ebf8283 2019-07-24 stsp return err;
4682 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4684 0ebf8283 2019-07-24 stsp goto done;
4686 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
4687 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
4688 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4691 0ebf8283 2019-07-24 stsp goto done;
4693 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4695 0ebf8283 2019-07-24 stsp goto done;
4697 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4699 0ebf8283 2019-07-24 stsp goto done;
4701 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4702 0ebf8283 2019-07-24 stsp worktree);
4704 0ebf8283 2019-07-24 stsp goto done;
4706 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4709 0ebf8283 2019-07-24 stsp goto done;
4711 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4713 0ebf8283 2019-07-24 stsp goto done;
4715 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
4717 0ebf8283 2019-07-24 stsp goto done;
4719 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4720 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4722 0ebf8283 2019-07-24 stsp goto done;
4723 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
4725 0ebf8283 2019-07-24 stsp goto done;
4726 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4727 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
4728 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
4729 0ebf8283 2019-07-24 stsp goto done;
4732 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4733 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4735 0ebf8283 2019-07-24 stsp goto done;
4736 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
4738 0ebf8283 2019-07-24 stsp goto done;
4740 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4742 0ebf8283 2019-07-24 stsp goto done;
4744 0ebf8283 2019-07-24 stsp free(fileindex_path);
4745 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4746 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4747 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4748 0ebf8283 2019-07-24 stsp if (wt_branch)
4749 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
4750 0ebf8283 2019-07-24 stsp if (err) {
4751 0ebf8283 2019-07-24 stsp if (*branch_ref) {
4752 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
4753 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4755 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4756 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4757 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4759 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4760 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4761 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4762 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4764 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
4766 0ebf8283 2019-07-24 stsp return err;
4769 0ebf8283 2019-07-24 stsp const struct got_error *
4770 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
4771 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4773 3e3a69f1 2019-07-25 stsp if (fileindex)
4774 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4775 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
4778 0ebf8283 2019-07-24 stsp const struct got_error *
4779 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
4780 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
4782 0ebf8283 2019-07-24 stsp const struct got_error *err;
4783 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4785 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4787 0ebf8283 2019-07-24 stsp return err;
4789 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4790 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4791 0ebf8283 2019-07-24 stsp return NULL;
4794 0ebf8283 2019-07-24 stsp const struct got_error *
4795 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
4796 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
4797 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4798 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4800 0ebf8283 2019-07-24 stsp const struct got_error *err;
4801 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4802 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4803 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4804 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4805 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4806 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
4808 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4809 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4810 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4811 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4813 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4815 3e3a69f1 2019-07-25 stsp return err;
4817 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4819 f032f1f7 2019-08-04 stsp goto done;
4821 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4822 f032f1f7 2019-08-04 stsp &have_staged_files);
4823 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
4824 f032f1f7 2019-08-04 stsp goto done;
4825 f032f1f7 2019-08-04 stsp if (have_staged_files) {
4826 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
4827 3e3a69f1 2019-07-25 stsp goto done;
4830 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4832 f032f1f7 2019-08-04 stsp goto done;
4834 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4836 0ebf8283 2019-07-24 stsp goto done;
4838 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4840 0ebf8283 2019-07-24 stsp goto done;
4842 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4843 0ebf8283 2019-07-24 stsp worktree);
4845 0ebf8283 2019-07-24 stsp goto done;
4847 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4849 0ebf8283 2019-07-24 stsp goto done;
4851 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4853 0ebf8283 2019-07-24 stsp goto done;
4854 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4856 0ebf8283 2019-07-24 stsp goto done;
4858 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4860 0ebf8283 2019-07-24 stsp goto done;
4861 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4863 0ebf8283 2019-07-24 stsp goto done;
4865 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4867 0ebf8283 2019-07-24 stsp goto done;
4869 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4870 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4871 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4872 0ebf8283 2019-07-24 stsp if (commit_ref)
4873 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4874 0ebf8283 2019-07-24 stsp if (base_commit_ref)
4875 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
4876 0ebf8283 2019-07-24 stsp if (err) {
4877 0ebf8283 2019-07-24 stsp free(*commit_id);
4878 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4879 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4880 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4881 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4882 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4883 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4885 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4886 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4887 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4889 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
4891 0ebf8283 2019-07-24 stsp return err;
4894 0ebf8283 2019-07-24 stsp static const struct got_error *
4895 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4897 0ebf8283 2019-07-24 stsp const struct got_error *err;
4898 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4899 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4901 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4903 0ebf8283 2019-07-24 stsp goto done;
4904 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
4906 0ebf8283 2019-07-24 stsp goto done;
4908 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4909 0ebf8283 2019-07-24 stsp worktree);
4911 0ebf8283 2019-07-24 stsp goto done;
4912 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
4914 0ebf8283 2019-07-24 stsp goto done;
4916 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4918 0ebf8283 2019-07-24 stsp goto done;
4919 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
4921 0ebf8283 2019-07-24 stsp goto done;
4923 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4925 0ebf8283 2019-07-24 stsp goto done;
4926 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4928 0ebf8283 2019-07-24 stsp goto done;
4930 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4931 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4932 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4933 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4934 0ebf8283 2019-07-24 stsp return err;
4937 0ebf8283 2019-07-24 stsp const struct got_error *
4938 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
4939 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4940 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
4941 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4943 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
4944 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4945 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4946 0ebf8283 2019-07-24 stsp struct got_pathlist_head revertible_paths;
4947 0ebf8283 2019-07-24 stsp struct got_pathlist_entry *pe;
4948 0ebf8283 2019-07-24 stsp struct collect_revertible_paths_arg crp_arg;
4949 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
4951 0ebf8283 2019-07-24 stsp TAILQ_INIT(&revertible_paths);
4953 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4955 0ebf8283 2019-07-24 stsp return err;
4957 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4958 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
4960 0ebf8283 2019-07-24 stsp goto done;
4962 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4964 0ebf8283 2019-07-24 stsp goto done;
4966 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4968 0ebf8283 2019-07-24 stsp goto done;
4970 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4971 0ebf8283 2019-07-24 stsp worktree->path_prefix);
4973 0ebf8283 2019-07-24 stsp goto done;
4975 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4977 0ebf8283 2019-07-24 stsp goto done;
4979 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4981 0ebf8283 2019-07-24 stsp goto done;
4983 0ebf8283 2019-07-24 stsp crp_arg.revertible_paths = &revertible_paths;
4984 0ebf8283 2019-07-24 stsp crp_arg.worktree = worktree;
4985 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
4986 0ebf8283 2019-07-24 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4988 0ebf8283 2019-07-24 stsp goto done;
4990 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4991 0ebf8283 2019-07-24 stsp err = revert_file(worktree, fileindex, pe->path,
4992 0ebf8283 2019-07-24 stsp progress_cb, progress_arg, repo);
4994 0ebf8283 2019-07-24 stsp goto sync;
4997 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4998 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
5000 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5001 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
5002 0ebf8283 2019-07-24 stsp err = sync_err;
5004 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
5005 0ebf8283 2019-07-24 stsp free(tree_id);
5006 818c7501 2019-07-11 stsp free(fileindex_path);
5007 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
5008 818c7501 2019-07-11 stsp free((char *)pe->path);
5009 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
5011 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5012 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5013 818c7501 2019-07-11 stsp err = unlockerr;
5014 818c7501 2019-07-11 stsp return err;
5017 0ebf8283 2019-07-24 stsp const struct got_error *
5018 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
5019 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5020 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
5022 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
5023 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
5024 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5026 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5028 0ebf8283 2019-07-24 stsp return err;
5030 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5031 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
5033 0ebf8283 2019-07-24 stsp goto done;
5035 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
5037 0ebf8283 2019-07-24 stsp goto done;
5039 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
5041 0ebf8283 2019-07-24 stsp goto done;
5043 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5045 0ebf8283 2019-07-24 stsp goto done;
5047 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5049 3e3a69f1 2019-07-25 stsp if (fileindex)
5050 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5051 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
5052 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5053 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5054 0ebf8283 2019-07-24 stsp err = unlockerr;
5055 0ebf8283 2019-07-24 stsp return err;
5058 0ebf8283 2019-07-24 stsp const struct got_error *
5059 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5060 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5062 0ebf8283 2019-07-24 stsp const struct got_error *err;
5063 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5065 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5067 0ebf8283 2019-07-24 stsp return err;
5069 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5071 0ebf8283 2019-07-24 stsp goto done;
5073 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5075 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5076 0cb83759 2019-08-03 stsp return err;
5079 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
5080 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
5081 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5082 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5083 2db2652d 2019-08-07 stsp struct got_repository *repo;
5084 2db2652d 2019-08-07 stsp int have_changes;
5087 2db2652d 2019-08-07 stsp const struct got_error *
5088 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
5089 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5090 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5091 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5093 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
5094 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
5095 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
5096 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
5097 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
5098 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
5100 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5101 735ef5ac 2019-08-03 stsp if (ie == NULL)
5102 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5104 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5105 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5106 735ef5ac 2019-08-03 stsp relpath) == -1)
5107 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
5109 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
5110 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
5111 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5112 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
5115 3aa5969e 2019-08-06 stsp if (status == GOT_STATUS_NO_CHANGE) {
5116 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5117 3aa5969e 2019-08-06 stsp goto done;
5118 3aa5969e 2019-08-06 stsp } else if (status == GOT_STATUS_CONFLICT) {
5119 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5120 3aa5969e 2019-08-06 stsp goto done;
5121 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
5122 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
5123 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
5124 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5125 735ef5ac 2019-08-03 stsp goto done;
5128 2db2652d 2019-08-07 stsp a->have_changes = 1;
5130 735ef5ac 2019-08-03 stsp p = in_repo_path;
5131 735ef5ac 2019-08-03 stsp while (p[0] == '/')
5133 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
5134 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
5135 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
5137 735ef5ac 2019-08-03 stsp free(in_repo_path);
5138 735ef5ac 2019-08-03 stsp return err;
5141 dc424a06 2019-08-07 stsp static const struct got_error *
5142 dc424a06 2019-08-07 stsp copy_one_line(FILE *infile, FILE *outfile)
5144 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
5145 dc424a06 2019-08-07 stsp char *line = NULL;
5146 dc424a06 2019-08-07 stsp size_t linesize = 0, n;
5147 dc424a06 2019-08-07 stsp ssize_t linelen;
5149 dc424a06 2019-08-07 stsp linelen = getline(&line, &linesize, infile);
5150 dc424a06 2019-08-07 stsp if (linelen == -1) {
5151 dc424a06 2019-08-07 stsp if (ferror(infile)) {
5152 dc424a06 2019-08-07 stsp err = got_error_from_errno("getline");
5153 dc424a06 2019-08-07 stsp goto done;
5155 dc424a06 2019-08-07 stsp return NULL;
5157 dc424a06 2019-08-07 stsp n = fwrite(line, 1, linelen, outfile);
5158 dc424a06 2019-08-07 stsp if (n != linelen)
5159 dc424a06 2019-08-07 stsp err = got_ferror(outfile, GOT_ERR_IO);
5161 dc424a06 2019-08-07 stsp free(line);
5162 dc424a06 2019-08-07 stsp return err;
5165 dc424a06 2019-08-07 stsp static const struct got_error *
5166 dc424a06 2019-08-07 stsp skip_one_line(FILE *f)
5168 dc424a06 2019-08-07 stsp char *line = NULL;
5169 dc424a06 2019-08-07 stsp size_t linesize = 0;
5170 dc424a06 2019-08-07 stsp ssize_t linelen;
5172 dc424a06 2019-08-07 stsp linelen = getline(&line, &linesize, f);
5173 dc424a06 2019-08-07 stsp free(line);
5174 dc424a06 2019-08-07 stsp if (linelen == -1 && ferror(f))
5175 dc424a06 2019-08-07 stsp return got_error_from_errno("getline");
5176 dc424a06 2019-08-07 stsp return NULL;
5179 735ef5ac 2019-08-03 stsp static const struct got_error *
5180 dc424a06 2019-08-07 stsp apply_or_reject_change(int *choice, struct got_diff_change *change,
5181 dc424a06 2019-08-07 stsp struct got_diff_state *ds, struct got_diff_args *args, int diff_flags,
5182 dc424a06 2019-08-07 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
5183 dc424a06 2019-08-07 stsp FILE *outfile, got_worktree_patch_cb patch_cb, void *patch_arg)
5185 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
5186 dc424a06 2019-08-07 stsp int start_old = change->cv.a;
5187 dc424a06 2019-08-07 stsp int end_old = change->cv.b;
5188 dc424a06 2019-08-07 stsp int start_new = change->cv.c;
5189 dc424a06 2019-08-07 stsp int end_new = change->cv.d;
5190 dc424a06 2019-08-07 stsp long pos1, pos2;
5191 dc424a06 2019-08-07 stsp FILE *hunkfile;
5193 dc424a06 2019-08-07 stsp *choice = GOT_PATCH_CHOICE_NONE;
5195 dc424a06 2019-08-07 stsp hunkfile = got_opentemp();
5196 dc424a06 2019-08-07 stsp if (hunkfile == NULL)
5197 dc424a06 2019-08-07 stsp return got_error_from_errno("got_opentemp");
5199 dc424a06 2019-08-07 stsp pos1 = ftell(f1);
5200 dc424a06 2019-08-07 stsp pos2 = ftell(f2);
5202 dc424a06 2019-08-07 stsp /* XXX TODO needs error checking */
5203 dc424a06 2019-08-07 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
5205 dc424a06 2019-08-07 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
5206 dc424a06 2019-08-07 stsp err = got_ferror(f1, GOT_ERR_IO);
5207 dc424a06 2019-08-07 stsp goto done;
5209 dc424a06 2019-08-07 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
5210 dc424a06 2019-08-07 stsp err = got_ferror(f1, GOT_ERR_IO);
5211 dc424a06 2019-08-07 stsp goto done;
5213 dc424a06 2019-08-07 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
5214 dc424a06 2019-08-07 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
5215 dc424a06 2019-08-07 stsp goto done;
5218 dc424a06 2019-08-07 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
5219 dc424a06 2019-08-07 stsp hunkfile);
5221 dc424a06 2019-08-07 stsp goto done;
5223 dc424a06 2019-08-07 stsp switch (*choice) {
5224 dc424a06 2019-08-07 stsp case GOT_PATCH_CHOICE_YES:
5225 dc424a06 2019-08-07 stsp /* Copy old file's lines leading up to patch. */
5226 dc424a06 2019-08-07 stsp while (!feof(f1) && *line_cur1 < start_old) {
5227 dc424a06 2019-08-07 stsp err = copy_one_line(f1, outfile);
5229 dc424a06 2019-08-07 stsp goto done;
5230 dc424a06 2019-08-07 stsp (*line_cur1)++;
5232 dc424a06 2019-08-07 stsp /* Skip new file's lines leading up to patch. */
5233 dc424a06 2019-08-07 stsp while (!feof(f2) && *line_cur2 < start_new) {
5234 dc424a06 2019-08-07 stsp err = skip_one_line(f2);
5236 dc424a06 2019-08-07 stsp goto done;
5237 dc424a06 2019-08-07 stsp (*line_cur2)++;
5239 dc424a06 2019-08-07 stsp /* Copy patched lines. */
5240 dc424a06 2019-08-07 stsp while (!feof(f2) && *line_cur2 <= end_new) {
5241 dc424a06 2019-08-07 stsp err = copy_one_line(f2, outfile);
5243 dc424a06 2019-08-07 stsp goto done;
5244 dc424a06 2019-08-07 stsp (*line_cur2)++;
5246 dc424a06 2019-08-07 stsp /* Skip over old file's replaced lines. */
5247 dc424a06 2019-08-07 stsp while (!feof(f1) && *line_cur1 <= end_new) {
5248 dc424a06 2019-08-07 stsp err = skip_one_line(f1);
5250 dc424a06 2019-08-07 stsp goto done;
5251 dc424a06 2019-08-07 stsp (*line_cur1)++;
5253 dc424a06 2019-08-07 stsp /* Copy old file's lines after patch. */
5254 dc424a06 2019-08-07 stsp while (!feof(f1) && *line_cur1 <= end_old) {
5255 dc424a06 2019-08-07 stsp err = skip_one_line(f1);
5257 dc424a06 2019-08-07 stsp goto done;
5258 dc424a06 2019-08-07 stsp (*line_cur1)++;
5261 dc424a06 2019-08-07 stsp case GOT_PATCH_CHOICE_NO:
5262 dc424a06 2019-08-07 stsp /* Copy old file's lines. */
5263 dc424a06 2019-08-07 stsp while (!feof(f1) && *line_cur1 <= end_old) {
5264 dc424a06 2019-08-07 stsp err = copy_one_line(f1, outfile);
5266 dc424a06 2019-08-07 stsp goto done;
5267 dc424a06 2019-08-07 stsp (*line_cur1)++;
5269 dc424a06 2019-08-07 stsp /* Skip over new file's lines. */
5270 dc424a06 2019-08-07 stsp while (!feof(f2) && *line_cur2 <= end_new) {
5271 dc424a06 2019-08-07 stsp err = skip_one_line(f2);
5273 dc424a06 2019-08-07 stsp goto done;
5274 dc424a06 2019-08-07 stsp (*line_cur2)++;
5277 b353a198 2019-08-07 stsp case GOT_PATCH_CHOICE_QUIT:
5278 b353a198 2019-08-07 stsp /* Copy old file's lines until EOF. */
5279 b353a198 2019-08-07 stsp while (!feof(f1)) {
5280 b353a198 2019-08-07 stsp err = copy_one_line(f1, outfile);
5282 b353a198 2019-08-07 stsp goto done;
5283 b353a198 2019-08-07 stsp (*line_cur1)++;
5287 dc424a06 2019-08-07 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5291 dc424a06 2019-08-07 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
5292 dc424a06 2019-08-07 stsp err = got_error_from_errno("fclose");
5293 dc424a06 2019-08-07 stsp return err;
5296 dc424a06 2019-08-07 stsp static const struct got_error *
5297 dc424a06 2019-08-07 stsp create_staged_content(char **path_outfile, struct got_object_id *blob_id,
5298 dc424a06 2019-08-07 stsp const char *path2, const char *relpath, struct got_repository *repo,
5299 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
5301 dc424a06 2019-08-07 stsp const struct got_error *err;
5302 dc424a06 2019-08-07 stsp struct got_blob_object *blob = NULL;
5303 dc424a06 2019-08-07 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5304 dc424a06 2019-08-07 stsp char *path1 = NULL, *id_str = NULL;
5305 dc424a06 2019-08-07 stsp struct stat sb1, sb2;
5306 dc424a06 2019-08-07 stsp struct got_diff_changes *changes = NULL;
5307 dc424a06 2019-08-07 stsp struct got_diff_state *ds = NULL;
5308 dc424a06 2019-08-07 stsp struct got_diff_args *args = NULL;
5309 dc424a06 2019-08-07 stsp struct got_diff_change *change;
5310 dc424a06 2019-08-07 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
5312 dc424a06 2019-08-07 stsp *path_outfile = NULL;
5314 dc424a06 2019-08-07 stsp err = got_object_id_str(&id_str, blob_id);
5316 dc424a06 2019-08-07 stsp return err;
5318 dc424a06 2019-08-07 stsp f2 = fopen(path2, "r");
5319 dc424a06 2019-08-07 stsp if (f2 == NULL) {
5320 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fopen", path2);
5321 dc424a06 2019-08-07 stsp goto done;
5324 dc424a06 2019-08-07 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5326 dc424a06 2019-08-07 stsp goto done;
5328 dc424a06 2019-08-07 stsp err = got_opentemp_named(&path1, &f1, "got-stage-blob");
5330 dc424a06 2019-08-07 stsp goto done;
5332 dc424a06 2019-08-07 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5334 dc424a06 2019-08-07 stsp goto done;
5336 dc424a06 2019-08-07 stsp if (stat(path1, &sb1) == -1) {
5337 dc424a06 2019-08-07 stsp err = got_error_from_errno2("stat", path1);
5338 dc424a06 2019-08-07 stsp goto done;
5340 dc424a06 2019-08-07 stsp if (stat(path2, &sb2) == -1) {
5341 dc424a06 2019-08-07 stsp err = got_error_from_errno2("stat", path2);
5342 dc424a06 2019-08-07 stsp goto done;
5345 dc424a06 2019-08-07 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
5346 dc424a06 2019-08-07 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
5348 dc424a06 2019-08-07 stsp goto done;
5350 dc424a06 2019-08-07 stsp err = got_opentemp_named(path_outfile, &outfile, "got-stage-content");
5352 dc424a06 2019-08-07 stsp goto done;
5354 dc424a06 2019-08-07 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5355 dc424a06 2019-08-07 stsp return got_ferror(f1, GOT_ERR_IO);
5356 dc424a06 2019-08-07 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5357 dc424a06 2019-08-07 stsp return got_ferror(f2, GOT_ERR_IO);
5358 dc424a06 2019-08-07 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5359 dc424a06 2019-08-07 stsp int choice;
5360 dc424a06 2019-08-07 stsp err = apply_or_reject_change(&choice, change, ds, args,
5361 dc424a06 2019-08-07 stsp diff_flags, relpath, f1, f2, &line_cur1, &line_cur2,
5362 dc424a06 2019-08-07 stsp outfile, patch_cb, patch_arg);
5364 dc424a06 2019-08-07 stsp goto done;
5365 dc424a06 2019-08-07 stsp if (choice == GOT_PATCH_CHOICE_YES)
5366 dc424a06 2019-08-07 stsp have_content = 1;
5367 b353a198 2019-08-07 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5371 dc424a06 2019-08-07 stsp free(id_str);
5373 dc424a06 2019-08-07 stsp got_object_blob_close(blob);
5374 dc424a06 2019-08-07 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5375 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fclose", path1);
5376 dc424a06 2019-08-07 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5377 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fclose", path2);
5378 dc424a06 2019-08-07 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5379 dc424a06 2019-08-07 stsp err = got_error_from_errno2("fclose", *path_outfile);
5380 dc424a06 2019-08-07 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5381 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path1);
5382 dc424a06 2019-08-07 stsp if (err || !have_content) {
5383 dc424a06 2019-08-07 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5384 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", *path_outfile);
5385 dc424a06 2019-08-07 stsp free(*path_outfile);
5386 dc424a06 2019-08-07 stsp *path_outfile = NULL;
5388 dc424a06 2019-08-07 stsp free(args);
5390 dc424a06 2019-08-07 stsp got_diff_state_free(ds);
5393 dc424a06 2019-08-07 stsp if (changes)
5394 dc424a06 2019-08-07 stsp got_diff_free_changes(changes);
5395 dc424a06 2019-08-07 stsp free(path1);
5396 dc424a06 2019-08-07 stsp return err;
5399 2db2652d 2019-08-07 stsp struct stage_path_arg {
5400 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5401 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5402 2db2652d 2019-08-07 stsp struct got_repository *repo;
5403 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
5404 2db2652d 2019-08-07 stsp void *status_arg;
5405 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
5406 2db2652d 2019-08-07 stsp void *patch_arg;
5409 2db2652d 2019-08-07 stsp static const struct got_error *
5410 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
5411 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5412 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5413 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5415 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
5416 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
5417 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
5418 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
5419 0cb83759 2019-08-03 stsp uint32_t stage;
5421 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5422 d3e7c587 2019-08-03 stsp if (ie == NULL)
5423 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5425 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5426 2db2652d 2019-08-07 stsp relpath)== -1)
5427 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
5429 0cb83759 2019-08-03 stsp switch (status) {
5430 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
5431 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
5432 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5433 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
5434 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5435 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5436 2db2652d 2019-08-07 stsp status, ie->path, NULL);
5439 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
5442 dc424a06 2019-08-07 stsp err = create_staged_content(&path_content,
5443 2db2652d 2019-08-07 stsp blob_id, ondisk_path, ie->path, a->repo,
5444 2db2652d 2019-08-07 stsp a->patch_cb, a->patch_arg);
5445 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
5449 537ac44b 2019-08-03 stsp err = got_object_blob_create(&staged_blob_id,
5450 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
5453 537ac44b 2019-08-03 stsp memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
5454 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5455 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5456 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
5458 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
5459 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5460 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5462 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5463 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
5464 537ac44b 2019-08-03 stsp staged_blob_id, NULL);
5466 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
5467 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
5469 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5470 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5471 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
5472 dc424a06 2019-08-07 stsp ie->path, NULL);
5475 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
5478 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
5479 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5480 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5482 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5483 537ac44b 2019-08-03 stsp get_staged_status(ie), relpath, NULL, NULL, NULL);
5485 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
5486 d3e7c587 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5488 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
5489 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5492 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5496 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
5497 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
5498 dc424a06 2019-08-07 stsp free(path_content);
5499 2db2652d 2019-08-07 stsp free(ondisk_path);
5500 0ebf8283 2019-07-24 stsp return err;
5503 0cb83759 2019-08-03 stsp const struct got_error *
5504 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
5505 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
5506 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
5507 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5508 1e71573e 2019-08-03 stsp struct got_repository *repo)
5510 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5511 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
5512 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5513 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
5514 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
5515 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
5516 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
5517 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
5519 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5521 0cb83759 2019-08-03 stsp return err;
5523 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
5524 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
5526 735ef5ac 2019-08-03 stsp goto done;
5527 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5529 735ef5ac 2019-08-03 stsp goto done;
5530 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5532 0cb83759 2019-08-03 stsp goto done;
5534 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
5535 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
5536 2db2652d 2019-08-07 stsp oka.worktree = worktree;
5537 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
5538 2db2652d 2019-08-07 stsp oka.repo = repo;
5539 2db2652d 2019-08-07 stsp oka.have_changes = 0;
5540 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5541 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5542 2db2652d 2019-08-07 stsp check_stage_ok, &oka, NULL, NULL);
5544 735ef5ac 2019-08-03 stsp goto done;
5546 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
5547 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5548 2db2652d 2019-08-07 stsp goto done;
5551 2db2652d 2019-08-07 stsp spa.worktree = worktree;
5552 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
5553 2db2652d 2019-08-07 stsp spa.repo = repo;
5554 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
5555 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
5556 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
5557 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
5558 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5559 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5560 2db2652d 2019-08-07 stsp stage_path, &spa, NULL, NULL);
5562 2db2652d 2019-08-07 stsp goto done;
5565 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5566 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
5567 0cb83759 2019-08-03 stsp err = sync_err;
5569 735ef5ac 2019-08-03 stsp if (head_ref)
5570 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
5571 735ef5ac 2019-08-03 stsp free(head_commit_id);
5572 0cb83759 2019-08-03 stsp free(fileindex_path);
5573 0cb83759 2019-08-03 stsp if (fileindex)
5574 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
5575 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5576 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
5577 0cb83759 2019-08-03 stsp err = unlockerr;
5578 0cb83759 2019-08-03 stsp return err;
5581 ad493afc 2019-08-03 stsp struct unstage_path_arg {
5582 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
5583 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
5584 ad493afc 2019-08-03 stsp struct got_repository *repo;
5585 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
5586 ad493afc 2019-08-03 stsp void *progress_arg;
5589 ad493afc 2019-08-03 stsp static const struct got_error *
5590 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
5591 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
5592 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5593 ad493afc 2019-08-03 stsp struct got_object_id *commit_id)
5595 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
5596 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
5597 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
5598 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5599 ad493afc 2019-08-03 stsp char *ondisk_path = NULL;
5600 ad493afc 2019-08-03 stsp int local_changes_subsumed;
5601 9bc94a15 2019-08-03 stsp struct stat sb;
5603 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5604 ad493afc 2019-08-03 stsp if (ie == NULL)
5605 ad493afc 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5607 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5609 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
5611 ad493afc 2019-08-03 stsp switch (staged_status) {
5612 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
5613 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
5614 ad493afc 2019-08-03 stsp blob_id, 8192);
5617 ad493afc 2019-08-03 stsp /* fall through */
5618 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
5619 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
5620 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
5625 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
5626 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
5627 ad493afc 2019-08-03 stsp got_fileindex_perms_to_st(ie), blob_staged,
5628 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
5629 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
5630 ad493afc 2019-08-03 stsp if (err == NULL)
5631 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5632 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5634 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
5635 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
5636 9bc94a15 2019-08-03 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
5639 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
5643 ad493afc 2019-08-03 stsp free(ondisk_path);
5644 ad493afc 2019-08-03 stsp if (blob_base)
5645 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
5646 ad493afc 2019-08-03 stsp if (blob_staged)
5647 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
5648 ad493afc 2019-08-03 stsp return err;
5651 ad493afc 2019-08-03 stsp const struct got_error *
5652 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
5653 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
5654 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5655 ad493afc 2019-08-03 stsp struct got_repository *repo)
5657 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5658 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5659 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5660 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
5661 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
5663 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5665 ad493afc 2019-08-03 stsp return err;
5667 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5669 ad493afc 2019-08-03 stsp goto done;
5671 ad493afc 2019-08-03 stsp upa.worktree = worktree;
5672 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
5673 ad493afc 2019-08-03 stsp upa.repo = repo;
5674 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
5675 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
5676 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5677 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5678 ad493afc 2019-08-03 stsp unstage_path, &upa, NULL, NULL);
5680 ad493afc 2019-08-03 stsp goto done;
5683 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5684 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
5685 ad493afc 2019-08-03 stsp err = sync_err;
5687 ad493afc 2019-08-03 stsp free(fileindex_path);
5688 ad493afc 2019-08-03 stsp if (fileindex)
5689 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
5690 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5691 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
5692 ad493afc 2019-08-03 stsp err = unlockerr;
5693 ad493afc 2019-08-03 stsp return err;