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