Blame


1 86c3caaf 2018-03-09 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
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.
7 86c3caaf 2018-03-09 stsp *
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.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
36 86c3caaf 2018-03-09 stsp
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 9d31a1d8 2018-03-11 stsp
57 9d31a1d8 2018-03-11 stsp #ifndef MIN
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 9d31a1d8 2018-03-11 stsp #endif
60 86c3caaf 2018-03-09 stsp
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
63 99724ed4 2018-03-10 stsp {
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
65 99724ed4 2018-03-10 stsp char *path;
66 99724ed4 2018-03-10 stsp
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
69 99724ed4 2018-03-10 stsp
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
71 99724ed4 2018-03-10 stsp free(path);
72 507dc3bb 2018-12-29 stsp return err;
73 507dc3bb 2018-12-29 stsp }
74 507dc3bb 2018-12-29 stsp
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
77 507dc3bb 2018-12-29 stsp {
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
82 507dc3bb 2018-12-29 stsp
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
86 507dc3bb 2018-12-29 stsp goto done;
87 507dc3bb 2018-12-29 stsp }
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
90 507dc3bb 2018-12-29 stsp if (err)
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp }
99 507dc3bb 2018-12-29 stsp }
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
104 507dc3bb 2018-12-29 stsp goto done;
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp done:
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
112 99724ed4 2018-03-10 stsp }
113 99724ed4 2018-03-10 stsp
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
116 09fe317a 2018-03-11 stsp {
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
120 09fe317a 2018-03-11 stsp ssize_t n;
121 09fe317a 2018-03-11 stsp struct stat sb;
122 09fe317a 2018-03-11 stsp
123 09fe317a 2018-03-11 stsp *content = NULL;
124 09fe317a 2018-03-11 stsp
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
128 09fe317a 2018-03-11 stsp goto done;
129 09fe317a 2018-03-11 stsp }
130 09fe317a 2018-03-11 stsp
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
135 f02eaa22 2019-03-11 stsp else
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
137 ef99fdb1 2018-03-11 stsp goto done;
138 ef99fdb1 2018-03-11 stsp }
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
142 09fe317a 2018-03-11 stsp goto done;
143 09fe317a 2018-03-11 stsp }
144 09fe317a 2018-03-11 stsp
145 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
146 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
147 d10c9b58 2019-02-19 stsp goto done;
148 d10c9b58 2019-02-19 stsp }
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
152 09fe317a 2018-03-11 stsp goto done;
153 09fe317a 2018-03-11 stsp }
154 09fe317a 2018-03-11 stsp
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
159 09fe317a 2018-03-11 stsp goto done;
160 09fe317a 2018-03-11 stsp }
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
163 09fe317a 2018-03-11 stsp goto done;
164 09fe317a 2018-03-11 stsp }
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
166 09fe317a 2018-03-11 stsp
167 09fe317a 2018-03-11 stsp done:
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
171 09fe317a 2018-03-11 stsp if (err) {
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
174 09fe317a 2018-03-11 stsp }
175 09fe317a 2018-03-11 stsp return err;
176 09fe317a 2018-03-11 stsp }
177 09fe317a 2018-03-11 stsp
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
180 024e9686 2019-05-14 stsp {
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
183 024e9686 2019-05-14 stsp
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
188 024e9686 2019-05-14 stsp } else {
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
192 024e9686 2019-05-14 stsp }
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
196 024e9686 2019-05-14 stsp }
197 024e9686 2019-05-14 stsp
198 86c3caaf 2018-03-09 stsp const struct got_error *
199 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
200 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
201 86c3caaf 2018-03-09 stsp {
202 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
203 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
204 ec22038e 2019-03-10 stsp uuid_t uuid;
205 ec22038e 2019-03-10 stsp uint32_t uuid_status;
206 65596e15 2018-12-24 stsp int obj_type;
207 7ac97322 2018-03-11 stsp char *path_got = NULL;
208 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
212 65596e15 2018-12-24 stsp
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
215 0c48fee2 2019-03-11 stsp goto done;
216 0c48fee2 2019-03-11 stsp }
217 0c48fee2 2019-03-11 stsp
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
219 65596e15 2018-12-24 stsp if (err)
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
222 65596e15 2018-12-24 stsp if (err)
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
226 86c3caaf 2018-03-09 stsp
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
230 0bb8a95e 2018-03-12 stsp }
231 577ec78f 2018-03-11 stsp
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
235 86c3caaf 2018-03-09 stsp goto done;
236 86c3caaf 2018-03-09 stsp }
237 86c3caaf 2018-03-09 stsp
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
245 86c3caaf 2018-03-09 stsp goto done;
246 86c3caaf 2018-03-09 stsp }
247 86c3caaf 2018-03-09 stsp
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 056e7441 2018-03-11 stsp if (err)
251 056e7441 2018-03-11 stsp goto done;
252 056e7441 2018-03-11 stsp
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 99724ed4 2018-03-10 stsp if (err)
256 86c3caaf 2018-03-09 stsp goto done;
257 86c3caaf 2018-03-09 stsp
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
260 65596e15 2018-12-24 stsp if (err)
261 65596e15 2018-12-24 stsp goto done;
262 65596e15 2018-12-24 stsp
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 99724ed4 2018-03-10 stsp if (err)
269 86c3caaf 2018-03-09 stsp goto done;
270 86c3caaf 2018-03-09 stsp
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
280 ec22038e 2019-03-10 stsp if (err)
281 ec22038e 2019-03-10 stsp goto done;
282 ec22038e 2019-03-10 stsp
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp }
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 577ec78f 2018-03-11 stsp if (err)
296 577ec78f 2018-03-11 stsp goto done;
297 577ec78f 2018-03-11 stsp
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
301 1451e70d 2018-03-10 stsp goto done;
302 1451e70d 2018-03-10 stsp }
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 99724ed4 2018-03-10 stsp if (err)
305 1451e70d 2018-03-10 stsp goto done;
306 1451e70d 2018-03-10 stsp
307 86c3caaf 2018-03-09 stsp done:
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
315 86c3caaf 2018-03-09 stsp }
316 86c3caaf 2018-03-09 stsp
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
319 86c3caaf 2018-03-09 stsp {
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
330 6d9d28c3 2018-03-11 stsp
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
332 6d9d28c3 2018-03-11 stsp
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
336 6d9d28c3 2018-03-11 stsp goto done;
337 6d9d28c3 2018-03-11 stsp }
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
349 6d9d28c3 2018-03-11 stsp goto done;
350 6d9d28c3 2018-03-11 stsp }
351 6d9d28c3 2018-03-11 stsp
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 6d9d28c3 2018-03-11 stsp if (err)
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
359 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp }
362 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
364 6d9d28c3 2018-03-11 stsp goto done;
365 6d9d28c3 2018-03-11 stsp }
366 6d9d28c3 2018-03-11 stsp
367 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
368 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 6d9d28c3 2018-03-11 stsp
374 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
375 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
376 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
377 6d9d28c3 2018-03-11 stsp goto done;
378 6d9d28c3 2018-03-11 stsp }
379 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
380 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
381 6d9d28c3 2018-03-11 stsp if (err)
382 6d9d28c3 2018-03-11 stsp goto done;
383 eaccb85f 2018-12-25 stsp
384 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
386 93a30277 2018-12-24 stsp if (err)
387 93a30277 2018-12-24 stsp goto done;
388 93a30277 2018-12-24 stsp
389 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
390 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
391 c442a90d 2019-03-10 stsp if (err)
392 c442a90d 2019-03-10 stsp goto done;
393 c442a90d 2019-03-10 stsp
394 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 eaccb85f 2018-12-25 stsp if (err)
396 c442a90d 2019-03-10 stsp goto done;
397 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
399 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
400 eaccb85f 2018-12-25 stsp goto done;
401 c442a90d 2019-03-10 stsp }
402 eaccb85f 2018-12-25 stsp
403 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
404 eaccb85f 2018-12-25 stsp if (err)
405 eaccb85f 2018-12-25 stsp goto done;
406 eaccb85f 2018-12-25 stsp
407 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 eaccb85f 2018-12-25 stsp base_commit_id_str);
409 f5baf295 2018-03-11 stsp if (err)
410 6d9d28c3 2018-03-11 stsp goto done;
411 6d9d28c3 2018-03-11 stsp
412 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
414 6d9d28c3 2018-03-11 stsp done:
415 eaccb85f 2018-12-25 stsp if (repo)
416 eaccb85f 2018-12-25 stsp got_repo_close(repo);
417 7ac97322 2018-03-11 stsp free(path_got);
418 056e7441 2018-03-11 stsp free(path_lock);
419 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
420 c442a90d 2019-03-10 stsp free(uuidstr);
421 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (err) {
423 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp close(fd);
425 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
426 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
427 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 6d9d28c3 2018-03-11 stsp } else
429 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp
431 6d9d28c3 2018-03-11 stsp return err;
432 86c3caaf 2018-03-09 stsp }
433 247140b2 2019-02-05 stsp
434 247140b2 2019-02-05 stsp const struct got_error *
435 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp {
437 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
438 86c3caaf 2018-03-09 stsp
439 247140b2 2019-02-05 stsp do {
440 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
441 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 247140b2 2019-02-05 stsp return err;
443 247140b2 2019-02-05 stsp if (*worktree)
444 247140b2 2019-02-05 stsp return NULL;
445 247140b2 2019-02-05 stsp path = dirname(path);
446 d1542a27 2019-02-05 stsp if (path == NULL)
447 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
448 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp
450 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
451 247140b2 2019-02-05 stsp }
452 247140b2 2019-02-05 stsp
453 3a6ce05a 2019-02-11 stsp const struct got_error *
454 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 86c3caaf 2018-03-09 stsp {
456 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
457 c88eb298 2018-03-11 stsp free(worktree->root_path);
458 cde76477 2018-03-11 stsp free(worktree->repo_path);
459 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
460 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
461 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
462 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
463 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
464 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
465 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
466 6d9d28c3 2018-03-11 stsp free(worktree);
467 3a6ce05a 2019-02-11 stsp return err;
468 86c3caaf 2018-03-09 stsp }
469 86c3caaf 2018-03-09 stsp
470 2fbdb5ae 2018-12-29 stsp const char *
471 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp {
473 c7f4312f 2019-02-05 stsp return worktree->root_path;
474 c7f4312f 2019-02-05 stsp }
475 c7f4312f 2019-02-05 stsp
476 c7f4312f 2019-02-05 stsp const char *
477 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 86c3caaf 2018-03-09 stsp {
479 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
480 49520a32 2018-12-29 stsp }
481 49520a32 2018-12-29 stsp
482 49520a32 2018-12-29 stsp const char *
483 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 49520a32 2018-12-29 stsp {
485 f609be2e 2018-12-29 stsp return worktree->path_prefix;
486 e5dc7198 2018-12-29 stsp }
487 e5dc7198 2018-12-29 stsp
488 e5dc7198 2018-12-29 stsp const struct got_error *
489 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp {
492 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp
494 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
495 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp }
498 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
499 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
500 e5dc7198 2018-12-29 stsp free(absprefix);
501 e5dc7198 2018-12-29 stsp return NULL;
502 86c3caaf 2018-03-09 stsp }
503 86c3caaf 2018-03-09 stsp
504 bc70eb79 2019-05-09 stsp const char *
505 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 86c3caaf 2018-03-09 stsp {
507 36a38700 2019-05-10 stsp return worktree->head_ref_name;
508 024e9686 2019-05-14 stsp }
509 024e9686 2019-05-14 stsp
510 024e9686 2019-05-14 stsp const struct got_error *
511 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
512 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp {
514 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
515 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp
517 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
519 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
520 024e9686 2019-05-14 stsp path_got = NULL;
521 024e9686 2019-05-14 stsp goto done;
522 024e9686 2019-05-14 stsp }
523 024e9686 2019-05-14 stsp
524 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
525 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
526 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
527 024e9686 2019-05-14 stsp goto done;
528 024e9686 2019-05-14 stsp }
529 024e9686 2019-05-14 stsp
530 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
531 024e9686 2019-05-14 stsp if (err)
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp
534 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
535 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp done:
537 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp if (err)
539 024e9686 2019-05-14 stsp free(head_ref_name);
540 024e9686 2019-05-14 stsp return err;
541 507dc3bb 2018-12-29 stsp }
542 507dc3bb 2018-12-29 stsp
543 b72f483a 2019-02-05 stsp struct got_object_id *
544 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp {
546 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
547 86c3caaf 2018-03-09 stsp }
548 86c3caaf 2018-03-09 stsp
549 507dc3bb 2018-12-29 stsp const struct got_error *
550 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp {
553 507dc3bb 2018-12-29 stsp const struct got_error *err;
554 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
555 507dc3bb 2018-12-29 stsp char *id_str = NULL;
556 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp
558 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
561 507dc3bb 2018-12-29 stsp path_got = NULL;
562 507dc3bb 2018-12-29 stsp goto done;
563 507dc3bb 2018-12-29 stsp }
564 507dc3bb 2018-12-29 stsp
565 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp if (err)
567 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp
569 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
571 507dc3bb 2018-12-29 stsp goto done;
572 507dc3bb 2018-12-29 stsp }
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp /* Record our base commit. */
575 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
576 507dc3bb 2018-12-29 stsp if (err)
577 507dc3bb 2018-12-29 stsp goto done;
578 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 507dc3bb 2018-12-29 stsp if (err)
580 507dc3bb 2018-12-29 stsp goto done;
581 507dc3bb 2018-12-29 stsp
582 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
583 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
584 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
585 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
586 507dc3bb 2018-12-29 stsp goto done;
587 507dc3bb 2018-12-29 stsp }
588 507dc3bb 2018-12-29 stsp done:
589 507dc3bb 2018-12-29 stsp if (obj)
590 507dc3bb 2018-12-29 stsp got_object_close(obj);
591 507dc3bb 2018-12-29 stsp free(id_str);
592 507dc3bb 2018-12-29 stsp free(path_got);
593 507dc3bb 2018-12-29 stsp return err;
594 507dc3bb 2018-12-29 stsp }
595 507dc3bb 2018-12-29 stsp
596 9d31a1d8 2018-03-11 stsp static const struct got_error *
597 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 86c3caaf 2018-03-09 stsp {
599 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
602 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
603 86c3caaf 2018-03-09 stsp return NULL;
604 21908da4 2019-01-13 stsp }
605 21908da4 2019-01-13 stsp
606 21908da4 2019-01-13 stsp static const struct got_error *
607 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp {
609 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
610 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp
612 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 4a1ddfc2 2019-01-12 stsp
615 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
616 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 ddcd8544 2019-03-11 stsp struct stat sb;
618 ddcd8544 2019-03-11 stsp err = NULL;
619 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
620 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
621 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
622 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
623 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
624 ddcd8544 2019-03-11 stsp }
625 ddcd8544 2019-03-11 stsp }
626 4a1ddfc2 2019-01-12 stsp free(abspath);
627 68c76935 2019-02-19 stsp return err;
628 68c76935 2019-02-19 stsp }
629 68c76935 2019-02-19 stsp
630 68c76935 2019-02-19 stsp static const struct got_error *
631 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp {
633 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
634 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
635 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
636 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
637 68c76935 2019-02-19 stsp
638 68c76935 2019-02-19 stsp *same = 1;
639 68c76935 2019-02-19 stsp
640 230a42bd 2019-05-11 jcs for (;;) {
641 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
643 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
644 68c76935 2019-02-19 stsp break;
645 68c76935 2019-02-19 stsp }
646 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp if (flen1 == 0) {
652 68c76935 2019-02-19 stsp if (flen2 != 0)
653 68c76935 2019-02-19 stsp *same = 0;
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
656 68c76935 2019-02-19 stsp if (flen1 != 0)
657 68c76935 2019-02-19 stsp *same = 0;
658 68c76935 2019-02-19 stsp break;
659 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
660 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 68c76935 2019-02-19 stsp *same = 0;
662 68c76935 2019-02-19 stsp break;
663 68c76935 2019-02-19 stsp }
664 68c76935 2019-02-19 stsp } else {
665 68c76935 2019-02-19 stsp *same = 0;
666 68c76935 2019-02-19 stsp break;
667 68c76935 2019-02-19 stsp }
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp
670 68c76935 2019-02-19 stsp return err;
671 68c76935 2019-02-19 stsp }
672 68c76935 2019-02-19 stsp
673 68c76935 2019-02-19 stsp static const struct got_error *
674 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp {
676 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
677 68c76935 2019-02-19 stsp struct stat sb;
678 68c76935 2019-02-19 stsp size_t size1, size2;
679 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
680 68c76935 2019-02-19 stsp
681 68c76935 2019-02-19 stsp *same = 1;
682 68c76935 2019-02-19 stsp
683 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
684 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
685 68c76935 2019-02-19 stsp goto done;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp
689 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
691 68c76935 2019-02-19 stsp goto done;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp *same = 0;
697 68c76935 2019-02-19 stsp return NULL;
698 68c76935 2019-02-19 stsp }
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
701 68c76935 2019-02-19 stsp if (f1 == NULL)
702 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp
704 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
705 68c76935 2019-02-19 stsp if (f2 == NULL) {
706 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
707 68c76935 2019-02-19 stsp goto done;
708 68c76935 2019-02-19 stsp }
709 68c76935 2019-02-19 stsp
710 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp done:
712 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
713 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
714 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
715 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 68c76935 2019-02-19 stsp
717 6353ad76 2019-02-08 stsp return err;
718 6353ad76 2019-02-08 stsp }
719 6353ad76 2019-02-08 stsp
720 6353ad76 2019-02-08 stsp /*
721 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
723 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
724 6353ad76 2019-02-08 stsp */
725 6353ad76 2019-02-08 stsp static const struct got_error *
726 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
727 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
728 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
729 14c901f1 2019-08-08 stsp const char *label_deriv, struct got_repository *repo,
730 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
731 6353ad76 2019-02-08 stsp {
732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
733 6353ad76 2019-02-08 stsp int merged_fd = -1;
734 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
735 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
736 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
737 234035bc 2019-06-01 stsp int overlapcnt = 0;
738 af54ae4a 2019-02-19 stsp char *parent;
739 6353ad76 2019-02-08 stsp
740 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
741 234035bc 2019-06-01 stsp
742 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
743 af54ae4a 2019-02-19 stsp if (parent == NULL)
744 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
745 af54ae4a 2019-02-19 stsp
746 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
747 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
748 af54ae4a 2019-02-19 stsp
749 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
750 6353ad76 2019-02-08 stsp if (err)
751 6353ad76 2019-02-08 stsp goto done;
752 6353ad76 2019-02-08 stsp
753 af54ae4a 2019-02-19 stsp free(base_path);
754 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
755 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
756 af54ae4a 2019-02-19 stsp base_path = NULL;
757 af54ae4a 2019-02-19 stsp goto done;
758 af54ae4a 2019-02-19 stsp }
759 af54ae4a 2019-02-19 stsp
760 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
761 6353ad76 2019-02-08 stsp if (err)
762 6353ad76 2019-02-08 stsp goto done;
763 46b6ee73 2019-06-04 stsp if (blob_orig) {
764 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
765 46b6ee73 2019-06-04 stsp blob_orig);
766 1430b4e0 2019-03-27 stsp if (err)
767 1430b4e0 2019-03-27 stsp goto done;
768 1430b4e0 2019-03-27 stsp } else {
769 1430b4e0 2019-03-27 stsp /*
770 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
771 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
772 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
773 1430b4e0 2019-03-27 stsp */
774 1430b4e0 2019-03-27 stsp }
775 6353ad76 2019-02-08 stsp
776 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
777 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
778 6353ad76 2019-02-08 stsp if (err)
779 6353ad76 2019-02-08 stsp goto done;
780 6353ad76 2019-02-08 stsp
781 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
782 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 1ee397ad 2019-07-12 stsp if (err)
784 1ee397ad 2019-07-12 stsp goto done;
785 6353ad76 2019-02-08 stsp
786 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
787 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
788 816dc654 2019-02-16 stsp goto done;
789 68c76935 2019-02-19 stsp }
790 68c76935 2019-02-19 stsp
791 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
792 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
793 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
794 68c76935 2019-02-19 stsp merged_path);
795 68c76935 2019-02-19 stsp if (err)
796 68c76935 2019-02-19 stsp goto done;
797 816dc654 2019-02-16 stsp }
798 6353ad76 2019-02-08 stsp
799 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
800 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
801 70a0c8ec 2019-02-20 stsp goto done;
802 70a0c8ec 2019-02-20 stsp }
803 70a0c8ec 2019-02-20 stsp
804 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
805 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
806 230a42bd 2019-05-11 jcs ondisk_path);
807 2a57020b 2019-02-20 stsp unlink(merged_path);
808 6353ad76 2019-02-08 stsp goto done;
809 6353ad76 2019-02-08 stsp }
810 6353ad76 2019-02-08 stsp
811 6353ad76 2019-02-08 stsp done:
812 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
813 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
814 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
815 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
816 6353ad76 2019-02-08 stsp free(merged_path);
817 af54ae4a 2019-02-19 stsp free(base_path);
818 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
819 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
820 46b6ee73 2019-06-04 stsp free(blob_orig_path);
821 6353ad76 2019-02-08 stsp }
822 14c901f1 2019-08-08 stsp return err;
823 14c901f1 2019-08-08 stsp }
824 14c901f1 2019-08-08 stsp
825 14c901f1 2019-08-08 stsp /*
826 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
827 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
828 14c901f1 2019-08-08 stsp * acts as the second derived version.
829 14c901f1 2019-08-08 stsp */
830 14c901f1 2019-08-08 stsp static const struct got_error *
831 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
832 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
833 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
834 14c901f1 2019-08-08 stsp struct got_object_id *deriv_base_commit_id,
835 14c901f1 2019-08-08 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
836 14c901f1 2019-08-08 stsp void *progress_arg)
837 14c901f1 2019-08-08 stsp {
838 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
839 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
840 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
841 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
842 14c901f1 2019-08-08 stsp
843 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
844 14c901f1 2019-08-08 stsp
845 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
846 14c901f1 2019-08-08 stsp if (parent == NULL)
847 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
848 14c901f1 2019-08-08 stsp
849 14c901f1 2019-08-08 stsp free(base_path);
850 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
851 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
852 14c901f1 2019-08-08 stsp base_path = NULL;
853 14c901f1 2019-08-08 stsp goto done;
854 14c901f1 2019-08-08 stsp }
855 14c901f1 2019-08-08 stsp
856 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
857 14c901f1 2019-08-08 stsp if (err)
858 14c901f1 2019-08-08 stsp goto done;
859 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
860 14c901f1 2019-08-08 stsp blob_deriv);
861 14c901f1 2019-08-08 stsp if (err)
862 14c901f1 2019-08-08 stsp goto done;
863 14c901f1 2019-08-08 stsp
864 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
865 14c901f1 2019-08-08 stsp if (err)
866 14c901f1 2019-08-08 stsp goto done;
867 14c901f1 2019-08-08 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
868 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
869 14c901f1 2019-08-08 stsp goto done;
870 14c901f1 2019-08-08 stsp }
871 14c901f1 2019-08-08 stsp
872 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
873 14c901f1 2019-08-08 stsp ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
874 14c901f1 2019-08-08 stsp repo, progress_cb, progress_arg);
875 14c901f1 2019-08-08 stsp done:
876 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
877 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
878 14c901f1 2019-08-08 stsp free(base_path);
879 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
880 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
881 14c901f1 2019-08-08 stsp free(blob_deriv_path);
882 14c901f1 2019-08-08 stsp }
883 6353ad76 2019-02-08 stsp free(id_str);
884 818c7501 2019-07-11 stsp free(label_deriv);
885 4a1ddfc2 2019-01-12 stsp return err;
886 4a1ddfc2 2019-01-12 stsp }
887 4a1ddfc2 2019-01-12 stsp
888 4a1ddfc2 2019-01-12 stsp static const struct got_error *
889 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
890 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
891 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
892 13d9040b 2019-03-27 stsp int update_timestamps)
893 13d9040b 2019-03-27 stsp {
894 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
895 13d9040b 2019-03-27 stsp
896 13d9040b 2019-03-27 stsp if (ie == NULL)
897 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
898 13d9040b 2019-03-27 stsp if (ie)
899 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
900 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
901 13d9040b 2019-03-27 stsp update_timestamps);
902 13d9040b 2019-03-27 stsp else {
903 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
904 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
905 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
906 13d9040b 2019-03-27 stsp if (!err)
907 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
908 13d9040b 2019-03-27 stsp }
909 13d9040b 2019-03-27 stsp return err;
910 13d9040b 2019-03-27 stsp }
911 13d9040b 2019-03-27 stsp
912 13d9040b 2019-03-27 stsp static const struct got_error *
913 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
914 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
915 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
916 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
917 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
918 9d31a1d8 2018-03-11 stsp {
919 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
920 507dc3bb 2018-12-29 stsp int fd = -1;
921 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
922 507dc3bb 2018-12-29 stsp int update = 0;
923 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
924 9d31a1d8 2018-03-11 stsp
925 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
926 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
927 9d31a1d8 2018-03-11 stsp if (fd == -1) {
928 21908da4 2019-01-13 stsp if (errno == ENOENT) {
929 21908da4 2019-01-13 stsp char *parent = dirname(path);
930 21908da4 2019-01-13 stsp if (parent == NULL)
931 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
932 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
933 21908da4 2019-01-13 stsp if (err)
934 21908da4 2019-01-13 stsp return err;
935 21908da4 2019-01-13 stsp fd = open(ondisk_path,
936 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
937 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
938 21908da4 2019-01-13 stsp if (fd == -1)
939 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
940 230a42bd 2019-05-11 jcs ondisk_path);
941 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
942 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
943 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
944 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
945 507dc3bb 2018-12-29 stsp goto done;
946 d70b8e30 2018-12-27 stsp } else {
947 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
948 507dc3bb 2018-12-29 stsp ondisk_path);
949 507dc3bb 2018-12-29 stsp if (err)
950 507dc3bb 2018-12-29 stsp goto done;
951 507dc3bb 2018-12-29 stsp update = 1;
952 9d31a1d8 2018-03-11 stsp }
953 507dc3bb 2018-12-29 stsp } else
954 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
955 9d31a1d8 2018-03-11 stsp }
956 9d31a1d8 2018-03-11 stsp
957 a378724f 2019-02-10 stsp if (restoring_missing_file)
958 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
959 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
960 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
961 a378724f 2019-02-10 stsp else
962 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
963 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
964 1ee397ad 2019-07-12 stsp if (err)
965 1ee397ad 2019-07-12 stsp goto done;
966 d7b62c98 2018-12-27 stsp
967 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
968 9d31a1d8 2018-03-11 stsp do {
969 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
970 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
971 9d31a1d8 2018-03-11 stsp if (err)
972 9d31a1d8 2018-03-11 stsp break;
973 9d31a1d8 2018-03-11 stsp if (len > 0) {
974 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
975 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
976 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
977 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
978 b87c6f83 2018-12-24 stsp goto done;
979 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
980 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
981 b87c6f83 2018-12-24 stsp goto done;
982 9d31a1d8 2018-03-11 stsp }
983 61d6eaa3 2018-12-24 stsp hdrlen = 0;
984 9d31a1d8 2018-03-11 stsp }
985 9d31a1d8 2018-03-11 stsp } while (len != 0);
986 9d31a1d8 2018-03-11 stsp
987 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
988 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
989 816dc654 2019-02-16 stsp goto done;
990 816dc654 2019-02-16 stsp }
991 9d31a1d8 2018-03-11 stsp
992 507dc3bb 2018-12-29 stsp if (update) {
993 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
994 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
995 230a42bd 2019-05-11 jcs ondisk_path);
996 2a57020b 2019-02-20 stsp unlink(tmppath);
997 68ed9ba5 2019-02-10 stsp goto done;
998 68ed9ba5 2019-02-10 stsp }
999 68ed9ba5 2019-02-10 stsp }
1000 ba8a0d4d 2019-02-10 stsp
1001 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
1002 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1003 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1004 507dc3bb 2018-12-29 stsp goto done;
1005 507dc3bb 2018-12-29 stsp }
1006 ba8a0d4d 2019-02-10 stsp } else {
1007 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1008 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1009 68ed9ba5 2019-02-10 stsp goto done;
1010 68ed9ba5 2019-02-10 stsp }
1011 507dc3bb 2018-12-29 stsp }
1012 507dc3bb 2018-12-29 stsp
1013 9d31a1d8 2018-03-11 stsp done:
1014 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1015 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1016 507dc3bb 2018-12-29 stsp free(tmppath);
1017 6353ad76 2019-02-08 stsp return err;
1018 6353ad76 2019-02-08 stsp }
1019 6353ad76 2019-02-08 stsp
1020 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1021 6353ad76 2019-02-08 stsp static const struct got_error *
1022 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1023 7154f6ce 2019-03-27 stsp {
1024 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1025 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1026 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1027 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1028 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1029 7154f6ce 2019-03-27 stsp };
1030 7154f6ce 2019-03-27 stsp int i = 0;
1031 7154f6ce 2019-03-27 stsp char *line;
1032 7154f6ce 2019-03-27 stsp size_t len;
1033 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1034 7154f6ce 2019-03-27 stsp
1035 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1036 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1037 7154f6ce 2019-03-27 stsp if (line == NULL) {
1038 7154f6ce 2019-03-27 stsp if (feof(f))
1039 7154f6ce 2019-03-27 stsp break;
1040 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1041 7154f6ce 2019-03-27 stsp break;
1042 7154f6ce 2019-03-27 stsp }
1043 7154f6ce 2019-03-27 stsp
1044 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1045 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1046 19332e6d 2019-05-13 stsp == 0)
1047 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1048 7154f6ce 2019-03-27 stsp else
1049 7154f6ce 2019-03-27 stsp i++;
1050 7154f6ce 2019-03-27 stsp }
1051 7154f6ce 2019-03-27 stsp }
1052 7154f6ce 2019-03-27 stsp
1053 7154f6ce 2019-03-27 stsp return err;
1054 e2b1e152 2019-07-13 stsp }
1055 e2b1e152 2019-07-13 stsp
1056 e2b1e152 2019-07-13 stsp static int
1057 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1058 e2b1e152 2019-07-13 stsp {
1059 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1060 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1061 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1062 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1063 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1064 c363b2c1 2019-08-03 stsp }
1065 c363b2c1 2019-08-03 stsp
1066 c363b2c1 2019-08-03 stsp static unsigned char
1067 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1068 c363b2c1 2019-08-03 stsp {
1069 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1070 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1071 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1072 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1073 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1074 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1075 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1076 c363b2c1 2019-08-03 stsp default:
1077 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1078 c363b2c1 2019-08-03 stsp }
1079 7154f6ce 2019-03-27 stsp }
1080 7154f6ce 2019-03-27 stsp
1081 7154f6ce 2019-03-27 stsp static const struct got_error *
1082 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1083 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1084 b8f41171 2019-02-10 stsp struct got_repository *repo)
1085 6353ad76 2019-02-08 stsp {
1086 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1087 6353ad76 2019-02-08 stsp struct got_object_id id;
1088 6353ad76 2019-02-08 stsp size_t hdrlen;
1089 6353ad76 2019-02-08 stsp FILE *f = NULL;
1090 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1091 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1092 6353ad76 2019-02-08 stsp size_t flen, blen;
1093 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1094 6353ad76 2019-02-08 stsp
1095 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1096 6353ad76 2019-02-08 stsp
1097 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1098 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1099 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1100 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1101 abb4604f 2019-07-27 stsp else
1102 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1103 a378724f 2019-02-10 stsp return NULL;
1104 a378724f 2019-02-10 stsp }
1105 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1106 a378724f 2019-02-10 stsp }
1107 6353ad76 2019-02-08 stsp
1108 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1109 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1110 339c298e 2019-07-27 stsp return NULL;
1111 3f148bc6 2019-07-27 stsp }
1112 efdd40df 2019-07-27 stsp
1113 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1114 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1115 339c298e 2019-07-27 stsp return NULL;
1116 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1117 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1118 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1119 339c298e 2019-07-27 stsp return NULL;
1120 d00136be 2019-03-26 stsp }
1121 b8f41171 2019-02-10 stsp
1122 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1123 339c298e 2019-07-27 stsp return NULL;
1124 6353ad76 2019-02-08 stsp
1125 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1126 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1127 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1128 c363b2c1 2019-08-03 stsp else
1129 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1130 c363b2c1 2019-08-03 stsp
1131 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1132 6353ad76 2019-02-08 stsp if (err)
1133 339c298e 2019-07-27 stsp return err;
1134 6353ad76 2019-02-08 stsp
1135 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1136 6353ad76 2019-02-08 stsp if (f == NULL) {
1137 339c298e 2019-07-27 stsp err = got_error_from_errno2("fopen", abspath);
1138 6353ad76 2019-02-08 stsp goto done;
1139 6353ad76 2019-02-08 stsp }
1140 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1141 656b1f76 2019-05-11 jcs for (;;) {
1142 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1143 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1144 6353ad76 2019-02-08 stsp if (err)
1145 7154f6ce 2019-03-27 stsp goto done;
1146 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1147 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1148 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1149 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1150 7154f6ce 2019-03-27 stsp goto done;
1151 80c5c120 2019-02-19 stsp }
1152 6353ad76 2019-02-08 stsp if (blen == 0) {
1153 6353ad76 2019-02-08 stsp if (flen != 0)
1154 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1155 6353ad76 2019-02-08 stsp break;
1156 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1157 6353ad76 2019-02-08 stsp if (blen != 0)
1158 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1159 6353ad76 2019-02-08 stsp break;
1160 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1161 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1162 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1163 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1164 6353ad76 2019-02-08 stsp break;
1165 6353ad76 2019-02-08 stsp }
1166 6353ad76 2019-02-08 stsp } else {
1167 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1168 6353ad76 2019-02-08 stsp break;
1169 6353ad76 2019-02-08 stsp }
1170 6353ad76 2019-02-08 stsp hdrlen = 0;
1171 6353ad76 2019-02-08 stsp }
1172 7154f6ce 2019-03-27 stsp
1173 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1174 7154f6ce 2019-03-27 stsp rewind(f);
1175 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1176 7154f6ce 2019-03-27 stsp }
1177 6353ad76 2019-02-08 stsp done:
1178 6353ad76 2019-02-08 stsp if (blob)
1179 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1180 6353ad76 2019-02-08 stsp if (f)
1181 6353ad76 2019-02-08 stsp fclose(f);
1182 9d31a1d8 2018-03-11 stsp return err;
1183 e2b1e152 2019-07-13 stsp }
1184 e2b1e152 2019-07-13 stsp
1185 e2b1e152 2019-07-13 stsp /*
1186 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1187 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1188 e2b1e152 2019-07-13 stsp */
1189 e2b1e152 2019-07-13 stsp static const struct got_error *
1190 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1191 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1192 e2b1e152 2019-07-13 stsp {
1193 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1194 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1195 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1196 e2b1e152 2019-07-13 stsp
1197 e2b1e152 2019-07-13 stsp return NULL;
1198 9d31a1d8 2018-03-11 stsp }
1199 9d31a1d8 2018-03-11 stsp
1200 9d31a1d8 2018-03-11 stsp static const struct got_error *
1201 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1202 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1203 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1204 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1205 0584f854 2019-04-06 stsp void *progress_arg)
1206 9d31a1d8 2018-03-11 stsp {
1207 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1208 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1209 6353ad76 2019-02-08 stsp char *ondisk_path;
1210 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1211 b8f41171 2019-02-10 stsp struct stat sb;
1212 9d31a1d8 2018-03-11 stsp
1213 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1214 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1215 6353ad76 2019-02-08 stsp
1216 abb4604f 2019-07-27 stsp if (ie) {
1217 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1218 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1219 a76c42e6 2019-08-03 stsp goto done;
1220 a76c42e6 2019-08-03 stsp }
1221 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1222 abb4604f 2019-07-27 stsp if (err)
1223 abb4604f 2019-07-27 stsp goto done;
1224 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1225 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1226 abb4604f 2019-07-27 stsp } else
1227 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1228 6353ad76 2019-02-08 stsp
1229 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1230 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1231 b8f41171 2019-02-10 stsp goto done;
1232 b8f41171 2019-02-10 stsp }
1233 b8f41171 2019-02-10 stsp
1234 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1235 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1236 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1237 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1238 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1239 e2b1e152 2019-07-13 stsp if (err)
1240 e2b1e152 2019-07-13 stsp goto done;
1241 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1242 b8f41171 2019-02-10 stsp path);
1243 6353ad76 2019-02-08 stsp goto done;
1244 a378724f 2019-02-10 stsp }
1245 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1246 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1247 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1248 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1249 b8f41171 2019-02-10 stsp goto done;
1250 e2b1e152 2019-07-13 stsp }
1251 9d31a1d8 2018-03-11 stsp }
1252 9d31a1d8 2018-03-11 stsp
1253 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1254 8da9e5f4 2019-01-12 stsp if (err)
1255 6353ad76 2019-02-08 stsp goto done;
1256 512f0d0e 2019-01-02 stsp
1257 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1258 234035bc 2019-06-01 stsp int update_timestamps;
1259 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1260 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1261 234035bc 2019-06-01 stsp struct got_object_id id2;
1262 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1263 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1264 234035bc 2019-06-01 stsp if (err)
1265 234035bc 2019-06-01 stsp goto done;
1266 234035bc 2019-06-01 stsp }
1267 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1268 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1269 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1270 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1271 234035bc 2019-06-01 stsp if (blob2)
1272 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1273 234035bc 2019-06-01 stsp /*
1274 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1275 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1276 234035bc 2019-06-01 stsp * unmodified files again.
1277 234035bc 2019-06-01 stsp */
1278 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1279 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1280 234035bc 2019-06-01 stsp update_timestamps);
1281 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1282 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1283 1ee397ad 2019-07-12 stsp if (err)
1284 1ee397ad 2019-07-12 stsp goto done;
1285 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1286 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1287 13d9040b 2019-03-27 stsp if (err)
1288 13d9040b 2019-03-27 stsp goto done;
1289 13d9040b 2019-03-27 stsp } else {
1290 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1291 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1292 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1293 13d9040b 2019-03-27 stsp if (err)
1294 13d9040b 2019-03-27 stsp goto done;
1295 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1296 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1297 13d9040b 2019-03-27 stsp if (err)
1298 13d9040b 2019-03-27 stsp goto done;
1299 13d9040b 2019-03-27 stsp }
1300 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1301 6353ad76 2019-02-08 stsp done:
1302 6353ad76 2019-02-08 stsp free(ondisk_path);
1303 8da9e5f4 2019-01-12 stsp return err;
1304 90285c3b 2019-01-08 stsp }
1305 90285c3b 2019-01-08 stsp
1306 90285c3b 2019-01-08 stsp static const struct got_error *
1307 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1308 90285c3b 2019-01-08 stsp {
1309 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1310 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1311 90285c3b 2019-01-08 stsp
1312 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1313 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1314 90285c3b 2019-01-08 stsp
1315 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1316 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1317 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1318 c97665ae 2019-01-13 stsp } else {
1319 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1320 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1321 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1322 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1323 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1324 230a42bd 2019-05-11 jcs parent);
1325 90285c3b 2019-01-08 stsp break;
1326 90285c3b 2019-01-08 stsp }
1327 90285c3b 2019-01-08 stsp parent = dirname(parent);
1328 90285c3b 2019-01-08 stsp }
1329 90285c3b 2019-01-08 stsp }
1330 90285c3b 2019-01-08 stsp free(ondisk_path);
1331 90285c3b 2019-01-08 stsp return err;
1332 512f0d0e 2019-01-02 stsp }
1333 512f0d0e 2019-01-02 stsp
1334 708d8e67 2019-03-27 stsp static const struct got_error *
1335 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1336 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1337 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1338 708d8e67 2019-03-27 stsp {
1339 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1340 708d8e67 2019-03-27 stsp unsigned char status;
1341 708d8e67 2019-03-27 stsp struct stat sb;
1342 708d8e67 2019-03-27 stsp char *ondisk_path;
1343 708d8e67 2019-03-27 stsp
1344 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1345 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1346 a76c42e6 2019-08-03 stsp
1347 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1348 708d8e67 2019-03-27 stsp == -1)
1349 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1350 708d8e67 2019-03-27 stsp
1351 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1352 708d8e67 2019-03-27 stsp if (err)
1353 708d8e67 2019-03-27 stsp return err;
1354 708d8e67 2019-03-27 stsp
1355 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1356 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1357 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1358 1ee397ad 2019-07-12 stsp if (err)
1359 1ee397ad 2019-07-12 stsp return err;
1360 fc6346c4 2019-03-27 stsp /*
1361 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1362 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1363 fc6346c4 2019-03-27 stsp */
1364 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1365 fc6346c4 2019-03-27 stsp 0);
1366 708d8e67 2019-03-27 stsp if (err)
1367 708d8e67 2019-03-27 stsp return err;
1368 fc6346c4 2019-03-27 stsp } else {
1369 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1370 1ee397ad 2019-07-12 stsp if (err)
1371 1ee397ad 2019-07-12 stsp return err;
1372 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1373 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1374 fc6346c4 2019-03-27 stsp if (err)
1375 fc6346c4 2019-03-27 stsp return err;
1376 fc6346c4 2019-03-27 stsp }
1377 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1378 708d8e67 2019-03-27 stsp }
1379 708d8e67 2019-03-27 stsp
1380 fc6346c4 2019-03-27 stsp return err;
1381 708d8e67 2019-03-27 stsp }
1382 708d8e67 2019-03-27 stsp
1383 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1384 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1385 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1386 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1387 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1388 8da9e5f4 2019-01-12 stsp void *progress_arg;
1389 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1390 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1391 8da9e5f4 2019-01-12 stsp };
1392 8da9e5f4 2019-01-12 stsp
1393 512f0d0e 2019-01-02 stsp static const struct got_error *
1394 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1395 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1396 512f0d0e 2019-01-02 stsp {
1397 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1398 0584f854 2019-04-06 stsp
1399 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1400 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1401 512f0d0e 2019-01-02 stsp
1402 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1403 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1404 8da9e5f4 2019-01-12 stsp }
1405 512f0d0e 2019-01-02 stsp
1406 8da9e5f4 2019-01-12 stsp static const struct got_error *
1407 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1408 8da9e5f4 2019-01-12 stsp {
1409 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1410 7a9df742 2019-01-08 stsp
1411 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1412 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1413 0584f854 2019-04-06 stsp
1414 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1415 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1416 9d31a1d8 2018-03-11 stsp }
1417 9d31a1d8 2018-03-11 stsp
1418 9d31a1d8 2018-03-11 stsp static const struct got_error *
1419 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1420 9d31a1d8 2018-03-11 stsp {
1421 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1422 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1423 8da9e5f4 2019-01-12 stsp char *path;
1424 9d31a1d8 2018-03-11 stsp
1425 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1426 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1427 0584f854 2019-04-06 stsp
1428 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1429 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1430 8da9e5f4 2019-01-12 stsp == -1)
1431 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1432 9d31a1d8 2018-03-11 stsp
1433 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1434 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1435 8da9e5f4 2019-01-12 stsp else
1436 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1437 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1438 9d31a1d8 2018-03-11 stsp
1439 8da9e5f4 2019-01-12 stsp free(path);
1440 0cd1c46a 2019-03-11 stsp return err;
1441 0cd1c46a 2019-03-11 stsp }
1442 0cd1c46a 2019-03-11 stsp
1443 818c7501 2019-07-11 stsp static const struct got_error *
1444 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1445 0cd1c46a 2019-03-11 stsp {
1446 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1447 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1448 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1449 0cd1c46a 2019-03-11 stsp
1450 517bab73 2019-03-11 stsp *refname = NULL;
1451 517bab73 2019-03-11 stsp
1452 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1453 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1454 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1455 0cd1c46a 2019-03-11 stsp
1456 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1457 0647c563 2019-03-11 stsp == -1) {
1458 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1459 0647c563 2019-03-11 stsp *refname = NULL;
1460 0cd1c46a 2019-03-11 stsp }
1461 517bab73 2019-03-11 stsp free(uuidstr);
1462 517bab73 2019-03-11 stsp return err;
1463 517bab73 2019-03-11 stsp }
1464 517bab73 2019-03-11 stsp
1465 818c7501 2019-07-11 stsp const struct got_error *
1466 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1467 818c7501 2019-07-11 stsp {
1468 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1469 818c7501 2019-07-11 stsp }
1470 818c7501 2019-07-11 stsp
1471 818c7501 2019-07-11 stsp static const struct got_error *
1472 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1473 818c7501 2019-07-11 stsp {
1474 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1475 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1476 818c7501 2019-07-11 stsp }
1477 818c7501 2019-07-11 stsp
1478 818c7501 2019-07-11 stsp static const struct got_error *
1479 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1480 818c7501 2019-07-11 stsp {
1481 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1482 818c7501 2019-07-11 stsp }
1483 818c7501 2019-07-11 stsp
1484 818c7501 2019-07-11 stsp static const struct got_error *
1485 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1486 818c7501 2019-07-11 stsp {
1487 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1488 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1489 818c7501 2019-07-11 stsp }
1490 818c7501 2019-07-11 stsp
1491 818c7501 2019-07-11 stsp static const struct got_error *
1492 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1493 818c7501 2019-07-11 stsp {
1494 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1495 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1496 0ebf8283 2019-07-24 stsp }
1497 0ebf8283 2019-07-24 stsp
1498 0ebf8283 2019-07-24 stsp static const struct got_error *
1499 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1500 0ebf8283 2019-07-24 stsp {
1501 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1502 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1503 0ebf8283 2019-07-24 stsp }
1504 0ebf8283 2019-07-24 stsp
1505 0ebf8283 2019-07-24 stsp static const struct got_error *
1506 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1507 0ebf8283 2019-07-24 stsp {
1508 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1509 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1510 0ebf8283 2019-07-24 stsp }
1511 0ebf8283 2019-07-24 stsp
1512 0ebf8283 2019-07-24 stsp static const struct got_error *
1513 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1514 0ebf8283 2019-07-24 stsp {
1515 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1516 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1517 818c7501 2019-07-11 stsp }
1518 818c7501 2019-07-11 stsp
1519 0ebf8283 2019-07-24 stsp static const struct got_error *
1520 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1521 0ebf8283 2019-07-24 stsp {
1522 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1523 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1524 0ebf8283 2019-07-24 stsp }
1525 818c7501 2019-07-11 stsp
1526 0ebf8283 2019-07-24 stsp const struct got_error *
1527 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1528 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1529 0ebf8283 2019-07-24 stsp {
1530 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1531 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1532 0ebf8283 2019-07-24 stsp *path = NULL;
1533 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1534 0ebf8283 2019-07-24 stsp }
1535 0ebf8283 2019-07-24 stsp return NULL;
1536 0ebf8283 2019-07-24 stsp }
1537 0ebf8283 2019-07-24 stsp
1538 517bab73 2019-03-11 stsp /*
1539 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1540 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1541 517bab73 2019-03-11 stsp */
1542 517bab73 2019-03-11 stsp static const struct got_error *
1543 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1544 517bab73 2019-03-11 stsp {
1545 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1546 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1547 517bab73 2019-03-11 stsp char *refname;
1548 517bab73 2019-03-11 stsp
1549 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1550 517bab73 2019-03-11 stsp if (err)
1551 517bab73 2019-03-11 stsp return err;
1552 0cd1c46a 2019-03-11 stsp
1553 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1554 0cd1c46a 2019-03-11 stsp if (err)
1555 0cd1c46a 2019-03-11 stsp goto done;
1556 0cd1c46a 2019-03-11 stsp
1557 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1558 0cd1c46a 2019-03-11 stsp done:
1559 0cd1c46a 2019-03-11 stsp free(refname);
1560 0cd1c46a 2019-03-11 stsp if (ref)
1561 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1562 3e3a69f1 2019-07-25 stsp return err;
1563 3e3a69f1 2019-07-25 stsp }
1564 3e3a69f1 2019-07-25 stsp
1565 3e3a69f1 2019-07-25 stsp static const struct got_error *
1566 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1567 3e3a69f1 2019-07-25 stsp {
1568 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1569 3e3a69f1 2019-07-25 stsp
1570 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1571 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1572 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1573 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1574 3e3a69f1 2019-07-25 stsp }
1575 9d31a1d8 2018-03-11 stsp return err;
1576 9d31a1d8 2018-03-11 stsp }
1577 9d31a1d8 2018-03-11 stsp
1578 3e3a69f1 2019-07-25 stsp
1579 ebf99748 2019-05-09 stsp static const struct got_error *
1580 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1581 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1582 ebf99748 2019-05-09 stsp {
1583 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1584 ebf99748 2019-05-09 stsp FILE *index = NULL;
1585 0cd1c46a 2019-03-11 stsp
1586 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1587 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1588 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1589 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1590 ebf99748 2019-05-09 stsp
1591 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1592 3e3a69f1 2019-07-25 stsp if (err)
1593 ebf99748 2019-05-09 stsp goto done;
1594 ebf99748 2019-05-09 stsp
1595 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1596 ebf99748 2019-05-09 stsp if (index == NULL) {
1597 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1598 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1599 ebf99748 2019-05-09 stsp } else {
1600 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1601 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1602 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1603 ebf99748 2019-05-09 stsp }
1604 ebf99748 2019-05-09 stsp done:
1605 ebf99748 2019-05-09 stsp if (err) {
1606 ebf99748 2019-05-09 stsp free(*fileindex_path);
1607 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1608 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1609 ebf99748 2019-05-09 stsp *fileindex = NULL;
1610 ebf99748 2019-05-09 stsp }
1611 ebf99748 2019-05-09 stsp return err;
1612 ebf99748 2019-05-09 stsp }
1613 c932eeeb 2019-05-22 stsp
1614 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1615 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1616 c932eeeb 2019-05-22 stsp const char *path;
1617 c932eeeb 2019-05-22 stsp size_t path_len;
1618 c932eeeb 2019-05-22 stsp const char *entry_name;
1619 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1620 a484d721 2019-06-10 stsp void *progress_arg;
1621 c932eeeb 2019-05-22 stsp };
1622 ebf99748 2019-05-09 stsp
1623 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1624 c932eeeb 2019-05-22 stsp static const struct got_error *
1625 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1626 c932eeeb 2019-05-22 stsp {
1627 1ee397ad 2019-07-12 stsp const struct got_error *err;
1628 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1629 c932eeeb 2019-05-22 stsp
1630 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1631 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1632 c932eeeb 2019-05-22 stsp return NULL;
1633 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1634 102ff934 2019-06-11 stsp return NULL;
1635 102ff934 2019-06-11 stsp
1636 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1637 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1638 c932eeeb 2019-05-22 stsp return NULL;
1639 c932eeeb 2019-05-22 stsp
1640 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1641 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1642 edd02c5e 2019-07-11 stsp ie->path);
1643 1ee397ad 2019-07-12 stsp if (err)
1644 1ee397ad 2019-07-12 stsp return err;
1645 1ee397ad 2019-07-12 stsp }
1646 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1647 c932eeeb 2019-05-22 stsp return NULL;
1648 9c6338c4 2019-06-02 stsp }
1649 9c6338c4 2019-06-02 stsp
1650 9c6338c4 2019-06-02 stsp static const struct got_error *
1651 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1652 9c6338c4 2019-06-02 stsp {
1653 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1654 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1655 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1656 9c6338c4 2019-06-02 stsp
1657 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1658 9c6338c4 2019-06-02 stsp fileindex_path);
1659 9c6338c4 2019-06-02 stsp if (err)
1660 9c6338c4 2019-06-02 stsp goto done;
1661 9c6338c4 2019-06-02 stsp
1662 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1663 9c6338c4 2019-06-02 stsp if (err)
1664 9c6338c4 2019-06-02 stsp goto done;
1665 9c6338c4 2019-06-02 stsp
1666 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1667 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1668 9c6338c4 2019-06-02 stsp fileindex_path);
1669 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1670 9c6338c4 2019-06-02 stsp }
1671 9c6338c4 2019-06-02 stsp done:
1672 9c6338c4 2019-06-02 stsp if (new_index)
1673 9c6338c4 2019-06-02 stsp fclose(new_index);
1674 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1675 9c6338c4 2019-06-02 stsp return err;
1676 c932eeeb 2019-05-22 stsp }
1677 6ced4176 2019-07-12 stsp
1678 6ced4176 2019-07-12 stsp static const struct got_error *
1679 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1680 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1681 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1682 6ced4176 2019-07-12 stsp {
1683 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1684 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1685 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1686 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1687 6ced4176 2019-07-12 stsp
1688 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1689 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1690 6ced4176 2019-07-12 stsp *tree_id = NULL;
1691 6ced4176 2019-07-12 stsp
1692 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1693 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1694 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1695 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1696 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1698 6ced4176 2019-07-12 stsp goto done;
1699 6ced4176 2019-07-12 stsp }
1700 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1701 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1702 6ced4176 2019-07-12 stsp if (err)
1703 6ced4176 2019-07-12 stsp goto done;
1704 6ced4176 2019-07-12 stsp return NULL;
1705 6ced4176 2019-07-12 stsp }
1706 6ced4176 2019-07-12 stsp
1707 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1708 6ced4176 2019-07-12 stsp
1709 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1710 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1711 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1712 6ced4176 2019-07-12 stsp goto done;
1713 6ced4176 2019-07-12 stsp }
1714 6ced4176 2019-07-12 stsp
1715 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1716 6ced4176 2019-07-12 stsp in_repo_path);
1717 6ced4176 2019-07-12 stsp if (err)
1718 6ced4176 2019-07-12 stsp goto done;
1719 6ced4176 2019-07-12 stsp
1720 6ced4176 2019-07-12 stsp free(in_repo_path);
1721 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1722 6ced4176 2019-07-12 stsp
1723 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1724 6ced4176 2019-07-12 stsp if (err)
1725 6ced4176 2019-07-12 stsp goto done;
1726 c932eeeb 2019-05-22 stsp
1727 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1728 6ced4176 2019-07-12 stsp /* Check out a single file. */
1729 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1730 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1731 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1732 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1733 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1734 6ced4176 2019-07-12 stsp goto done;
1735 6ced4176 2019-07-12 stsp }
1736 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1737 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1738 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1739 6ced4176 2019-07-12 stsp goto done;
1740 6ced4176 2019-07-12 stsp }
1741 6ced4176 2019-07-12 stsp } else {
1742 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1743 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1744 6ced4176 2019-07-12 stsp if (err)
1745 6ced4176 2019-07-12 stsp return err;
1746 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1747 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1748 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1749 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1750 6ced4176 2019-07-12 stsp goto done;
1751 6ced4176 2019-07-12 stsp }
1752 6ced4176 2019-07-12 stsp }
1753 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1754 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1755 6ced4176 2019-07-12 stsp } else {
1756 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1757 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1758 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1759 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1760 6ced4176 2019-07-12 stsp goto done;
1761 6ced4176 2019-07-12 stsp }
1762 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1763 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1764 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1765 6ced4176 2019-07-12 stsp goto done;
1766 6ced4176 2019-07-12 stsp }
1767 6ced4176 2019-07-12 stsp }
1768 6ced4176 2019-07-12 stsp done:
1769 6ced4176 2019-07-12 stsp free(id);
1770 6ced4176 2019-07-12 stsp free(in_repo_path);
1771 6ced4176 2019-07-12 stsp if (err) {
1772 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1773 6ced4176 2019-07-12 stsp free(*tree_relpath);
1774 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1775 6ced4176 2019-07-12 stsp free(*tree_id);
1776 6ced4176 2019-07-12 stsp *tree_id = NULL;
1777 6ced4176 2019-07-12 stsp }
1778 07ed472d 2019-07-12 stsp return err;
1779 07ed472d 2019-07-12 stsp }
1780 07ed472d 2019-07-12 stsp
1781 07ed472d 2019-07-12 stsp static const struct got_error *
1782 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1783 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1784 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1785 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1786 07ed472d 2019-07-12 stsp {
1787 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1788 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1789 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1790 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1791 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1792 07ed472d 2019-07-12 stsp
1793 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1794 07ed472d 2019-07-12 stsp if (err)
1795 07ed472d 2019-07-12 stsp goto done;
1796 07ed472d 2019-07-12 stsp
1797 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1798 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1799 07ed472d 2019-07-12 stsp if (err)
1800 07ed472d 2019-07-12 stsp goto done;
1801 07ed472d 2019-07-12 stsp
1802 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1803 07ed472d 2019-07-12 stsp if (err)
1804 07ed472d 2019-07-12 stsp goto done;
1805 07ed472d 2019-07-12 stsp
1806 07ed472d 2019-07-12 stsp if (entry_name &&
1807 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1808 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1809 07ed472d 2019-07-12 stsp goto done;
1810 07ed472d 2019-07-12 stsp }
1811 07ed472d 2019-07-12 stsp
1812 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1813 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1814 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1815 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1816 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1817 07ed472d 2019-07-12 stsp arg.repo = repo;
1818 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1819 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1820 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1821 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1822 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1823 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1824 07ed472d 2019-07-12 stsp done:
1825 07ed472d 2019-07-12 stsp if (tree)
1826 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1827 07ed472d 2019-07-12 stsp if (commit)
1828 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1829 6ced4176 2019-07-12 stsp return err;
1830 6ced4176 2019-07-12 stsp }
1831 6ced4176 2019-07-12 stsp
1832 9d31a1d8 2018-03-11 stsp const struct got_error *
1833 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1834 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1835 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1836 f2ea84fa 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1837 9d31a1d8 2018-03-11 stsp {
1838 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1839 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1840 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1841 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1842 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1843 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1844 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1845 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1846 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1847 f2ea84fa 2019-07-27 stsp int entry_type;
1848 f2ea84fa 2019-07-27 stsp char *relpath;
1849 f2ea84fa 2019-07-27 stsp char *entry_name;
1850 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1851 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1852 9d31a1d8 2018-03-11 stsp
1853 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1854 f2ea84fa 2019-07-27 stsp
1855 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1856 9d31a1d8 2018-03-11 stsp if (err)
1857 9d31a1d8 2018-03-11 stsp return err;
1858 93a30277 2018-12-24 stsp
1859 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1860 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1861 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1862 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1863 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1864 f2ea84fa 2019-07-27 stsp goto done;
1865 f2ea84fa 2019-07-27 stsp }
1866 6ced4176 2019-07-12 stsp
1867 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1868 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1869 f2ea84fa 2019-07-27 stsp if (err) {
1870 f2ea84fa 2019-07-27 stsp free(tpd);
1871 6ced4176 2019-07-12 stsp goto done;
1872 6ced4176 2019-07-12 stsp }
1873 f2ea84fa 2019-07-27 stsp
1874 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1875 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1876 f2ea84fa 2019-07-27 stsp if (err) {
1877 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1878 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1879 f2ea84fa 2019-07-27 stsp free(tpd);
1880 f2ea84fa 2019-07-27 stsp goto done;
1881 f2ea84fa 2019-07-27 stsp }
1882 f2ea84fa 2019-07-27 stsp } else
1883 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1884 f2ea84fa 2019-07-27 stsp
1885 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1886 6ced4176 2019-07-12 stsp }
1887 6ced4176 2019-07-12 stsp
1888 51514078 2018-12-25 stsp /*
1889 51514078 2018-12-25 stsp * Read the file index.
1890 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1891 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1892 51514078 2018-12-25 stsp */
1893 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1894 8da9e5f4 2019-01-12 stsp if (err)
1895 c4cdcb68 2019-04-03 stsp goto done;
1896 c4cdcb68 2019-04-03 stsp
1897 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1898 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1899 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1900 f2ea84fa 2019-07-27 stsp
1901 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1902 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1903 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1904 f2ea84fa 2019-07-27 stsp if (err)
1905 f2ea84fa 2019-07-27 stsp break;
1906 f2ea84fa 2019-07-27 stsp
1907 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1908 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1909 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1910 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
1911 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1912 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1913 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1914 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1915 f2ea84fa 2019-07-27 stsp if (err)
1916 f2ea84fa 2019-07-27 stsp break;
1917 f2ea84fa 2019-07-27 stsp
1918 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1919 711d9cd9 2019-07-12 stsp }
1920 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1921 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1922 af12c6b9 2019-06-04 stsp err = sync_err;
1923 9d31a1d8 2018-03-11 stsp done:
1924 9c6338c4 2019-06-02 stsp free(fileindex_path);
1925 edfa7d7f 2018-09-11 stsp if (tree)
1926 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1927 9d31a1d8 2018-03-11 stsp if (commit)
1928 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1929 5ade8233 2019-07-12 stsp if (fileindex)
1930 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1931 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1932 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1933 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1934 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1935 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1936 f2ea84fa 2019-07-27 stsp free(tpd);
1937 f2ea84fa 2019-07-27 stsp }
1938 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1939 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1940 9d31a1d8 2018-03-11 stsp err = unlockerr;
1941 f8d1f275 2019-02-04 stsp return err;
1942 f8d1f275 2019-02-04 stsp }
1943 f8d1f275 2019-02-04 stsp
1944 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1945 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1946 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1947 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1948 234035bc 2019-06-01 stsp void *progress_arg;
1949 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1950 234035bc 2019-06-01 stsp void *cancel_arg;
1951 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1952 234035bc 2019-06-01 stsp };
1953 234035bc 2019-06-01 stsp
1954 234035bc 2019-06-01 stsp static const struct got_error *
1955 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1956 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1957 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1958 234035bc 2019-06-01 stsp struct got_repository *repo)
1959 234035bc 2019-06-01 stsp {
1960 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1961 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1962 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1963 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1964 234035bc 2019-06-01 stsp struct stat sb;
1965 234035bc 2019-06-01 stsp unsigned char status;
1966 234035bc 2019-06-01 stsp int local_changes_subsumed;
1967 234035bc 2019-06-01 stsp
1968 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1969 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1970 d6c87207 2019-08-02 stsp strlen(path2));
1971 1ee397ad 2019-07-12 stsp if (ie == NULL)
1972 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1973 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1974 234035bc 2019-06-01 stsp
1975 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1976 234035bc 2019-06-01 stsp path2) == -1)
1977 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1978 234035bc 2019-06-01 stsp
1979 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1980 234035bc 2019-06-01 stsp if (err)
1981 234035bc 2019-06-01 stsp goto done;
1982 234035bc 2019-06-01 stsp
1983 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1984 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1985 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1986 234035bc 2019-06-01 stsp goto done;
1987 234035bc 2019-06-01 stsp }
1988 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1989 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1990 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1991 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1992 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1993 234035bc 2019-06-01 stsp goto done;
1994 234035bc 2019-06-01 stsp }
1995 234035bc 2019-06-01 stsp
1996 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1997 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1998 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1999 234035bc 2019-06-01 stsp } else if (blob1) {
2000 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2001 d6c87207 2019-08-02 stsp strlen(path1));
2002 1ee397ad 2019-07-12 stsp if (ie == NULL)
2003 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2004 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2005 2b92fad7 2019-06-02 stsp
2006 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2007 2b92fad7 2019-06-02 stsp path1) == -1)
2008 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2009 2b92fad7 2019-06-02 stsp
2010 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2011 2b92fad7 2019-06-02 stsp if (err)
2012 2b92fad7 2019-06-02 stsp goto done;
2013 2b92fad7 2019-06-02 stsp
2014 2b92fad7 2019-06-02 stsp switch (status) {
2015 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2016 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2017 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2018 1ee397ad 2019-07-12 stsp if (err)
2019 1ee397ad 2019-07-12 stsp goto done;
2020 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2021 2b92fad7 2019-06-02 stsp if (err)
2022 2b92fad7 2019-06-02 stsp goto done;
2023 2b92fad7 2019-06-02 stsp if (ie)
2024 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2025 2b92fad7 2019-06-02 stsp break;
2026 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2027 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2028 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2029 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2030 1ee397ad 2019-07-12 stsp if (err)
2031 1ee397ad 2019-07-12 stsp goto done;
2032 2b92fad7 2019-06-02 stsp if (ie)
2033 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2034 2b92fad7 2019-06-02 stsp break;
2035 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2036 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2037 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2039 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2040 1ee397ad 2019-07-12 stsp if (err)
2041 1ee397ad 2019-07-12 stsp goto done;
2042 2b92fad7 2019-06-02 stsp break;
2043 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2044 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2045 1ee397ad 2019-07-12 stsp if (err)
2046 1ee397ad 2019-07-12 stsp goto done;
2047 2b92fad7 2019-06-02 stsp break;
2048 2b92fad7 2019-06-02 stsp default:
2049 2b92fad7 2019-06-02 stsp break;
2050 2b92fad7 2019-06-02 stsp }
2051 234035bc 2019-06-01 stsp } else if (blob2) {
2052 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2053 234035bc 2019-06-01 stsp path2) == -1)
2054 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2055 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2056 d6c87207 2019-08-02 stsp strlen(path2));
2057 234035bc 2019-06-01 stsp if (ie) {
2058 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2059 234035bc 2019-06-01 stsp repo);
2060 234035bc 2019-06-01 stsp if (err)
2061 234035bc 2019-06-01 stsp goto done;
2062 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2063 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2064 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2065 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2066 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2067 1ee397ad 2019-07-12 stsp status, path2);
2068 234035bc 2019-06-01 stsp goto done;
2069 234035bc 2019-06-01 stsp }
2070 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2071 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2072 818c7501 2019-07-11 stsp a->commit_id2, repo,
2073 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2074 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2075 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2076 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2077 234035bc 2019-06-01 stsp blob2, 0);
2078 234035bc 2019-06-01 stsp if (err)
2079 234035bc 2019-06-01 stsp goto done;
2080 234035bc 2019-06-01 stsp }
2081 234035bc 2019-06-01 stsp } else {
2082 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2083 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2084 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2085 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2086 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2087 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2088 234035bc 2019-06-01 stsp if (err)
2089 234035bc 2019-06-01 stsp goto done;
2090 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2091 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2092 234035bc 2019-06-01 stsp if (err)
2093 234035bc 2019-06-01 stsp goto done;
2094 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2095 2b92fad7 2019-06-02 stsp if (err) {
2096 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2097 2b92fad7 2019-06-02 stsp goto done;
2098 2b92fad7 2019-06-02 stsp }
2099 234035bc 2019-06-01 stsp }
2100 234035bc 2019-06-01 stsp }
2101 234035bc 2019-06-01 stsp done:
2102 234035bc 2019-06-01 stsp free(ondisk_path);
2103 234035bc 2019-06-01 stsp return err;
2104 234035bc 2019-06-01 stsp }
2105 234035bc 2019-06-01 stsp
2106 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2107 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2108 234035bc 2019-06-01 stsp struct got_repository *repo;
2109 234035bc 2019-06-01 stsp };
2110 234035bc 2019-06-01 stsp
2111 234035bc 2019-06-01 stsp static const struct got_error *
2112 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2113 234035bc 2019-06-01 stsp {
2114 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2115 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2116 234035bc 2019-06-01 stsp unsigned char status;
2117 234035bc 2019-06-01 stsp struct stat sb;
2118 234035bc 2019-06-01 stsp char *ondisk_path;
2119 234035bc 2019-06-01 stsp
2120 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2121 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2122 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2123 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2124 234035bc 2019-06-01 stsp
2125 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2126 234035bc 2019-06-01 stsp == -1)
2127 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2128 234035bc 2019-06-01 stsp
2129 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2130 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2131 234035bc 2019-06-01 stsp if (err)
2132 234035bc 2019-06-01 stsp return err;
2133 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2134 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2135 234035bc 2019-06-01 stsp
2136 234035bc 2019-06-01 stsp return NULL;
2137 234035bc 2019-06-01 stsp }
2138 234035bc 2019-06-01 stsp
2139 818c7501 2019-07-11 stsp static const struct got_error *
2140 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2142 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2143 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2144 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2145 234035bc 2019-06-01 stsp {
2146 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2147 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2148 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2149 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2150 234035bc 2019-06-01 stsp
2151 03415a1a 2019-06-02 stsp if (commit_id1) {
2152 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2153 03415a1a 2019-06-02 stsp worktree->path_prefix);
2154 03415a1a 2019-06-02 stsp if (err)
2155 03415a1a 2019-06-02 stsp goto done;
2156 03415a1a 2019-06-02 stsp
2157 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2158 03415a1a 2019-06-02 stsp if (err)
2159 03415a1a 2019-06-02 stsp goto done;
2160 03415a1a 2019-06-02 stsp }
2161 234035bc 2019-06-01 stsp
2162 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2163 234035bc 2019-06-01 stsp worktree->path_prefix);
2164 234035bc 2019-06-01 stsp if (err)
2165 234035bc 2019-06-01 stsp goto done;
2166 234035bc 2019-06-01 stsp
2167 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2168 234035bc 2019-06-01 stsp if (err)
2169 234035bc 2019-06-01 stsp goto done;
2170 234035bc 2019-06-01 stsp
2171 234035bc 2019-06-01 stsp arg.worktree = worktree;
2172 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2173 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2174 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2175 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2176 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2177 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2178 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2179 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2180 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2181 af12c6b9 2019-06-04 stsp err = sync_err;
2182 234035bc 2019-06-01 stsp done:
2183 234035bc 2019-06-01 stsp if (tree1)
2184 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2185 234035bc 2019-06-01 stsp if (tree2)
2186 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2187 818c7501 2019-07-11 stsp return err;
2188 818c7501 2019-07-11 stsp }
2189 234035bc 2019-06-01 stsp
2190 818c7501 2019-07-11 stsp const struct got_error *
2191 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2192 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2193 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2194 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2195 818c7501 2019-07-11 stsp {
2196 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2197 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2198 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2199 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2200 818c7501 2019-07-11 stsp
2201 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2202 818c7501 2019-07-11 stsp if (err)
2203 818c7501 2019-07-11 stsp return err;
2204 818c7501 2019-07-11 stsp
2205 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2206 818c7501 2019-07-11 stsp if (err)
2207 818c7501 2019-07-11 stsp goto done;
2208 818c7501 2019-07-11 stsp
2209 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2210 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2211 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2212 818c7501 2019-07-11 stsp &mok_arg);
2213 818c7501 2019-07-11 stsp if (err)
2214 818c7501 2019-07-11 stsp goto done;
2215 818c7501 2019-07-11 stsp
2216 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2217 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2218 818c7501 2019-07-11 stsp done:
2219 818c7501 2019-07-11 stsp if (fileindex)
2220 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2221 818c7501 2019-07-11 stsp free(fileindex_path);
2222 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2223 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2224 234035bc 2019-06-01 stsp err = unlockerr;
2225 234035bc 2019-06-01 stsp return err;
2226 234035bc 2019-06-01 stsp }
2227 234035bc 2019-06-01 stsp
2228 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2229 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2230 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2231 927df6b7 2019-02-10 stsp const char *status_path;
2232 927df6b7 2019-02-10 stsp size_t status_path_len;
2233 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2234 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2235 f8d1f275 2019-02-04 stsp void *status_arg;
2236 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2237 f8d1f275 2019-02-04 stsp void *cancel_arg;
2238 f8d1f275 2019-02-04 stsp };
2239 88d0e355 2019-08-03 stsp
2240 f8d1f275 2019-02-04 stsp static const struct got_error *
2241 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2242 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2243 927df6b7 2019-02-10 stsp struct got_repository *repo)
2244 927df6b7 2019-02-10 stsp {
2245 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2246 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2247 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2248 927df6b7 2019-02-10 stsp struct stat sb;
2249 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2250 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2251 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2252 927df6b7 2019-02-10 stsp
2253 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2254 98eaaa12 2019-08-03 stsp if (err)
2255 98eaaa12 2019-08-03 stsp return err;
2256 98eaaa12 2019-08-03 stsp
2257 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2258 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_NO_CHANGE)
2259 98eaaa12 2019-08-03 stsp return NULL;
2260 98eaaa12 2019-08-03 stsp
2261 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2262 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2263 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2264 98eaaa12 2019-08-03 stsp }
2265 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2266 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2267 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2268 927df6b7 2019-02-10 stsp }
2269 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2270 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2271 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2272 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2273 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2274 98eaaa12 2019-08-03 stsp }
2275 98eaaa12 2019-08-03 stsp
2276 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2277 98eaaa12 2019-08-03 stsp ie->path, blob_idp, staged_blob_idp, commit_idp);
2278 927df6b7 2019-02-10 stsp }
2279 927df6b7 2019-02-10 stsp
2280 927df6b7 2019-02-10 stsp static const struct got_error *
2281 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2282 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2283 f8d1f275 2019-02-04 stsp {
2284 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2285 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2286 f8d1f275 2019-02-04 stsp char *abspath;
2287 f8d1f275 2019-02-04 stsp
2288 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2289 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2290 0584f854 2019-04-06 stsp
2291 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2292 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2293 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2294 927df6b7 2019-02-10 stsp return NULL;
2295 927df6b7 2019-02-10 stsp
2296 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2297 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2298 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2299 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2300 f8d1f275 2019-02-04 stsp } else {
2301 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2302 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2303 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2304 f8d1f275 2019-02-04 stsp }
2305 f8d1f275 2019-02-04 stsp
2306 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2307 927df6b7 2019-02-10 stsp a->repo);
2308 f8d1f275 2019-02-04 stsp free(abspath);
2309 9d31a1d8 2018-03-11 stsp return err;
2310 9d31a1d8 2018-03-11 stsp }
2311 f8d1f275 2019-02-04 stsp
2312 f8d1f275 2019-02-04 stsp static const struct got_error *
2313 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2314 f8d1f275 2019-02-04 stsp {
2315 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2316 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2317 2ec1f75b 2019-03-26 stsp unsigned char status;
2318 927df6b7 2019-02-10 stsp
2319 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2320 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2321 0584f854 2019-04-06 stsp
2322 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2323 927df6b7 2019-02-10 stsp return NULL;
2324 927df6b7 2019-02-10 stsp
2325 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2326 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2327 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2328 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2329 2ec1f75b 2019-03-26 stsp else
2330 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2331 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2332 537ac44b 2019-08-03 stsp ie->path, &blob_id, NULL, &commit_id);
2333 f8d1f275 2019-02-04 stsp }
2334 f8d1f275 2019-02-04 stsp
2335 f8d1f275 2019-02-04 stsp static const struct got_error *
2336 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2337 f8d1f275 2019-02-04 stsp {
2338 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2339 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2340 f8d1f275 2019-02-04 stsp char *path = NULL;
2341 f8d1f275 2019-02-04 stsp
2342 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2343 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2344 0584f854 2019-04-06 stsp
2345 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2346 2c201a36 2019-02-10 stsp return NULL;
2347 2c201a36 2019-02-10 stsp
2348 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2349 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2350 927df6b7 2019-02-10 stsp return NULL;
2351 927df6b7 2019-02-10 stsp
2352 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2353 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2354 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2355 f8d1f275 2019-02-04 stsp } else {
2356 f8d1f275 2019-02-04 stsp path = de->d_name;
2357 f8d1f275 2019-02-04 stsp }
2358 f8d1f275 2019-02-04 stsp
2359 c577a9ce 2019-07-27 stsp if (got_path_is_child(path, a->status_path, a->status_path_len))
2360 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2361 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2362 f8d1f275 2019-02-04 stsp if (parent_path[0])
2363 f8d1f275 2019-02-04 stsp free(path);
2364 b72f483a 2019-02-05 stsp return err;
2365 f8d1f275 2019-02-04 stsp }
2366 f8d1f275 2019-02-04 stsp
2367 347d1d3e 2019-07-12 stsp static const struct got_error *
2368 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2369 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2370 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2371 abb4604f 2019-07-27 stsp {
2372 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2373 abb4604f 2019-07-27 stsp struct stat sb;
2374 abb4604f 2019-07-27 stsp
2375 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2376 abb4604f 2019-07-27 stsp if (ie)
2377 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2378 abb4604f 2019-07-27 stsp status_arg, repo);
2379 abb4604f 2019-07-27 stsp
2380 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2381 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2382 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2383 abb4604f 2019-07-27 stsp return NULL;
2384 abb4604f 2019-07-27 stsp }
2385 abb4604f 2019-07-27 stsp
2386 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2387 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2388 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2389 abb4604f 2019-07-27 stsp
2390 abb4604f 2019-07-27 stsp return NULL;
2391 abb4604f 2019-07-27 stsp }
2392 abb4604f 2019-07-27 stsp
2393 abb4604f 2019-07-27 stsp static const struct got_error *
2394 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2395 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2396 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2397 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2398 f8d1f275 2019-02-04 stsp {
2399 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2400 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2401 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2402 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2403 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2404 f8d1f275 2019-02-04 stsp
2405 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2406 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2407 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2408 8dc303cc 2019-07-27 stsp
2409 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2410 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2411 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2412 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2413 abb4604f 2019-07-27 stsp else
2414 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2415 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2416 abb4604f 2019-07-27 stsp } else {
2417 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2418 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2419 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2420 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2421 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2422 abb4604f 2019-07-27 stsp arg.status_path = path;
2423 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2424 abb4604f 2019-07-27 stsp arg.repo = repo;
2425 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2426 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2427 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2428 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2429 abb4604f 2019-07-27 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2430 abb4604f 2019-07-27 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2431 927df6b7 2019-02-10 stsp }
2432 8dc303cc 2019-07-27 stsp
2433 f8d1f275 2019-02-04 stsp if (workdir)
2434 f8d1f275 2019-02-04 stsp closedir(workdir);
2435 927df6b7 2019-02-10 stsp free(ondisk_path);
2436 347d1d3e 2019-07-12 stsp return err;
2437 347d1d3e 2019-07-12 stsp }
2438 347d1d3e 2019-07-12 stsp
2439 347d1d3e 2019-07-12 stsp const struct got_error *
2440 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2441 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2442 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2443 72ea6654 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2444 347d1d3e 2019-07-12 stsp {
2445 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2446 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2447 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2448 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2449 347d1d3e 2019-07-12 stsp
2450 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2451 347d1d3e 2019-07-12 stsp if (err)
2452 347d1d3e 2019-07-12 stsp return err;
2453 347d1d3e 2019-07-12 stsp
2454 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2455 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2456 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2457 72ea6654 2019-07-27 stsp if (err)
2458 72ea6654 2019-07-27 stsp break;
2459 72ea6654 2019-07-27 stsp }
2460 f8d1f275 2019-02-04 stsp free(fileindex_path);
2461 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2462 f8d1f275 2019-02-04 stsp return err;
2463 f8d1f275 2019-02-04 stsp }
2464 6c7ab921 2019-03-18 stsp
2465 6c7ab921 2019-03-18 stsp const struct got_error *
2466 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2467 6c7ab921 2019-03-18 stsp const char *arg)
2468 6c7ab921 2019-03-18 stsp {
2469 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2470 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2471 6c7ab921 2019-03-18 stsp size_t len;
2472 6c7ab921 2019-03-18 stsp
2473 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2474 6c7ab921 2019-03-18 stsp
2475 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2476 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2477 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2478 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2479 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2480 d0710d08 2019-07-22 stsp if (cwd == NULL)
2481 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2482 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2483 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2484 d0710d08 2019-07-22 stsp goto done;
2485 d0710d08 2019-07-22 stsp }
2486 d0710d08 2019-07-22 stsp }
2487 6c7ab921 2019-03-18 stsp
2488 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2489 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2490 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2491 6c7ab921 2019-03-18 stsp goto done;
2492 6c7ab921 2019-03-18 stsp }
2493 6c7ab921 2019-03-18 stsp
2494 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2495 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2496 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2497 f6d88e1a 2019-05-29 stsp if (err)
2498 f6d88e1a 2019-05-29 stsp goto done;
2499 f6d88e1a 2019-05-29 stsp } else {
2500 f6d88e1a 2019-05-29 stsp path = strdup("");
2501 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2502 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2503 f6d88e1a 2019-05-29 stsp goto done;
2504 f6d88e1a 2019-05-29 stsp }
2505 6c7ab921 2019-03-18 stsp }
2506 6c7ab921 2019-03-18 stsp
2507 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2508 6c7ab921 2019-03-18 stsp len = strlen(path);
2509 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2510 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2511 6c7ab921 2019-03-18 stsp len--;
2512 6c7ab921 2019-03-18 stsp }
2513 6c7ab921 2019-03-18 stsp done:
2514 6c7ab921 2019-03-18 stsp free(resolved);
2515 d0710d08 2019-07-22 stsp free(cwd);
2516 6c7ab921 2019-03-18 stsp if (err == NULL)
2517 6c7ab921 2019-03-18 stsp *wt_path = path;
2518 6c7ab921 2019-03-18 stsp else
2519 6c7ab921 2019-03-18 stsp free(path);
2520 d00136be 2019-03-26 stsp return err;
2521 d00136be 2019-03-26 stsp }
2522 d00136be 2019-03-26 stsp
2523 07f5b47a 2019-06-02 stsp static const struct got_error *
2524 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2525 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2526 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2527 07f5b47a 2019-06-02 stsp {
2528 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2529 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2530 6d022e97 2019-08-04 stsp unsigned char status;
2531 6d022e97 2019-08-04 stsp struct stat sb;
2532 07f5b47a 2019-06-02 stsp
2533 6d022e97 2019-08-04 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2534 6d022e97 2019-08-04 stsp if (ie) {
2535 6d022e97 2019-08-04 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2536 6d022e97 2019-08-04 stsp if (err)
2537 6d022e97 2019-08-04 stsp return err;
2538 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
2539 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
2540 6d022e97 2019-08-04 stsp return NULL;
2541 6d022e97 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2542 6d022e97 2019-08-04 stsp }
2543 07f5b47a 2019-06-02 stsp
2544 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2545 07f5b47a 2019-06-02 stsp if (err)
2546 07f5b47a 2019-06-02 stsp return err;
2547 07f5b47a 2019-06-02 stsp
2548 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2549 07f5b47a 2019-06-02 stsp if (err) {
2550 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2551 07f5b47a 2019-06-02 stsp return err;
2552 07f5b47a 2019-06-02 stsp }
2553 07f5b47a 2019-06-02 stsp
2554 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2555 07f5b47a 2019-06-02 stsp }
2556 07f5b47a 2019-06-02 stsp
2557 d00136be 2019-03-26 stsp const struct got_error *
2558 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2559 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
2560 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2561 031a5338 2019-03-26 stsp struct got_repository *repo)
2562 d00136be 2019-03-26 stsp {
2563 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2564 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2565 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2566 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2567 d00136be 2019-03-26 stsp
2568 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2569 d00136be 2019-03-26 stsp if (err)
2570 d00136be 2019-03-26 stsp return err;
2571 d00136be 2019-03-26 stsp
2572 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2573 d00136be 2019-03-26 stsp if (err)
2574 d00136be 2019-03-26 stsp goto done;
2575 d00136be 2019-03-26 stsp
2576 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2577 6d022e97 2019-08-04 stsp char *ondisk_path;
2578 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2579 6d022e97 2019-08-04 stsp pe->path) == -1)
2580 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2581 6d022e97 2019-08-04 stsp err = schedule_addition(ondisk_path, fileindex, pe->path,
2582 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2583 6d022e97 2019-08-04 stsp free(ondisk_path);
2584 1dd54920 2019-05-11 stsp if (err)
2585 af12c6b9 2019-06-04 stsp break;
2586 1dd54920 2019-05-11 stsp }
2587 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2588 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2589 af12c6b9 2019-06-04 stsp err = sync_err;
2590 d00136be 2019-03-26 stsp done:
2591 fb399478 2019-07-12 stsp free(fileindex_path);
2592 d00136be 2019-03-26 stsp if (fileindex)
2593 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2594 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2595 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2596 d00136be 2019-03-26 stsp err = unlockerr;
2597 6c7ab921 2019-03-18 stsp return err;
2598 6c7ab921 2019-03-18 stsp }
2599 17ed4618 2019-06-02 stsp
2600 17ed4618 2019-06-02 stsp static const struct got_error *
2601 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2602 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2603 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2604 17ed4618 2019-06-02 stsp struct got_repository *repo)
2605 17ed4618 2019-06-02 stsp {
2606 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2607 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2608 9acbc4fa 2019-08-03 stsp unsigned char status, staged_status;
2609 17ed4618 2019-06-02 stsp struct stat sb;
2610 17ed4618 2019-06-02 stsp
2611 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2612 17ed4618 2019-06-02 stsp if (ie == NULL)
2613 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2614 2ec1f75b 2019-03-26 stsp
2615 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
2616 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
2617 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2618 9acbc4fa 2019-08-03 stsp return NULL;
2619 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2620 9acbc4fa 2019-08-03 stsp }
2621 9acbc4fa 2019-08-03 stsp
2622 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2623 17ed4618 2019-06-02 stsp if (err)
2624 17ed4618 2019-06-02 stsp return err;
2625 17ed4618 2019-06-02 stsp
2626 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2627 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2628 de426ea6 2019-08-03 stsp return NULL;
2629 f0b0c0ce 2019-08-04 stsp if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2630 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2631 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MODIFY &&
2632 f0b0c0ce 2019-08-04 stsp status != GOT_STATUS_MISSING)
2633 f0b0c0ce 2019-08-04 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2634 17ed4618 2019-06-02 stsp }
2635 17ed4618 2019-06-02 stsp
2636 f0b0c0ce 2019-08-04 stsp if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2637 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2638 17ed4618 2019-06-02 stsp
2639 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2640 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2641 17ed4618 2019-06-02 stsp }
2642 17ed4618 2019-06-02 stsp
2643 2ec1f75b 2019-03-26 stsp const struct got_error *
2644 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2645 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
2646 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2647 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2648 2ec1f75b 2019-03-26 stsp {
2649 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2650 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2651 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2652 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2653 2ec1f75b 2019-03-26 stsp
2654 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2655 2ec1f75b 2019-03-26 stsp if (err)
2656 2ec1f75b 2019-03-26 stsp return err;
2657 2ec1f75b 2019-03-26 stsp
2658 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2659 2ec1f75b 2019-03-26 stsp if (err)
2660 2ec1f75b 2019-03-26 stsp goto done;
2661 2ec1f75b 2019-03-26 stsp
2662 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
2663 6d022e97 2019-08-04 stsp char *ondisk_path;
2664 6d022e97 2019-08-04 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2665 6d022e97 2019-08-04 stsp pe->path) == -1)
2666 6d022e97 2019-08-04 stsp return got_error_from_errno("asprintf");
2667 6d022e97 2019-08-04 stsp err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2668 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2669 6d022e97 2019-08-04 stsp free(ondisk_path);
2670 17ed4618 2019-06-02 stsp if (err)
2671 af12c6b9 2019-06-04 stsp break;
2672 2ec1f75b 2019-03-26 stsp }
2673 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2674 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2675 af12c6b9 2019-06-04 stsp err = sync_err;
2676 2ec1f75b 2019-03-26 stsp done:
2677 fb399478 2019-07-12 stsp free(fileindex_path);
2678 2ec1f75b 2019-03-26 stsp if (fileindex)
2679 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2680 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2681 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2682 2ec1f75b 2019-03-26 stsp err = unlockerr;
2683 33aa809d 2019-08-08 stsp return err;
2684 33aa809d 2019-08-08 stsp }
2685 33aa809d 2019-08-08 stsp
2686 33aa809d 2019-08-08 stsp static const struct got_error *
2687 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2688 33aa809d 2019-08-08 stsp {
2689 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
2690 33aa809d 2019-08-08 stsp char *line = NULL;
2691 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
2692 33aa809d 2019-08-08 stsp ssize_t linelen;
2693 33aa809d 2019-08-08 stsp
2694 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
2695 33aa809d 2019-08-08 stsp if (linelen == -1) {
2696 33aa809d 2019-08-08 stsp if (ferror(infile)) {
2697 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
2698 33aa809d 2019-08-08 stsp goto done;
2699 33aa809d 2019-08-08 stsp }
2700 33aa809d 2019-08-08 stsp return NULL;
2701 33aa809d 2019-08-08 stsp }
2702 33aa809d 2019-08-08 stsp if (outfile) {
2703 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
2704 33aa809d 2019-08-08 stsp if (n != linelen) {
2705 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
2706 33aa809d 2019-08-08 stsp goto done;
2707 33aa809d 2019-08-08 stsp }
2708 33aa809d 2019-08-08 stsp }
2709 33aa809d 2019-08-08 stsp if (rejectfile) {
2710 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
2711 33aa809d 2019-08-08 stsp if (n != linelen)
2712 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
2713 33aa809d 2019-08-08 stsp }
2714 33aa809d 2019-08-08 stsp done:
2715 33aa809d 2019-08-08 stsp free(line);
2716 2ec1f75b 2019-03-26 stsp return err;
2717 2ec1f75b 2019-03-26 stsp }
2718 1f1abb7e 2019-08-08 stsp
2719 33aa809d 2019-08-08 stsp static const struct got_error *
2720 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
2721 33aa809d 2019-08-08 stsp {
2722 33aa809d 2019-08-08 stsp char *line = NULL;
2723 33aa809d 2019-08-08 stsp size_t linesize = 0;
2724 33aa809d 2019-08-08 stsp ssize_t linelen;
2725 33aa809d 2019-08-08 stsp
2726 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
2727 33aa809d 2019-08-08 stsp free(line);
2728 33aa809d 2019-08-08 stsp if (linelen == -1 && ferror(f))
2729 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
2730 33aa809d 2019-08-08 stsp return NULL;
2731 33aa809d 2019-08-08 stsp }
2732 33aa809d 2019-08-08 stsp
2733 33aa809d 2019-08-08 stsp static const struct got_error *
2734 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2735 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
2736 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
2737 33aa809d 2019-08-08 stsp {
2738 33aa809d 2019-08-08 stsp const struct got_error *err;
2739 33aa809d 2019-08-08 stsp
2740 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
2741 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
2742 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
2743 33aa809d 2019-08-08 stsp if (err)
2744 33aa809d 2019-08-08 stsp return err;
2745 33aa809d 2019-08-08 stsp (*line_cur1)++;
2746 33aa809d 2019-08-08 stsp }
2747 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
2748 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
2749 33aa809d 2019-08-08 stsp if (rejectfile)
2750 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
2751 33aa809d 2019-08-08 stsp else
2752 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
2753 33aa809d 2019-08-08 stsp if (err)
2754 33aa809d 2019-08-08 stsp return err;
2755 33aa809d 2019-08-08 stsp (*line_cur2)++;
2756 33aa809d 2019-08-08 stsp }
2757 33aa809d 2019-08-08 stsp /* Copy patched lines. */
2758 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
2759 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
2760 33aa809d 2019-08-08 stsp if (err)
2761 33aa809d 2019-08-08 stsp return err;
2762 33aa809d 2019-08-08 stsp (*line_cur2)++;
2763 33aa809d 2019-08-08 stsp }
2764 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
2765 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 <= end_new) {
2766 33aa809d 2019-08-08 stsp if (rejectfile)
2767 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
2768 33aa809d 2019-08-08 stsp else
2769 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
2770 33aa809d 2019-08-08 stsp if (err)
2771 33aa809d 2019-08-08 stsp return err;
2772 33aa809d 2019-08-08 stsp (*line_cur1)++;
2773 33aa809d 2019-08-08 stsp }
2774 33aa809d 2019-08-08 stsp /* Copy old file's lines after patch. */
2775 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 <= end_old) {
2776 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, rejectfile);
2777 33aa809d 2019-08-08 stsp if (err)
2778 33aa809d 2019-08-08 stsp return err;
2779 33aa809d 2019-08-08 stsp (*line_cur1)++;
2780 33aa809d 2019-08-08 stsp }
2781 33aa809d 2019-08-08 stsp
2782 33aa809d 2019-08-08 stsp return NULL;
2783 33aa809d 2019-08-08 stsp }
2784 33aa809d 2019-08-08 stsp
2785 33aa809d 2019-08-08 stsp static const struct got_error *
2786 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2787 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2788 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2789 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
2790 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
2791 33aa809d 2019-08-08 stsp {
2792 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
2793 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
2794 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
2795 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
2796 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
2797 33aa809d 2019-08-08 stsp long pos1, pos2;
2798 33aa809d 2019-08-08 stsp FILE *hunkfile;
2799 33aa809d 2019-08-08 stsp
2800 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
2801 33aa809d 2019-08-08 stsp
2802 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
2803 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
2804 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
2805 33aa809d 2019-08-08 stsp
2806 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
2807 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
2808 33aa809d 2019-08-08 stsp
2809 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
2810 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2811 33aa809d 2019-08-08 stsp
2812 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
2813 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
2814 33aa809d 2019-08-08 stsp goto done;
2815 33aa809d 2019-08-08 stsp }
2816 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
2817 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
2818 33aa809d 2019-08-08 stsp goto done;
2819 33aa809d 2019-08-08 stsp }
2820 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2821 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
2822 33aa809d 2019-08-08 stsp goto done;
2823 33aa809d 2019-08-08 stsp }
2824 33aa809d 2019-08-08 stsp
2825 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2826 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
2827 33aa809d 2019-08-08 stsp if (err)
2828 33aa809d 2019-08-08 stsp goto done;
2829 33aa809d 2019-08-08 stsp
2830 33aa809d 2019-08-08 stsp switch (*choice) {
2831 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
2832 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2833 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
2834 33aa809d 2019-08-08 stsp break;
2835 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
2836 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2837 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
2838 33aa809d 2019-08-08 stsp break;
2839 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
2840 33aa809d 2019-08-08 stsp if (outfile) {
2841 33aa809d 2019-08-08 stsp /* Copy old file's lines until EOF. */
2842 33aa809d 2019-08-08 stsp while (!feof(f1)) {
2843 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
2844 33aa809d 2019-08-08 stsp if (err)
2845 33aa809d 2019-08-08 stsp goto done;
2846 33aa809d 2019-08-08 stsp (*line_cur1)++;
2847 33aa809d 2019-08-08 stsp }
2848 33aa809d 2019-08-08 stsp }
2849 33aa809d 2019-08-08 stsp if (rejectfile) {
2850 33aa809d 2019-08-08 stsp /* Copy new file's lines until EOF. */
2851 33aa809d 2019-08-08 stsp while (!feof(f2)) {
2852 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
2853 33aa809d 2019-08-08 stsp if (err)
2854 33aa809d 2019-08-08 stsp goto done;
2855 33aa809d 2019-08-08 stsp (*line_cur2)++;
2856 33aa809d 2019-08-08 stsp }
2857 33aa809d 2019-08-08 stsp }
2858 33aa809d 2019-08-08 stsp break;
2859 33aa809d 2019-08-08 stsp default:
2860 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
2861 33aa809d 2019-08-08 stsp break;
2862 33aa809d 2019-08-08 stsp }
2863 33aa809d 2019-08-08 stsp done:
2864 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
2865 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
2866 33aa809d 2019-08-08 stsp return err;
2867 33aa809d 2019-08-08 stsp }
2868 33aa809d 2019-08-08 stsp
2869 1f1abb7e 2019-08-08 stsp struct revert_file_args {
2870 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
2871 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
2872 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
2873 1f1abb7e 2019-08-08 stsp void *progress_arg;
2874 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
2875 33aa809d 2019-08-08 stsp void *patch_arg;
2876 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
2877 1f1abb7e 2019-08-08 stsp };
2878 a129376b 2019-03-28 stsp
2879 e20a8b6f 2019-06-04 stsp static const struct got_error *
2880 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
2881 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
2882 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
2883 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
2884 33aa809d 2019-08-08 stsp {
2885 33aa809d 2019-08-08 stsp const struct got_error *err;
2886 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
2887 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
2888 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
2889 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
2890 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
2891 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
2892 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
2893 33aa809d 2019-08-08 stsp struct got_diff_change *change;
2894 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
2895 33aa809d 2019-08-08 stsp int n = 0;
2896 33aa809d 2019-08-08 stsp
2897 33aa809d 2019-08-08 stsp *path_outfile = NULL;
2898 33aa809d 2019-08-08 stsp
2899 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
2900 33aa809d 2019-08-08 stsp if (err)
2901 33aa809d 2019-08-08 stsp return err;
2902 33aa809d 2019-08-08 stsp
2903 33aa809d 2019-08-08 stsp f2 = fopen(path2, "r");
2904 33aa809d 2019-08-08 stsp if (f2 == NULL) {
2905 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fopen", path2);
2906 33aa809d 2019-08-08 stsp goto done;
2907 33aa809d 2019-08-08 stsp }
2908 33aa809d 2019-08-08 stsp
2909 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
2910 33aa809d 2019-08-08 stsp if (err)
2911 33aa809d 2019-08-08 stsp goto done;
2912 33aa809d 2019-08-08 stsp
2913 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
2914 33aa809d 2019-08-08 stsp if (err)
2915 33aa809d 2019-08-08 stsp goto done;
2916 33aa809d 2019-08-08 stsp
2917 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
2918 33aa809d 2019-08-08 stsp if (err)
2919 33aa809d 2019-08-08 stsp goto done;
2920 33aa809d 2019-08-08 stsp
2921 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
2922 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
2923 33aa809d 2019-08-08 stsp goto done;
2924 33aa809d 2019-08-08 stsp }
2925 33aa809d 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
2926 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
2927 33aa809d 2019-08-08 stsp goto done;
2928 33aa809d 2019-08-08 stsp }
2929 33aa809d 2019-08-08 stsp
2930 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
2931 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
2932 33aa809d 2019-08-08 stsp if (err)
2933 33aa809d 2019-08-08 stsp goto done;
2934 33aa809d 2019-08-08 stsp
2935 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
2936 33aa809d 2019-08-08 stsp if (err)
2937 33aa809d 2019-08-08 stsp goto done;
2938 33aa809d 2019-08-08 stsp
2939 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
2940 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
2941 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
2942 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
2943 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
2944 33aa809d 2019-08-08 stsp int choice;
2945 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
2946 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
2947 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
2948 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
2949 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
2950 e635744c 2019-08-08 stsp patch_cb, patch_arg);
2951 33aa809d 2019-08-08 stsp if (err)
2952 33aa809d 2019-08-08 stsp goto done;
2953 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
2954 33aa809d 2019-08-08 stsp have_content = 1;
2955 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
2956 33aa809d 2019-08-08 stsp break;
2957 33aa809d 2019-08-08 stsp }
2958 33aa809d 2019-08-08 stsp done:
2959 33aa809d 2019-08-08 stsp free(id_str);
2960 33aa809d 2019-08-08 stsp if (blob)
2961 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
2962 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
2963 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
2964 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2965 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
2966 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
2967 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
2968 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
2969 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
2970 33aa809d 2019-08-08 stsp if (err || !have_content) {
2971 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
2972 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
2973 33aa809d 2019-08-08 stsp free(*path_outfile);
2974 33aa809d 2019-08-08 stsp *path_outfile = NULL;
2975 33aa809d 2019-08-08 stsp }
2976 33aa809d 2019-08-08 stsp free(args);
2977 33aa809d 2019-08-08 stsp if (ds) {
2978 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
2979 33aa809d 2019-08-08 stsp free(ds);
2980 33aa809d 2019-08-08 stsp }
2981 33aa809d 2019-08-08 stsp if (changes)
2982 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
2983 33aa809d 2019-08-08 stsp free(path1);
2984 33aa809d 2019-08-08 stsp return err;
2985 33aa809d 2019-08-08 stsp }
2986 33aa809d 2019-08-08 stsp
2987 33aa809d 2019-08-08 stsp static const struct got_error *
2988 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
2989 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
2990 1f1abb7e 2019-08-08 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2991 a129376b 2019-03-28 stsp {
2992 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
2993 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2994 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
2995 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2996 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2997 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2998 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
2999 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
3000 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
3001 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
3002 a129376b 2019-03-28 stsp
3003 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
3004 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
3005 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
3006 d3bcc3d1 2019-08-08 stsp return NULL;
3007 d3bcc3d1 2019-08-08 stsp
3008 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3009 65084dad 2019-08-08 stsp if (ie == NULL)
3010 65084dad 2019-08-08 stsp return got_error(GOT_ERR_BAD_PATH);
3011 a129376b 2019-03-28 stsp
3012 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
3013 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
3014 a129376b 2019-03-28 stsp if (err) {
3015 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
3016 a129376b 2019-03-28 stsp goto done;
3017 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
3018 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
3019 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
3020 e20a8b6f 2019-06-04 stsp goto done;
3021 e20a8b6f 2019-06-04 stsp }
3022 a129376b 2019-03-28 stsp }
3023 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
3024 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
3025 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3026 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3027 a129376b 2019-03-28 stsp goto done;
3028 a129376b 2019-03-28 stsp }
3029 a129376b 2019-03-28 stsp } else {
3030 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
3031 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
3032 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3033 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3034 a129376b 2019-03-28 stsp goto done;
3035 a129376b 2019-03-28 stsp }
3036 a129376b 2019-03-28 stsp } else {
3037 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
3038 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
3039 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3040 a129376b 2019-03-28 stsp goto done;
3041 a129376b 2019-03-28 stsp }
3042 a129376b 2019-03-28 stsp }
3043 a129376b 2019-03-28 stsp }
3044 a129376b 2019-03-28 stsp
3045 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
3046 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
3047 a9fa2909 2019-07-27 stsp if (err) {
3048 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3049 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
3050 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
3051 a9fa2909 2019-07-27 stsp goto done;
3052 a9fa2909 2019-07-27 stsp } else {
3053 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
3054 a9fa2909 2019-07-27 stsp if (err)
3055 a9fa2909 2019-07-27 stsp goto done;
3056 a9fa2909 2019-07-27 stsp
3057 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
3058 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
3059 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
3060 a9fa2909 2019-07-27 stsp goto done;
3061 a9fa2909 2019-07-27 stsp }
3062 a9fa2909 2019-07-27 stsp
3063 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
3064 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
3065 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
3066 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
3067 a9fa2909 2019-07-27 stsp goto done;
3068 a9fa2909 2019-07-27 stsp }
3069 a129376b 2019-03-28 stsp }
3070 a129376b 2019-03-28 stsp
3071 a129376b 2019-03-28 stsp switch (status) {
3072 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
3073 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3074 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3075 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3076 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3077 33aa809d 2019-08-08 stsp if (err)
3078 33aa809d 2019-08-08 stsp goto done;
3079 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3080 33aa809d 2019-08-08 stsp break;
3081 33aa809d 2019-08-08 stsp }
3082 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3083 1f1abb7e 2019-08-08 stsp ie->path);
3084 1ee397ad 2019-07-12 stsp if (err)
3085 1ee397ad 2019-07-12 stsp goto done;
3086 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
3087 a129376b 2019-03-28 stsp break;
3088 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
3089 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3090 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3091 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3092 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3093 33aa809d 2019-08-08 stsp if (err)
3094 33aa809d 2019-08-08 stsp goto done;
3095 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3096 33aa809d 2019-08-08 stsp break;
3097 33aa809d 2019-08-08 stsp }
3098 33aa809d 2019-08-08 stsp /* fall through */
3099 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
3100 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
3101 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
3102 e20a8b6f 2019-06-04 stsp struct got_object_id id;
3103 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3104 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3105 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
3106 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3107 24278f30 2019-08-03 stsp } else
3108 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
3109 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3110 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3111 a129376b 2019-03-28 stsp if (err)
3112 65084dad 2019-08-08 stsp goto done;
3113 65084dad 2019-08-08 stsp
3114 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3115 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
3116 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
3117 a129376b 2019-03-28 stsp goto done;
3118 65084dad 2019-08-08 stsp }
3119 65084dad 2019-08-08 stsp
3120 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3121 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
3122 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
3123 33aa809d 2019-08-08 stsp ondisk_path, ie->path, a->repo,
3124 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
3125 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
3126 33aa809d 2019-08-08 stsp break;
3127 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
3128 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
3129 33aa809d 2019-08-08 stsp path_content, ondisk_path);
3130 33aa809d 2019-08-08 stsp goto done;
3131 33aa809d 2019-08-08 stsp }
3132 33aa809d 2019-08-08 stsp } else {
3133 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
3134 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
3135 33aa809d 2019-08-08 stsp got_fileindex_perms_to_st(ie), blob, 0, 1,
3136 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
3137 a129376b 2019-03-28 stsp if (err)
3138 a129376b 2019-03-28 stsp goto done;
3139 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_DELETE) {
3140 33aa809d 2019-08-08 stsp err = update_blob_fileindex_entry(a->worktree,
3141 33aa809d 2019-08-08 stsp a->fileindex, ie, ondisk_path, ie->path,
3142 33aa809d 2019-08-08 stsp blob, 1);
3143 33aa809d 2019-08-08 stsp if (err)
3144 33aa809d 2019-08-08 stsp goto done;
3145 33aa809d 2019-08-08 stsp }
3146 a129376b 2019-03-28 stsp }
3147 a129376b 2019-03-28 stsp break;
3148 e20a8b6f 2019-06-04 stsp }
3149 a129376b 2019-03-28 stsp default:
3150 1f1abb7e 2019-08-08 stsp break;
3151 a129376b 2019-03-28 stsp }
3152 a129376b 2019-03-28 stsp done:
3153 1f1abb7e 2019-08-08 stsp free(ondisk_path);
3154 33aa809d 2019-08-08 stsp free(path_content);
3155 e20a8b6f 2019-06-04 stsp free(parent_path);
3156 a129376b 2019-03-28 stsp free(tree_path);
3157 a129376b 2019-03-28 stsp if (blob)
3158 a129376b 2019-03-28 stsp got_object_blob_close(blob);
3159 a129376b 2019-03-28 stsp if (tree)
3160 a129376b 2019-03-28 stsp got_object_tree_close(tree);
3161 a129376b 2019-03-28 stsp free(tree_id);
3162 e20a8b6f 2019-06-04 stsp return err;
3163 e20a8b6f 2019-06-04 stsp }
3164 e20a8b6f 2019-06-04 stsp
3165 e20a8b6f 2019-06-04 stsp const struct got_error *
3166 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
3167 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
3168 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3169 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
3170 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
3171 e20a8b6f 2019-06-04 stsp {
3172 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
3173 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
3174 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3175 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
3176 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3177 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
3178 e20a8b6f 2019-06-04 stsp
3179 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
3180 e20a8b6f 2019-06-04 stsp if (err)
3181 e20a8b6f 2019-06-04 stsp return err;
3182 e20a8b6f 2019-06-04 stsp
3183 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3184 e20a8b6f 2019-06-04 stsp if (err)
3185 e20a8b6f 2019-06-04 stsp goto done;
3186 e20a8b6f 2019-06-04 stsp
3187 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
3188 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
3189 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
3190 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
3191 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
3192 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
3193 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
3194 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
3195 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3196 1f1abb7e 2019-08-08 stsp revert_file, &rfa, NULL, NULL);
3197 e20a8b6f 2019-06-04 stsp if (err)
3198 af12c6b9 2019-06-04 stsp break;
3199 e20a8b6f 2019-06-04 stsp }
3200 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3201 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
3202 e20a8b6f 2019-06-04 stsp err = sync_err;
3203 af12c6b9 2019-06-04 stsp done:
3204 fb399478 2019-07-12 stsp free(fileindex_path);
3205 a129376b 2019-03-28 stsp if (fileindex)
3206 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
3207 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3208 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
3209 a129376b 2019-03-28 stsp err = unlockerr;
3210 c4296144 2019-05-09 stsp return err;
3211 c4296144 2019-05-09 stsp }
3212 c4296144 2019-05-09 stsp
3213 cf066bf8 2019-05-09 stsp static void
3214 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
3215 cf066bf8 2019-05-09 stsp {
3216 24519714 2019-05-09 stsp free(ct->path);
3217 44d03001 2019-05-09 stsp free(ct->in_repo_path);
3218 768aea60 2019-05-09 stsp free(ct->ondisk_path);
3219 e75eb4da 2019-05-10 stsp free(ct->blob_id);
3220 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
3221 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
3222 b416585c 2019-05-13 stsp free(ct->base_commit_id);
3223 cf066bf8 2019-05-09 stsp free(ct);
3224 cf066bf8 2019-05-09 stsp }
3225 24519714 2019-05-09 stsp
3226 ed175427 2019-05-09 stsp struct collect_commitables_arg {
3227 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
3228 24519714 2019-05-09 stsp struct got_repository *repo;
3229 24519714 2019-05-09 stsp struct got_worktree *worktree;
3230 5f8a88c6 2019-08-03 stsp int have_staged_files;
3231 24519714 2019-05-09 stsp };
3232 cf066bf8 2019-05-09 stsp
3233 c4296144 2019-05-09 stsp static const struct got_error *
3234 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
3235 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
3236 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3237 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
3238 c4296144 2019-05-09 stsp {
3239 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
3240 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
3241 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3242 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
3243 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
3244 768aea60 2019-05-09 stsp struct stat sb;
3245 c4296144 2019-05-09 stsp
3246 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
3247 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3248 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3249 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3250 5f8a88c6 2019-08-03 stsp return NULL;
3251 5f8a88c6 2019-08-03 stsp } else {
3252 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
3253 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
3254 c4296144 2019-05-09 stsp
3255 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3256 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
3257 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
3258 5f8a88c6 2019-08-03 stsp return NULL;
3259 5f8a88c6 2019-08-03 stsp }
3260 0b5cc0d6 2019-05-09 stsp
3261 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
3262 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3263 036813ee 2019-05-09 stsp goto done;
3264 036813ee 2019-05-09 stsp }
3265 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
3266 036813ee 2019-05-09 stsp parent_path = strdup("");
3267 69960a46 2019-05-09 stsp if (parent_path == NULL)
3268 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3269 69960a46 2019-05-09 stsp } else {
3270 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
3271 69960a46 2019-05-09 stsp if (err)
3272 69960a46 2019-05-09 stsp return err;
3273 69960a46 2019-05-09 stsp }
3274 c4296144 2019-05-09 stsp
3275 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
3276 cf066bf8 2019-05-09 stsp if (ct == NULL) {
3277 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3278 c4296144 2019-05-09 stsp goto done;
3279 768aea60 2019-05-09 stsp }
3280 768aea60 2019-05-09 stsp
3281 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3282 768aea60 2019-05-09 stsp relpath) == -1) {
3283 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3284 768aea60 2019-05-09 stsp goto done;
3285 768aea60 2019-05-09 stsp }
3286 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3287 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3288 768aea60 2019-05-09 stsp } else {
3289 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
3290 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
3291 768aea60 2019-05-09 stsp goto done;
3292 768aea60 2019-05-09 stsp }
3293 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
3294 c4296144 2019-05-09 stsp }
3295 c4296144 2019-05-09 stsp
3296 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3297 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3298 44d03001 2019-05-09 stsp relpath) == -1) {
3299 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3300 44d03001 2019-05-09 stsp goto done;
3301 44d03001 2019-05-09 stsp }
3302 44d03001 2019-05-09 stsp
3303 cf066bf8 2019-05-09 stsp ct->status = status;
3304 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
3305 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
3306 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
3307 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
3308 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
3309 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
3310 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3311 b416585c 2019-05-13 stsp goto done;
3312 b416585c 2019-05-13 stsp }
3313 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
3314 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
3315 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
3316 f0b75401 2019-08-03 stsp goto done;
3317 f0b75401 2019-08-03 stsp }
3318 f0b75401 2019-08-03 stsp }
3319 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3320 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3321 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3322 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
3323 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3324 036813ee 2019-05-09 stsp goto done;
3325 036813ee 2019-05-09 stsp }
3326 ca2503ea 2019-05-09 stsp }
3327 24519714 2019-05-09 stsp ct->path = strdup(path);
3328 24519714 2019-05-09 stsp if (ct->path == NULL) {
3329 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3330 24519714 2019-05-09 stsp goto done;
3331 24519714 2019-05-09 stsp }
3332 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3333 c4296144 2019-05-09 stsp done:
3334 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
3335 ed175427 2019-05-09 stsp free_commitable(ct);
3336 24519714 2019-05-09 stsp free(parent_path);
3337 036813ee 2019-05-09 stsp free(path);
3338 a129376b 2019-03-28 stsp return err;
3339 a129376b 2019-03-28 stsp }
3340 c4296144 2019-05-09 stsp
3341 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
3342 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
3343 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3344 036813ee 2019-05-09 stsp struct got_repository *);
3345 ed175427 2019-05-09 stsp
3346 ed175427 2019-05-09 stsp static const struct got_error *
3347 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
3348 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
3349 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3350 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3351 afa376bf 2019-05-09 stsp struct got_repository *repo)
3352 ed175427 2019-05-09 stsp {
3353 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
3354 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
3355 036813ee 2019-05-09 stsp char *subpath;
3356 ed175427 2019-05-09 stsp
3357 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
3358 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3359 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3360 ed175427 2019-05-09 stsp
3361 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
3362 ed175427 2019-05-09 stsp if (err)
3363 ed175427 2019-05-09 stsp return err;
3364 ed175427 2019-05-09 stsp
3365 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3366 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3367 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
3368 036813ee 2019-05-09 stsp free(subpath);
3369 036813ee 2019-05-09 stsp return err;
3370 036813ee 2019-05-09 stsp }
3371 ed175427 2019-05-09 stsp
3372 036813ee 2019-05-09 stsp static const struct got_error *
3373 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3374 036813ee 2019-05-09 stsp {
3375 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3376 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
3377 ed175427 2019-05-09 stsp
3378 036813ee 2019-05-09 stsp *match = 0;
3379 ed175427 2019-05-09 stsp
3380 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
3381 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
3382 0f63689d 2019-05-10 stsp return NULL;
3383 036813ee 2019-05-09 stsp }
3384 0b5cc0d6 2019-05-09 stsp
3385 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3386 0f63689d 2019-05-10 stsp if (err)
3387 0f63689d 2019-05-10 stsp return err;
3388 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
3389 036813ee 2019-05-09 stsp free(ct_parent_path);
3390 036813ee 2019-05-09 stsp return err;
3391 036813ee 2019-05-09 stsp }
3392 0b5cc0d6 2019-05-09 stsp
3393 768aea60 2019-05-09 stsp static mode_t
3394 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
3395 768aea60 2019-05-09 stsp {
3396 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3397 768aea60 2019-05-09 stsp }
3398 768aea60 2019-05-09 stsp
3399 036813ee 2019-05-09 stsp static const struct got_error *
3400 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3401 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3402 036813ee 2019-05-09 stsp {
3403 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3404 ca2503ea 2019-05-09 stsp
3405 036813ee 2019-05-09 stsp *new_te = NULL;
3406 0b5cc0d6 2019-05-09 stsp
3407 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3408 036813ee 2019-05-09 stsp if (err)
3409 036813ee 2019-05-09 stsp goto done;
3410 0b5cc0d6 2019-05-09 stsp
3411 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3412 036813ee 2019-05-09 stsp
3413 036813ee 2019-05-09 stsp free((*new_te)->id);
3414 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
3415 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3416 5f8a88c6 2019-08-03 stsp else
3417 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3418 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3419 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3420 036813ee 2019-05-09 stsp goto done;
3421 036813ee 2019-05-09 stsp }
3422 036813ee 2019-05-09 stsp done:
3423 036813ee 2019-05-09 stsp if (err && *new_te) {
3424 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3425 036813ee 2019-05-09 stsp *new_te = NULL;
3426 036813ee 2019-05-09 stsp }
3427 036813ee 2019-05-09 stsp return err;
3428 036813ee 2019-05-09 stsp }
3429 036813ee 2019-05-09 stsp
3430 036813ee 2019-05-09 stsp static const struct got_error *
3431 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3432 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3433 036813ee 2019-05-09 stsp {
3434 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3435 036813ee 2019-05-09 stsp char *ct_name;
3436 036813ee 2019-05-09 stsp
3437 036813ee 2019-05-09 stsp *new_te = NULL;
3438 036813ee 2019-05-09 stsp
3439 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3440 036813ee 2019-05-09 stsp if (*new_te == NULL)
3441 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3442 036813ee 2019-05-09 stsp
3443 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3444 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3445 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3446 036813ee 2019-05-09 stsp goto done;
3447 036813ee 2019-05-09 stsp }
3448 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
3449 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
3450 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3451 036813ee 2019-05-09 stsp goto done;
3452 036813ee 2019-05-09 stsp }
3453 036813ee 2019-05-09 stsp
3454 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3455 036813ee 2019-05-09 stsp
3456 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
3457 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3458 5f8a88c6 2019-08-03 stsp else
3459 5f8a88c6 2019-08-03 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3460 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3461 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3462 036813ee 2019-05-09 stsp goto done;
3463 036813ee 2019-05-09 stsp }
3464 036813ee 2019-05-09 stsp done:
3465 036813ee 2019-05-09 stsp if (err && *new_te) {
3466 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3467 036813ee 2019-05-09 stsp *new_te = NULL;
3468 036813ee 2019-05-09 stsp }
3469 036813ee 2019-05-09 stsp return err;
3470 036813ee 2019-05-09 stsp }
3471 036813ee 2019-05-09 stsp
3472 036813ee 2019-05-09 stsp static const struct got_error *
3473 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3474 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3475 036813ee 2019-05-09 stsp {
3476 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3477 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3478 036813ee 2019-05-09 stsp
3479 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3480 036813ee 2019-05-09 stsp if (err)
3481 036813ee 2019-05-09 stsp return err;
3482 036813ee 2019-05-09 stsp if (new_pe == NULL)
3483 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3484 036813ee 2019-05-09 stsp return NULL;
3485 afa376bf 2019-05-09 stsp }
3486 afa376bf 2019-05-09 stsp
3487 afa376bf 2019-05-09 stsp static const struct got_error *
3488 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3489 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3490 afa376bf 2019-05-09 stsp {
3491 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3492 5f8a88c6 2019-08-03 stsp unsigned char status;
3493 5f8a88c6 2019-08-03 stsp
3494 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3495 afa376bf 2019-05-09 stsp ct_path++;
3496 5f8a88c6 2019-08-03 stsp
3497 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3498 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
3499 5f8a88c6 2019-08-03 stsp else
3500 5f8a88c6 2019-08-03 stsp status = ct->status;
3501 5f8a88c6 2019-08-03 stsp
3502 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3503 537ac44b 2019-08-03 stsp ct_path, ct->blob_id, NULL, NULL);
3504 036813ee 2019-05-09 stsp }
3505 036813ee 2019-05-09 stsp
3506 036813ee 2019-05-09 stsp static const struct got_error *
3507 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3508 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3509 44d03001 2019-05-09 stsp {
3510 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3511 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3512 44d03001 2019-05-09 stsp char *te_path;
3513 44d03001 2019-05-09 stsp
3514 44d03001 2019-05-09 stsp *modified = 0;
3515 44d03001 2019-05-09 stsp
3516 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3517 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3518 44d03001 2019-05-09 stsp te->name) == -1)
3519 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3520 44d03001 2019-05-09 stsp
3521 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3522 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3523 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3524 44d03001 2019-05-09 stsp strlen(te_path));
3525 44d03001 2019-05-09 stsp if (*modified)
3526 44d03001 2019-05-09 stsp break;
3527 44d03001 2019-05-09 stsp }
3528 44d03001 2019-05-09 stsp
3529 44d03001 2019-05-09 stsp free(te_path);
3530 44d03001 2019-05-09 stsp return err;
3531 44d03001 2019-05-09 stsp }
3532 44d03001 2019-05-09 stsp
3533 44d03001 2019-05-09 stsp static const struct got_error *
3534 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3535 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3536 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3537 036813ee 2019-05-09 stsp {
3538 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3539 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3540 036813ee 2019-05-09 stsp
3541 036813ee 2019-05-09 stsp *ctp = NULL;
3542 036813ee 2019-05-09 stsp
3543 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3544 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3545 036813ee 2019-05-09 stsp char *ct_name = NULL;
3546 036813ee 2019-05-09 stsp int path_matches;
3547 036813ee 2019-05-09 stsp
3548 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3549 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
3550 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
3551 5f8a88c6 2019-08-03 stsp continue;
3552 5f8a88c6 2019-08-03 stsp } else {
3553 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
3554 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
3555 5f8a88c6 2019-08-03 stsp continue;
3556 5f8a88c6 2019-08-03 stsp }
3557 036813ee 2019-05-09 stsp
3558 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3559 036813ee 2019-05-09 stsp continue;
3560 036813ee 2019-05-09 stsp
3561 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3562 036813ee 2019-05-09 stsp if (err)
3563 036813ee 2019-05-09 stsp return err;
3564 036813ee 2019-05-09 stsp if (!path_matches)
3565 036813ee 2019-05-09 stsp continue;
3566 036813ee 2019-05-09 stsp
3567 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3568 036813ee 2019-05-09 stsp if (ct_name == NULL)
3569 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3570 036813ee 2019-05-09 stsp
3571 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3572 036813ee 2019-05-09 stsp continue;
3573 036813ee 2019-05-09 stsp
3574 036813ee 2019-05-09 stsp *ctp = ct;
3575 036813ee 2019-05-09 stsp break;
3576 036813ee 2019-05-09 stsp }
3577 2b496619 2019-07-10 stsp
3578 2b496619 2019-07-10 stsp return err;
3579 2b496619 2019-07-10 stsp }
3580 2b496619 2019-07-10 stsp
3581 2b496619 2019-07-10 stsp static const struct got_error *
3582 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3583 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3584 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3585 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3586 2b496619 2019-07-10 stsp struct got_repository *repo)
3587 2b496619 2019-07-10 stsp {
3588 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3589 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3590 2b496619 2019-07-10 stsp char *subtree_path;
3591 2b496619 2019-07-10 stsp
3592 2b496619 2019-07-10 stsp *new_tep = NULL;
3593 2b496619 2019-07-10 stsp
3594 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3595 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3596 2b496619 2019-07-10 stsp child_path) == -1)
3597 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3598 036813ee 2019-05-09 stsp
3599 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3600 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3601 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3602 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3603 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3604 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3605 2b496619 2019-07-10 stsp goto done;
3606 2b496619 2019-07-10 stsp }
3607 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3608 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3609 2b496619 2019-07-10 stsp if (err) {
3610 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3611 2b496619 2019-07-10 stsp goto done;
3612 2b496619 2019-07-10 stsp }
3613 2b496619 2019-07-10 stsp done:
3614 2b496619 2019-07-10 stsp free(subtree_path);
3615 2b496619 2019-07-10 stsp if (err == NULL)
3616 2b496619 2019-07-10 stsp *new_tep = new_te;
3617 036813ee 2019-05-09 stsp return err;
3618 036813ee 2019-05-09 stsp }
3619 036813ee 2019-05-09 stsp
3620 036813ee 2019-05-09 stsp static const struct got_error *
3621 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3622 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3623 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3624 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3625 036813ee 2019-05-09 stsp struct got_repository *repo)
3626 036813ee 2019-05-09 stsp {
3627 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3628 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3629 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3630 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3631 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3632 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3633 036813ee 2019-05-09 stsp
3634 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3635 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3636 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3637 036813ee 2019-05-09 stsp
3638 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3639 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3640 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3641 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3642 036813ee 2019-05-09 stsp
3643 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
3644 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
3645 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3646 036813ee 2019-05-09 stsp continue;
3647 036813ee 2019-05-09 stsp
3648 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3649 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3650 036813ee 2019-05-09 stsp continue;
3651 036813ee 2019-05-09 stsp
3652 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3653 036813ee 2019-05-09 stsp pe->path);
3654 036813ee 2019-05-09 stsp if (err)
3655 036813ee 2019-05-09 stsp goto done;
3656 036813ee 2019-05-09 stsp
3657 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3658 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3659 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3660 036813ee 2019-05-09 stsp if (err)
3661 036813ee 2019-05-09 stsp goto done;
3662 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3663 afa376bf 2019-05-09 stsp if (err)
3664 afa376bf 2019-05-09 stsp goto done;
3665 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3666 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3667 2b496619 2019-07-10 stsp if (err)
3668 2f51b5b3 2019-05-09 stsp goto done;
3669 2b496619 2019-07-10 stsp } else {
3670 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3671 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3672 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3673 2b496619 2019-07-10 stsp == NULL) {
3674 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3675 2b496619 2019-07-10 stsp child_path, path_base_tree,
3676 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3677 2b496619 2019-07-10 stsp repo);
3678 2b496619 2019-07-10 stsp if (err)
3679 2b496619 2019-07-10 stsp goto done;
3680 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3681 2b496619 2019-07-10 stsp if (err)
3682 2b496619 2019-07-10 stsp goto done;
3683 9ba0479c 2019-05-10 stsp }
3684 ed175427 2019-05-09 stsp }
3685 2f51b5b3 2019-05-09 stsp }
3686 2f51b5b3 2019-05-09 stsp
3687 2f51b5b3 2019-05-09 stsp if (base_tree) {
3688 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3689 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3690 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3691 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3692 2f51b5b3 2019-05-09 stsp
3693 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3694 44d03001 2019-05-09 stsp int modified;
3695 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3696 036813ee 2019-05-09 stsp if (err)
3697 036813ee 2019-05-09 stsp goto done;
3698 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3699 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3700 2f51b5b3 2019-05-09 stsp if (err)
3701 2f51b5b3 2019-05-09 stsp goto done;
3702 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3703 44d03001 2019-05-09 stsp if (modified) {
3704 44d03001 2019-05-09 stsp free(new_te->id);
3705 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3706 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3707 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3708 44d03001 2019-05-09 stsp if (err)
3709 44d03001 2019-05-09 stsp goto done;
3710 44d03001 2019-05-09 stsp }
3711 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3712 036813ee 2019-05-09 stsp if (err)
3713 036813ee 2019-05-09 stsp goto done;
3714 2f51b5b3 2019-05-09 stsp continue;
3715 036813ee 2019-05-09 stsp }
3716 2f51b5b3 2019-05-09 stsp
3717 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3718 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3719 2f51b5b3 2019-05-09 stsp if (ct) {
3720 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3721 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
3722 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3723 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3724 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3725 2f51b5b3 2019-05-09 stsp if (err)
3726 2f51b5b3 2019-05-09 stsp goto done;
3727 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3728 2f51b5b3 2019-05-09 stsp if (err)
3729 2f51b5b3 2019-05-09 stsp goto done;
3730 2f51b5b3 2019-05-09 stsp }
3731 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3732 b416585c 2019-05-13 stsp status_arg);
3733 afa376bf 2019-05-09 stsp if (err)
3734 afa376bf 2019-05-09 stsp goto done;
3735 2f51b5b3 2019-05-09 stsp } else {
3736 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3737 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3738 2f51b5b3 2019-05-09 stsp if (err)
3739 2f51b5b3 2019-05-09 stsp goto done;
3740 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3741 2f51b5b3 2019-05-09 stsp if (err)
3742 2f51b5b3 2019-05-09 stsp goto done;
3743 2f51b5b3 2019-05-09 stsp }
3744 ca2503ea 2019-05-09 stsp }
3745 ed175427 2019-05-09 stsp }
3746 0b5cc0d6 2019-05-09 stsp
3747 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3748 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3749 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3750 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3751 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3752 0b5cc0d6 2019-05-09 stsp }
3753 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3754 ed175427 2019-05-09 stsp done:
3755 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3756 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3757 ed175427 2019-05-09 stsp return err;
3758 ed175427 2019-05-09 stsp }
3759 ed175427 2019-05-09 stsp
3760 ebf99748 2019-05-09 stsp static const struct got_error *
3761 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3762 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3763 ebf99748 2019-05-09 stsp {
3764 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3765 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3766 ebf99748 2019-05-09 stsp
3767 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3768 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3769 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3770 ebf99748 2019-05-09 stsp
3771 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3772 ebf99748 2019-05-09 stsp if (ie) {
3773 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
3774 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
3775 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3776 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3777 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
3778 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
3779 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
3780 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
3781 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
3782 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
3783 5f8a88c6 2019-08-03 stsp new_base_commit_id->sha1, 1);
3784 ebf99748 2019-05-09 stsp } else
3785 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3786 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3787 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3788 ebf99748 2019-05-09 stsp } else {
3789 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3790 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3791 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3792 ebf99748 2019-05-09 stsp if (err)
3793 af12c6b9 2019-06-04 stsp break;
3794 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3795 ebf99748 2019-05-09 stsp if (err)
3796 af12c6b9 2019-06-04 stsp break;
3797 ebf99748 2019-05-09 stsp }
3798 ebf99748 2019-05-09 stsp }
3799 d56d26ce 2019-05-10 stsp return err;
3800 d56d26ce 2019-05-10 stsp }
3801 735ef5ac 2019-08-03 stsp
3802 d56d26ce 2019-05-10 stsp
3803 d56d26ce 2019-05-10 stsp static const struct got_error *
3804 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
3805 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
3806 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
3807 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
3808 735ef5ac 2019-08-03 stsp int ood_errcode)
3809 d56d26ce 2019-05-10 stsp {
3810 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3811 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
3812 1a36436d 2019-06-10 stsp
3813 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3814 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3815 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3816 1a36436d 2019-06-10 stsp return NULL;
3817 9bead371 2019-07-28 stsp /*
3818 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
3819 9bead371 2019-07-28 stsp * on matches file content in the branch head.
3820 9bead371 2019-07-28 stsp */
3821 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3822 735ef5ac 2019-08-03 stsp in_repo_path);
3823 9bead371 2019-07-28 stsp if (err) {
3824 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
3825 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
3826 1a36436d 2019-06-10 stsp goto done;
3827 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
3828 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
3829 1a36436d 2019-06-10 stsp } else {
3830 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3831 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
3832 735ef5ac 2019-08-03 stsp in_repo_path);
3833 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3834 1a36436d 2019-06-10 stsp goto done;
3835 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
3836 1a36436d 2019-06-10 stsp }
3837 1a36436d 2019-06-10 stsp done:
3838 1a36436d 2019-06-10 stsp free(id);
3839 1a36436d 2019-06-10 stsp return err;
3840 ebf99748 2019-05-09 stsp }
3841 ebf99748 2019-05-09 stsp
3842 c4296144 2019-05-09 stsp const struct got_error *
3843 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3844 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3845 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3846 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
3847 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3848 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3849 afa376bf 2019-05-09 stsp struct got_repository *repo)
3850 c4296144 2019-05-09 stsp {
3851 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3852 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3853 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3854 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3855 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3856 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3857 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3858 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3859 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3860 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3861 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3862 c4296144 2019-05-09 stsp
3863 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3864 c4296144 2019-05-09 stsp
3865 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3866 675c7539 2019-05-09 stsp
3867 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3868 588edf97 2019-05-10 stsp if (err)
3869 588edf97 2019-05-10 stsp goto done;
3870 de18fc63 2019-05-09 stsp
3871 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3872 588edf97 2019-05-10 stsp if (err)
3873 588edf97 2019-05-10 stsp goto done;
3874 588edf97 2019-05-10 stsp
3875 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3876 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3877 33ad4cbe 2019-05-12 jcs if (err)
3878 33ad4cbe 2019-05-12 jcs goto done;
3879 33ad4cbe 2019-05-12 jcs }
3880 c4296144 2019-05-09 stsp
3881 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3882 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3883 33ad4cbe 2019-05-12 jcs goto done;
3884 33ad4cbe 2019-05-12 jcs }
3885 33ad4cbe 2019-05-12 jcs
3886 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3887 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3888 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3889 cf066bf8 2019-05-09 stsp char *ondisk_path;
3890 cf066bf8 2019-05-09 stsp
3891 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
3892 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
3893 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
3894 5f8a88c6 2019-08-03 stsp continue;
3895 5f8a88c6 2019-08-03 stsp
3896 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3897 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3898 cf066bf8 2019-05-09 stsp continue;
3899 cf066bf8 2019-05-09 stsp
3900 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3901 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3902 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3903 cf066bf8 2019-05-09 stsp goto done;
3904 cf066bf8 2019-05-09 stsp }
3905 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3906 a7055788 2019-05-09 stsp free(ondisk_path);
3907 cf066bf8 2019-05-09 stsp if (err)
3908 cf066bf8 2019-05-09 stsp goto done;
3909 cf066bf8 2019-05-09 stsp }
3910 036813ee 2019-05-09 stsp
3911 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3912 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3913 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3914 036813ee 2019-05-09 stsp if (err)
3915 036813ee 2019-05-09 stsp goto done;
3916 036813ee 2019-05-09 stsp
3917 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3918 de18fc63 2019-05-09 stsp if (err)
3919 de18fc63 2019-05-09 stsp goto done;
3920 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3921 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3922 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3923 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3924 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3925 33ad4cbe 2019-05-12 jcs free(logmsg);
3926 9d40349a 2019-05-09 stsp if (err)
3927 9d40349a 2019-05-09 stsp goto done;
3928 9d40349a 2019-05-09 stsp
3929 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3930 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3931 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3932 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3933 09f5bd90 2019-05-09 stsp goto done;
3934 09f5bd90 2019-05-09 stsp }
3935 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3936 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3937 09f5bd90 2019-05-09 stsp if (err)
3938 09f5bd90 2019-05-09 stsp goto done;
3939 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3940 ebf99748 2019-05-09 stsp if (err)
3941 ebf99748 2019-05-09 stsp goto done;
3942 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3943 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3944 09f5bd90 2019-05-09 stsp goto done;
3945 09f5bd90 2019-05-09 stsp }
3946 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3947 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3948 f2c16586 2019-05-09 stsp if (err)
3949 f2c16586 2019-05-09 stsp goto done;
3950 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3951 f2c16586 2019-05-09 stsp if (err)
3952 f2c16586 2019-05-09 stsp goto done;
3953 f2c16586 2019-05-09 stsp
3954 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3955 09f5bd90 2019-05-09 stsp if (err)
3956 09f5bd90 2019-05-09 stsp goto done;
3957 09f5bd90 2019-05-09 stsp
3958 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3959 ebf99748 2019-05-09 stsp if (err)
3960 ebf99748 2019-05-09 stsp goto done;
3961 c4296144 2019-05-09 stsp done:
3962 588edf97 2019-05-10 stsp if (head_tree)
3963 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3964 588edf97 2019-05-10 stsp if (head_commit)
3965 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3966 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3967 2f17228e 2019-05-12 stsp if (head_ref2) {
3968 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3969 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3970 2f17228e 2019-05-12 stsp err = unlockerr;
3971 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3972 39cd0ff6 2019-07-12 stsp }
3973 39cd0ff6 2019-07-12 stsp return err;
3974 39cd0ff6 2019-07-12 stsp }
3975 5c1e53bc 2019-07-28 stsp
3976 5c1e53bc 2019-07-28 stsp static const struct got_error *
3977 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
3978 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
3979 5c1e53bc 2019-07-28 stsp {
3980 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
3981 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
3982 39cd0ff6 2019-07-12 stsp
3983 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
3984 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
3985 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
3986 5c1e53bc 2019-07-28 stsp
3987 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
3988 5c1e53bc 2019-07-28 stsp ct_path++;
3989 5c1e53bc 2019-07-28 stsp
3990 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
3991 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
3992 5c1e53bc 2019-07-28 stsp break;
3993 5c1e53bc 2019-07-28 stsp }
3994 5c1e53bc 2019-07-28 stsp
3995 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
3996 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
3997 5c1e53bc 2019-07-28 stsp
3998 5c1e53bc 2019-07-28 stsp return NULL;
3999 5c1e53bc 2019-07-28 stsp }
4000 5c1e53bc 2019-07-28 stsp
4001 f0b75401 2019-08-03 stsp static const struct got_error *
4002 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
4003 f0b75401 2019-08-03 stsp {
4004 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
4005 f0b75401 2019-08-03 stsp
4006 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4007 f0b75401 2019-08-03 stsp *have_staged_files = 1;
4008 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
4009 f0b75401 2019-08-03 stsp }
4010 f0b75401 2019-08-03 stsp
4011 f0b75401 2019-08-03 stsp return NULL;
4012 f0b75401 2019-08-03 stsp }
4013 f0b75401 2019-08-03 stsp
4014 5f8a88c6 2019-08-03 stsp static const struct got_error *
4015 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
4016 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
4017 5f8a88c6 2019-08-03 stsp {
4018 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
4019 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
4020 5f8a88c6 2019-08-03 stsp
4021 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
4022 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
4023 5f8a88c6 2019-08-03 stsp continue;
4024 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4025 5f8a88c6 2019-08-03 stsp if (ie == NULL)
4026 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4027 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4028 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
4029 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
4030 5f8a88c6 2019-08-03 stsp }
4031 5f8a88c6 2019-08-03 stsp
4032 5f8a88c6 2019-08-03 stsp return NULL;
4033 5f8a88c6 2019-08-03 stsp }
4034 5f8a88c6 2019-08-03 stsp
4035 39cd0ff6 2019-07-12 stsp const struct got_error *
4036 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
4037 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
4038 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
4039 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4040 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4041 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
4042 39cd0ff6 2019-07-12 stsp {
4043 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4044 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4045 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
4046 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4047 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4048 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
4049 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
4050 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4051 f0b75401 2019-08-03 stsp int have_staged_files = 0;
4052 39cd0ff6 2019-07-12 stsp
4053 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
4054 39cd0ff6 2019-07-12 stsp
4055 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4056 39cd0ff6 2019-07-12 stsp
4057 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
4058 39cd0ff6 2019-07-12 stsp if (err)
4059 39cd0ff6 2019-07-12 stsp goto done;
4060 39cd0ff6 2019-07-12 stsp
4061 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4062 39cd0ff6 2019-07-12 stsp if (err)
4063 39cd0ff6 2019-07-12 stsp goto done;
4064 39cd0ff6 2019-07-12 stsp
4065 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4066 39cd0ff6 2019-07-12 stsp if (err)
4067 39cd0ff6 2019-07-12 stsp goto done;
4068 39cd0ff6 2019-07-12 stsp
4069 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4070 39cd0ff6 2019-07-12 stsp if (err)
4071 39cd0ff6 2019-07-12 stsp goto done;
4072 39cd0ff6 2019-07-12 stsp
4073 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4074 5f8a88c6 2019-08-03 stsp &have_staged_files);
4075 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
4076 5f8a88c6 2019-08-03 stsp goto done;
4077 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
4078 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
4079 5f8a88c6 2019-08-03 stsp if (err)
4080 5f8a88c6 2019-08-03 stsp goto done;
4081 5f8a88c6 2019-08-03 stsp }
4082 5f8a88c6 2019-08-03 stsp
4083 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4084 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
4085 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
4086 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
4087 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4088 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4089 5c1e53bc 2019-07-28 stsp collect_commitables, &cc_arg, NULL, NULL);
4090 5c1e53bc 2019-07-28 stsp if (err)
4091 5c1e53bc 2019-07-28 stsp goto done;
4092 5c1e53bc 2019-07-28 stsp }
4093 39cd0ff6 2019-07-12 stsp
4094 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4095 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4096 39cd0ff6 2019-07-12 stsp goto done;
4097 5c1e53bc 2019-07-28 stsp }
4098 5c1e53bc 2019-07-28 stsp
4099 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4100 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
4101 5c1e53bc 2019-07-28 stsp if (err)
4102 5c1e53bc 2019-07-28 stsp goto done;
4103 39cd0ff6 2019-07-12 stsp }
4104 f0b75401 2019-08-03 stsp
4105 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4106 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
4107 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
4108 f0b75401 2019-08-03 stsp
4109 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
4110 f0b75401 2019-08-03 stsp ct_path++;
4111 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
4112 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4113 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4114 b50cabdf 2019-07-12 stsp if (err)
4115 b50cabdf 2019-07-12 stsp goto done;
4116 f0b75401 2019-08-03 stsp
4117 b50cabdf 2019-07-12 stsp }
4118 b50cabdf 2019-07-12 stsp
4119 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
4120 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
4121 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4122 39cd0ff6 2019-07-12 stsp if (err)
4123 39cd0ff6 2019-07-12 stsp goto done;
4124 39cd0ff6 2019-07-12 stsp
4125 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4126 93ec1b5f 2019-07-12 stsp fileindex);
4127 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4128 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
4129 39cd0ff6 2019-07-12 stsp err = sync_err;
4130 39cd0ff6 2019-07-12 stsp done:
4131 39cd0ff6 2019-07-12 stsp if (fileindex)
4132 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
4133 39cd0ff6 2019-07-12 stsp free(fileindex_path);
4134 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4135 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
4136 39cd0ff6 2019-07-12 stsp err = unlockerr;
4137 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4138 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
4139 39cd0ff6 2019-07-12 stsp free_commitable(ct);
4140 39cd0ff6 2019-07-12 stsp }
4141 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
4142 c4296144 2019-05-09 stsp return err;
4143 8656d6c4 2019-05-20 stsp }
4144 8656d6c4 2019-05-20 stsp
4145 8656d6c4 2019-05-20 stsp const char *
4146 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
4147 8656d6c4 2019-05-20 stsp {
4148 8656d6c4 2019-05-20 stsp return ct->path;
4149 8656d6c4 2019-05-20 stsp }
4150 8656d6c4 2019-05-20 stsp
4151 8656d6c4 2019-05-20 stsp unsigned int
4152 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
4153 8656d6c4 2019-05-20 stsp {
4154 8656d6c4 2019-05-20 stsp return ct->status;
4155 818c7501 2019-07-11 stsp }
4156 818c7501 2019-07-11 stsp
4157 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
4158 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4159 818c7501 2019-07-11 stsp struct got_repository *repo;
4160 818c7501 2019-07-11 stsp };
4161 818c7501 2019-07-11 stsp
4162 818c7501 2019-07-11 stsp static const struct got_error *
4163 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4164 818c7501 2019-07-11 stsp {
4165 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4166 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
4167 818c7501 2019-07-11 stsp unsigned char status;
4168 818c7501 2019-07-11 stsp struct stat sb;
4169 818c7501 2019-07-11 stsp char *ondisk_path;
4170 818c7501 2019-07-11 stsp
4171 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
4172 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4173 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
4174 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
4175 818c7501 2019-07-11 stsp
4176 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4177 818c7501 2019-07-11 stsp == -1)
4178 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4179 818c7501 2019-07-11 stsp
4180 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
4181 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4182 818c7501 2019-07-11 stsp free(ondisk_path);
4183 818c7501 2019-07-11 stsp if (err)
4184 818c7501 2019-07-11 stsp return err;
4185 818c7501 2019-07-11 stsp
4186 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
4187 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
4188 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4189 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4190 818c7501 2019-07-11 stsp
4191 818c7501 2019-07-11 stsp return NULL;
4192 818c7501 2019-07-11 stsp }
4193 818c7501 2019-07-11 stsp
4194 818c7501 2019-07-11 stsp const struct got_error *
4195 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4196 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4197 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
4198 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4199 818c7501 2019-07-11 stsp {
4200 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4201 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4202 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
4203 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4204 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
4205 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4206 818c7501 2019-07-11 stsp
4207 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4208 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4209 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4210 818c7501 2019-07-11 stsp
4211 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4212 818c7501 2019-07-11 stsp if (err)
4213 818c7501 2019-07-11 stsp return err;
4214 818c7501 2019-07-11 stsp
4215 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4216 818c7501 2019-07-11 stsp if (err)
4217 818c7501 2019-07-11 stsp goto done;
4218 818c7501 2019-07-11 stsp
4219 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
4220 818c7501 2019-07-11 stsp ok_arg.repo = repo;
4221 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4222 818c7501 2019-07-11 stsp &ok_arg);
4223 818c7501 2019-07-11 stsp if (err)
4224 818c7501 2019-07-11 stsp goto done;
4225 818c7501 2019-07-11 stsp
4226 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4227 818c7501 2019-07-11 stsp if (err)
4228 818c7501 2019-07-11 stsp goto done;
4229 818c7501 2019-07-11 stsp
4230 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4231 818c7501 2019-07-11 stsp if (err)
4232 818c7501 2019-07-11 stsp goto done;
4233 818c7501 2019-07-11 stsp
4234 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4235 818c7501 2019-07-11 stsp if (err)
4236 818c7501 2019-07-11 stsp goto done;
4237 818c7501 2019-07-11 stsp
4238 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4239 818c7501 2019-07-11 stsp 0);
4240 818c7501 2019-07-11 stsp if (err)
4241 818c7501 2019-07-11 stsp goto done;
4242 818c7501 2019-07-11 stsp
4243 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
4244 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
4245 818c7501 2019-07-11 stsp if (err)
4246 818c7501 2019-07-11 stsp goto done;
4247 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
4248 818c7501 2019-07-11 stsp if (err)
4249 818c7501 2019-07-11 stsp goto done;
4250 818c7501 2019-07-11 stsp
4251 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
4252 818c7501 2019-07-11 stsp
4253 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4254 818c7501 2019-07-11 stsp if (err)
4255 818c7501 2019-07-11 stsp goto done;
4256 818c7501 2019-07-11 stsp
4257 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
4258 818c7501 2019-07-11 stsp if (err)
4259 818c7501 2019-07-11 stsp goto done;
4260 818c7501 2019-07-11 stsp
4261 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4262 818c7501 2019-07-11 stsp worktree->base_commit_id);
4263 818c7501 2019-07-11 stsp if (err)
4264 818c7501 2019-07-11 stsp goto done;
4265 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
4266 818c7501 2019-07-11 stsp if (err)
4267 818c7501 2019-07-11 stsp goto done;
4268 818c7501 2019-07-11 stsp
4269 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4270 818c7501 2019-07-11 stsp if (err)
4271 818c7501 2019-07-11 stsp goto done;
4272 818c7501 2019-07-11 stsp done:
4273 818c7501 2019-07-11 stsp free(fileindex_path);
4274 818c7501 2019-07-11 stsp free(tmp_branch_name);
4275 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
4276 818c7501 2019-07-11 stsp free(branch_ref_name);
4277 818c7501 2019-07-11 stsp if (branch_ref)
4278 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4279 818c7501 2019-07-11 stsp if (wt_branch)
4280 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
4281 818c7501 2019-07-11 stsp if (err) {
4282 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
4283 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
4284 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
4285 818c7501 2019-07-11 stsp }
4286 818c7501 2019-07-11 stsp if (*tmp_branch) {
4287 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4288 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4289 818c7501 2019-07-11 stsp }
4290 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4291 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4292 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4293 3e3a69f1 2019-07-25 stsp }
4294 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
4295 818c7501 2019-07-11 stsp }
4296 818c7501 2019-07-11 stsp return err;
4297 818c7501 2019-07-11 stsp }
4298 818c7501 2019-07-11 stsp
4299 818c7501 2019-07-11 stsp const struct got_error *
4300 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
4301 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4302 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
4303 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
4304 818c7501 2019-07-11 stsp {
4305 818c7501 2019-07-11 stsp const struct got_error *err;
4306 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4307 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4308 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4309 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4310 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
4311 818c7501 2019-07-11 stsp
4312 818c7501 2019-07-11 stsp *commit_id = NULL;
4313 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
4314 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
4315 3e3a69f1 2019-07-25 stsp *branch = NULL;
4316 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4317 3e3a69f1 2019-07-25 stsp
4318 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4319 3e3a69f1 2019-07-25 stsp if (err)
4320 3e3a69f1 2019-07-25 stsp return err;
4321 818c7501 2019-07-11 stsp
4322 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4323 3e3a69f1 2019-07-25 stsp if (err)
4324 f032f1f7 2019-08-04 stsp goto done;
4325 f032f1f7 2019-08-04 stsp
4326 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4327 f032f1f7 2019-08-04 stsp &have_staged_files);
4328 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
4329 f032f1f7 2019-08-04 stsp goto done;
4330 f032f1f7 2019-08-04 stsp if (have_staged_files) {
4331 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
4332 3e3a69f1 2019-07-25 stsp goto done;
4333 f032f1f7 2019-08-04 stsp }
4334 3e3a69f1 2019-07-25 stsp
4335 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4336 818c7501 2019-07-11 stsp if (err)
4337 f032f1f7 2019-08-04 stsp goto done;
4338 818c7501 2019-07-11 stsp
4339 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4340 818c7501 2019-07-11 stsp if (err)
4341 818c7501 2019-07-11 stsp goto done;
4342 818c7501 2019-07-11 stsp
4343 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4344 818c7501 2019-07-11 stsp if (err)
4345 818c7501 2019-07-11 stsp goto done;
4346 818c7501 2019-07-11 stsp
4347 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4348 818c7501 2019-07-11 stsp if (err)
4349 818c7501 2019-07-11 stsp goto done;
4350 818c7501 2019-07-11 stsp
4351 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4352 818c7501 2019-07-11 stsp if (err)
4353 818c7501 2019-07-11 stsp goto done;
4354 818c7501 2019-07-11 stsp
4355 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
4356 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
4357 818c7501 2019-07-11 stsp if (err)
4358 818c7501 2019-07-11 stsp goto done;
4359 818c7501 2019-07-11 stsp
4360 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4361 818c7501 2019-07-11 stsp if (err)
4362 818c7501 2019-07-11 stsp goto done;
4363 818c7501 2019-07-11 stsp
4364 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4365 818c7501 2019-07-11 stsp if (err)
4366 818c7501 2019-07-11 stsp goto done;
4367 818c7501 2019-07-11 stsp
4368 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
4369 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
4370 818c7501 2019-07-11 stsp if (err)
4371 818c7501 2019-07-11 stsp goto done;
4372 818c7501 2019-07-11 stsp
4373 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4374 818c7501 2019-07-11 stsp if (err)
4375 818c7501 2019-07-11 stsp goto done;
4376 818c7501 2019-07-11 stsp done:
4377 818c7501 2019-07-11 stsp free(commit_ref_name);
4378 818c7501 2019-07-11 stsp free(branch_ref_name);
4379 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4380 818c7501 2019-07-11 stsp if (commit_ref)
4381 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4382 818c7501 2019-07-11 stsp if (branch_ref)
4383 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
4384 818c7501 2019-07-11 stsp if (err) {
4385 818c7501 2019-07-11 stsp free(*commit_id);
4386 818c7501 2019-07-11 stsp *commit_id = NULL;
4387 818c7501 2019-07-11 stsp if (*tmp_branch) {
4388 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
4389 818c7501 2019-07-11 stsp *tmp_branch = NULL;
4390 818c7501 2019-07-11 stsp }
4391 818c7501 2019-07-11 stsp if (*new_base_branch) {
4392 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
4393 818c7501 2019-07-11 stsp *new_base_branch = NULL;
4394 818c7501 2019-07-11 stsp }
4395 818c7501 2019-07-11 stsp if (*branch) {
4396 818c7501 2019-07-11 stsp got_ref_close(*branch);
4397 818c7501 2019-07-11 stsp *branch = NULL;
4398 818c7501 2019-07-11 stsp }
4399 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4400 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4401 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4402 3e3a69f1 2019-07-25 stsp }
4403 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
4404 818c7501 2019-07-11 stsp }
4405 818c7501 2019-07-11 stsp return err;
4406 c4296144 2019-05-09 stsp }
4407 818c7501 2019-07-11 stsp
4408 818c7501 2019-07-11 stsp const struct got_error *
4409 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4410 818c7501 2019-07-11 stsp {
4411 818c7501 2019-07-11 stsp const struct got_error *err;
4412 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
4413 818c7501 2019-07-11 stsp
4414 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4415 818c7501 2019-07-11 stsp if (err)
4416 818c7501 2019-07-11 stsp return err;
4417 818c7501 2019-07-11 stsp
4418 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4419 818c7501 2019-07-11 stsp free(tmp_branch_name);
4420 818c7501 2019-07-11 stsp return NULL;
4421 818c7501 2019-07-11 stsp }
4422 818c7501 2019-07-11 stsp
4423 818c7501 2019-07-11 stsp static const struct got_error *
4424 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4425 818c7501 2019-07-11 stsp char **logmsg, void *arg)
4426 818c7501 2019-07-11 stsp {
4427 0ebf8283 2019-07-24 stsp *logmsg = arg;
4428 818c7501 2019-07-11 stsp return NULL;
4429 818c7501 2019-07-11 stsp }
4430 818c7501 2019-07-11 stsp
4431 818c7501 2019-07-11 stsp static const struct got_error *
4432 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4433 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4434 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4435 818c7501 2019-07-11 stsp {
4436 818c7501 2019-07-11 stsp return NULL;
4437 01757395 2019-07-12 stsp }
4438 01757395 2019-07-12 stsp
4439 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
4440 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
4441 01757395 2019-07-12 stsp void *progress_arg;
4442 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
4443 01757395 2019-07-12 stsp };
4444 01757395 2019-07-12 stsp
4445 01757395 2019-07-12 stsp static const struct got_error *
4446 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
4447 01757395 2019-07-12 stsp {
4448 01757395 2019-07-12 stsp const struct got_error *err;
4449 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
4450 01757395 2019-07-12 stsp char *p;
4451 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
4452 01757395 2019-07-12 stsp
4453 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
4454 01757395 2019-07-12 stsp if (err)
4455 01757395 2019-07-12 stsp return err;
4456 01757395 2019-07-12 stsp
4457 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
4458 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
4459 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
4460 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
4461 01757395 2019-07-12 stsp return NULL;
4462 01757395 2019-07-12 stsp
4463 01757395 2019-07-12 stsp p = strdup(path);
4464 01757395 2019-07-12 stsp if (p == NULL)
4465 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
4466 01757395 2019-07-12 stsp
4467 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4468 01757395 2019-07-12 stsp if (err || new == NULL)
4469 01757395 2019-07-12 stsp free(p);
4470 01757395 2019-07-12 stsp return err;
4471 01757395 2019-07-12 stsp }
4472 01757395 2019-07-12 stsp
4473 01757395 2019-07-12 stsp void
4474 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4475 01757395 2019-07-12 stsp {
4476 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4477 01757395 2019-07-12 stsp
4478 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
4479 01757395 2019-07-12 stsp free((char *)pe->path);
4480 01757395 2019-07-12 stsp
4481 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
4482 818c7501 2019-07-11 stsp }
4483 818c7501 2019-07-11 stsp
4484 0ebf8283 2019-07-24 stsp static const struct got_error *
4485 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4486 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4487 818c7501 2019-07-11 stsp {
4488 818c7501 2019-07-11 stsp const struct got_error *err;
4489 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
4490 818c7501 2019-07-11 stsp
4491 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4492 818c7501 2019-07-11 stsp if (err) {
4493 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
4494 818c7501 2019-07-11 stsp goto done;
4495 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4496 818c7501 2019-07-11 stsp if (err)
4497 818c7501 2019-07-11 stsp goto done;
4498 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
4499 818c7501 2019-07-11 stsp if (err)
4500 818c7501 2019-07-11 stsp goto done;
4501 818c7501 2019-07-11 stsp } else {
4502 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
4503 818c7501 2019-07-11 stsp int cmp;
4504 818c7501 2019-07-11 stsp
4505 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
4506 818c7501 2019-07-11 stsp if (err)
4507 818c7501 2019-07-11 stsp goto done;
4508 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
4509 818c7501 2019-07-11 stsp free(stored_id);
4510 818c7501 2019-07-11 stsp if (cmp != 0) {
4511 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4512 818c7501 2019-07-11 stsp goto done;
4513 818c7501 2019-07-11 stsp }
4514 818c7501 2019-07-11 stsp }
4515 0ebf8283 2019-07-24 stsp done:
4516 0ebf8283 2019-07-24 stsp if (commit_ref)
4517 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4518 0ebf8283 2019-07-24 stsp return err;
4519 0ebf8283 2019-07-24 stsp }
4520 0ebf8283 2019-07-24 stsp
4521 0ebf8283 2019-07-24 stsp static const struct got_error *
4522 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
4523 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
4524 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4525 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
4526 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4527 3e3a69f1 2019-07-25 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4528 0ebf8283 2019-07-24 stsp {
4529 0ebf8283 2019-07-24 stsp const struct got_error *err;
4530 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4531 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
4532 3e3a69f1 2019-07-25 stsp char *fileindex_path;
4533 818c7501 2019-07-11 stsp
4534 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4535 0ebf8283 2019-07-24 stsp
4536 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4537 0ebf8283 2019-07-24 stsp if (err)
4538 0ebf8283 2019-07-24 stsp return err;
4539 0ebf8283 2019-07-24 stsp
4540 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
4541 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
4542 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
4543 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
4544 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
4545 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
4546 818c7501 2019-07-11 stsp if (commit_ref)
4547 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4548 818c7501 2019-07-11 stsp return err;
4549 818c7501 2019-07-11 stsp }
4550 818c7501 2019-07-11 stsp
4551 818c7501 2019-07-11 stsp const struct got_error *
4552 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4553 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4554 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4555 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4556 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4557 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4558 818c7501 2019-07-11 stsp {
4559 0ebf8283 2019-07-24 stsp const struct got_error *err;
4560 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4561 0ebf8283 2019-07-24 stsp
4562 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4563 0ebf8283 2019-07-24 stsp if (err)
4564 0ebf8283 2019-07-24 stsp return err;
4565 0ebf8283 2019-07-24 stsp
4566 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4567 0ebf8283 2019-07-24 stsp if (err)
4568 0ebf8283 2019-07-24 stsp goto done;
4569 0ebf8283 2019-07-24 stsp
4570 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4571 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4572 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4573 0ebf8283 2019-07-24 stsp done:
4574 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4575 0ebf8283 2019-07-24 stsp return err;
4576 0ebf8283 2019-07-24 stsp }
4577 0ebf8283 2019-07-24 stsp
4578 0ebf8283 2019-07-24 stsp const struct got_error *
4579 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4580 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4581 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4582 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4583 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4584 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4585 0ebf8283 2019-07-24 stsp {
4586 0ebf8283 2019-07-24 stsp const struct got_error *err;
4587 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4588 0ebf8283 2019-07-24 stsp
4589 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4590 0ebf8283 2019-07-24 stsp if (err)
4591 0ebf8283 2019-07-24 stsp return err;
4592 0ebf8283 2019-07-24 stsp
4593 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4594 0ebf8283 2019-07-24 stsp if (err)
4595 0ebf8283 2019-07-24 stsp goto done;
4596 0ebf8283 2019-07-24 stsp
4597 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4598 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4599 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4600 0ebf8283 2019-07-24 stsp done:
4601 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4602 0ebf8283 2019-07-24 stsp return err;
4603 0ebf8283 2019-07-24 stsp }
4604 0ebf8283 2019-07-24 stsp
4605 0ebf8283 2019-07-24 stsp static const struct got_error *
4606 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4607 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4608 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4609 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4610 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4611 0ebf8283 2019-07-24 stsp {
4612 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4613 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4614 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4615 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4616 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4617 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4618 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4619 818c7501 2019-07-11 stsp
4620 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4621 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4622 a0e95631 2019-07-12 stsp
4623 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4624 818c7501 2019-07-11 stsp
4625 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4626 a0e95631 2019-07-12 stsp if (err)
4627 3e3a69f1 2019-07-25 stsp return err;
4628 a0e95631 2019-07-12 stsp
4629 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4630 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4631 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4632 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
4633 01757395 2019-07-12 stsp /*
4634 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4635 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4636 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4637 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4638 01757395 2019-07-12 stsp */
4639 01757395 2019-07-12 stsp if (merged_paths) {
4640 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4641 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4642 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4643 8f8646e5 2019-07-25 stsp goto done;
4644 8f8646e5 2019-07-25 stsp }
4645 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4646 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4647 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4648 01757395 2019-07-12 stsp if (err)
4649 01757395 2019-07-12 stsp goto done;
4650 01757395 2019-07-12 stsp }
4651 01757395 2019-07-12 stsp } else {
4652 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4653 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4654 01757395 2019-07-12 stsp if (err)
4655 01757395 2019-07-12 stsp goto done;
4656 01757395 2019-07-12 stsp }
4657 a0e95631 2019-07-12 stsp
4658 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4659 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4660 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4661 a0e95631 2019-07-12 stsp if (err)
4662 a0e95631 2019-07-12 stsp goto done;
4663 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4664 a0e95631 2019-07-12 stsp goto done;
4665 ff0d2220 2019-07-11 stsp }
4666 818c7501 2019-07-11 stsp
4667 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4668 edd02c5e 2019-07-11 stsp if (err)
4669 edd02c5e 2019-07-11 stsp goto done;
4670 edd02c5e 2019-07-11 stsp
4671 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4672 818c7501 2019-07-11 stsp if (err)
4673 818c7501 2019-07-11 stsp goto done;
4674 818c7501 2019-07-11 stsp
4675 0ebf8283 2019-07-24 stsp if (new_logmsg)
4676 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4677 0ebf8283 2019-07-24 stsp else
4678 0ebf8283 2019-07-24 stsp logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4679 0ebf8283 2019-07-24 stsp if (logmsg == NULL)
4680 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
4681 0ebf8283 2019-07-24 stsp
4682 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4683 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
4684 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4685 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4686 a0e95631 2019-07-12 stsp if (err)
4687 a0e95631 2019-07-12 stsp goto done;
4688 a0e95631 2019-07-12 stsp
4689 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4690 a0e95631 2019-07-12 stsp if (err)
4691 a0e95631 2019-07-12 stsp goto done;
4692 a0e95631 2019-07-12 stsp
4693 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4694 a0e95631 2019-07-12 stsp if (err)
4695 a0e95631 2019-07-12 stsp goto done;
4696 a0e95631 2019-07-12 stsp
4697 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4698 93ec1b5f 2019-07-12 stsp fileindex);
4699 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4700 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4701 a0e95631 2019-07-12 stsp err = sync_err;
4702 818c7501 2019-07-11 stsp done:
4703 a0e95631 2019-07-12 stsp free(fileindex_path);
4704 a0e95631 2019-07-12 stsp free(head_commit_id);
4705 a0e95631 2019-07-12 stsp if (head_ref)
4706 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4707 818c7501 2019-07-11 stsp if (err) {
4708 818c7501 2019-07-11 stsp free(*new_commit_id);
4709 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4710 818c7501 2019-07-11 stsp }
4711 818c7501 2019-07-11 stsp return err;
4712 818c7501 2019-07-11 stsp }
4713 818c7501 2019-07-11 stsp
4714 818c7501 2019-07-11 stsp const struct got_error *
4715 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4716 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4717 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4718 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4719 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4720 0ebf8283 2019-07-24 stsp {
4721 0ebf8283 2019-07-24 stsp const struct got_error *err;
4722 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4723 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4724 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4725 0ebf8283 2019-07-24 stsp
4726 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4727 0ebf8283 2019-07-24 stsp if (err)
4728 0ebf8283 2019-07-24 stsp return err;
4729 0ebf8283 2019-07-24 stsp
4730 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4731 0ebf8283 2019-07-24 stsp if (err)
4732 0ebf8283 2019-07-24 stsp goto done;
4733 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4734 0ebf8283 2019-07-24 stsp if (err)
4735 0ebf8283 2019-07-24 stsp goto done;
4736 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4737 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4738 0ebf8283 2019-07-24 stsp goto done;
4739 0ebf8283 2019-07-24 stsp }
4740 0ebf8283 2019-07-24 stsp
4741 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4742 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4743 0ebf8283 2019-07-24 stsp done:
4744 0ebf8283 2019-07-24 stsp if (commit_ref)
4745 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4746 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4747 0ebf8283 2019-07-24 stsp free(commit_id);
4748 0ebf8283 2019-07-24 stsp return err;
4749 0ebf8283 2019-07-24 stsp }
4750 0ebf8283 2019-07-24 stsp
4751 0ebf8283 2019-07-24 stsp const struct got_error *
4752 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4753 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4754 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4755 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4756 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4757 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4758 0ebf8283 2019-07-24 stsp {
4759 0ebf8283 2019-07-24 stsp const struct got_error *err;
4760 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4761 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4762 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4763 0ebf8283 2019-07-24 stsp
4764 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4765 0ebf8283 2019-07-24 stsp if (err)
4766 0ebf8283 2019-07-24 stsp return err;
4767 0ebf8283 2019-07-24 stsp
4768 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4769 0ebf8283 2019-07-24 stsp if (err)
4770 0ebf8283 2019-07-24 stsp goto done;
4771 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4772 0ebf8283 2019-07-24 stsp if (err)
4773 0ebf8283 2019-07-24 stsp goto done;
4774 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4775 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4776 0ebf8283 2019-07-24 stsp goto done;
4777 0ebf8283 2019-07-24 stsp }
4778 0ebf8283 2019-07-24 stsp
4779 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4780 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4781 0ebf8283 2019-07-24 stsp done:
4782 0ebf8283 2019-07-24 stsp if (commit_ref)
4783 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4784 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4785 0ebf8283 2019-07-24 stsp free(commit_id);
4786 0ebf8283 2019-07-24 stsp return err;
4787 0ebf8283 2019-07-24 stsp }
4788 0ebf8283 2019-07-24 stsp
4789 0ebf8283 2019-07-24 stsp const struct got_error *
4790 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4791 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4792 818c7501 2019-07-11 stsp {
4793 3e3a69f1 2019-07-25 stsp if (fileindex)
4794 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4795 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4796 69844fba 2019-07-11 stsp }
4797 69844fba 2019-07-11 stsp
4798 69844fba 2019-07-11 stsp static const struct got_error *
4799 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4800 69844fba 2019-07-11 stsp {
4801 69844fba 2019-07-11 stsp const struct got_error *err;
4802 69844fba 2019-07-11 stsp struct got_reference *ref;
4803 69844fba 2019-07-11 stsp
4804 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4805 69844fba 2019-07-11 stsp if (err) {
4806 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4807 69844fba 2019-07-11 stsp return NULL;
4808 69844fba 2019-07-11 stsp return err;
4809 69844fba 2019-07-11 stsp }
4810 69844fba 2019-07-11 stsp
4811 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4812 69844fba 2019-07-11 stsp got_ref_close(ref);
4813 69844fba 2019-07-11 stsp return err;
4814 818c7501 2019-07-11 stsp }
4815 818c7501 2019-07-11 stsp
4816 69844fba 2019-07-11 stsp static const struct got_error *
4817 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4818 69844fba 2019-07-11 stsp {
4819 69844fba 2019-07-11 stsp const struct got_error *err;
4820 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4821 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4822 69844fba 2019-07-11 stsp
4823 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4824 69844fba 2019-07-11 stsp if (err)
4825 69844fba 2019-07-11 stsp goto done;
4826 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4827 69844fba 2019-07-11 stsp if (err)
4828 69844fba 2019-07-11 stsp goto done;
4829 69844fba 2019-07-11 stsp
4830 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4831 69844fba 2019-07-11 stsp if (err)
4832 69844fba 2019-07-11 stsp goto done;
4833 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4834 69844fba 2019-07-11 stsp if (err)
4835 69844fba 2019-07-11 stsp goto done;
4836 69844fba 2019-07-11 stsp
4837 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4838 69844fba 2019-07-11 stsp if (err)
4839 69844fba 2019-07-11 stsp goto done;
4840 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4841 69844fba 2019-07-11 stsp if (err)
4842 69844fba 2019-07-11 stsp goto done;
4843 69844fba 2019-07-11 stsp
4844 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4845 69844fba 2019-07-11 stsp if (err)
4846 69844fba 2019-07-11 stsp goto done;
4847 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4848 69844fba 2019-07-11 stsp if (err)
4849 69844fba 2019-07-11 stsp goto done;
4850 69844fba 2019-07-11 stsp
4851 69844fba 2019-07-11 stsp done:
4852 69844fba 2019-07-11 stsp free(tmp_branch_name);
4853 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4854 69844fba 2019-07-11 stsp free(branch_ref_name);
4855 69844fba 2019-07-11 stsp free(commit_ref_name);
4856 69844fba 2019-07-11 stsp return err;
4857 69844fba 2019-07-11 stsp }
4858 69844fba 2019-07-11 stsp
4859 818c7501 2019-07-11 stsp const struct got_error *
4860 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4861 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4862 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4863 818c7501 2019-07-11 stsp struct got_repository *repo)
4864 818c7501 2019-07-11 stsp {
4865 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4866 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4867 818c7501 2019-07-11 stsp
4868 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4869 818c7501 2019-07-11 stsp if (err)
4870 818c7501 2019-07-11 stsp return err;
4871 818c7501 2019-07-11 stsp
4872 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4873 818c7501 2019-07-11 stsp if (err)
4874 818c7501 2019-07-11 stsp goto done;
4875 818c7501 2019-07-11 stsp
4876 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4877 818c7501 2019-07-11 stsp if (err)
4878 818c7501 2019-07-11 stsp goto done;
4879 818c7501 2019-07-11 stsp
4880 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4881 818c7501 2019-07-11 stsp if (err)
4882 818c7501 2019-07-11 stsp goto done;
4883 818c7501 2019-07-11 stsp
4884 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4885 818c7501 2019-07-11 stsp done:
4886 3e3a69f1 2019-07-25 stsp if (fileindex)
4887 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4888 818c7501 2019-07-11 stsp free(new_head_commit_id);
4889 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4890 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4891 818c7501 2019-07-11 stsp err = unlockerr;
4892 818c7501 2019-07-11 stsp return err;
4893 818c7501 2019-07-11 stsp }
4894 818c7501 2019-07-11 stsp
4895 818c7501 2019-07-11 stsp const struct got_error *
4896 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4897 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4898 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
4899 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4900 818c7501 2019-07-11 stsp {
4901 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4902 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4903 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4904 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4905 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4906 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4907 818c7501 2019-07-11 stsp
4908 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4909 818c7501 2019-07-11 stsp if (err)
4910 818c7501 2019-07-11 stsp return err;
4911 818c7501 2019-07-11 stsp
4912 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4913 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4914 818c7501 2019-07-11 stsp if (err)
4915 818c7501 2019-07-11 stsp goto done;
4916 818c7501 2019-07-11 stsp
4917 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4918 818c7501 2019-07-11 stsp if (err)
4919 818c7501 2019-07-11 stsp goto done;
4920 818c7501 2019-07-11 stsp
4921 818c7501 2019-07-11 stsp /*
4922 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4923 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4924 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4925 818c7501 2019-07-11 stsp */
4926 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4927 818c7501 2019-07-11 stsp if (err)
4928 818c7501 2019-07-11 stsp goto done;
4929 818c7501 2019-07-11 stsp
4930 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4931 818c7501 2019-07-11 stsp if (err)
4932 818c7501 2019-07-11 stsp goto done;
4933 818c7501 2019-07-11 stsp
4934 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4935 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4936 ca355955 2019-07-12 stsp if (err)
4937 ca355955 2019-07-12 stsp goto done;
4938 ca355955 2019-07-12 stsp
4939 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4940 ca355955 2019-07-12 stsp if (err)
4941 ca355955 2019-07-12 stsp goto done;
4942 ca355955 2019-07-12 stsp
4943 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4944 818c7501 2019-07-11 stsp if (err)
4945 818c7501 2019-07-11 stsp goto done;
4946 818c7501 2019-07-11 stsp
4947 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4948 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4949 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4950 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4951 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
4952 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
4953 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4954 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4955 1f1abb7e 2019-08-08 stsp revert_file, &rfa, NULL, NULL);
4956 55bd499d 2019-07-12 stsp if (err)
4957 1f1abb7e 2019-08-08 stsp goto sync;
4958 55bd499d 2019-07-12 stsp
4959 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4960 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4961 ca355955 2019-07-12 stsp sync:
4962 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4963 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4964 ca355955 2019-07-12 stsp err = sync_err;
4965 818c7501 2019-07-11 stsp done:
4966 818c7501 2019-07-11 stsp got_ref_close(resolved);
4967 a3a2faf2 2019-07-12 stsp free(tree_id);
4968 818c7501 2019-07-11 stsp free(commit_id);
4969 0ebf8283 2019-07-24 stsp if (fileindex)
4970 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
4971 0ebf8283 2019-07-24 stsp free(fileindex_path);
4972 0ebf8283 2019-07-24 stsp
4973 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4974 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4975 0ebf8283 2019-07-24 stsp err = unlockerr;
4976 0ebf8283 2019-07-24 stsp return err;
4977 0ebf8283 2019-07-24 stsp }
4978 0ebf8283 2019-07-24 stsp
4979 0ebf8283 2019-07-24 stsp const struct got_error *
4980 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4981 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4982 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
4983 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4984 0ebf8283 2019-07-24 stsp {
4985 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4986 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4987 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
4988 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
4989 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4990 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
4991 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
4992 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4993 0ebf8283 2019-07-24 stsp
4994 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4995 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4996 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4997 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4998 0ebf8283 2019-07-24 stsp
4999 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5000 0ebf8283 2019-07-24 stsp if (err)
5001 0ebf8283 2019-07-24 stsp return err;
5002 0ebf8283 2019-07-24 stsp
5003 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5004 0ebf8283 2019-07-24 stsp if (err)
5005 0ebf8283 2019-07-24 stsp goto done;
5006 0ebf8283 2019-07-24 stsp
5007 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
5008 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
5009 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5010 0ebf8283 2019-07-24 stsp &ok_arg);
5011 0ebf8283 2019-07-24 stsp if (err)
5012 0ebf8283 2019-07-24 stsp goto done;
5013 0ebf8283 2019-07-24 stsp
5014 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5015 0ebf8283 2019-07-24 stsp if (err)
5016 0ebf8283 2019-07-24 stsp goto done;
5017 0ebf8283 2019-07-24 stsp
5018 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5019 0ebf8283 2019-07-24 stsp if (err)
5020 0ebf8283 2019-07-24 stsp goto done;
5021 0ebf8283 2019-07-24 stsp
5022 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5023 0ebf8283 2019-07-24 stsp worktree);
5024 0ebf8283 2019-07-24 stsp if (err)
5025 0ebf8283 2019-07-24 stsp goto done;
5026 0ebf8283 2019-07-24 stsp
5027 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5028 0ebf8283 2019-07-24 stsp 0);
5029 0ebf8283 2019-07-24 stsp if (err)
5030 0ebf8283 2019-07-24 stsp goto done;
5031 0ebf8283 2019-07-24 stsp
5032 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5033 0ebf8283 2019-07-24 stsp if (err)
5034 0ebf8283 2019-07-24 stsp goto done;
5035 0ebf8283 2019-07-24 stsp
5036 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
5037 0ebf8283 2019-07-24 stsp if (err)
5038 0ebf8283 2019-07-24 stsp goto done;
5039 0ebf8283 2019-07-24 stsp
5040 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5041 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5042 0ebf8283 2019-07-24 stsp if (err)
5043 0ebf8283 2019-07-24 stsp goto done;
5044 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
5045 0ebf8283 2019-07-24 stsp if (err)
5046 0ebf8283 2019-07-24 stsp goto done;
5047 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5048 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
5049 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
5050 0ebf8283 2019-07-24 stsp goto done;
5051 0ebf8283 2019-07-24 stsp }
5052 0ebf8283 2019-07-24 stsp
5053 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5054 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5055 0ebf8283 2019-07-24 stsp if (err)
5056 0ebf8283 2019-07-24 stsp goto done;
5057 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
5058 0ebf8283 2019-07-24 stsp if (err)
5059 0ebf8283 2019-07-24 stsp goto done;
5060 0ebf8283 2019-07-24 stsp
5061 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5062 0ebf8283 2019-07-24 stsp if (err)
5063 0ebf8283 2019-07-24 stsp goto done;
5064 0ebf8283 2019-07-24 stsp done:
5065 0ebf8283 2019-07-24 stsp free(fileindex_path);
5066 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5067 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5068 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5069 0ebf8283 2019-07-24 stsp if (wt_branch)
5070 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
5071 0ebf8283 2019-07-24 stsp if (err) {
5072 0ebf8283 2019-07-24 stsp if (*branch_ref) {
5073 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
5074 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5075 0ebf8283 2019-07-24 stsp }
5076 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5077 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5078 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5079 0ebf8283 2019-07-24 stsp }
5080 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5081 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5082 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5083 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5084 3e3a69f1 2019-07-25 stsp }
5085 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
5086 0ebf8283 2019-07-24 stsp }
5087 0ebf8283 2019-07-24 stsp return err;
5088 0ebf8283 2019-07-24 stsp }
5089 0ebf8283 2019-07-24 stsp
5090 0ebf8283 2019-07-24 stsp const struct got_error *
5091 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
5092 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5093 0ebf8283 2019-07-24 stsp {
5094 3e3a69f1 2019-07-25 stsp if (fileindex)
5095 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5096 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
5097 0ebf8283 2019-07-24 stsp }
5098 0ebf8283 2019-07-24 stsp
5099 0ebf8283 2019-07-24 stsp const struct got_error *
5100 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
5101 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
5102 0ebf8283 2019-07-24 stsp {
5103 0ebf8283 2019-07-24 stsp const struct got_error *err;
5104 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5105 0ebf8283 2019-07-24 stsp
5106 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5107 0ebf8283 2019-07-24 stsp if (err)
5108 0ebf8283 2019-07-24 stsp return err;
5109 0ebf8283 2019-07-24 stsp
5110 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5111 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5112 0ebf8283 2019-07-24 stsp return NULL;
5113 0ebf8283 2019-07-24 stsp }
5114 0ebf8283 2019-07-24 stsp
5115 0ebf8283 2019-07-24 stsp const struct got_error *
5116 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
5117 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
5118 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5119 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5120 0ebf8283 2019-07-24 stsp {
5121 0ebf8283 2019-07-24 stsp const struct got_error *err;
5122 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5123 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5124 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5125 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5126 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5127 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5128 0ebf8283 2019-07-24 stsp
5129 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5130 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5131 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5132 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5133 0ebf8283 2019-07-24 stsp
5134 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5135 3e3a69f1 2019-07-25 stsp if (err)
5136 3e3a69f1 2019-07-25 stsp return err;
5137 3e3a69f1 2019-07-25 stsp
5138 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5139 3e3a69f1 2019-07-25 stsp if (err)
5140 f032f1f7 2019-08-04 stsp goto done;
5141 f032f1f7 2019-08-04 stsp
5142 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5143 f032f1f7 2019-08-04 stsp &have_staged_files);
5144 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5145 f032f1f7 2019-08-04 stsp goto done;
5146 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5147 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5148 3e3a69f1 2019-07-25 stsp goto done;
5149 f032f1f7 2019-08-04 stsp }
5150 3e3a69f1 2019-07-25 stsp
5151 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5152 0ebf8283 2019-07-24 stsp if (err)
5153 f032f1f7 2019-08-04 stsp goto done;
5154 0ebf8283 2019-07-24 stsp
5155 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5156 0ebf8283 2019-07-24 stsp if (err)
5157 0ebf8283 2019-07-24 stsp goto done;
5158 0ebf8283 2019-07-24 stsp
5159 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5160 0ebf8283 2019-07-24 stsp if (err)
5161 0ebf8283 2019-07-24 stsp goto done;
5162 0ebf8283 2019-07-24 stsp
5163 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5164 0ebf8283 2019-07-24 stsp worktree);
5165 0ebf8283 2019-07-24 stsp if (err)
5166 0ebf8283 2019-07-24 stsp goto done;
5167 0ebf8283 2019-07-24 stsp
5168 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5169 0ebf8283 2019-07-24 stsp if (err)
5170 0ebf8283 2019-07-24 stsp goto done;
5171 0ebf8283 2019-07-24 stsp
5172 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5173 0ebf8283 2019-07-24 stsp if (err)
5174 0ebf8283 2019-07-24 stsp goto done;
5175 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5176 0ebf8283 2019-07-24 stsp if (err)
5177 0ebf8283 2019-07-24 stsp goto done;
5178 0ebf8283 2019-07-24 stsp
5179 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5180 0ebf8283 2019-07-24 stsp if (err)
5181 0ebf8283 2019-07-24 stsp goto done;
5182 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5183 0ebf8283 2019-07-24 stsp if (err)
5184 0ebf8283 2019-07-24 stsp goto done;
5185 0ebf8283 2019-07-24 stsp
5186 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5187 0ebf8283 2019-07-24 stsp if (err)
5188 0ebf8283 2019-07-24 stsp goto done;
5189 0ebf8283 2019-07-24 stsp done:
5190 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5191 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5192 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5193 0ebf8283 2019-07-24 stsp if (commit_ref)
5194 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5195 0ebf8283 2019-07-24 stsp if (base_commit_ref)
5196 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
5197 0ebf8283 2019-07-24 stsp if (err) {
5198 0ebf8283 2019-07-24 stsp free(*commit_id);
5199 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5200 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5201 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5202 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5203 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5204 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5205 0ebf8283 2019-07-24 stsp }
5206 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5207 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5208 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5209 3e3a69f1 2019-07-25 stsp }
5210 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
5211 0ebf8283 2019-07-24 stsp }
5212 0ebf8283 2019-07-24 stsp return err;
5213 0ebf8283 2019-07-24 stsp }
5214 0ebf8283 2019-07-24 stsp
5215 0ebf8283 2019-07-24 stsp static const struct got_error *
5216 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5217 0ebf8283 2019-07-24 stsp {
5218 0ebf8283 2019-07-24 stsp const struct got_error *err;
5219 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5220 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5221 0ebf8283 2019-07-24 stsp
5222 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5223 0ebf8283 2019-07-24 stsp if (err)
5224 0ebf8283 2019-07-24 stsp goto done;
5225 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
5226 0ebf8283 2019-07-24 stsp if (err)
5227 0ebf8283 2019-07-24 stsp goto done;
5228 0ebf8283 2019-07-24 stsp
5229 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5230 0ebf8283 2019-07-24 stsp worktree);
5231 0ebf8283 2019-07-24 stsp if (err)
5232 0ebf8283 2019-07-24 stsp goto done;
5233 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
5234 0ebf8283 2019-07-24 stsp if (err)
5235 0ebf8283 2019-07-24 stsp goto done;
5236 0ebf8283 2019-07-24 stsp
5237 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5238 0ebf8283 2019-07-24 stsp if (err)
5239 0ebf8283 2019-07-24 stsp goto done;
5240 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
5241 0ebf8283 2019-07-24 stsp if (err)
5242 0ebf8283 2019-07-24 stsp goto done;
5243 0ebf8283 2019-07-24 stsp
5244 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5245 0ebf8283 2019-07-24 stsp if (err)
5246 0ebf8283 2019-07-24 stsp goto done;
5247 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5248 0ebf8283 2019-07-24 stsp if (err)
5249 0ebf8283 2019-07-24 stsp goto done;
5250 0ebf8283 2019-07-24 stsp done:
5251 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5252 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5253 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5254 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5255 0ebf8283 2019-07-24 stsp return err;
5256 0ebf8283 2019-07-24 stsp }
5257 0ebf8283 2019-07-24 stsp
5258 0ebf8283 2019-07-24 stsp const struct got_error *
5259 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
5260 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5261 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
5262 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5263 0ebf8283 2019-07-24 stsp {
5264 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
5265 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5266 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5267 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
5268 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5269 0ebf8283 2019-07-24 stsp
5270 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5271 0ebf8283 2019-07-24 stsp if (err)
5272 0ebf8283 2019-07-24 stsp return err;
5273 0ebf8283 2019-07-24 stsp
5274 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5275 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
5276 0ebf8283 2019-07-24 stsp if (err)
5277 0ebf8283 2019-07-24 stsp goto done;
5278 0ebf8283 2019-07-24 stsp
5279 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5280 0ebf8283 2019-07-24 stsp if (err)
5281 0ebf8283 2019-07-24 stsp goto done;
5282 0ebf8283 2019-07-24 stsp
5283 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5284 0ebf8283 2019-07-24 stsp if (err)
5285 0ebf8283 2019-07-24 stsp goto done;
5286 0ebf8283 2019-07-24 stsp
5287 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5288 0ebf8283 2019-07-24 stsp worktree->path_prefix);
5289 0ebf8283 2019-07-24 stsp if (err)
5290 0ebf8283 2019-07-24 stsp goto done;
5291 0ebf8283 2019-07-24 stsp
5292 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5293 0ebf8283 2019-07-24 stsp if (err)
5294 0ebf8283 2019-07-24 stsp goto done;
5295 0ebf8283 2019-07-24 stsp
5296 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5297 0ebf8283 2019-07-24 stsp if (err)
5298 0ebf8283 2019-07-24 stsp goto done;
5299 0ebf8283 2019-07-24 stsp
5300 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5301 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5302 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5303 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5304 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5305 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5306 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5307 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
5308 1f1abb7e 2019-08-08 stsp revert_file, &rfa, NULL, NULL);
5309 0ebf8283 2019-07-24 stsp if (err)
5310 1f1abb7e 2019-08-08 stsp goto sync;
5311 0ebf8283 2019-07-24 stsp
5312 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5313 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
5314 0ebf8283 2019-07-24 stsp sync:
5315 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5316 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
5317 0ebf8283 2019-07-24 stsp err = sync_err;
5318 0ebf8283 2019-07-24 stsp done:
5319 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
5320 0ebf8283 2019-07-24 stsp free(tree_id);
5321 818c7501 2019-07-11 stsp free(fileindex_path);
5322 818c7501 2019-07-11 stsp
5323 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5324 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5325 818c7501 2019-07-11 stsp err = unlockerr;
5326 818c7501 2019-07-11 stsp return err;
5327 818c7501 2019-07-11 stsp }
5328 0ebf8283 2019-07-24 stsp
5329 0ebf8283 2019-07-24 stsp const struct got_error *
5330 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
5331 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5332 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
5333 0ebf8283 2019-07-24 stsp {
5334 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
5335 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
5336 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
5337 0ebf8283 2019-07-24 stsp
5338 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5339 0ebf8283 2019-07-24 stsp if (err)
5340 0ebf8283 2019-07-24 stsp return err;
5341 0ebf8283 2019-07-24 stsp
5342 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
5343 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
5344 0ebf8283 2019-07-24 stsp if (err)
5345 0ebf8283 2019-07-24 stsp goto done;
5346 0ebf8283 2019-07-24 stsp
5347 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
5348 0ebf8283 2019-07-24 stsp if (err)
5349 0ebf8283 2019-07-24 stsp goto done;
5350 0ebf8283 2019-07-24 stsp
5351 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
5352 0ebf8283 2019-07-24 stsp if (err)
5353 0ebf8283 2019-07-24 stsp goto done;
5354 0ebf8283 2019-07-24 stsp
5355 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
5356 0ebf8283 2019-07-24 stsp if (err)
5357 0ebf8283 2019-07-24 stsp goto done;
5358 0ebf8283 2019-07-24 stsp
5359 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
5360 0ebf8283 2019-07-24 stsp done:
5361 3e3a69f1 2019-07-25 stsp if (fileindex)
5362 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5363 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
5364 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5365 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5366 0ebf8283 2019-07-24 stsp err = unlockerr;
5367 0ebf8283 2019-07-24 stsp return err;
5368 0ebf8283 2019-07-24 stsp }
5369 0ebf8283 2019-07-24 stsp
5370 0ebf8283 2019-07-24 stsp const struct got_error *
5371 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5372 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5373 0ebf8283 2019-07-24 stsp {
5374 0ebf8283 2019-07-24 stsp const struct got_error *err;
5375 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5376 0ebf8283 2019-07-24 stsp
5377 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5378 0ebf8283 2019-07-24 stsp if (err)
5379 0ebf8283 2019-07-24 stsp return err;
5380 0ebf8283 2019-07-24 stsp
5381 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
5382 0ebf8283 2019-07-24 stsp if (err)
5383 0ebf8283 2019-07-24 stsp goto done;
5384 0ebf8283 2019-07-24 stsp
5385 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
5386 0ebf8283 2019-07-24 stsp done:
5387 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5388 0cb83759 2019-08-03 stsp return err;
5389 0cb83759 2019-08-03 stsp }
5390 0cb83759 2019-08-03 stsp
5391 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
5392 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
5393 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5394 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5395 2db2652d 2019-08-07 stsp struct got_repository *repo;
5396 2db2652d 2019-08-07 stsp int have_changes;
5397 2db2652d 2019-08-07 stsp };
5398 2db2652d 2019-08-07 stsp
5399 2db2652d 2019-08-07 stsp const struct got_error *
5400 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
5401 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5402 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5403 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5404 735ef5ac 2019-08-03 stsp {
5405 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
5406 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
5407 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
5408 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
5409 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
5410 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
5411 8b13ce36 2019-08-08 stsp
5412 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
5413 8b13ce36 2019-08-08 stsp return NULL;
5414 735ef5ac 2019-08-03 stsp
5415 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5416 735ef5ac 2019-08-03 stsp if (ie == NULL)
5417 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5418 735ef5ac 2019-08-03 stsp
5419 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5420 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5421 735ef5ac 2019-08-03 stsp relpath) == -1)
5422 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
5423 735ef5ac 2019-08-03 stsp
5424 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
5425 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
5426 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5427 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
5428 735ef5ac 2019-08-03 stsp }
5429 735ef5ac 2019-08-03 stsp
5430 3aa5969e 2019-08-06 stsp if (status == GOT_STATUS_NO_CHANGE) {
5431 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5432 3aa5969e 2019-08-06 stsp goto done;
5433 3aa5969e 2019-08-06 stsp } else if (status == GOT_STATUS_CONFLICT) {
5434 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5435 3aa5969e 2019-08-06 stsp goto done;
5436 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
5437 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
5438 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
5439 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5440 735ef5ac 2019-08-03 stsp goto done;
5441 3aa5969e 2019-08-06 stsp }
5442 735ef5ac 2019-08-03 stsp
5443 2db2652d 2019-08-07 stsp a->have_changes = 1;
5444 2db2652d 2019-08-07 stsp
5445 735ef5ac 2019-08-03 stsp p = in_repo_path;
5446 735ef5ac 2019-08-03 stsp while (p[0] == '/')
5447 735ef5ac 2019-08-03 stsp p++;
5448 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
5449 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
5450 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
5451 735ef5ac 2019-08-03 stsp done:
5452 735ef5ac 2019-08-03 stsp free(in_repo_path);
5453 dc424a06 2019-08-07 stsp return err;
5454 dc424a06 2019-08-07 stsp }
5455 dc424a06 2019-08-07 stsp
5456 2db2652d 2019-08-07 stsp struct stage_path_arg {
5457 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
5458 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
5459 2db2652d 2019-08-07 stsp struct got_repository *repo;
5460 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
5461 2db2652d 2019-08-07 stsp void *status_arg;
5462 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
5463 2db2652d 2019-08-07 stsp void *patch_arg;
5464 2db2652d 2019-08-07 stsp };
5465 2db2652d 2019-08-07 stsp
5466 2db2652d 2019-08-07 stsp static const struct got_error *
5467 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
5468 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
5469 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5470 2db2652d 2019-08-07 stsp struct got_object_id *commit_id)
5471 0cb83759 2019-08-03 stsp {
5472 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
5473 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
5474 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
5475 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
5476 0cb83759 2019-08-03 stsp uint32_t stage;
5477 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
5478 8b13ce36 2019-08-08 stsp
5479 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
5480 8b13ce36 2019-08-08 stsp return NULL;
5481 0cb83759 2019-08-03 stsp
5482 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5483 d3e7c587 2019-08-03 stsp if (ie == NULL)
5484 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5485 0cb83759 2019-08-03 stsp
5486 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5487 2db2652d 2019-08-07 stsp relpath)== -1)
5488 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
5489 0cb83759 2019-08-03 stsp
5490 0cb83759 2019-08-03 stsp switch (status) {
5491 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
5492 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
5493 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5494 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
5495 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5496 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5497 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5498 dc424a06 2019-08-07 stsp if (err)
5499 dc424a06 2019-08-07 stsp break;
5500 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
5501 dc424a06 2019-08-07 stsp break;
5502 dc424a06 2019-08-07 stsp } else {
5503 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
5504 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
5505 af5a81b2 2019-08-08 stsp ondisk_path, ie->path, a->repo,
5506 2db2652d 2019-08-07 stsp a->patch_cb, a->patch_arg);
5507 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
5508 dc424a06 2019-08-07 stsp break;
5509 dc424a06 2019-08-07 stsp }
5510 dc424a06 2019-08-07 stsp }
5511 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
5512 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
5513 0cb83759 2019-08-03 stsp if (err)
5514 dc424a06 2019-08-07 stsp break;
5515 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5516 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
5517 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5518 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
5519 0cb83759 2019-08-03 stsp else
5520 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
5521 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5522 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5523 dc424a06 2019-08-07 stsp break;
5524 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5525 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
5526 af5a81b2 2019-08-08 stsp new_staged_blob_id, NULL);
5527 0cb83759 2019-08-03 stsp break;
5528 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
5529 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
5530 d3e7c587 2019-08-03 stsp break;
5531 2db2652d 2019-08-07 stsp if (a->patch_cb) {
5532 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
5533 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
5534 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
5535 dc424a06 2019-08-07 stsp if (err)
5536 dc424a06 2019-08-07 stsp break;
5537 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
5538 88f33a19 2019-08-08 stsp break;
5539 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
5540 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5541 dc424a06 2019-08-07 stsp break;
5542 88f33a19 2019-08-08 stsp }
5543 dc424a06 2019-08-07 stsp }
5544 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
5545 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
5546 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
5547 dc424a06 2019-08-07 stsp break;
5548 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5549 537ac44b 2019-08-03 stsp get_staged_status(ie), relpath, NULL, NULL, NULL);
5550 0cb83759 2019-08-03 stsp break;
5551 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
5552 d3e7c587 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5553 d3e7c587 2019-08-03 stsp break;
5554 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
5555 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5556 ebf48fd5 2019-08-03 stsp break;
5557 0cb83759 2019-08-03 stsp default:
5558 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5559 537ac44b 2019-08-03 stsp break;
5560 0cb83759 2019-08-03 stsp }
5561 dc424a06 2019-08-07 stsp
5562 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
5563 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
5564 dc424a06 2019-08-07 stsp free(path_content);
5565 2db2652d 2019-08-07 stsp free(ondisk_path);
5566 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
5567 0ebf8283 2019-07-24 stsp return err;
5568 0ebf8283 2019-07-24 stsp }
5569 0cb83759 2019-08-03 stsp
5570 0cb83759 2019-08-03 stsp const struct got_error *
5571 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
5572 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
5573 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
5574 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5575 1e71573e 2019-08-03 stsp struct got_repository *repo)
5576 0cb83759 2019-08-03 stsp {
5577 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5578 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
5579 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5580 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
5581 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
5582 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
5583 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
5584 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
5585 0cb83759 2019-08-03 stsp
5586 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5587 0cb83759 2019-08-03 stsp if (err)
5588 0cb83759 2019-08-03 stsp return err;
5589 0cb83759 2019-08-03 stsp
5590 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
5591 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
5592 735ef5ac 2019-08-03 stsp if (err)
5593 735ef5ac 2019-08-03 stsp goto done;
5594 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5595 735ef5ac 2019-08-03 stsp if (err)
5596 735ef5ac 2019-08-03 stsp goto done;
5597 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5598 0cb83759 2019-08-03 stsp if (err)
5599 0cb83759 2019-08-03 stsp goto done;
5600 0cb83759 2019-08-03 stsp
5601 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
5602 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
5603 2db2652d 2019-08-07 stsp oka.worktree = worktree;
5604 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
5605 2db2652d 2019-08-07 stsp oka.repo = repo;
5606 2db2652d 2019-08-07 stsp oka.have_changes = 0;
5607 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5608 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5609 2db2652d 2019-08-07 stsp check_stage_ok, &oka, NULL, NULL);
5610 735ef5ac 2019-08-03 stsp if (err)
5611 735ef5ac 2019-08-03 stsp goto done;
5612 735ef5ac 2019-08-03 stsp }
5613 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
5614 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5615 2db2652d 2019-08-07 stsp goto done;
5616 2db2652d 2019-08-07 stsp }
5617 735ef5ac 2019-08-03 stsp
5618 2db2652d 2019-08-07 stsp spa.worktree = worktree;
5619 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
5620 2db2652d 2019-08-07 stsp spa.repo = repo;
5621 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
5622 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
5623 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
5624 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
5625 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5626 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5627 2db2652d 2019-08-07 stsp stage_path, &spa, NULL, NULL);
5628 42005733 2019-08-03 stsp if (err)
5629 2db2652d 2019-08-07 stsp goto done;
5630 0cb83759 2019-08-03 stsp }
5631 0cb83759 2019-08-03 stsp
5632 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5633 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
5634 0cb83759 2019-08-03 stsp err = sync_err;
5635 0cb83759 2019-08-03 stsp done:
5636 735ef5ac 2019-08-03 stsp if (head_ref)
5637 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
5638 735ef5ac 2019-08-03 stsp free(head_commit_id);
5639 0cb83759 2019-08-03 stsp free(fileindex_path);
5640 0cb83759 2019-08-03 stsp if (fileindex)
5641 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
5642 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5643 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
5644 0cb83759 2019-08-03 stsp err = unlockerr;
5645 0cb83759 2019-08-03 stsp return err;
5646 0cb83759 2019-08-03 stsp }
5647 ad493afc 2019-08-03 stsp
5648 ad493afc 2019-08-03 stsp struct unstage_path_arg {
5649 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
5650 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
5651 ad493afc 2019-08-03 stsp struct got_repository *repo;
5652 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
5653 ad493afc 2019-08-03 stsp void *progress_arg;
5654 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
5655 2e1f37b0 2019-08-08 stsp void *patch_arg;
5656 ad493afc 2019-08-03 stsp };
5657 2e1f37b0 2019-08-08 stsp
5658 2e1f37b0 2019-08-08 stsp static const struct got_error *
5659 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
5660 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
5661 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
5662 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
5663 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
5664 2e1f37b0 2019-08-08 stsp {
5665 2e1f37b0 2019-08-08 stsp const struct got_error *err;
5666 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
5667 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5668 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5669 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
5670 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
5671 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
5672 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
5673 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
5674 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5675 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
5676 2e1f37b0 2019-08-08 stsp
5677 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
5678 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
5679 2e1f37b0 2019-08-08 stsp
5680 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
5681 2e1f37b0 2019-08-08 stsp if (err)
5682 2e1f37b0 2019-08-08 stsp return err;
5683 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5684 2e1f37b0 2019-08-08 stsp if (err)
5685 2e1f37b0 2019-08-08 stsp goto done;
5686 2e1f37b0 2019-08-08 stsp
5687 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5688 2e1f37b0 2019-08-08 stsp if (err)
5689 2e1f37b0 2019-08-08 stsp goto done;
5690 2e1f37b0 2019-08-08 stsp
5691 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5692 2e1f37b0 2019-08-08 stsp if (err)
5693 2e1f37b0 2019-08-08 stsp goto done;
5694 2e1f37b0 2019-08-08 stsp
5695 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5696 2e1f37b0 2019-08-08 stsp if (err)
5697 2e1f37b0 2019-08-08 stsp goto done;
5698 2e1f37b0 2019-08-08 stsp
5699 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5700 2e1f37b0 2019-08-08 stsp if (err)
5701 2e1f37b0 2019-08-08 stsp goto done;
5702 ad493afc 2019-08-03 stsp
5703 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5704 2e1f37b0 2019-08-08 stsp if (err)
5705 2e1f37b0 2019-08-08 stsp goto done;
5706 2e1f37b0 2019-08-08 stsp
5707 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
5708 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
5709 2e1f37b0 2019-08-08 stsp goto done;
5710 2e1f37b0 2019-08-08 stsp }
5711 2e1f37b0 2019-08-08 stsp
5712 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
5713 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
5714 2e1f37b0 2019-08-08 stsp goto done;
5715 2e1f37b0 2019-08-08 stsp }
5716 2e1f37b0 2019-08-08 stsp
5717 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
5718 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5719 2e1f37b0 2019-08-08 stsp if (err)
5720 2e1f37b0 2019-08-08 stsp goto done;
5721 2e1f37b0 2019-08-08 stsp
5722 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
5723 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
5724 2e1f37b0 2019-08-08 stsp if (err)
5725 2e1f37b0 2019-08-08 stsp goto done;
5726 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
5727 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
5728 2e1f37b0 2019-08-08 stsp if (err)
5729 2e1f37b0 2019-08-08 stsp goto done;
5730 2e1f37b0 2019-08-08 stsp
5731 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
5732 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
5733 2e1f37b0 2019-08-08 stsp goto done;
5734 2e1f37b0 2019-08-08 stsp }
5735 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
5736 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
5737 2e1f37b0 2019-08-08 stsp goto done;
5738 2e1f37b0 2019-08-08 stsp }
5739 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5740 2e1f37b0 2019-08-08 stsp int choice;
5741 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
5742 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
5743 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
5744 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
5745 2e1f37b0 2019-08-08 stsp if (err)
5746 2e1f37b0 2019-08-08 stsp goto done;
5747 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5748 2e1f37b0 2019-08-08 stsp have_content = 1;
5749 19e4b907 2019-08-08 stsp else
5750 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
5751 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
5752 2e1f37b0 2019-08-08 stsp break;
5753 2e1f37b0 2019-08-08 stsp }
5754 2e1f37b0 2019-08-08 stsp done:
5755 2e1f37b0 2019-08-08 stsp free(label1);
5756 2e1f37b0 2019-08-08 stsp if (blob)
5757 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
5758 2e1f37b0 2019-08-08 stsp if (staged_blob)
5759 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
5760 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5761 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5762 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5763 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5764 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5765 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
5766 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5767 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
5768 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5769 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5770 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
5771 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
5772 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
5773 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
5774 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
5775 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
5776 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
5777 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
5778 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
5779 2e1f37b0 2019-08-08 stsp }
5780 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
5781 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
5782 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
5783 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
5784 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
5785 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
5786 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
5787 2e1f37b0 2019-08-08 stsp }
5788 2e1f37b0 2019-08-08 stsp free(args);
5789 2e1f37b0 2019-08-08 stsp if (ds) {
5790 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
5791 2e1f37b0 2019-08-08 stsp free(ds);
5792 2e1f37b0 2019-08-08 stsp }
5793 2e1f37b0 2019-08-08 stsp if (changes)
5794 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
5795 2e1f37b0 2019-08-08 stsp free(path1);
5796 2e1f37b0 2019-08-08 stsp free(path2);
5797 2e1f37b0 2019-08-08 stsp return err;
5798 2e1f37b0 2019-08-08 stsp }
5799 2e1f37b0 2019-08-08 stsp
5800 ad493afc 2019-08-03 stsp static const struct got_error *
5801 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
5802 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
5803 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5804 ad493afc 2019-08-03 stsp struct got_object_id *commit_id)
5805 ad493afc 2019-08-03 stsp {
5806 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
5807 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
5808 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
5809 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5810 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
5811 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
5812 ad493afc 2019-08-03 stsp int local_changes_subsumed;
5813 9bc94a15 2019-08-03 stsp struct stat sb;
5814 ad493afc 2019-08-03 stsp
5815 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
5816 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
5817 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
5818 2e1f37b0 2019-08-08 stsp return NULL;
5819 2e1f37b0 2019-08-08 stsp
5820 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5821 ad493afc 2019-08-03 stsp if (ie == NULL)
5822 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5823 9bc94a15 2019-08-03 stsp
5824 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5825 9bc94a15 2019-08-03 stsp == -1)
5826 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
5827 ad493afc 2019-08-03 stsp
5828 ad493afc 2019-08-03 stsp switch (staged_status) {
5829 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
5830 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
5831 ad493afc 2019-08-03 stsp blob_id, 8192);
5832 ad493afc 2019-08-03 stsp if (err)
5833 ad493afc 2019-08-03 stsp break;
5834 ad493afc 2019-08-03 stsp /* fall through */
5835 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
5836 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
5837 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
5838 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5839 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5840 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
5841 2e1f37b0 2019-08-08 stsp if (err)
5842 2e1f37b0 2019-08-08 stsp break;
5843 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5844 2e1f37b0 2019-08-08 stsp break;
5845 2e1f37b0 2019-08-08 stsp } else {
5846 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
5847 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
5848 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
5849 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
5850 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
5851 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
5852 2e1f37b0 2019-08-08 stsp break;
5853 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
5854 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
5855 2e1f37b0 2019-08-08 stsp &staged_blob_id,
5856 2e1f37b0 2019-08-08 stsp path_new_staged_content,
5857 2e1f37b0 2019-08-08 stsp a->repo);
5858 2e1f37b0 2019-08-08 stsp if (err)
5859 2e1f37b0 2019-08-08 stsp break;
5860 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
5861 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
5862 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
5863 2e1f37b0 2019-08-08 stsp }
5864 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
5865 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
5866 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
5867 2e1f37b0 2019-08-08 stsp path_unstaged_content, "unstaged",
5868 2e1f37b0 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
5869 2e1f37b0 2019-08-08 stsp if (err == NULL &&
5870 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
5871 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
5872 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
5873 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
5874 2e1f37b0 2019-08-08 stsp }
5875 2e1f37b0 2019-08-08 stsp }
5876 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
5877 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
5878 ad493afc 2019-08-03 stsp if (err)
5879 ad493afc 2019-08-03 stsp break;
5880 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
5881 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
5882 ad493afc 2019-08-03 stsp got_fileindex_perms_to_st(ie), blob_staged,
5883 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
5884 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
5885 ad493afc 2019-08-03 stsp if (err == NULL)
5886 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5887 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5888 ad493afc 2019-08-03 stsp break;
5889 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
5890 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
5891 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5892 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5893 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
5894 2e1f37b0 2019-08-08 stsp if (err)
5895 2e1f37b0 2019-08-08 stsp break;
5896 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
5897 2e1f37b0 2019-08-08 stsp break;
5898 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
5899 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
5900 2e1f37b0 2019-08-08 stsp break;
5901 2e1f37b0 2019-08-08 stsp }
5902 2e1f37b0 2019-08-08 stsp }
5903 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
5904 9bc94a15 2019-08-03 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
5905 9bc94a15 2019-08-03 stsp if (err)
5906 9bc94a15 2019-08-03 stsp break;
5907 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
5908 ad493afc 2019-08-03 stsp break;
5909 ad493afc 2019-08-03 stsp }
5910 ad493afc 2019-08-03 stsp
5911 ad493afc 2019-08-03 stsp free(ondisk_path);
5912 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
5913 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
5914 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
5915 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
5916 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
5917 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
5918 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
5919 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
5920 ad493afc 2019-08-03 stsp if (blob_base)
5921 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
5922 ad493afc 2019-08-03 stsp if (blob_staged)
5923 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
5924 ad493afc 2019-08-03 stsp return err;
5925 ad493afc 2019-08-03 stsp }
5926 ad493afc 2019-08-03 stsp
5927 ad493afc 2019-08-03 stsp const struct got_error *
5928 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
5929 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
5930 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5931 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5932 ad493afc 2019-08-03 stsp struct got_repository *repo)
5933 ad493afc 2019-08-03 stsp {
5934 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
5935 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5936 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5937 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
5938 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
5939 ad493afc 2019-08-03 stsp
5940 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5941 ad493afc 2019-08-03 stsp if (err)
5942 ad493afc 2019-08-03 stsp return err;
5943 ad493afc 2019-08-03 stsp
5944 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5945 ad493afc 2019-08-03 stsp if (err)
5946 ad493afc 2019-08-03 stsp goto done;
5947 ad493afc 2019-08-03 stsp
5948 ad493afc 2019-08-03 stsp upa.worktree = worktree;
5949 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
5950 ad493afc 2019-08-03 stsp upa.repo = repo;
5951 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
5952 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
5953 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
5954 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
5955 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5956 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5957 ad493afc 2019-08-03 stsp unstage_path, &upa, NULL, NULL);
5958 ad493afc 2019-08-03 stsp if (err)
5959 ad493afc 2019-08-03 stsp goto done;
5960 ad493afc 2019-08-03 stsp }
5961 ad493afc 2019-08-03 stsp
5962 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5963 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
5964 ad493afc 2019-08-03 stsp err = sync_err;
5965 ad493afc 2019-08-03 stsp done:
5966 ad493afc 2019-08-03 stsp free(fileindex_path);
5967 ad493afc 2019-08-03 stsp if (fileindex)
5968 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
5969 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5970 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
5971 ad493afc 2019-08-03 stsp err = unlockerr;
5972 ad493afc 2019-08-03 stsp return err;
5973 ad493afc 2019-08-03 stsp }