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 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.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 e6209546 2019-08-22 stsp #include "got_cancel.h"
43 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 234035bc 2019-06-01 stsp #include "got_diff.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
50 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
53 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
54 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
55 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
56 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 99724ed4 2018-03-10 stsp static const struct got_error *
63 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
65 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
68 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
69 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
71 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
76 507dc3bb 2018-12-29 stsp static const struct got_error *
77 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
79 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
80 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
81 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
82 507dc3bb 2018-12-29 stsp char *path = NULL;
84 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
85 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
86 507dc3bb 2018-12-29 stsp path = NULL;
90 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
94 507dc3bb 2018-12-29 stsp if (content) {
95 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
96 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
97 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
102 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
103 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
104 2a57020b 2019-02-20 stsp unlink(tmppath);
109 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
110 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
111 230a42bd 2019-05-11 jcs free(tmppath);
112 99724ed4 2018-03-10 stsp return err;
115 09fe317a 2018-03-11 stsp static const struct got_error *
116 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
118 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
119 09fe317a 2018-03-11 stsp char *path;
120 09fe317a 2018-03-11 stsp int fd = -1;
122 09fe317a 2018-03-11 stsp struct stat sb;
124 09fe317a 2018-03-11 stsp *content = NULL;
126 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
127 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
128 09fe317a 2018-03-11 stsp path = NULL;
132 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
133 09fe317a 2018-03-11 stsp if (fd == -1) {
134 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
135 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
137 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
140 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
141 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
142 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
146 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
147 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
150 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
151 09fe317a 2018-03-11 stsp if (*content == NULL) {
152 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
156 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
157 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
158 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
159 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
162 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
163 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
166 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
169 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
170 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
171 09fe317a 2018-03-11 stsp free(path);
173 09fe317a 2018-03-11 stsp free(*content);
174 09fe317a 2018-03-11 stsp *content = NULL;
176 09fe317a 2018-03-11 stsp return err;
179 024e9686 2019-05-14 stsp static const struct got_error *
180 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
182 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
183 024e9686 2019-05-14 stsp char *refstr = NULL;
185 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
186 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
187 024e9686 2019-05-14 stsp if (refstr == NULL)
188 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
190 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
194 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
195 024e9686 2019-05-14 stsp free(refstr);
196 024e9686 2019-05-14 stsp return err;
199 86c3caaf 2018-03-09 stsp const struct got_error *
200 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
201 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
203 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
204 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
205 ec22038e 2019-03-10 stsp uuid_t uuid;
206 ec22038e 2019-03-10 stsp uint32_t uuid_status;
207 65596e15 2018-12-24 stsp int obj_type;
208 7ac97322 2018-03-11 stsp char *path_got = NULL;
209 1451e70d 2018-03-10 stsp char *formatstr = NULL;
210 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
211 65596e15 2018-12-24 stsp char *basestr = NULL;
212 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
214 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
219 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
221 65596e15 2018-12-24 stsp return err;
222 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
224 65596e15 2018-12-24 stsp return err;
225 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
228 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
229 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
230 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
233 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
234 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
239 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
240 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
244 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
249 056e7441 2018-03-11 stsp /* Create an empty lock file. */
250 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
260 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
264 65596e15 2018-12-24 stsp /* Record our base commit. */
265 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
268 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 1451e70d 2018-03-10 stsp /* Store path to repository. */
273 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
274 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
278 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
279 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
280 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
284 ec22038e 2019-03-10 stsp /* Generate UUID. */
285 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
286 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
287 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
290 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
295 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
300 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
301 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
304 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 65596e15 2018-12-24 stsp free(commit_id);
310 7ac97322 2018-03-11 stsp free(path_got);
311 1451e70d 2018-03-10 stsp free(formatstr);
312 0bb8a95e 2018-03-12 stsp free(absprefix);
313 65596e15 2018-12-24 stsp free(basestr);
314 ec22038e 2019-03-10 stsp free(uuidstr);
315 86c3caaf 2018-03-09 stsp return err;
318 247140b2 2019-02-05 stsp static const struct got_error *
319 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
321 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
322 7ac97322 2018-03-11 stsp char *path_got;
323 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
324 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
325 056e7441 2018-03-11 stsp char *path_lock = NULL;
326 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
327 6d9d28c3 2018-03-11 stsp int version, fd = -1;
328 6d9d28c3 2018-03-11 stsp const char *errstr;
329 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
330 c442a90d 2019-03-10 stsp uint32_t uuid_status;
332 6d9d28c3 2018-03-11 stsp *worktree = NULL;
334 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
335 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
336 7ac97322 2018-03-11 stsp path_got = NULL;
340 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
341 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
342 056e7441 2018-03-11 stsp path_lock = NULL;
346 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
347 6d9d28c3 2018-03-11 stsp if (fd == -1) {
348 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
349 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
353 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
358 6d9d28c3 2018-03-11 stsp if (errstr) {
359 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
360 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
363 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
364 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
368 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
369 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
370 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
373 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
375 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
376 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
377 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
380 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
381 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
385 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
390 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
391 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
395 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
398 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
399 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
400 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
404 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
408 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
409 eaccb85f 2018-12-25 stsp base_commit_id_str);
413 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
414 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
417 eaccb85f 2018-12-25 stsp got_repo_close(repo);
418 7ac97322 2018-03-11 stsp free(path_got);
419 056e7441 2018-03-11 stsp free(path_lock);
420 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
421 c442a90d 2019-03-10 stsp free(uuidstr);
422 bd165944 2019-03-10 stsp free(formatstr);
424 6d9d28c3 2018-03-11 stsp if (fd != -1)
426 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
427 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
428 6d9d28c3 2018-03-11 stsp *worktree = NULL;
430 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
432 6d9d28c3 2018-03-11 stsp return err;
435 247140b2 2019-02-05 stsp const struct got_error *
436 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
438 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
441 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
442 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
443 247140b2 2019-02-05 stsp return err;
444 247140b2 2019-02-05 stsp if (*worktree)
445 247140b2 2019-02-05 stsp return NULL;
446 247140b2 2019-02-05 stsp path = dirname(path);
447 d1542a27 2019-02-05 stsp if (path == NULL)
448 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
449 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
451 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
454 3a6ce05a 2019-02-11 stsp const struct got_error *
455 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
457 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
458 c88eb298 2018-03-11 stsp free(worktree->root_path);
459 cde76477 2018-03-11 stsp free(worktree->repo_path);
460 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
461 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
462 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
463 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
464 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
465 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
466 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
467 6d9d28c3 2018-03-11 stsp free(worktree);
468 3a6ce05a 2019-02-11 stsp return err;
471 2fbdb5ae 2018-12-29 stsp const char *
472 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
474 c7f4312f 2019-02-05 stsp return worktree->root_path;
477 c7f4312f 2019-02-05 stsp const char *
478 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
480 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
483 49520a32 2018-12-29 stsp const char *
484 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
486 f609be2e 2018-12-29 stsp return worktree->path_prefix;
489 e5dc7198 2018-12-29 stsp const struct got_error *
490 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
491 e5dc7198 2018-12-29 stsp const char *path_prefix)
493 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
495 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
496 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
497 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
499 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
500 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
501 e5dc7198 2018-12-29 stsp free(absprefix);
502 e5dc7198 2018-12-29 stsp return NULL;
505 bc70eb79 2019-05-09 stsp const char *
506 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
508 36a38700 2019-05-10 stsp return worktree->head_ref_name;
511 024e9686 2019-05-14 stsp const struct got_error *
512 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
513 024e9686 2019-05-14 stsp struct got_reference *head_ref)
515 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
516 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
518 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
519 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
520 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
521 024e9686 2019-05-14 stsp path_got = NULL;
525 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
526 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
527 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
531 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
535 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
536 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
538 024e9686 2019-05-14 stsp free(path_got);
540 024e9686 2019-05-14 stsp free(head_ref_name);
541 024e9686 2019-05-14 stsp return err;
544 b72f483a 2019-02-05 stsp struct got_object_id *
545 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
547 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
550 507dc3bb 2018-12-29 stsp const struct got_error *
551 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
552 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
554 507dc3bb 2018-12-29 stsp const struct got_error *err;
555 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
556 507dc3bb 2018-12-29 stsp char *id_str = NULL;
557 507dc3bb 2018-12-29 stsp char *path_got = NULL;
559 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
560 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
561 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
562 507dc3bb 2018-12-29 stsp path_got = NULL;
566 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
568 507dc3bb 2018-12-29 stsp return err;
570 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
571 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
575 507dc3bb 2018-12-29 stsp /* Record our base commit. */
576 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
579 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
583 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
584 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
585 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
586 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
591 507dc3bb 2018-12-29 stsp got_object_close(obj);
592 507dc3bb 2018-12-29 stsp free(id_str);
593 507dc3bb 2018-12-29 stsp free(path_got);
594 507dc3bb 2018-12-29 stsp return err;
597 9d31a1d8 2018-03-11 stsp static const struct got_error *
598 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
600 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
601 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
602 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
603 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
604 86c3caaf 2018-03-09 stsp return NULL;
607 21908da4 2019-01-13 stsp static const struct got_error *
608 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
610 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
611 4a1ddfc2 2019-01-12 stsp char *abspath;
613 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
614 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
616 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
617 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
618 ddcd8544 2019-03-11 stsp struct stat sb;
619 ddcd8544 2019-03-11 stsp err = NULL;
620 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
621 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
622 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
623 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
624 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
627 4a1ddfc2 2019-01-12 stsp free(abspath);
628 68c76935 2019-02-19 stsp return err;
631 68c76935 2019-02-19 stsp static const struct got_error *
632 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
634 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
635 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
636 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
637 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
642 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
643 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
644 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
647 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
648 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
649 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
652 68c76935 2019-02-19 stsp if (flen1 == 0) {
653 68c76935 2019-02-19 stsp if (flen2 != 0)
656 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
657 68c76935 2019-02-19 stsp if (flen1 != 0)
660 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
661 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
671 68c76935 2019-02-19 stsp return err;
674 68c76935 2019-02-19 stsp static const struct got_error *
675 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
677 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
678 68c76935 2019-02-19 stsp struct stat sb;
679 68c76935 2019-02-19 stsp size_t size1, size2;
680 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
684 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
685 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
688 68c76935 2019-02-19 stsp size1 = sb.st_size;
690 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
691 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
694 68c76935 2019-02-19 stsp size2 = sb.st_size;
696 68c76935 2019-02-19 stsp if (size1 != size2) {
698 68c76935 2019-02-19 stsp return NULL;
701 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
702 68c76935 2019-02-19 stsp if (f1 == NULL)
703 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
705 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
706 68c76935 2019-02-19 stsp if (f2 == NULL) {
707 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
711 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
713 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
715 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
716 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
718 6353ad76 2019-02-08 stsp return err;
722 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
723 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
724 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
726 6353ad76 2019-02-08 stsp static const struct got_error *
727 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
728 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
729 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
730 14c901f1 2019-08-08 stsp const char *label_deriv, struct got_repository *repo,
731 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, 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 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
736 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
737 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
738 234035bc 2019-06-01 stsp int overlapcnt = 0;
739 af54ae4a 2019-02-19 stsp char *parent;
741 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
743 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
744 af54ae4a 2019-02-19 stsp if (parent == NULL)
745 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
747 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
750 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
754 af54ae4a 2019-02-19 stsp free(base_path);
755 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
756 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
757 af54ae4a 2019-02-19 stsp base_path = NULL;
761 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
764 46b6ee73 2019-06-04 stsp if (blob_orig) {
765 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
766 46b6ee73 2019-06-04 stsp blob_orig);
771 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
772 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
773 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
777 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
778 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
782 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
783 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
787 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
788 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
792 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
793 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
794 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
795 68c76935 2019-02-19 stsp merged_path);
800 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
801 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
805 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
806 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
807 230a42bd 2019-05-11 jcs ondisk_path);
808 2a57020b 2019-02-20 stsp unlink(merged_path);
813 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
814 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
815 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
816 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
817 6353ad76 2019-02-08 stsp free(merged_path);
818 af54ae4a 2019-02-19 stsp free(base_path);
819 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
820 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
821 46b6ee73 2019-06-04 stsp free(blob_orig_path);
823 14c901f1 2019-08-08 stsp return err;
827 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
828 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
829 14c901f1 2019-08-08 stsp * acts as the second derived version.
831 14c901f1 2019-08-08 stsp static const struct got_error *
832 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
833 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
834 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
835 14c901f1 2019-08-08 stsp struct got_object_id *deriv_base_commit_id,
836 14c901f1 2019-08-08 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
837 14c901f1 2019-08-08 stsp void *progress_arg)
839 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
840 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
841 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
842 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
844 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
846 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
847 14c901f1 2019-08-08 stsp if (parent == NULL)
848 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
850 14c901f1 2019-08-08 stsp free(base_path);
851 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
852 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
853 14c901f1 2019-08-08 stsp base_path = NULL;
857 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
860 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
861 14c901f1 2019-08-08 stsp blob_deriv);
865 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
868 14c901f1 2019-08-08 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
869 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
873 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
874 14c901f1 2019-08-08 stsp ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
875 14c901f1 2019-08-08 stsp repo, progress_cb, progress_arg);
877 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
878 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
879 14c901f1 2019-08-08 stsp free(base_path);
880 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
881 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
882 14c901f1 2019-08-08 stsp free(blob_deriv_path);
884 6353ad76 2019-02-08 stsp free(id_str);
885 818c7501 2019-07-11 stsp free(label_deriv);
886 4a1ddfc2 2019-01-12 stsp return err;
889 4a1ddfc2 2019-01-12 stsp static const struct got_error *
890 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
891 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
892 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
893 13d9040b 2019-03-27 stsp int update_timestamps)
895 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
897 13d9040b 2019-03-27 stsp if (ie == NULL)
898 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
900 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
901 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
902 13d9040b 2019-03-27 stsp update_timestamps);
904 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
905 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
906 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
908 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
910 13d9040b 2019-03-27 stsp return err;
913 13d9040b 2019-03-27 stsp static const struct got_error *
914 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
915 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
916 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
917 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
918 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
920 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
921 507dc3bb 2018-12-29 stsp int fd = -1;
922 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
923 507dc3bb 2018-12-29 stsp int update = 0;
924 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
926 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
927 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
928 9d31a1d8 2018-03-11 stsp if (fd == -1) {
929 21908da4 2019-01-13 stsp if (errno == ENOENT) {
930 21908da4 2019-01-13 stsp char *parent = dirname(path);
931 21908da4 2019-01-13 stsp if (parent == NULL)
932 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
933 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
935 21908da4 2019-01-13 stsp return err;
936 21908da4 2019-01-13 stsp fd = open(ondisk_path,
937 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
938 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
939 21908da4 2019-01-13 stsp if (fd == -1)
940 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
941 230a42bd 2019-05-11 jcs ondisk_path);
942 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
943 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
944 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
945 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
948 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
949 507dc3bb 2018-12-29 stsp ondisk_path);
952 507dc3bb 2018-12-29 stsp update = 1;
955 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
958 a378724f 2019-02-10 stsp if (restoring_missing_file)
959 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
960 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
961 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
963 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
964 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
968 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
970 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
971 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
974 9d31a1d8 2018-03-11 stsp if (len > 0) {
975 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
976 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
977 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
978 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
980 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
981 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
984 61d6eaa3 2018-12-24 stsp hdrlen = 0;
986 9d31a1d8 2018-03-11 stsp } while (len != 0);
988 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
989 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
993 507dc3bb 2018-12-29 stsp if (update) {
994 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
995 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
996 230a42bd 2019-05-11 jcs ondisk_path);
997 2a57020b 2019-02-20 stsp unlink(tmppath);
1002 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
1003 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1004 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1005 507dc3bb 2018-12-29 stsp goto done;
1008 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1009 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1010 68ed9ba5 2019-02-10 stsp goto done;
1015 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1016 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1017 507dc3bb 2018-12-29 stsp free(tmppath);
1018 6353ad76 2019-02-08 stsp return err;
1021 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1022 6353ad76 2019-02-08 stsp static const struct got_error *
1023 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1025 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1026 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1027 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1028 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1029 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1031 7154f6ce 2019-03-27 stsp int i = 0;
1032 7154f6ce 2019-03-27 stsp char *line;
1033 7154f6ce 2019-03-27 stsp size_t len;
1034 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1036 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1037 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1038 7154f6ce 2019-03-27 stsp if (line == NULL) {
1039 7154f6ce 2019-03-27 stsp if (feof(f))
1041 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1045 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1046 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1048 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1054 7154f6ce 2019-03-27 stsp return err;
1057 e2b1e152 2019-07-13 stsp static int
1058 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1060 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1061 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1062 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1063 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1064 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1067 c363b2c1 2019-08-03 stsp static unsigned char
1068 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1070 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1071 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1072 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1073 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1074 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1075 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1076 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1078 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1082 7154f6ce 2019-03-27 stsp static const struct got_error *
1083 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1084 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1085 b8f41171 2019-02-10 stsp struct got_repository *repo)
1087 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1088 6353ad76 2019-02-08 stsp struct got_object_id id;
1089 6353ad76 2019-02-08 stsp size_t hdrlen;
1090 6353ad76 2019-02-08 stsp FILE *f = NULL;
1091 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1092 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1093 6353ad76 2019-02-08 stsp size_t flen, blen;
1094 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1096 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1098 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1099 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1100 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1101 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1103 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1104 a378724f 2019-02-10 stsp return NULL;
1106 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1109 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1110 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1111 339c298e 2019-07-27 stsp return NULL;
1114 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1115 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1116 339c298e 2019-07-27 stsp return NULL;
1117 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1118 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1119 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1120 339c298e 2019-07-27 stsp return NULL;
1123 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1124 339c298e 2019-07-27 stsp return NULL;
1126 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1127 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1128 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1130 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1132 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1134 339c298e 2019-07-27 stsp return err;
1136 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1137 6353ad76 2019-02-08 stsp if (f == NULL) {
1138 339c298e 2019-07-27 stsp err = got_error_from_errno2("fopen", abspath);
1139 6353ad76 2019-02-08 stsp goto done;
1141 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1143 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1144 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1146 7154f6ce 2019-03-27 stsp goto done;
1147 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1148 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1149 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1150 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1151 7154f6ce 2019-03-27 stsp goto done;
1153 6353ad76 2019-02-08 stsp if (blen == 0) {
1154 6353ad76 2019-02-08 stsp if (flen != 0)
1155 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1157 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1158 6353ad76 2019-02-08 stsp if (blen != 0)
1159 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1161 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1162 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1163 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1164 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1168 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1171 6353ad76 2019-02-08 stsp hdrlen = 0;
1174 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1175 7154f6ce 2019-03-27 stsp rewind(f);
1176 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1180 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1182 6353ad76 2019-02-08 stsp fclose(f);
1183 9d31a1d8 2018-03-11 stsp return err;
1187 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1188 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1190 e2b1e152 2019-07-13 stsp static const struct got_error *
1191 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1192 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1194 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1195 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1196 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1198 e2b1e152 2019-07-13 stsp return NULL;
1201 9d31a1d8 2018-03-11 stsp static const struct got_error *
1202 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1203 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1204 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1205 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1206 0584f854 2019-04-06 stsp void *progress_arg)
1208 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1209 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1210 6353ad76 2019-02-08 stsp char *ondisk_path;
1211 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1212 b8f41171 2019-02-10 stsp struct stat sb;
1214 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1215 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1218 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1219 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1220 a76c42e6 2019-08-03 stsp goto done;
1222 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1224 abb4604f 2019-07-27 stsp goto done;
1225 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1226 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1228 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1230 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1231 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1232 b8f41171 2019-02-10 stsp goto done;
1235 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1236 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1237 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1238 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1239 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1241 e2b1e152 2019-07-13 stsp goto done;
1242 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1244 6353ad76 2019-02-08 stsp goto done;
1246 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1247 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1248 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1249 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1250 b8f41171 2019-02-10 stsp goto done;
1254 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1256 6353ad76 2019-02-08 stsp goto done;
1258 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1259 234035bc 2019-06-01 stsp int update_timestamps;
1260 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1261 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1262 234035bc 2019-06-01 stsp struct got_object_id id2;
1263 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1264 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1266 234035bc 2019-06-01 stsp goto done;
1268 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1269 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1270 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1271 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1272 234035bc 2019-06-01 stsp if (blob2)
1273 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1275 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1276 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1277 234035bc 2019-06-01 stsp * unmodified files again.
1279 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1280 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1281 234035bc 2019-06-01 stsp update_timestamps);
1282 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1283 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1285 1ee397ad 2019-07-12 stsp goto done;
1286 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1287 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1289 13d9040b 2019-03-27 stsp goto done;
1291 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1292 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1293 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1295 13d9040b 2019-03-27 stsp goto done;
1296 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1297 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1299 13d9040b 2019-03-27 stsp goto done;
1301 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1303 6353ad76 2019-02-08 stsp free(ondisk_path);
1304 8da9e5f4 2019-01-12 stsp return err;
1307 90285c3b 2019-01-08 stsp static const struct got_error *
1308 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1310 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1311 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1313 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1314 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1316 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1317 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1318 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1320 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1321 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1322 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1323 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1324 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1328 90285c3b 2019-01-08 stsp parent = dirname(parent);
1331 90285c3b 2019-01-08 stsp free(ondisk_path);
1332 90285c3b 2019-01-08 stsp return err;
1335 708d8e67 2019-03-27 stsp static const struct got_error *
1336 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1337 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1338 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1340 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1341 708d8e67 2019-03-27 stsp unsigned char status;
1342 708d8e67 2019-03-27 stsp struct stat sb;
1343 708d8e67 2019-03-27 stsp char *ondisk_path;
1345 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1346 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1348 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1350 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1352 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1354 708d8e67 2019-03-27 stsp return err;
1356 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1357 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1358 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1360 1ee397ad 2019-07-12 stsp return err;
1362 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1363 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1365 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1368 708d8e67 2019-03-27 stsp return err;
1370 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1372 1ee397ad 2019-07-12 stsp return err;
1373 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1374 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1376 fc6346c4 2019-03-27 stsp return err;
1378 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1381 fc6346c4 2019-03-27 stsp return err;
1384 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1385 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1386 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1387 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1388 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1389 8da9e5f4 2019-01-12 stsp void *progress_arg;
1390 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
1391 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1394 512f0d0e 2019-01-02 stsp static const struct got_error *
1395 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1396 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1398 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1400 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1401 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1403 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1404 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1407 8da9e5f4 2019-01-12 stsp static const struct got_error *
1408 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1410 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1412 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1413 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1415 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1416 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1419 9d31a1d8 2018-03-11 stsp static const struct got_error *
1420 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1422 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1423 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1424 8da9e5f4 2019-01-12 stsp char *path;
1426 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1427 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1429 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1430 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1432 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1434 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1435 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1437 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1438 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1440 8da9e5f4 2019-01-12 stsp free(path);
1441 0cd1c46a 2019-03-11 stsp return err;
1444 818c7501 2019-07-11 stsp static const struct got_error *
1445 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1447 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1448 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1449 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1451 517bab73 2019-03-11 stsp *refname = NULL;
1453 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1454 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1455 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1457 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1459 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1460 0647c563 2019-03-11 stsp *refname = NULL;
1462 517bab73 2019-03-11 stsp free(uuidstr);
1463 517bab73 2019-03-11 stsp return err;
1466 818c7501 2019-07-11 stsp const struct got_error *
1467 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1469 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1472 818c7501 2019-07-11 stsp static const struct got_error *
1473 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1475 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1476 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1479 818c7501 2019-07-11 stsp static const struct got_error *
1480 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1482 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1485 818c7501 2019-07-11 stsp static const struct got_error *
1486 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1488 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1489 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1492 818c7501 2019-07-11 stsp static const struct got_error *
1493 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1495 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1496 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1499 0ebf8283 2019-07-24 stsp static const struct got_error *
1500 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1502 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1503 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1506 0ebf8283 2019-07-24 stsp static const struct got_error *
1507 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1509 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1510 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1513 0ebf8283 2019-07-24 stsp static const struct got_error *
1514 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1516 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1517 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1520 0ebf8283 2019-07-24 stsp static const struct got_error *
1521 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1523 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1524 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1527 0ebf8283 2019-07-24 stsp const struct got_error *
1528 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1529 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1531 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1532 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1533 0ebf8283 2019-07-24 stsp *path = NULL;
1534 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1536 0ebf8283 2019-07-24 stsp return NULL;
1540 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1541 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1543 517bab73 2019-03-11 stsp static const struct got_error *
1544 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1546 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1547 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1548 517bab73 2019-03-11 stsp char *refname;
1550 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1552 517bab73 2019-03-11 stsp return err;
1554 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1556 0cd1c46a 2019-03-11 stsp goto done;
1558 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1560 0cd1c46a 2019-03-11 stsp free(refname);
1562 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1563 3e3a69f1 2019-07-25 stsp return err;
1566 3e3a69f1 2019-07-25 stsp static const struct got_error *
1567 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1569 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1571 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1572 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1573 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1574 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1576 9d31a1d8 2018-03-11 stsp return err;
1580 ebf99748 2019-05-09 stsp static const struct got_error *
1581 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1582 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1584 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1585 ebf99748 2019-05-09 stsp FILE *index = NULL;
1587 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1588 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1589 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1590 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1592 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1594 ebf99748 2019-05-09 stsp goto done;
1596 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1597 ebf99748 2019-05-09 stsp if (index == NULL) {
1598 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1599 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1601 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1602 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1603 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1606 ebf99748 2019-05-09 stsp if (err) {
1607 ebf99748 2019-05-09 stsp free(*fileindex_path);
1608 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1609 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1610 ebf99748 2019-05-09 stsp *fileindex = NULL;
1612 ebf99748 2019-05-09 stsp return err;
1615 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1616 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1617 c932eeeb 2019-05-22 stsp const char *path;
1618 c932eeeb 2019-05-22 stsp size_t path_len;
1619 c932eeeb 2019-05-22 stsp const char *entry_name;
1620 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1621 a484d721 2019-06-10 stsp void *progress_arg;
1624 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1625 c932eeeb 2019-05-22 stsp static const struct got_error *
1626 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1628 1ee397ad 2019-07-12 stsp const struct got_error *err;
1629 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1631 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1632 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1633 c932eeeb 2019-05-22 stsp return NULL;
1634 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1635 102ff934 2019-06-11 stsp return NULL;
1637 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1638 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1639 c932eeeb 2019-05-22 stsp return NULL;
1641 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1642 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1643 edd02c5e 2019-07-11 stsp ie->path);
1645 1ee397ad 2019-07-12 stsp return err;
1647 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1648 c932eeeb 2019-05-22 stsp return NULL;
1651 9c6338c4 2019-06-02 stsp static const struct got_error *
1652 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1654 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1655 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1656 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1658 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1659 9c6338c4 2019-06-02 stsp fileindex_path);
1661 9c6338c4 2019-06-02 stsp goto done;
1663 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1665 9c6338c4 2019-06-02 stsp goto done;
1667 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1668 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1669 9c6338c4 2019-06-02 stsp fileindex_path);
1670 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1673 9c6338c4 2019-06-02 stsp if (new_index)
1674 9c6338c4 2019-06-02 stsp fclose(new_index);
1675 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1676 9c6338c4 2019-06-02 stsp return err;
1679 6ced4176 2019-07-12 stsp static const struct got_error *
1680 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1681 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1682 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1684 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1685 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1686 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1687 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1689 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1690 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1691 6ced4176 2019-07-12 stsp *tree_id = NULL;
1693 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1694 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1695 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1696 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1697 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1698 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1699 6ced4176 2019-07-12 stsp goto done;
1701 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1702 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1704 6ced4176 2019-07-12 stsp goto done;
1705 6ced4176 2019-07-12 stsp return NULL;
1708 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1710 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1711 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1712 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1713 6ced4176 2019-07-12 stsp goto done;
1716 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1717 6ced4176 2019-07-12 stsp in_repo_path);
1719 6ced4176 2019-07-12 stsp goto done;
1721 6ced4176 2019-07-12 stsp free(in_repo_path);
1722 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1724 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1726 6ced4176 2019-07-12 stsp goto done;
1728 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1729 6ced4176 2019-07-12 stsp /* Check out a single file. */
1730 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1731 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1732 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1733 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1734 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1735 6ced4176 2019-07-12 stsp goto done;
1737 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1738 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1739 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1740 6ced4176 2019-07-12 stsp goto done;
1743 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1744 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1746 6ced4176 2019-07-12 stsp return err;
1747 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1748 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1749 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1750 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1751 6ced4176 2019-07-12 stsp goto done;
1754 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1755 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1757 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1758 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1759 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1760 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1761 6ced4176 2019-07-12 stsp goto done;
1763 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1764 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1765 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1766 6ced4176 2019-07-12 stsp goto done;
1771 6ced4176 2019-07-12 stsp free(in_repo_path);
1772 6ced4176 2019-07-12 stsp if (err) {
1773 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1774 6ced4176 2019-07-12 stsp free(*tree_relpath);
1775 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1776 6ced4176 2019-07-12 stsp free(*tree_id);
1777 6ced4176 2019-07-12 stsp *tree_id = NULL;
1779 07ed472d 2019-07-12 stsp return err;
1782 07ed472d 2019-07-12 stsp static const struct got_error *
1783 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1784 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1785 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1786 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1788 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1789 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1790 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1791 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1792 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1794 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1796 07ed472d 2019-07-12 stsp goto done;
1798 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1799 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1801 07ed472d 2019-07-12 stsp goto done;
1803 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1805 07ed472d 2019-07-12 stsp goto done;
1807 07ed472d 2019-07-12 stsp if (entry_name &&
1808 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1809 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1810 07ed472d 2019-07-12 stsp goto done;
1813 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1814 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1815 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1816 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1817 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1818 07ed472d 2019-07-12 stsp arg.repo = repo;
1819 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1820 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1821 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1822 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1823 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1824 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1827 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1828 07ed472d 2019-07-12 stsp if (commit)
1829 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1830 6ced4176 2019-07-12 stsp return err;
1833 9d31a1d8 2018-03-11 stsp const struct got_error *
1834 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1835 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1836 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1837 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1839 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1840 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1841 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1842 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1843 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1844 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1845 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1846 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1847 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1848 f2ea84fa 2019-07-27 stsp int entry_type;
1849 f2ea84fa 2019-07-27 stsp char *relpath;
1850 f2ea84fa 2019-07-27 stsp char *entry_name;
1851 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1852 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1854 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1856 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1858 9d31a1d8 2018-03-11 stsp return err;
1860 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1861 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1862 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1863 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1864 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1865 f2ea84fa 2019-07-27 stsp goto done;
1868 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1869 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1870 f2ea84fa 2019-07-27 stsp if (err) {
1871 f2ea84fa 2019-07-27 stsp free(tpd);
1872 6ced4176 2019-07-12 stsp goto done;
1875 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1876 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1877 f2ea84fa 2019-07-27 stsp if (err) {
1878 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1879 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1880 f2ea84fa 2019-07-27 stsp free(tpd);
1881 f2ea84fa 2019-07-27 stsp goto done;
1884 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1886 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1890 51514078 2018-12-25 stsp * Read the file index.
1891 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1892 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1894 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1896 c4cdcb68 2019-04-03 stsp goto done;
1898 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1899 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1900 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1902 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1903 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1904 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1908 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1909 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1910 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1911 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
1912 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1913 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1914 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1915 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1919 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1921 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1922 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1923 af12c6b9 2019-06-04 stsp err = sync_err;
1925 9c6338c4 2019-06-02 stsp free(fileindex_path);
1927 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1928 9d31a1d8 2018-03-11 stsp if (commit)
1929 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1930 5ade8233 2019-07-12 stsp if (fileindex)
1931 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1932 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1933 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1934 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1935 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1936 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1937 f2ea84fa 2019-07-27 stsp free(tpd);
1939 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1940 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1941 9d31a1d8 2018-03-11 stsp err = unlockerr;
1942 f8d1f275 2019-02-04 stsp return err;
1945 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1946 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1947 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1948 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1949 234035bc 2019-06-01 stsp void *progress_arg;
1950 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
1951 234035bc 2019-06-01 stsp void *cancel_arg;
1952 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1955 234035bc 2019-06-01 stsp static const struct got_error *
1956 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1957 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1958 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1959 234035bc 2019-06-01 stsp struct got_repository *repo)
1961 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1962 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1963 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1964 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1965 234035bc 2019-06-01 stsp struct stat sb;
1966 234035bc 2019-06-01 stsp unsigned char status;
1967 234035bc 2019-06-01 stsp int local_changes_subsumed;
1969 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1970 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1971 d6c87207 2019-08-02 stsp strlen(path2));
1972 1ee397ad 2019-07-12 stsp if (ie == NULL)
1973 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1974 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1976 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1977 234035bc 2019-06-01 stsp path2) == -1)
1978 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1980 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1982 234035bc 2019-06-01 stsp goto done;
1984 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1985 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1986 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1987 234035bc 2019-06-01 stsp goto done;
1989 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1990 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1991 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1992 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1993 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1994 234035bc 2019-06-01 stsp goto done;
1997 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1998 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1999 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2000 234035bc 2019-06-01 stsp } else if (blob1) {
2001 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2002 d6c87207 2019-08-02 stsp strlen(path1));
2003 1ee397ad 2019-07-12 stsp if (ie == NULL)
2004 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2005 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2007 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2008 2b92fad7 2019-06-02 stsp path1) == -1)
2009 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2011 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2013 2b92fad7 2019-06-02 stsp goto done;
2015 2b92fad7 2019-06-02 stsp switch (status) {
2016 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2017 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2018 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2020 1ee397ad 2019-07-12 stsp goto done;
2021 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2023 2b92fad7 2019-06-02 stsp goto done;
2025 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2027 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2028 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2029 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2030 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2032 1ee397ad 2019-07-12 stsp goto done;
2034 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2036 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2037 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2038 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2039 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2040 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2042 1ee397ad 2019-07-12 stsp goto done;
2044 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2045 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2047 1ee397ad 2019-07-12 stsp goto done;
2052 234035bc 2019-06-01 stsp } else if (blob2) {
2053 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2054 234035bc 2019-06-01 stsp path2) == -1)
2055 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2056 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2057 d6c87207 2019-08-02 stsp strlen(path2));
2059 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2062 234035bc 2019-06-01 stsp goto done;
2063 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2064 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2065 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2066 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2067 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2068 1ee397ad 2019-07-12 stsp status, path2);
2069 234035bc 2019-06-01 stsp goto done;
2071 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2072 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2073 818c7501 2019-07-11 stsp a->commit_id2, repo,
2074 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2075 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2076 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2077 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2078 234035bc 2019-06-01 stsp blob2, 0);
2080 234035bc 2019-06-01 stsp goto done;
2083 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2084 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2085 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2086 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2087 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2088 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2090 234035bc 2019-06-01 stsp goto done;
2091 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2092 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2094 234035bc 2019-06-01 stsp goto done;
2095 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2096 2b92fad7 2019-06-02 stsp if (err) {
2097 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2098 2b92fad7 2019-06-02 stsp goto done;
2103 234035bc 2019-06-01 stsp free(ondisk_path);
2104 234035bc 2019-06-01 stsp return err;
2107 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2108 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2109 234035bc 2019-06-01 stsp struct got_repository *repo;
2112 234035bc 2019-06-01 stsp static const struct got_error *
2113 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2115 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2116 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2117 234035bc 2019-06-01 stsp unsigned char status;
2118 234035bc 2019-06-01 stsp struct stat sb;
2119 234035bc 2019-06-01 stsp char *ondisk_path;
2121 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2122 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2123 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2124 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2126 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2128 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2130 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2131 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2133 234035bc 2019-06-01 stsp return err;
2134 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2135 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2137 234035bc 2019-06-01 stsp return NULL;
2140 818c7501 2019-07-11 stsp static const struct got_error *
2141 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2142 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2143 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2144 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2145 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2147 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2148 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2149 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2150 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2152 03415a1a 2019-06-02 stsp if (commit_id1) {
2153 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2154 03415a1a 2019-06-02 stsp worktree->path_prefix);
2156 03415a1a 2019-06-02 stsp goto done;
2158 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2160 03415a1a 2019-06-02 stsp goto done;
2163 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2164 234035bc 2019-06-01 stsp worktree->path_prefix);
2166 234035bc 2019-06-01 stsp goto done;
2168 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2170 234035bc 2019-06-01 stsp goto done;
2172 234035bc 2019-06-01 stsp arg.worktree = worktree;
2173 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2174 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2175 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2176 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2177 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2178 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2179 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2180 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2181 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2182 af12c6b9 2019-06-04 stsp err = sync_err;
2184 234035bc 2019-06-01 stsp if (tree1)
2185 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2186 234035bc 2019-06-01 stsp if (tree2)
2187 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2188 818c7501 2019-07-11 stsp return err;
2191 818c7501 2019-07-11 stsp const struct got_error *
2192 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2193 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2194 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2195 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2197 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2198 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2199 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2200 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2202 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2204 818c7501 2019-07-11 stsp return err;
2206 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2208 818c7501 2019-07-11 stsp goto done;
2210 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2211 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2212 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2213 818c7501 2019-07-11 stsp &mok_arg);
2215 818c7501 2019-07-11 stsp goto done;
2217 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2218 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2220 818c7501 2019-07-11 stsp if (fileindex)
2221 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2222 818c7501 2019-07-11 stsp free(fileindex_path);
2223 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2224 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2225 234035bc 2019-06-01 stsp err = unlockerr;
2226 234035bc 2019-06-01 stsp return err;
2229 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2230 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2231 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2232 927df6b7 2019-02-10 stsp const char *status_path;
2233 927df6b7 2019-02-10 stsp size_t status_path_len;
2234 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2235 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2236 f8d1f275 2019-02-04 stsp void *status_arg;
2237 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2238 f8d1f275 2019-02-04 stsp void *cancel_arg;
2239 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2240 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2243 f8d1f275 2019-02-04 stsp static const struct got_error *
2244 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2245 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2246 927df6b7 2019-02-10 stsp struct got_repository *repo)
2248 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2249 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2250 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2251 927df6b7 2019-02-10 stsp struct stat sb;
2252 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2253 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2254 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2256 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2258 98eaaa12 2019-08-03 stsp return err;
2260 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2261 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_NO_CHANGE)
2262 98eaaa12 2019-08-03 stsp return NULL;
2264 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2265 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2266 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2268 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2269 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2270 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2272 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2273 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2274 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2275 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2276 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2279 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2280 98eaaa12 2019-08-03 stsp ie->path, blob_idp, staged_blob_idp, commit_idp);
2283 927df6b7 2019-02-10 stsp static const struct got_error *
2284 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2285 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2287 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2288 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2289 f8d1f275 2019-02-04 stsp char *abspath;
2291 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2292 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2294 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2295 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2296 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2297 927df6b7 2019-02-10 stsp return NULL;
2299 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2300 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2301 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2302 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2304 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2305 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2306 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2309 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2311 f8d1f275 2019-02-04 stsp free(abspath);
2312 9d31a1d8 2018-03-11 stsp return err;
2315 f8d1f275 2019-02-04 stsp static const struct got_error *
2316 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2318 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2319 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2320 2ec1f75b 2019-03-26 stsp unsigned char status;
2322 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2323 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2325 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2326 927df6b7 2019-02-10 stsp return NULL;
2328 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2329 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2330 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2331 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2333 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2334 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2335 537ac44b 2019-08-03 stsp ie->path, &blob_id, NULL, &commit_id);
2339 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
2341 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2343 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
2344 6841da00 2019-08-08 stsp free((char *)pe->path);
2345 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
2349 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
2351 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2353 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
2354 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2355 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2356 6841da00 2019-08-08 stsp free((char *)pe->path);
2358 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
2361 6841da00 2019-08-08 stsp static const struct got_error *
2362 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2364 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2365 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
2366 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
2367 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
2368 6841da00 2019-08-08 stsp size_t linesize = 0;
2369 6841da00 2019-08-08 stsp ssize_t linelen;
2371 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
2372 6841da00 2019-08-08 stsp if (ignorelist == NULL)
2373 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
2374 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
2376 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2377 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
2378 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
2379 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2380 6841da00 2019-08-08 stsp line) == -1) {
2381 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
2382 6841da00 2019-08-08 stsp goto done;
2384 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2386 6841da00 2019-08-08 stsp goto done;
2388 6841da00 2019-08-08 stsp if (ferror(f)) {
2389 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
2390 6841da00 2019-08-08 stsp goto done;
2393 6841da00 2019-08-08 stsp dirpath = strdup(path);
2394 6841da00 2019-08-08 stsp if (dirpath == NULL) {
2395 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
2396 6841da00 2019-08-08 stsp goto done;
2398 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2400 6841da00 2019-08-08 stsp free(line);
2401 6841da00 2019-08-08 stsp if (err || pe == NULL) {
2402 6841da00 2019-08-08 stsp free(dirpath);
2403 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2405 6841da00 2019-08-08 stsp return err;
2409 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
2411 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2414 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
2415 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
2416 6841da00 2019-08-08 stsp * ignores backwards.
2418 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
2419 6841da00 2019-08-08 stsp while (pe) {
2420 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
2421 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2422 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
2423 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2424 6841da00 2019-08-08 stsp if (fnmatch(pi->path, path,
2425 6841da00 2019-08-08 stsp FNM_PATHNAME | FNM_LEADING_DIR))
2430 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2436 6841da00 2019-08-08 stsp static const struct got_error *
2437 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2438 b80270a7 2019-08-08 stsp const char *path)
2440 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2441 6841da00 2019-08-08 stsp char *ignorespath;
2442 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
2444 6841da00 2019-08-08 stsp /* TODO: read .gitignores as well... */
2445 b80270a7 2019-08-08 stsp if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2446 b80270a7 2019-08-08 stsp path[0] ? "/" : "") == -1)
2447 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
2449 6841da00 2019-08-08 stsp ignoresfile = fopen(ignorespath, "r");
2450 6841da00 2019-08-08 stsp if (ignoresfile == NULL) {
2451 6841da00 2019-08-08 stsp if (errno != ENOENT)
2452 6841da00 2019-08-08 stsp err = got_error_from_errno2("fopen",
2453 6841da00 2019-08-08 stsp ignorespath);
2455 6841da00 2019-08-08 stsp err = read_ignores(ignores, path, ignoresfile);
2457 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2458 6841da00 2019-08-08 stsp err = got_error_from_errno2("flose", path);
2459 6841da00 2019-08-08 stsp free(ignorespath);
2460 6841da00 2019-08-08 stsp return err;
2463 f8d1f275 2019-02-04 stsp static const struct got_error *
2464 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2466 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2467 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2468 f8d1f275 2019-02-04 stsp char *path = NULL;
2470 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2471 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2473 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2474 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2475 927df6b7 2019-02-10 stsp return NULL;
2477 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2478 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2479 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2481 f8d1f275 2019-02-04 stsp path = de->d_name;
2484 6841da00 2019-08-08 stsp if (de->d_type == DT_DIR)
2485 b80270a7 2019-08-08 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path);
2486 6841da00 2019-08-08 stsp else if (got_path_is_child(path, a->status_path, a->status_path_len)
2487 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
2488 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2489 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2490 f8d1f275 2019-02-04 stsp if (parent_path[0])
2491 f8d1f275 2019-02-04 stsp free(path);
2492 b72f483a 2019-02-05 stsp return err;
2495 347d1d3e 2019-07-12 stsp static const struct got_error *
2496 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2497 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2498 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2500 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2501 abb4604f 2019-07-27 stsp struct stat sb;
2503 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2505 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2506 abb4604f 2019-07-27 stsp status_arg, repo);
2508 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2509 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2510 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2511 abb4604f 2019-07-27 stsp return NULL;
2514 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2515 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2516 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2518 abb4604f 2019-07-27 stsp return NULL;
2521 abb4604f 2019-07-27 stsp static const struct got_error *
2522 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2523 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2524 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2525 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2527 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2528 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2529 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2530 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2531 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2533 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2534 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2535 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2537 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2538 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2539 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2540 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2542 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2543 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2545 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2546 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2547 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2548 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2549 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2550 abb4604f 2019-07-27 stsp arg.status_path = path;
2551 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2552 abb4604f 2019-07-27 stsp arg.repo = repo;
2553 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2554 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2555 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2556 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2557 6841da00 2019-08-08 stsp TAILQ_INIT(&arg.ignores);
2558 b80270a7 2019-08-08 stsp err = add_ignores(&arg.ignores, worktree->root_path, path);
2559 6841da00 2019-08-08 stsp if (err == NULL)
2560 6841da00 2019-08-08 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2561 6841da00 2019-08-08 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2562 6841da00 2019-08-08 stsp free_ignores(&arg.ignores);
2565 f8d1f275 2019-02-04 stsp if (workdir)
2566 f8d1f275 2019-02-04 stsp closedir(workdir);
2567 927df6b7 2019-02-10 stsp free(ondisk_path);
2568 347d1d3e 2019-07-12 stsp return err;
2571 347d1d3e 2019-07-12 stsp const struct got_error *
2572 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2573 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2574 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2575 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2577 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2578 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2579 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2580 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2582 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2584 347d1d3e 2019-07-12 stsp return err;
2586 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2587 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2588 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2592 f8d1f275 2019-02-04 stsp free(fileindex_path);
2593 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2594 f8d1f275 2019-02-04 stsp return err;
2597 6c7ab921 2019-03-18 stsp const struct got_error *
2598 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2599 6c7ab921 2019-03-18 stsp const char *arg)
2601 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2602 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2603 6c7ab921 2019-03-18 stsp size_t len;
2605 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2607 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2608 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2609 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2610 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2611 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2612 d0710d08 2019-07-22 stsp if (cwd == NULL)
2613 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2614 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2615 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2616 d0710d08 2019-07-22 stsp goto done;
2620 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2621 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2622 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2623 6c7ab921 2019-03-18 stsp goto done;
2626 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2627 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2628 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2630 f6d88e1a 2019-05-29 stsp goto done;
2632 f6d88e1a 2019-05-29 stsp path = strdup("");
2633 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2634 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2635 f6d88e1a 2019-05-29 stsp goto done;
2639 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2640 6c7ab921 2019-03-18 stsp len = strlen(path);
2641 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2642 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2646 6c7ab921 2019-03-18 stsp free(resolved);
2647 d0710d08 2019-07-22 stsp free(cwd);
2648 6c7ab921 2019-03-18 stsp if (err == NULL)
2649 6c7ab921 2019-03-18 stsp *wt_path = path;
2651 6c7ab921 2019-03-18 stsp free(path);
2652 d00136be 2019-03-26 stsp return err;
2655 07f5b47a 2019-06-02 stsp static const struct got_error *
2656 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2657 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2658 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2660 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2661 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2662 6d022e97 2019-08-04 stsp unsigned char status;
2663 6d022e97 2019-08-04 stsp struct stat sb;
2665 6d022e97 2019-08-04 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2667 6d022e97 2019-08-04 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2669 6d022e97 2019-08-04 stsp return err;
2670 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
2671 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
2672 6d022e97 2019-08-04 stsp return NULL;
2673 6d022e97 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2676 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2678 07f5b47a 2019-06-02 stsp return err;
2680 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2681 07f5b47a 2019-06-02 stsp if (err) {
2682 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2683 07f5b47a 2019-06-02 stsp return err;
2686 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2689 d00136be 2019-03-26 stsp const struct got_error *
2690 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2691 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
2692 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2693 031a5338 2019-03-26 stsp struct got_repository *repo)
2695 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2696 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2697 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2698 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2700 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2702 d00136be 2019-03-26 stsp return err;
2704 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2706 d00136be 2019-03-26 stsp goto done;
2708 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2709 6d022e97 2019-08-04 stsp char *ondisk_path;
2710 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2711 6d022e97 2019-08-04 stsp pe->path) == -1)
2712 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2713 6d022e97 2019-08-04 stsp err = schedule_addition(ondisk_path, fileindex, pe->path,
2714 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2715 6d022e97 2019-08-04 stsp free(ondisk_path);
2719 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2720 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2721 af12c6b9 2019-06-04 stsp err = sync_err;
2723 fb399478 2019-07-12 stsp free(fileindex_path);
2724 d00136be 2019-03-26 stsp if (fileindex)
2725 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2726 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2727 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2728 d00136be 2019-03-26 stsp err = unlockerr;
2729 6c7ab921 2019-03-18 stsp return err;
2732 17ed4618 2019-06-02 stsp static const struct got_error *
2733 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2734 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2735 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2736 17ed4618 2019-06-02 stsp struct got_repository *repo)
2738 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2739 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2740 9acbc4fa 2019-08-03 stsp unsigned char status, staged_status;
2741 17ed4618 2019-06-02 stsp struct stat sb;
2743 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2744 17ed4618 2019-06-02 stsp if (ie == NULL)
2745 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2747 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
2748 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
2749 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2750 9acbc4fa 2019-08-03 stsp return NULL;
2751 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2754 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2756 17ed4618 2019-06-02 stsp return err;
2758 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2759 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2760 de426ea6 2019-08-03 stsp return NULL;
2761 f0b0c0ce 2019-08-04 stsp if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2762 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2763 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MODIFY &&
2764 f0b0c0ce 2019-08-04 stsp status != GOT_STATUS_MISSING)
2765 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2768 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2769 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2771 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2772 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2775 2ec1f75b 2019-03-26 stsp const struct got_error *
2776 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2777 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
2778 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2779 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2781 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2782 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2783 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2784 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2786 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2788 2ec1f75b 2019-03-26 stsp return err;
2790 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2792 2ec1f75b 2019-03-26 stsp goto done;
2794 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2795 6d022e97 2019-08-04 stsp char *ondisk_path;
2796 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2797 6d022e97 2019-08-04 stsp pe->path) == -1)
2798 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2799 6d022e97 2019-08-04 stsp err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2800 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2801 6d022e97 2019-08-04 stsp free(ondisk_path);
2805 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2806 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2807 af12c6b9 2019-06-04 stsp err = sync_err;
2809 fb399478 2019-07-12 stsp free(fileindex_path);
2810 2ec1f75b 2019-03-26 stsp if (fileindex)
2811 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2812 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2813 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2814 2ec1f75b 2019-03-26 stsp err = unlockerr;
2815 33aa809d 2019-08-08 stsp return err;
2818 33aa809d 2019-08-08 stsp static const struct got_error *
2819 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2821 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
2822 33aa809d 2019-08-08 stsp char *line = NULL;
2823 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
2824 33aa809d 2019-08-08 stsp ssize_t linelen;
2826 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
2827 33aa809d 2019-08-08 stsp if (linelen == -1) {
2828 33aa809d 2019-08-08 stsp if (ferror(infile)) {
2829 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
2830 33aa809d 2019-08-08 stsp goto done;
2832 33aa809d 2019-08-08 stsp return NULL;
2834 33aa809d 2019-08-08 stsp if (outfile) {
2835 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
2836 33aa809d 2019-08-08 stsp if (n != linelen) {
2837 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
2838 33aa809d 2019-08-08 stsp goto done;
2841 33aa809d 2019-08-08 stsp if (rejectfile) {
2842 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
2843 33aa809d 2019-08-08 stsp if (n != linelen)
2844 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
2847 33aa809d 2019-08-08 stsp free(line);
2848 2ec1f75b 2019-03-26 stsp return err;
2851 33aa809d 2019-08-08 stsp static const struct got_error *
2852 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
2854 33aa809d 2019-08-08 stsp char *line = NULL;
2855 33aa809d 2019-08-08 stsp size_t linesize = 0;
2856 33aa809d 2019-08-08 stsp ssize_t linelen;
2858 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
2859 33aa809d 2019-08-08 stsp free(line);
2860 33aa809d 2019-08-08 stsp if (linelen == -1 && ferror(f))
2861 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
2862 33aa809d 2019-08-08 stsp return NULL;
2865 33aa809d 2019-08-08 stsp static const struct got_error *
2866 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2867 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
2868 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
2870 33aa809d 2019-08-08 stsp const struct got_error *err;
2872 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
2873 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
2874 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
2876 33aa809d 2019-08-08 stsp return err;
2877 33aa809d 2019-08-08 stsp (*line_cur1)++;
2879 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
2880 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
2881 33aa809d 2019-08-08 stsp if (rejectfile)
2882 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
2884 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
2886 33aa809d 2019-08-08 stsp return err;
2887 33aa809d 2019-08-08 stsp (*line_cur2)++;
2889 33aa809d 2019-08-08 stsp /* Copy patched lines. */
2890 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
2891 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
2893 33aa809d 2019-08-08 stsp return err;
2894 33aa809d 2019-08-08 stsp (*line_cur2)++;
2896 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
2897 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
2898 33aa809d 2019-08-08 stsp if (rejectfile)
2899 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
2901 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
2903 33aa809d 2019-08-08 stsp return err;
2904 33aa809d 2019-08-08 stsp (*line_cur1)++;
2907 f1e81a05 2019-08-10 stsp return NULL;
2910 f1e81a05 2019-08-10 stsp static const struct got_error *
2911 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2912 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
2914 f1e81a05 2019-08-10 stsp const struct got_error *err;
2916 f1e81a05 2019-08-10 stsp if (outfile) {
2917 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
2918 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
2919 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
2921 f1e81a05 2019-08-10 stsp return err;
2922 f1e81a05 2019-08-10 stsp (*line_cur1)++;
2925 f1e81a05 2019-08-10 stsp if (rejectfile) {
2926 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
2927 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
2928 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
2930 f1e81a05 2019-08-10 stsp return err;
2931 f1e81a05 2019-08-10 stsp (*line_cur2)++;
2935 33aa809d 2019-08-08 stsp return NULL;
2938 33aa809d 2019-08-08 stsp static const struct got_error *
2939 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2940 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2941 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2942 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
2943 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
2945 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
2946 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
2947 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
2948 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
2949 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
2950 33aa809d 2019-08-08 stsp long pos1, pos2;
2951 33aa809d 2019-08-08 stsp FILE *hunkfile;
2953 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
2955 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
2956 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
2957 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
2959 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
2960 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
2962 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
2963 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2965 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
2966 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
2967 33aa809d 2019-08-08 stsp goto done;
2969 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
2970 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
2971 33aa809d 2019-08-08 stsp goto done;
2973 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2974 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
2975 33aa809d 2019-08-08 stsp goto done;
2978 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2979 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
2981 33aa809d 2019-08-08 stsp goto done;
2983 33aa809d 2019-08-08 stsp switch (*choice) {
2984 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
2985 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2986 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
2988 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
2989 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2990 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
2992 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
2995 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
2999 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3000 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3001 33aa809d 2019-08-08 stsp return err;
3004 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3005 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3006 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3007 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3008 1f1abb7e 2019-08-08 stsp void *progress_arg;
3009 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3010 33aa809d 2019-08-08 stsp void *patch_arg;
3011 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3014 e20a8b6f 2019-06-04 stsp static const struct got_error *
3015 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3016 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3017 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3018 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3020 33aa809d 2019-08-08 stsp const struct got_error *err;
3021 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3022 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3023 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3024 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3025 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3026 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3027 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3028 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3029 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3030 33aa809d 2019-08-08 stsp int n = 0;
3032 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3034 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3036 33aa809d 2019-08-08 stsp return err;
3038 33aa809d 2019-08-08 stsp f2 = fopen(path2, "r");
3039 33aa809d 2019-08-08 stsp if (f2 == NULL) {
3040 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fopen", path2);
3041 33aa809d 2019-08-08 stsp goto done;
3044 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3046 33aa809d 2019-08-08 stsp goto done;
3048 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3050 33aa809d 2019-08-08 stsp goto done;
3052 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3054 33aa809d 2019-08-08 stsp goto done;
3056 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
3057 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
3058 33aa809d 2019-08-08 stsp goto done;
3060 33aa809d 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
3061 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
3062 33aa809d 2019-08-08 stsp goto done;
3065 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
3066 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3068 33aa809d 2019-08-08 stsp goto done;
3070 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3072 33aa809d 2019-08-08 stsp goto done;
3074 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
3075 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
3076 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
3077 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
3078 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3079 33aa809d 2019-08-08 stsp int choice;
3080 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
3081 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
3082 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
3083 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
3084 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
3085 e635744c 2019-08-08 stsp patch_cb, patch_arg);
3087 33aa809d 2019-08-08 stsp goto done;
3088 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
3089 33aa809d 2019-08-08 stsp have_content = 1;
3090 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
3093 f1e81a05 2019-08-10 stsp if (have_content)
3094 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3095 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
3096 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
3098 33aa809d 2019-08-08 stsp free(id_str);
3100 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
3101 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3102 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
3103 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3104 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
3105 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
3106 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
3107 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
3108 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
3109 33aa809d 2019-08-08 stsp if (err || !have_content) {
3110 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3111 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
3112 33aa809d 2019-08-08 stsp free(*path_outfile);
3113 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3115 33aa809d 2019-08-08 stsp free(args);
3117 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
3120 33aa809d 2019-08-08 stsp if (changes)
3121 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
3122 33aa809d 2019-08-08 stsp free(path1);
3123 33aa809d 2019-08-08 stsp return err;
3126 33aa809d 2019-08-08 stsp static const struct got_error *
3127 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
3128 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
3129 1f1abb7e 2019-08-08 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3131 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
3132 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
3133 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
3134 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
3135 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
3136 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
3137 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
3138 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
3139 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
3140 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
3142 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
3143 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
3144 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
3145 d3bcc3d1 2019-08-08 stsp return NULL;
3147 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
3148 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
3149 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
3151 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3152 65084dad 2019-08-08 stsp if (ie == NULL)
3153 65084dad 2019-08-08 stsp return got_error(GOT_ERR_BAD_PATH);
3155 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
3156 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
3157 a129376b 2019-03-28 stsp if (err) {
3158 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
3159 a129376b 2019-03-28 stsp goto done;
3160 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
3161 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
3162 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
3163 e20a8b6f 2019-06-04 stsp goto done;
3166 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
3167 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
3168 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3169 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3170 a129376b 2019-03-28 stsp goto done;
3173 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
3174 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
3175 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3176 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3177 a129376b 2019-03-28 stsp goto done;
3180 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
3181 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
3182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3183 a129376b 2019-03-28 stsp goto done;
3188 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
3189 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
3190 a9fa2909 2019-07-27 stsp if (err) {
3191 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3192 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
3193 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
3194 a9fa2909 2019-07-27 stsp goto done;
3196 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
3198 a9fa2909 2019-07-27 stsp goto done;
3200 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
3201 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
3202 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
3203 a9fa2909 2019-07-27 stsp goto done;
3206 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
3207 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
3208 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
3209 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
3210 a9fa2909 2019-07-27 stsp goto done;
3214 a129376b 2019-03-28 stsp switch (status) {
3215 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
3216 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3217 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3218 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3219 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3221 33aa809d 2019-08-08 stsp goto done;
3222 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3225 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3226 1f1abb7e 2019-08-08 stsp ie->path);
3228 1ee397ad 2019-07-12 stsp goto done;
3229 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
3231 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
3232 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3233 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3234 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3235 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3237 33aa809d 2019-08-08 stsp goto done;
3238 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3241 33aa809d 2019-08-08 stsp /* fall through */
3242 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
3243 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
3244 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
3245 e20a8b6f 2019-06-04 stsp struct got_object_id id;
3246 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3247 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3248 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
3249 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3251 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
3252 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3253 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3255 65084dad 2019-08-08 stsp goto done;
3257 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3258 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
3259 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
3260 a129376b 2019-03-28 stsp goto done;
3263 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3264 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
3265 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
3266 33aa809d 2019-08-08 stsp ondisk_path, ie->path, a->repo,
3267 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
3268 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
3270 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
3271 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
3272 33aa809d 2019-08-08 stsp path_content, ondisk_path);
3273 33aa809d 2019-08-08 stsp goto done;
3276 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
3277 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
3278 33aa809d 2019-08-08 stsp got_fileindex_perms_to_st(ie), blob, 0, 1,
3279 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
3281 a129376b 2019-03-28 stsp goto done;
3282 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_DELETE) {
3283 33aa809d 2019-08-08 stsp err = update_blob_fileindex_entry(a->worktree,
3284 33aa809d 2019-08-08 stsp a->fileindex, ie, ondisk_path, ie->path,
3287 33aa809d 2019-08-08 stsp goto done;
3296 1f1abb7e 2019-08-08 stsp free(ondisk_path);
3297 33aa809d 2019-08-08 stsp free(path_content);
3298 e20a8b6f 2019-06-04 stsp free(parent_path);
3299 a129376b 2019-03-28 stsp free(tree_path);
3301 a129376b 2019-03-28 stsp got_object_blob_close(blob);
3303 a129376b 2019-03-28 stsp got_object_tree_close(tree);
3304 a129376b 2019-03-28 stsp free(tree_id);
3305 e20a8b6f 2019-06-04 stsp return err;
3308 e20a8b6f 2019-06-04 stsp const struct got_error *
3309 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
3310 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
3311 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3312 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
3313 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
3315 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
3316 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
3317 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3318 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
3319 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3320 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
3322 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
3324 e20a8b6f 2019-06-04 stsp return err;
3326 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3328 e20a8b6f 2019-06-04 stsp goto done;
3330 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
3331 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
3332 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
3333 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
3334 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
3335 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
3336 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
3337 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
3338 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3339 1f1abb7e 2019-08-08 stsp revert_file, &rfa, NULL, NULL);
3343 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3344 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
3345 e20a8b6f 2019-06-04 stsp err = sync_err;
3347 fb399478 2019-07-12 stsp free(fileindex_path);
3348 a129376b 2019-03-28 stsp if (fileindex)
3349 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
3350 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3351 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
3352 a129376b 2019-03-28 stsp err = unlockerr;
3353 c4296144 2019-05-09 stsp return err;
3356 cf066bf8 2019-05-09 stsp static void
3357 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
3359 24519714 2019-05-09 stsp free(ct->path);
3360 44d03001 2019-05-09 stsp free(ct->in_repo_path);
3361 768aea60 2019-05-09 stsp free(ct->ondisk_path);
3362 e75eb4da 2019-05-10 stsp free(ct->blob_id);
3363 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
3364 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
3365 b416585c 2019-05-13 stsp free(ct->base_commit_id);
3369 ed175427 2019-05-09 stsp struct collect_commitables_arg {
3370 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
3371 24519714 2019-05-09 stsp struct got_repository *repo;
3372 24519714 2019-05-09 stsp struct got_worktree *worktree;
3373 5f8a88c6 2019-08-03 stsp int have_staged_files;
3376 c4296144 2019-05-09 stsp static const struct got_error *
3377 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
3378 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
3379 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3380 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
3382 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
3383 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
3384 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3385 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
3386 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
3387 768aea60 2019-05-09 stsp struct stat sb;
3389 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
3390 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3391 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3392 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3393 5f8a88c6 2019-08-03 stsp return NULL;
3395 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
3396 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
3398 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3399 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
3400 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
3401 5f8a88c6 2019-08-03 stsp return NULL;
3404 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
3405 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3406 036813ee 2019-05-09 stsp goto done;
3408 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
3409 036813ee 2019-05-09 stsp parent_path = strdup("");
3410 69960a46 2019-05-09 stsp if (parent_path == NULL)
3411 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3413 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
3415 69960a46 2019-05-09 stsp return err;
3418 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
3419 cf066bf8 2019-05-09 stsp if (ct == NULL) {
3420 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3421 c4296144 2019-05-09 stsp goto done;
3424 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3425 768aea60 2019-05-09 stsp relpath) == -1) {
3426 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3427 768aea60 2019-05-09 stsp goto done;
3429 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3430 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3432 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
3433 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
3434 768aea60 2019-05-09 stsp goto done;
3436 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
3439 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3440 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3441 44d03001 2019-05-09 stsp relpath) == -1) {
3442 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3443 44d03001 2019-05-09 stsp goto done;
3446 cf066bf8 2019-05-09 stsp ct->status = status;
3447 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
3448 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
3449 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
3450 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
3451 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
3452 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
3453 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3454 b416585c 2019-05-13 stsp goto done;
3456 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
3457 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
3458 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
3459 f0b75401 2019-08-03 stsp goto done;
3462 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3463 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3464 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3465 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
3466 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3467 036813ee 2019-05-09 stsp goto done;
3470 24519714 2019-05-09 stsp ct->path = strdup(path);
3471 24519714 2019-05-09 stsp if (ct->path == NULL) {
3472 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3473 24519714 2019-05-09 stsp goto done;
3475 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3477 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
3478 ed175427 2019-05-09 stsp free_commitable(ct);
3479 24519714 2019-05-09 stsp free(parent_path);
3480 036813ee 2019-05-09 stsp free(path);
3481 a129376b 2019-03-28 stsp return err;
3484 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
3485 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
3486 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3487 036813ee 2019-05-09 stsp struct got_repository *);
3489 ed175427 2019-05-09 stsp static const struct got_error *
3490 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
3491 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
3492 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3493 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3494 afa376bf 2019-05-09 stsp struct got_repository *repo)
3496 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
3497 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
3498 036813ee 2019-05-09 stsp char *subpath;
3500 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
3501 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3502 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3504 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
3506 ed175427 2019-05-09 stsp return err;
3508 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3509 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3510 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
3511 036813ee 2019-05-09 stsp free(subpath);
3512 036813ee 2019-05-09 stsp return err;
3515 036813ee 2019-05-09 stsp static const struct got_error *
3516 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3518 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3519 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
3521 036813ee 2019-05-09 stsp *match = 0;
3523 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
3524 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
3525 0f63689d 2019-05-10 stsp return NULL;
3528 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3530 0f63689d 2019-05-10 stsp return err;
3531 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
3532 036813ee 2019-05-09 stsp free(ct_parent_path);
3533 036813ee 2019-05-09 stsp return err;
3536 768aea60 2019-05-09 stsp static mode_t
3537 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
3539 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3542 036813ee 2019-05-09 stsp static const struct got_error *
3543 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3544 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3546 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3548 036813ee 2019-05-09 stsp *new_te = NULL;
3550 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3552 036813ee 2019-05-09 stsp goto done;
3554 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3556 036813ee 2019-05-09 stsp free((*new_te)->id);
3557 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
3558 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3560 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3561 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3562 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3563 036813ee 2019-05-09 stsp goto done;
3566 036813ee 2019-05-09 stsp if (err && *new_te) {
3567 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3568 036813ee 2019-05-09 stsp *new_te = NULL;
3570 036813ee 2019-05-09 stsp return err;
3573 036813ee 2019-05-09 stsp static const struct got_error *
3574 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3575 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3577 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3578 036813ee 2019-05-09 stsp char *ct_name;
3580 036813ee 2019-05-09 stsp *new_te = NULL;
3582 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3583 036813ee 2019-05-09 stsp if (*new_te == NULL)
3584 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3586 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3587 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3588 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3589 036813ee 2019-05-09 stsp goto done;
3591 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
3592 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
3593 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3594 036813ee 2019-05-09 stsp goto done;
3597 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3599 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
3600 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3602 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3603 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3605 036813ee 2019-05-09 stsp goto done;
3608 036813ee 2019-05-09 stsp if (err && *new_te) {
3609 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3610 036813ee 2019-05-09 stsp *new_te = NULL;
3612 036813ee 2019-05-09 stsp return err;
3615 036813ee 2019-05-09 stsp static const struct got_error *
3616 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3617 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3619 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3620 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3622 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3624 036813ee 2019-05-09 stsp return err;
3625 036813ee 2019-05-09 stsp if (new_pe == NULL)
3626 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3627 036813ee 2019-05-09 stsp return NULL;
3630 afa376bf 2019-05-09 stsp static const struct got_error *
3631 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3632 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3634 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3635 5f8a88c6 2019-08-03 stsp unsigned char status;
3637 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3638 afa376bf 2019-05-09 stsp ct_path++;
3640 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3641 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
3643 5f8a88c6 2019-08-03 stsp status = ct->status;
3645 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3646 537ac44b 2019-08-03 stsp ct_path, ct->blob_id, NULL, NULL);
3649 036813ee 2019-05-09 stsp static const struct got_error *
3650 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3651 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3653 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3654 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3655 44d03001 2019-05-09 stsp char *te_path;
3657 44d03001 2019-05-09 stsp *modified = 0;
3659 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3660 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3661 44d03001 2019-05-09 stsp te->name) == -1)
3662 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3664 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3665 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3666 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3667 44d03001 2019-05-09 stsp strlen(te_path));
3668 44d03001 2019-05-09 stsp if (*modified)
3672 44d03001 2019-05-09 stsp free(te_path);
3673 44d03001 2019-05-09 stsp return err;
3676 44d03001 2019-05-09 stsp static const struct got_error *
3677 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3678 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3679 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3681 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3682 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3684 036813ee 2019-05-09 stsp *ctp = NULL;
3686 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3687 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3688 036813ee 2019-05-09 stsp char *ct_name = NULL;
3689 036813ee 2019-05-09 stsp int path_matches;
3691 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3692 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
3693 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
3696 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
3697 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
3701 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3704 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3706 036813ee 2019-05-09 stsp return err;
3707 036813ee 2019-05-09 stsp if (!path_matches)
3710 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3711 036813ee 2019-05-09 stsp if (ct_name == NULL)
3712 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3714 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3717 036813ee 2019-05-09 stsp *ctp = ct;
3721 2b496619 2019-07-10 stsp return err;
3724 2b496619 2019-07-10 stsp static const struct got_error *
3725 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3726 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3727 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3728 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3729 2b496619 2019-07-10 stsp struct got_repository *repo)
3731 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3732 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3733 2b496619 2019-07-10 stsp char *subtree_path;
3735 2b496619 2019-07-10 stsp *new_tep = NULL;
3737 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3738 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3739 2b496619 2019-07-10 stsp child_path) == -1)
3740 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3742 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3743 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3744 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3745 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3746 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3747 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3748 2b496619 2019-07-10 stsp goto done;
3750 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3751 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3752 2b496619 2019-07-10 stsp if (err) {
3753 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3754 2b496619 2019-07-10 stsp goto done;
3757 2b496619 2019-07-10 stsp free(subtree_path);
3758 2b496619 2019-07-10 stsp if (err == NULL)
3759 2b496619 2019-07-10 stsp *new_tep = new_te;
3760 036813ee 2019-05-09 stsp return err;
3763 036813ee 2019-05-09 stsp static const struct got_error *
3764 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3765 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3766 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3767 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3768 036813ee 2019-05-09 stsp struct got_repository *repo)
3770 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3771 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3772 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3773 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3774 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3775 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3777 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3778 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3779 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3781 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3782 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3783 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3784 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3786 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
3787 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
3788 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3791 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3792 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3795 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3796 036813ee 2019-05-09 stsp pe->path);
3798 036813ee 2019-05-09 stsp goto done;
3800 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3801 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3802 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3804 036813ee 2019-05-09 stsp goto done;
3805 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3807 afa376bf 2019-05-09 stsp goto done;
3808 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3809 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3811 2f51b5b3 2019-05-09 stsp goto done;
3813 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3814 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3815 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3816 2b496619 2019-07-10 stsp == NULL) {
3817 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3818 2b496619 2019-07-10 stsp child_path, path_base_tree,
3819 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3822 2b496619 2019-07-10 stsp goto done;
3823 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3825 2b496619 2019-07-10 stsp goto done;
3830 2f51b5b3 2019-05-09 stsp if (base_tree) {
3831 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3832 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3833 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3834 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3836 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3837 44d03001 2019-05-09 stsp int modified;
3838 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3840 036813ee 2019-05-09 stsp goto done;
3841 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3842 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3844 2f51b5b3 2019-05-09 stsp goto done;
3845 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3846 44d03001 2019-05-09 stsp if (modified) {
3847 44d03001 2019-05-09 stsp free(new_te->id);
3848 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3849 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3850 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3852 44d03001 2019-05-09 stsp goto done;
3854 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3856 036813ee 2019-05-09 stsp goto done;
3860 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3861 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3863 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3864 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
3865 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3866 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3867 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3869 2f51b5b3 2019-05-09 stsp goto done;
3870 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3872 2f51b5b3 2019-05-09 stsp goto done;
3874 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3875 b416585c 2019-05-13 stsp status_arg);
3877 afa376bf 2019-05-09 stsp goto done;
3879 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3880 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3882 2f51b5b3 2019-05-09 stsp goto done;
3883 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3885 2f51b5b3 2019-05-09 stsp goto done;
3890 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3891 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3892 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3893 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3894 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3896 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3898 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3899 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3900 ed175427 2019-05-09 stsp return err;
3903 ebf99748 2019-05-09 stsp static const struct got_error *
3904 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3905 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3907 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3908 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3910 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3911 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3912 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3914 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3916 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
3917 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
3918 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3919 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3920 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
3921 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3922 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
3923 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
3924 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
3925 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
3926 5f8a88c6 2019-08-03 stsp new_base_commit_id->sha1, 1);
3928 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3929 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3930 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3932 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3933 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3934 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3937 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3942 d56d26ce 2019-05-10 stsp return err;
3946 d56d26ce 2019-05-10 stsp static const struct got_error *
3947 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
3948 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
3949 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
3950 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
3951 735ef5ac 2019-08-03 stsp int ood_errcode)
3953 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3954 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
3956 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3957 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3958 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3959 1a36436d 2019-06-10 stsp return NULL;
3961 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
3962 9bead371 2019-07-28 stsp * on matches file content in the branch head.
3964 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3965 735ef5ac 2019-08-03 stsp in_repo_path);
3966 9bead371 2019-07-28 stsp if (err) {
3967 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
3968 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
3969 1a36436d 2019-06-10 stsp goto done;
3970 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
3971 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
3973 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3974 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3975 735ef5ac 2019-08-03 stsp in_repo_path);
3976 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3977 1a36436d 2019-06-10 stsp goto done;
3978 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
3982 1a36436d 2019-06-10 stsp return err;
3985 c4296144 2019-05-09 stsp const struct got_error *
3986 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3987 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3988 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3989 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
3990 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3991 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3992 afa376bf 2019-05-09 stsp struct got_repository *repo)
3994 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3995 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3996 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3997 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3998 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3999 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
4000 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
4001 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
4002 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
4003 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
4004 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
4006 c4296144 2019-05-09 stsp *new_commit_id = NULL;
4008 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
4010 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4012 588edf97 2019-05-10 stsp goto done;
4014 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4016 588edf97 2019-05-10 stsp goto done;
4018 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
4019 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4024 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
4025 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4029 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
4030 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4031 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4032 cf066bf8 2019-05-09 stsp char *ondisk_path;
4034 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
4035 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4036 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
4039 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
4040 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
4043 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
4044 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
4045 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4046 cf066bf8 2019-05-09 stsp goto done;
4048 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4049 a7055788 2019-05-09 stsp free(ondisk_path);
4051 cf066bf8 2019-05-09 stsp goto done;
4054 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
4055 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4056 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
4058 036813ee 2019-05-09 stsp goto done;
4060 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4062 de18fc63 2019-05-09 stsp goto done;
4063 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4064 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4065 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4066 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
4067 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
4068 33ad4cbe 2019-05-12 jcs free(logmsg);
4070 9d40349a 2019-05-09 stsp goto done;
4072 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
4073 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
4074 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
4075 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
4076 09f5bd90 2019-05-09 stsp goto done;
4078 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
4079 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4081 09f5bd90 2019-05-09 stsp goto done;
4082 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4084 ebf99748 2019-05-09 stsp goto done;
4085 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4086 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4087 09f5bd90 2019-05-09 stsp goto done;
4089 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
4090 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
4092 f2c16586 2019-05-09 stsp goto done;
4093 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
4095 f2c16586 2019-05-09 stsp goto done;
4097 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4099 09f5bd90 2019-05-09 stsp goto done;
4101 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
4103 ebf99748 2019-05-09 stsp goto done;
4105 588edf97 2019-05-10 stsp if (head_tree)
4106 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
4107 588edf97 2019-05-10 stsp if (head_commit)
4108 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
4109 09f5bd90 2019-05-09 stsp free(head_commit_id2);
4110 2f17228e 2019-05-12 stsp if (head_ref2) {
4111 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
4112 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
4113 2f17228e 2019-05-12 stsp err = unlockerr;
4114 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
4116 39cd0ff6 2019-07-12 stsp return err;
4119 5c1e53bc 2019-07-28 stsp static const struct got_error *
4120 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
4121 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
4123 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
4124 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
4126 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
4127 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
4128 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
4130 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
4131 5c1e53bc 2019-07-28 stsp ct_path++;
4133 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
4134 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
4138 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
4139 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
4141 5c1e53bc 2019-07-28 stsp return NULL;
4144 f0b75401 2019-08-03 stsp static const struct got_error *
4145 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
4147 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
4149 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4150 f0b75401 2019-08-03 stsp *have_staged_files = 1;
4151 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
4154 f0b75401 2019-08-03 stsp return NULL;
4157 5f8a88c6 2019-08-03 stsp static const struct got_error *
4158 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
4159 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
4161 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
4162 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
4164 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
4165 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
4167 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4168 5f8a88c6 2019-08-03 stsp if (ie == NULL)
4169 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4170 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4171 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
4172 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
4175 5f8a88c6 2019-08-03 stsp return NULL;
4178 39cd0ff6 2019-07-12 stsp const struct got_error *
4179 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
4180 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
4181 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
4182 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4183 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4184 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
4186 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4187 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4188 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
4189 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4190 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4191 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
4192 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
4193 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4194 f0b75401 2019-08-03 stsp int have_staged_files = 0;
4196 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
4198 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4200 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
4202 39cd0ff6 2019-07-12 stsp goto done;
4204 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4206 39cd0ff6 2019-07-12 stsp goto done;
4208 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4210 39cd0ff6 2019-07-12 stsp goto done;
4212 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4214 39cd0ff6 2019-07-12 stsp goto done;
4216 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4217 5f8a88c6 2019-08-03 stsp &have_staged_files);
4218 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
4219 5f8a88c6 2019-08-03 stsp goto done;
4220 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
4221 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
4223 5f8a88c6 2019-08-03 stsp goto done;
4226 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4227 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
4228 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
4229 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
4230 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4231 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4232 5c1e53bc 2019-07-28 stsp collect_commitables, &cc_arg, NULL, NULL);
4234 5c1e53bc 2019-07-28 stsp goto done;
4237 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4238 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4239 39cd0ff6 2019-07-12 stsp goto done;
4242 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4243 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
4245 5c1e53bc 2019-07-28 stsp goto done;
4248 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4249 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
4250 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
4252 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
4253 f0b75401 2019-08-03 stsp ct_path++;
4254 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
4255 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4256 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4258 b50cabdf 2019-07-12 stsp goto done;
4262 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
4263 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
4264 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4266 39cd0ff6 2019-07-12 stsp goto done;
4268 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4269 93ec1b5f 2019-07-12 stsp fileindex);
4270 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4271 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
4272 39cd0ff6 2019-07-12 stsp err = sync_err;
4274 39cd0ff6 2019-07-12 stsp if (fileindex)
4275 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
4276 39cd0ff6 2019-07-12 stsp free(fileindex_path);
4277 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4278 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
4279 39cd0ff6 2019-07-12 stsp err = unlockerr;
4280 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4281 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
4282 39cd0ff6 2019-07-12 stsp free_commitable(ct);
4284 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
4285 c4296144 2019-05-09 stsp return err;
4288 8656d6c4 2019-05-20 stsp const char *
4289 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
4291 8656d6c4 2019-05-20 stsp return ct->path;
4294 8656d6c4 2019-05-20 stsp unsigned int
4295 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
4297 8656d6c4 2019-05-20 stsp return ct->status;
4300 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
4301 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4302 818c7501 2019-07-11 stsp struct got_repository *repo;
4305 818c7501 2019-07-11 stsp static const struct got_error *
4306 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4308 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4309 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
4310 818c7501 2019-07-11 stsp unsigned char status;
4311 818c7501 2019-07-11 stsp struct stat sb;
4312 818c7501 2019-07-11 stsp char *ondisk_path;
4314 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
4315 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4316 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
4317 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
4319 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4321 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4323 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
4324 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4325 818c7501 2019-07-11 stsp free(ondisk_path);
4327 818c7501 2019-07-11 stsp return err;
4329 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
4330 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
4331 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4332 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4334 818c7501 2019-07-11 stsp return NULL;
4337 818c7501 2019-07-11 stsp const struct got_error *
4338 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4339 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4340 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
4341 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4343 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4344 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4345 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
4346 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4347 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
4348 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4350 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4351 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4352 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4354 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4356 818c7501 2019-07-11 stsp return err;
4358 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4360 818c7501 2019-07-11 stsp goto done;
4362 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
4363 818c7501 2019-07-11 stsp ok_arg.repo = repo;
4364 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4367 818c7501 2019-07-11 stsp goto done;
4369 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4371 818c7501 2019-07-11 stsp goto done;
4373 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4375 818c7501 2019-07-11 stsp goto done;
4377 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4379 818c7501 2019-07-11 stsp goto done;
4381 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4384 818c7501 2019-07-11 stsp goto done;
4386 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
4387 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
4389 818c7501 2019-07-11 stsp goto done;
4390 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
4392 818c7501 2019-07-11 stsp goto done;
4394 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
4396 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4398 818c7501 2019-07-11 stsp goto done;
4400 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
4402 818c7501 2019-07-11 stsp goto done;
4404 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4405 818c7501 2019-07-11 stsp worktree->base_commit_id);
4407 818c7501 2019-07-11 stsp goto done;
4408 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
4410 818c7501 2019-07-11 stsp goto done;
4412 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4414 818c7501 2019-07-11 stsp goto done;
4416 818c7501 2019-07-11 stsp free(fileindex_path);
4417 818c7501 2019-07-11 stsp free(tmp_branch_name);
4418 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
4419 818c7501 2019-07-11 stsp free(branch_ref_name);
4420 818c7501 2019-07-11 stsp if (branch_ref)
4421 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4422 818c7501 2019-07-11 stsp if (wt_branch)
4423 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
4424 818c7501 2019-07-11 stsp if (err) {
4425 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
4426 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
4427 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4429 818c7501 2019-07-11 stsp if (*tmp_branch) {
4430 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4431 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4433 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4434 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4435 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4437 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
4439 818c7501 2019-07-11 stsp return err;
4442 818c7501 2019-07-11 stsp const struct got_error *
4443 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
4444 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4445 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
4446 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
4448 818c7501 2019-07-11 stsp const struct got_error *err;
4449 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4450 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4451 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4452 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4453 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
4455 818c7501 2019-07-11 stsp *commit_id = NULL;
4456 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
4457 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
4458 3e3a69f1 2019-07-25 stsp *branch = NULL;
4459 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4461 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4463 3e3a69f1 2019-07-25 stsp return err;
4465 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4467 f032f1f7 2019-08-04 stsp goto done;
4469 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4470 f032f1f7 2019-08-04 stsp &have_staged_files);
4471 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
4472 f032f1f7 2019-08-04 stsp goto done;
4473 f032f1f7 2019-08-04 stsp if (have_staged_files) {
4474 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
4475 3e3a69f1 2019-07-25 stsp goto done;
4478 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4480 f032f1f7 2019-08-04 stsp goto done;
4482 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4484 818c7501 2019-07-11 stsp goto done;
4486 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4488 818c7501 2019-07-11 stsp goto done;
4490 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4492 818c7501 2019-07-11 stsp goto done;
4494 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4496 818c7501 2019-07-11 stsp goto done;
4498 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
4499 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
4501 818c7501 2019-07-11 stsp goto done;
4503 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4505 818c7501 2019-07-11 stsp goto done;
4507 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4509 818c7501 2019-07-11 stsp goto done;
4511 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
4512 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
4514 818c7501 2019-07-11 stsp goto done;
4516 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4518 818c7501 2019-07-11 stsp goto done;
4520 818c7501 2019-07-11 stsp free(commit_ref_name);
4521 818c7501 2019-07-11 stsp free(branch_ref_name);
4522 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4523 818c7501 2019-07-11 stsp if (commit_ref)
4524 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4525 818c7501 2019-07-11 stsp if (branch_ref)
4526 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4527 818c7501 2019-07-11 stsp if (err) {
4528 818c7501 2019-07-11 stsp free(*commit_id);
4529 818c7501 2019-07-11 stsp *commit_id = NULL;
4530 818c7501 2019-07-11 stsp if (*tmp_branch) {
4531 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4532 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4534 818c7501 2019-07-11 stsp if (*new_base_branch) {
4535 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
4536 818c7501 2019-07-11 stsp *new_base_branch = NULL;
4538 818c7501 2019-07-11 stsp if (*branch) {
4539 818c7501 2019-07-11 stsp got_ref_close(*branch);
4540 818c7501 2019-07-11 stsp *branch = NULL;
4542 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4543 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4544 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4546 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
4548 818c7501 2019-07-11 stsp return err;
4551 818c7501 2019-07-11 stsp const struct got_error *
4552 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4554 818c7501 2019-07-11 stsp const struct got_error *err;
4555 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
4557 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4559 818c7501 2019-07-11 stsp return err;
4561 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4562 818c7501 2019-07-11 stsp free(tmp_branch_name);
4563 818c7501 2019-07-11 stsp return NULL;
4566 818c7501 2019-07-11 stsp static const struct got_error *
4567 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4568 818c7501 2019-07-11 stsp char **logmsg, void *arg)
4570 0ebf8283 2019-07-24 stsp *logmsg = arg;
4571 818c7501 2019-07-11 stsp return NULL;
4574 818c7501 2019-07-11 stsp static const struct got_error *
4575 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4576 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4577 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4579 818c7501 2019-07-11 stsp return NULL;
4582 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
4583 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
4584 01757395 2019-07-12 stsp void *progress_arg;
4585 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
4588 01757395 2019-07-12 stsp static const struct got_error *
4589 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
4591 01757395 2019-07-12 stsp const struct got_error *err;
4592 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
4594 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
4596 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
4598 01757395 2019-07-12 stsp return err;
4600 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
4601 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
4602 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
4603 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
4604 01757395 2019-07-12 stsp return NULL;
4606 01757395 2019-07-12 stsp p = strdup(path);
4607 01757395 2019-07-12 stsp if (p == NULL)
4608 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
4610 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4611 01757395 2019-07-12 stsp if (err || new == NULL)
4613 01757395 2019-07-12 stsp return err;
4617 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4619 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4621 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
4622 01757395 2019-07-12 stsp free((char *)pe->path);
4624 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
4627 0ebf8283 2019-07-24 stsp static const struct got_error *
4628 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4629 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4631 818c7501 2019-07-11 stsp const struct got_error *err;
4632 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
4634 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4635 818c7501 2019-07-11 stsp if (err) {
4636 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
4637 818c7501 2019-07-11 stsp goto done;
4638 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4640 818c7501 2019-07-11 stsp goto done;
4641 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
4643 818c7501 2019-07-11 stsp goto done;
4645 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
4648 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
4650 818c7501 2019-07-11 stsp goto done;
4651 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
4652 818c7501 2019-07-11 stsp free(stored_id);
4653 818c7501 2019-07-11 stsp if (cmp != 0) {
4654 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4655 818c7501 2019-07-11 stsp goto done;
4659 0ebf8283 2019-07-24 stsp if (commit_ref)
4660 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4661 0ebf8283 2019-07-24 stsp return err;
4664 0ebf8283 2019-07-24 stsp static const struct got_error *
4665 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
4666 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
4667 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4668 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
4669 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4670 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4672 0ebf8283 2019-07-24 stsp const struct got_error *err;
4673 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4674 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
4675 3e3a69f1 2019-07-25 stsp char *fileindex_path;
4677 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4679 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4681 0ebf8283 2019-07-24 stsp return err;
4683 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
4684 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
4685 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
4686 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
4687 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
4688 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
4689 818c7501 2019-07-11 stsp if (commit_ref)
4690 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4691 818c7501 2019-07-11 stsp return err;
4694 818c7501 2019-07-11 stsp const struct got_error *
4695 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4696 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4697 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4698 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4699 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4700 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4702 0ebf8283 2019-07-24 stsp const struct got_error *err;
4703 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4705 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4707 0ebf8283 2019-07-24 stsp return err;
4709 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4711 0ebf8283 2019-07-24 stsp goto done;
4713 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4714 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4715 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4717 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4718 0ebf8283 2019-07-24 stsp return err;
4721 0ebf8283 2019-07-24 stsp const struct got_error *
4722 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4723 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4724 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4725 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4726 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4727 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4729 0ebf8283 2019-07-24 stsp const struct got_error *err;
4730 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4732 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4734 0ebf8283 2019-07-24 stsp return err;
4736 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4738 0ebf8283 2019-07-24 stsp goto done;
4740 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4741 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4742 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4744 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4745 0ebf8283 2019-07-24 stsp return err;
4748 0ebf8283 2019-07-24 stsp static const struct got_error *
4749 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4750 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4751 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4752 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4753 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4755 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4756 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4757 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4758 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4759 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4760 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4761 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4763 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4764 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4766 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4768 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4770 3e3a69f1 2019-07-25 stsp return err;
4772 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4773 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4774 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4775 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
4777 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4778 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4779 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4780 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4782 01757395 2019-07-12 stsp if (merged_paths) {
4783 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4784 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4785 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4786 8f8646e5 2019-07-25 stsp goto done;
4788 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4789 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4790 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4792 01757395 2019-07-12 stsp goto done;
4795 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4796 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4798 01757395 2019-07-12 stsp goto done;
4801 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4802 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4803 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4805 a0e95631 2019-07-12 stsp goto done;
4806 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4807 a0e95631 2019-07-12 stsp goto done;
4810 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4812 edd02c5e 2019-07-11 stsp goto done;
4814 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4816 818c7501 2019-07-11 stsp goto done;
4818 5943eee2 2019-08-13 stsp if (new_logmsg) {
4819 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4820 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
4821 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4822 5943eee2 2019-08-13 stsp goto done;
4825 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4827 5943eee2 2019-08-13 stsp goto done;
4830 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
4831 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4832 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
4833 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4834 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4836 a0e95631 2019-07-12 stsp goto done;
4838 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4840 a0e95631 2019-07-12 stsp goto done;
4842 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4844 a0e95631 2019-07-12 stsp goto done;
4846 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4847 93ec1b5f 2019-07-12 stsp fileindex);
4848 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4849 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4850 a0e95631 2019-07-12 stsp err = sync_err;
4852 a0e95631 2019-07-12 stsp free(fileindex_path);
4853 a0e95631 2019-07-12 stsp free(head_commit_id);
4854 a0e95631 2019-07-12 stsp if (head_ref)
4855 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4856 818c7501 2019-07-11 stsp if (err) {
4857 818c7501 2019-07-11 stsp free(*new_commit_id);
4858 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4860 818c7501 2019-07-11 stsp return err;
4863 818c7501 2019-07-11 stsp const struct got_error *
4864 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4865 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4866 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4867 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4868 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4870 0ebf8283 2019-07-24 stsp const struct got_error *err;
4871 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4872 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4873 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4875 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4877 0ebf8283 2019-07-24 stsp return err;
4879 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4881 0ebf8283 2019-07-24 stsp goto done;
4882 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4884 0ebf8283 2019-07-24 stsp goto done;
4885 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4886 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4887 0ebf8283 2019-07-24 stsp goto done;
4890 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4891 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4893 0ebf8283 2019-07-24 stsp if (commit_ref)
4894 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4895 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4896 0ebf8283 2019-07-24 stsp free(commit_id);
4897 0ebf8283 2019-07-24 stsp return err;
4900 0ebf8283 2019-07-24 stsp const struct got_error *
4901 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4902 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4903 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4904 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4905 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4906 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4908 0ebf8283 2019-07-24 stsp const struct got_error *err;
4909 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4910 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4911 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4913 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4915 0ebf8283 2019-07-24 stsp return err;
4917 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4919 0ebf8283 2019-07-24 stsp goto done;
4920 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4922 0ebf8283 2019-07-24 stsp goto done;
4923 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4924 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4925 0ebf8283 2019-07-24 stsp goto done;
4928 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4929 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4931 0ebf8283 2019-07-24 stsp if (commit_ref)
4932 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4933 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4934 0ebf8283 2019-07-24 stsp free(commit_id);
4935 0ebf8283 2019-07-24 stsp return err;
4938 0ebf8283 2019-07-24 stsp const struct got_error *
4939 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4940 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4942 3e3a69f1 2019-07-25 stsp if (fileindex)
4943 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4944 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4947 69844fba 2019-07-11 stsp static const struct got_error *
4948 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4950 69844fba 2019-07-11 stsp const struct got_error *err;
4951 69844fba 2019-07-11 stsp struct got_reference *ref;
4953 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4954 69844fba 2019-07-11 stsp if (err) {
4955 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4956 69844fba 2019-07-11 stsp return NULL;
4957 69844fba 2019-07-11 stsp return err;
4960 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4961 69844fba 2019-07-11 stsp got_ref_close(ref);
4962 69844fba 2019-07-11 stsp return err;
4965 69844fba 2019-07-11 stsp static const struct got_error *
4966 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4968 69844fba 2019-07-11 stsp const struct got_error *err;
4969 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4970 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4972 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4974 69844fba 2019-07-11 stsp goto done;
4975 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4977 69844fba 2019-07-11 stsp goto done;
4979 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4981 69844fba 2019-07-11 stsp goto done;
4982 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4984 69844fba 2019-07-11 stsp goto done;
4986 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4988 69844fba 2019-07-11 stsp goto done;
4989 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4991 69844fba 2019-07-11 stsp goto done;
4993 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4995 69844fba 2019-07-11 stsp goto done;
4996 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4998 69844fba 2019-07-11 stsp goto done;
5001 69844fba 2019-07-11 stsp free(tmp_branch_name);
5002 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
5003 69844fba 2019-07-11 stsp free(branch_ref_name);
5004 69844fba 2019-07-11 stsp free(commit_ref_name);
5005 69844fba 2019-07-11 stsp return err;
5008 818c7501 2019-07-11 stsp const struct got_error *
5009 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
5010 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5011 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5012 818c7501 2019-07-11 stsp struct got_repository *repo)
5014 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
5015 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
5017 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5019 818c7501 2019-07-11 stsp return err;
5021 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5023 818c7501 2019-07-11 stsp goto done;
5025 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
5027 818c7501 2019-07-11 stsp goto done;
5029 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
5031 818c7501 2019-07-11 stsp goto done;
5033 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
5035 3e3a69f1 2019-07-25 stsp if (fileindex)
5036 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5037 818c7501 2019-07-11 stsp free(new_head_commit_id);
5038 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5039 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5040 818c7501 2019-07-11 stsp err = unlockerr;
5041 818c7501 2019-07-11 stsp return err;
5044 818c7501 2019-07-11 stsp const struct got_error *
5045 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
5046 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5047 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
5048 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5050 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
5051 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
5052 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
5053 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5054 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5055 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
5057 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5059 818c7501 2019-07-11 stsp return err;
5061 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
5062 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
5064 818c7501 2019-07-11 stsp goto done;
5066 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
5068 818c7501 2019-07-11 stsp goto done;
5071 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
5072 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
5073 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
5075 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
5077 818c7501 2019-07-11 stsp goto done;
5079 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5081 818c7501 2019-07-11 stsp goto done;
5083 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
5084 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
5086 ca355955 2019-07-12 stsp goto done;
5088 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
5090 ca355955 2019-07-12 stsp goto done;
5092 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5094 818c7501 2019-07-11 stsp goto done;
5096 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5097 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5098 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5099 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5100 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5101 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5102 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5103 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5104 1f1abb7e 2019-08-08 stsp revert_file, &rfa, NULL, NULL);
5106 1f1abb7e 2019-08-08 stsp goto sync;
5108 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5109 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
5111 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5112 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
5113 ca355955 2019-07-12 stsp err = sync_err;
5115 818c7501 2019-07-11 stsp got_ref_close(resolved);
5116 a3a2faf2 2019-07-12 stsp free(tree_id);
5117 818c7501 2019-07-11 stsp free(commit_id);
5118 0ebf8283 2019-07-24 stsp if (fileindex)
5119 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
5120 0ebf8283 2019-07-24 stsp free(fileindex_path);
5122 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5123 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5124 0ebf8283 2019-07-24 stsp err = unlockerr;
5125 0ebf8283 2019-07-24 stsp return err;
5128 0ebf8283 2019-07-24 stsp const struct got_error *
5129 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5130 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5131 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
5132 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5134 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5135 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5136 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
5137 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
5138 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5139 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
5140 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
5141 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5143 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5144 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5145 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5146 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5148 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5150 0ebf8283 2019-07-24 stsp return err;
5152 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5154 0ebf8283 2019-07-24 stsp goto done;
5156 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
5157 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
5158 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5161 0ebf8283 2019-07-24 stsp goto done;
5163 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5165 0ebf8283 2019-07-24 stsp goto done;
5167 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5169 0ebf8283 2019-07-24 stsp goto done;
5171 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5172 0ebf8283 2019-07-24 stsp worktree);
5174 0ebf8283 2019-07-24 stsp goto done;
5176 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5179 0ebf8283 2019-07-24 stsp goto done;
5181 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5183 0ebf8283 2019-07-24 stsp goto done;
5185 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
5187 0ebf8283 2019-07-24 stsp goto done;
5189 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5190 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5192 0ebf8283 2019-07-24 stsp goto done;
5193 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
5195 0ebf8283 2019-07-24 stsp goto done;
5196 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5197 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
5198 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
5199 0ebf8283 2019-07-24 stsp goto done;
5202 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5203 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5205 0ebf8283 2019-07-24 stsp goto done;
5206 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
5208 0ebf8283 2019-07-24 stsp goto done;
5210 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5212 0ebf8283 2019-07-24 stsp goto done;
5214 0ebf8283 2019-07-24 stsp free(fileindex_path);
5215 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5216 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5217 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5218 0ebf8283 2019-07-24 stsp if (wt_branch)
5219 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
5220 0ebf8283 2019-07-24 stsp if (err) {
5221 0ebf8283 2019-07-24 stsp if (*branch_ref) {
5222 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
5223 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5225 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5226 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5227 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5229 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5230 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5231 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5232 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5234 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
5236 0ebf8283 2019-07-24 stsp return err;
5239 0ebf8283 2019-07-24 stsp const struct got_error *
5240 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
5241 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5243 3e3a69f1 2019-07-25 stsp if (fileindex)
5244 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5245 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
5248 0ebf8283 2019-07-24 stsp const struct got_error *
5249 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
5250 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
5252 0ebf8283 2019-07-24 stsp const struct got_error *err;
5253 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5255 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5257 0ebf8283 2019-07-24 stsp return err;
5259 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5260 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5261 0ebf8283 2019-07-24 stsp return NULL;
5264 0ebf8283 2019-07-24 stsp const struct got_error *
5265 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
5266 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
5267 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5268 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5270 0ebf8283 2019-07-24 stsp const struct got_error *err;
5271 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5272 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5273 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5274 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5275 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5276 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5278 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5279 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5280 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5281 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5283 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5285 3e3a69f1 2019-07-25 stsp return err;
5287 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5289 f032f1f7 2019-08-04 stsp goto done;
5291 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5292 f032f1f7 2019-08-04 stsp &have_staged_files);
5293 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5294 f032f1f7 2019-08-04 stsp goto done;
5295 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5296 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5297 3e3a69f1 2019-07-25 stsp goto done;
5300 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5302 f032f1f7 2019-08-04 stsp goto done;
5304 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5306 0ebf8283 2019-07-24 stsp goto done;
5308 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5310 0ebf8283 2019-07-24 stsp goto done;
5312 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5313 0ebf8283 2019-07-24 stsp worktree);
5315 0ebf8283 2019-07-24 stsp goto done;
5317 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5319 0ebf8283 2019-07-24 stsp goto done;
5321 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5323 0ebf8283 2019-07-24 stsp goto done;
5324 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5326 0ebf8283 2019-07-24 stsp goto done;
5328 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5330 0ebf8283 2019-07-24 stsp goto done;
5331 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5333 0ebf8283 2019-07-24 stsp goto done;
5335 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5337 0ebf8283 2019-07-24 stsp goto done;
5339 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5340 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5341 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5342 0ebf8283 2019-07-24 stsp if (commit_ref)
5343 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5344 0ebf8283 2019-07-24 stsp if (base_commit_ref)
5345 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
5346 0ebf8283 2019-07-24 stsp if (err) {
5347 0ebf8283 2019-07-24 stsp free(*commit_id);
5348 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5349 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5350 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5351 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5352 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5353 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5355 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5356 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5357 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5359 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
5361 0ebf8283 2019-07-24 stsp return err;
5364 0ebf8283 2019-07-24 stsp static const struct got_error *
5365 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5367 0ebf8283 2019-07-24 stsp const struct got_error *err;
5368 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5369 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5371 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5373 0ebf8283 2019-07-24 stsp goto done;
5374 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
5376 0ebf8283 2019-07-24 stsp goto done;
5378 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5379 0ebf8283 2019-07-24 stsp worktree);
5381 0ebf8283 2019-07-24 stsp goto done;
5382 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
5384 0ebf8283 2019-07-24 stsp goto done;
5386 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5388 0ebf8283 2019-07-24 stsp goto done;
5389 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
5391 0ebf8283 2019-07-24 stsp goto done;
5393 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5395 0ebf8283 2019-07-24 stsp goto done;
5396 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5398 0ebf8283 2019-07-24 stsp goto done;
5400 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5401 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5402 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5403 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5404 0ebf8283 2019-07-24 stsp return err;
5407 0ebf8283 2019-07-24 stsp const struct got_error *
5408 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
5409 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5410 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
5411 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5413 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
5414 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5415 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5416 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
5417 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5419 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5421 0ebf8283 2019-07-24 stsp return err;
5423 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5424 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
5426 0ebf8283 2019-07-24 stsp goto done;
5428 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5430 0ebf8283 2019-07-24 stsp goto done;
5432 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5434 0ebf8283 2019-07-24 stsp goto done;
5436 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5437 0ebf8283 2019-07-24 stsp worktree->path_prefix);
5439 0ebf8283 2019-07-24 stsp goto done;
5441 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5443 0ebf8283 2019-07-24 stsp goto done;
5445 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5447 0ebf8283 2019-07-24 stsp goto done;
5449 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5450 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5451 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5452 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5453 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5454 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5455 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5456 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
5457 1f1abb7e 2019-08-08 stsp revert_file, &rfa, NULL, NULL);
5459 1f1abb7e 2019-08-08 stsp goto sync;
5461 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5462 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
5464 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5465 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
5466 0ebf8283 2019-07-24 stsp err = sync_err;
5468 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
5469 0ebf8283 2019-07-24 stsp free(tree_id);
5470 818c7501 2019-07-11 stsp free(fileindex_path);
5472 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5473 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5474 818c7501 2019-07-11 stsp err = unlockerr;
5475 818c7501 2019-07-11 stsp return err;
5478 0ebf8283 2019-07-24 stsp const struct got_error *
5479 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
5480 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5481 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
5483 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
5484 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
5485 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5487 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5489 0ebf8283 2019-07-24 stsp return err;
5491 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5492 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
5494 0ebf8283 2019-07-24 stsp goto done;
5496 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
5498 0ebf8283 2019-07-24 stsp goto done;
5500 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
5502 0ebf8283 2019-07-24 stsp goto done;
5504 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5506 0ebf8283 2019-07-24 stsp goto done;
5508 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5510 3e3a69f1 2019-07-25 stsp if (fileindex)
5511 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5512 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
5513 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5514 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5515 0ebf8283 2019-07-24 stsp err = unlockerr;
5516 0ebf8283 2019-07-24 stsp return err;
5519 0ebf8283 2019-07-24 stsp const struct got_error *
5520 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5521 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5523 0ebf8283 2019-07-24 stsp const struct got_error *err;
5524 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5526 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5528 0ebf8283 2019-07-24 stsp return err;
5530 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5532 0ebf8283 2019-07-24 stsp goto done;
5534 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5536 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5537 0cb83759 2019-08-03 stsp return err;
5540 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
5541 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
5542 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5543 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5544 2db2652d 2019-08-07 stsp struct got_repository *repo;
5545 2db2652d 2019-08-07 stsp int have_changes;
5548 2db2652d 2019-08-07 stsp const struct got_error *
5549 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
5550 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5551 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5552 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5554 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
5555 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
5556 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
5557 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
5558 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
5559 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
5561 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
5562 8b13ce36 2019-08-08 stsp return NULL;
5564 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5565 735ef5ac 2019-08-03 stsp if (ie == NULL)
5566 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5568 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5569 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5570 735ef5ac 2019-08-03 stsp relpath) == -1)
5571 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
5573 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
5574 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
5575 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5576 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
5579 3aa5969e 2019-08-06 stsp if (status == GOT_STATUS_NO_CHANGE) {
5580 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5581 3aa5969e 2019-08-06 stsp goto done;
5582 3aa5969e 2019-08-06 stsp } else if (status == GOT_STATUS_CONFLICT) {
5583 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5584 3aa5969e 2019-08-06 stsp goto done;
5585 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
5586 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
5587 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
5588 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5589 735ef5ac 2019-08-03 stsp goto done;
5592 2db2652d 2019-08-07 stsp a->have_changes = 1;
5594 735ef5ac 2019-08-03 stsp p = in_repo_path;
5595 735ef5ac 2019-08-03 stsp while (p[0] == '/')
5597 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
5598 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
5599 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
5601 735ef5ac 2019-08-03 stsp free(in_repo_path);
5602 dc424a06 2019-08-07 stsp return err;
5605 2db2652d 2019-08-07 stsp struct stage_path_arg {
5606 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5607 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5608 2db2652d 2019-08-07 stsp struct got_repository *repo;
5609 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
5610 2db2652d 2019-08-07 stsp void *status_arg;
5611 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
5612 2db2652d 2019-08-07 stsp void *patch_arg;
5615 2db2652d 2019-08-07 stsp static const struct got_error *
5616 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
5617 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5618 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5619 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5621 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
5622 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
5623 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
5624 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
5625 0cb83759 2019-08-03 stsp uint32_t stage;
5626 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
5628 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
5629 8b13ce36 2019-08-08 stsp return NULL;
5631 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5632 d3e7c587 2019-08-03 stsp if (ie == NULL)
5633 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5635 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5636 2db2652d 2019-08-07 stsp relpath)== -1)
5637 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
5639 0cb83759 2019-08-03 stsp switch (status) {
5640 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
5641 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
5642 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5643 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
5644 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5645 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5646 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5649 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
5652 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
5653 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
5654 af5a81b2 2019-08-08 stsp ondisk_path, ie->path, a->repo,
5655 2db2652d 2019-08-07 stsp a->patch_cb, a->patch_arg);
5656 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
5660 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
5661 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
5664 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5665 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5666 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5667 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
5669 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
5670 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5671 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5673 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5674 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
5675 af5a81b2 2019-08-08 stsp new_staged_blob_id, NULL);
5677 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
5678 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
5680 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5681 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5682 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
5683 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
5686 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
5688 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
5689 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5693 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
5694 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5695 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5697 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5698 537ac44b 2019-08-03 stsp get_staged_status(ie), relpath, NULL, NULL, NULL);
5700 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
5701 d3e7c587 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5703 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
5704 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5707 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5711 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
5712 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
5713 dc424a06 2019-08-07 stsp free(path_content);
5714 2db2652d 2019-08-07 stsp free(ondisk_path);
5715 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
5716 0ebf8283 2019-07-24 stsp return err;
5719 0cb83759 2019-08-03 stsp const struct got_error *
5720 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
5721 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
5722 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
5723 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5724 1e71573e 2019-08-03 stsp struct got_repository *repo)
5726 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5727 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
5728 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5729 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
5730 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
5731 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
5732 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
5733 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
5735 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5737 0cb83759 2019-08-03 stsp return err;
5739 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
5740 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
5742 735ef5ac 2019-08-03 stsp goto done;
5743 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5745 735ef5ac 2019-08-03 stsp goto done;
5746 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5748 0cb83759 2019-08-03 stsp goto done;
5750 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
5751 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
5752 2db2652d 2019-08-07 stsp oka.worktree = worktree;
5753 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
5754 2db2652d 2019-08-07 stsp oka.repo = repo;
5755 2db2652d 2019-08-07 stsp oka.have_changes = 0;
5756 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5757 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5758 2db2652d 2019-08-07 stsp check_stage_ok, &oka, NULL, NULL);
5760 735ef5ac 2019-08-03 stsp goto done;
5762 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
5763 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5764 2db2652d 2019-08-07 stsp goto done;
5767 2db2652d 2019-08-07 stsp spa.worktree = worktree;
5768 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
5769 2db2652d 2019-08-07 stsp spa.repo = repo;
5770 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
5771 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
5772 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
5773 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
5774 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5775 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5776 2db2652d 2019-08-07 stsp stage_path, &spa, NULL, NULL);
5778 2db2652d 2019-08-07 stsp goto done;
5781 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5782 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
5783 0cb83759 2019-08-03 stsp err = sync_err;
5785 735ef5ac 2019-08-03 stsp if (head_ref)
5786 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
5787 735ef5ac 2019-08-03 stsp free(head_commit_id);
5788 0cb83759 2019-08-03 stsp free(fileindex_path);
5789 0cb83759 2019-08-03 stsp if (fileindex)
5790 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
5791 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5792 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
5793 0cb83759 2019-08-03 stsp err = unlockerr;
5794 0cb83759 2019-08-03 stsp return err;
5797 ad493afc 2019-08-03 stsp struct unstage_path_arg {
5798 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
5799 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
5800 ad493afc 2019-08-03 stsp struct got_repository *repo;
5801 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
5802 ad493afc 2019-08-03 stsp void *progress_arg;
5803 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
5804 2e1f37b0 2019-08-08 stsp void *patch_arg;
5807 2e1f37b0 2019-08-08 stsp static const struct got_error *
5808 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
5809 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
5810 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
5811 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
5812 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
5814 2e1f37b0 2019-08-08 stsp const struct got_error *err;
5815 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
5816 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5817 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5818 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
5819 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
5820 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
5821 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
5822 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
5823 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5824 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
5826 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
5827 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
5829 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
5831 2e1f37b0 2019-08-08 stsp return err;
5832 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5834 2e1f37b0 2019-08-08 stsp goto done;
5836 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5838 2e1f37b0 2019-08-08 stsp goto done;
5840 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5842 2e1f37b0 2019-08-08 stsp goto done;
5844 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5846 2e1f37b0 2019-08-08 stsp goto done;
5848 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5850 2e1f37b0 2019-08-08 stsp goto done;
5852 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5854 2e1f37b0 2019-08-08 stsp goto done;
5856 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
5857 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
5858 2e1f37b0 2019-08-08 stsp goto done;
5861 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
5862 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
5863 2e1f37b0 2019-08-08 stsp goto done;
5866 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
5867 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5869 2e1f37b0 2019-08-08 stsp goto done;
5871 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
5872 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
5874 2e1f37b0 2019-08-08 stsp goto done;
5875 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
5876 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
5878 2e1f37b0 2019-08-08 stsp goto done;
5880 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
5881 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
5882 2e1f37b0 2019-08-08 stsp goto done;
5884 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
5885 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
5886 2e1f37b0 2019-08-08 stsp goto done;
5888 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5889 2e1f37b0 2019-08-08 stsp int choice;
5890 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
5891 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
5892 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
5893 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
5895 2e1f37b0 2019-08-08 stsp goto done;
5896 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5897 2e1f37b0 2019-08-08 stsp have_content = 1;
5899 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
5900 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
5903 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
5904 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5905 f1e81a05 2019-08-10 stsp outfile, rejectfile);
5907 2e1f37b0 2019-08-08 stsp free(label1);
5909 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
5910 2e1f37b0 2019-08-08 stsp if (staged_blob)
5911 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
5912 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5913 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5914 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5915 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5916 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5917 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
5918 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5919 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
5920 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5921 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5922 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
5923 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
5924 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
5925 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
5926 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
5927 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
5928 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
5929 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
5930 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
5932 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
5933 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
5934 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
5935 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
5936 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
5937 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
5938 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
5940 2e1f37b0 2019-08-08 stsp free(args);
5942 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
5945 2e1f37b0 2019-08-08 stsp if (changes)
5946 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
5947 2e1f37b0 2019-08-08 stsp free(path1);
5948 2e1f37b0 2019-08-08 stsp free(path2);
5949 2e1f37b0 2019-08-08 stsp return err;
5952 ad493afc 2019-08-03 stsp static const struct got_error *
5953 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
5954 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
5955 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5956 ad493afc 2019-08-03 stsp struct got_object_id *commit_id)
5958 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
5959 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
5960 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
5961 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5962 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
5963 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
5964 ad493afc 2019-08-03 stsp int local_changes_subsumed;
5965 9bc94a15 2019-08-03 stsp struct stat sb;
5967 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
5968 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
5969 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
5970 2e1f37b0 2019-08-08 stsp return NULL;
5972 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5973 ad493afc 2019-08-03 stsp if (ie == NULL)
5974 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5976 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5978 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
5980 ad493afc 2019-08-03 stsp switch (staged_status) {
5981 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
5982 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
5983 ad493afc 2019-08-03 stsp blob_id, 8192);
5986 ad493afc 2019-08-03 stsp /* fall through */
5987 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
5988 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
5989 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
5990 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5991 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5992 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
5995 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5998 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
5999 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
6000 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
6001 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
6002 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
6003 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
6005 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
6006 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
6007 2e1f37b0 2019-08-08 stsp &staged_blob_id,
6008 2e1f37b0 2019-08-08 stsp path_new_staged_content,
6012 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
6013 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
6014 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
6016 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
6017 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
6018 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
6019 2e1f37b0 2019-08-08 stsp path_unstaged_content, "unstaged",
6020 2e1f37b0 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
6021 2e1f37b0 2019-08-08 stsp if (err == NULL &&
6022 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
6023 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
6024 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
6025 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
6028 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
6029 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
6032 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
6033 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
6034 ad493afc 2019-08-03 stsp got_fileindex_perms_to_st(ie), blob_staged,
6035 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
6036 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
6037 ad493afc 2019-08-03 stsp if (err == NULL)
6038 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6039 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6041 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
6042 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6043 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6044 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6045 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6048 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6050 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6051 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6055 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6056 9bc94a15 2019-08-03 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6059 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
6063 ad493afc 2019-08-03 stsp free(ondisk_path);
6064 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
6065 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
6066 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
6067 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
6068 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
6069 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
6070 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
6071 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
6072 ad493afc 2019-08-03 stsp if (blob_base)
6073 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
6074 ad493afc 2019-08-03 stsp if (blob_staged)
6075 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
6076 ad493afc 2019-08-03 stsp return err;
6079 ad493afc 2019-08-03 stsp const struct got_error *
6080 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
6081 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
6082 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6083 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6084 ad493afc 2019-08-03 stsp struct got_repository *repo)
6086 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6087 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6088 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6089 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
6090 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
6092 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6094 ad493afc 2019-08-03 stsp return err;
6096 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6098 ad493afc 2019-08-03 stsp goto done;
6100 ad493afc 2019-08-03 stsp upa.worktree = worktree;
6101 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
6102 ad493afc 2019-08-03 stsp upa.repo = repo;
6103 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
6104 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
6105 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
6106 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
6107 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6108 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6109 ad493afc 2019-08-03 stsp unstage_path, &upa, NULL, NULL);
6111 ad493afc 2019-08-03 stsp goto done;
6114 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6115 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
6116 ad493afc 2019-08-03 stsp err = sync_err;
6118 ad493afc 2019-08-03 stsp free(fileindex_path);
6119 ad493afc 2019-08-03 stsp if (fileindex)
6120 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
6121 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6122 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
6123 ad493afc 2019-08-03 stsp err = unlockerr;
6124 ad493afc 2019-08-03 stsp return err;