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