Blame


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