Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 f69721c3 2019-10-21 stsp
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 86c3caaf 2018-03-09 stsp
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
68 99724ed4 2018-03-10 stsp {
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
70 99724ed4 2018-03-10 stsp char *path;
71 99724ed4 2018-03-10 stsp
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
76 99724ed4 2018-03-10 stsp free(path);
77 507dc3bb 2018-12-29 stsp return err;
78 507dc3bb 2018-12-29 stsp }
79 507dc3bb 2018-12-29 stsp
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
82 507dc3bb 2018-12-29 stsp {
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
87 507dc3bb 2018-12-29 stsp
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp }
93 507dc3bb 2018-12-29 stsp
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
95 507dc3bb 2018-12-29 stsp if (err)
96 507dc3bb 2018-12-29 stsp goto done;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
102 507dc3bb 2018-12-29 stsp goto done;
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
109 507dc3bb 2018-12-29 stsp goto done;
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp
112 507dc3bb 2018-12-29 stsp done:
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
117 99724ed4 2018-03-10 stsp }
118 99724ed4 2018-03-10 stsp
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
121 09fe317a 2018-03-11 stsp {
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
125 09fe317a 2018-03-11 stsp ssize_t n;
126 09fe317a 2018-03-11 stsp struct stat sb;
127 09fe317a 2018-03-11 stsp
128 09fe317a 2018-03-11 stsp *content = NULL;
129 09fe317a 2018-03-11 stsp
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
133 09fe317a 2018-03-11 stsp goto done;
134 09fe317a 2018-03-11 stsp }
135 09fe317a 2018-03-11 stsp
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 f02eaa22 2019-03-11 stsp else
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
142 ef99fdb1 2018-03-11 stsp goto done;
143 ef99fdb1 2018-03-11 stsp }
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
147 09fe317a 2018-03-11 stsp goto done;
148 09fe317a 2018-03-11 stsp }
149 09fe317a 2018-03-11 stsp
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
152 d10c9b58 2019-02-19 stsp goto done;
153 d10c9b58 2019-02-19 stsp }
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
157 09fe317a 2018-03-11 stsp goto done;
158 09fe317a 2018-03-11 stsp }
159 09fe317a 2018-03-11 stsp
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
164 09fe317a 2018-03-11 stsp goto done;
165 09fe317a 2018-03-11 stsp }
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 09fe317a 2018-03-11 stsp goto done;
169 09fe317a 2018-03-11 stsp }
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
171 09fe317a 2018-03-11 stsp
172 09fe317a 2018-03-11 stsp done:
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
176 09fe317a 2018-03-11 stsp if (err) {
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
179 09fe317a 2018-03-11 stsp }
180 09fe317a 2018-03-11 stsp return err;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
185 024e9686 2019-05-14 stsp {
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
188 024e9686 2019-05-14 stsp
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
193 024e9686 2019-05-14 stsp } else {
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
197 024e9686 2019-05-14 stsp }
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
201 024e9686 2019-05-14 stsp }
202 024e9686 2019-05-14 stsp
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
206 86c3caaf 2018-03-09 stsp {
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
217 65596e15 2018-12-24 stsp
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
220 0c48fee2 2019-03-11 stsp goto done;
221 0c48fee2 2019-03-11 stsp }
222 0c48fee2 2019-03-11 stsp
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
224 65596e15 2018-12-24 stsp if (err)
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
227 65596e15 2018-12-24 stsp if (err)
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
231 86c3caaf 2018-03-09 stsp
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
235 0bb8a95e 2018-03-12 stsp }
236 577ec78f 2018-03-11 stsp
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
240 86c3caaf 2018-03-09 stsp goto done;
241 86c3caaf 2018-03-09 stsp }
242 86c3caaf 2018-03-09 stsp
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 86c3caaf 2018-03-09 stsp goto done;
247 86c3caaf 2018-03-09 stsp }
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
250 86c3caaf 2018-03-09 stsp goto done;
251 86c3caaf 2018-03-09 stsp }
252 86c3caaf 2018-03-09 stsp
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 056e7441 2018-03-11 stsp if (err)
256 056e7441 2018-03-11 stsp goto done;
257 056e7441 2018-03-11 stsp
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 99724ed4 2018-03-10 stsp if (err)
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 65596e15 2018-12-24 stsp
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
270 65596e15 2018-12-24 stsp if (err)
271 65596e15 2018-12-24 stsp goto done;
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 99724ed4 2018-03-10 stsp if (err)
274 86c3caaf 2018-03-09 stsp goto done;
275 86c3caaf 2018-03-09 stsp
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
279 99724ed4 2018-03-10 stsp if (err)
280 86c3caaf 2018-03-09 stsp goto done;
281 86c3caaf 2018-03-09 stsp
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
285 ec22038e 2019-03-10 stsp if (err)
286 ec22038e 2019-03-10 stsp goto done;
287 ec22038e 2019-03-10 stsp
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
297 ec22038e 2019-03-10 stsp goto done;
298 ec22038e 2019-03-10 stsp }
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 577ec78f 2018-03-11 stsp if (err)
301 577ec78f 2018-03-11 stsp goto done;
302 577ec78f 2018-03-11 stsp
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
306 1451e70d 2018-03-10 stsp goto done;
307 1451e70d 2018-03-10 stsp }
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 99724ed4 2018-03-10 stsp if (err)
310 1451e70d 2018-03-10 stsp goto done;
311 1451e70d 2018-03-10 stsp
312 86c3caaf 2018-03-09 stsp done:
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
324 86c3caaf 2018-03-09 stsp {
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 6d9d28c3 2018-03-11 stsp
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
341 6d9d28c3 2018-03-11 stsp goto done;
342 6d9d28c3 2018-03-11 stsp }
343 6d9d28c3 2018-03-11 stsp
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
347 6d9d28c3 2018-03-11 stsp goto done;
348 6d9d28c3 2018-03-11 stsp }
349 6d9d28c3 2018-03-11 stsp
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp }
356 6d9d28c3 2018-03-11 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 6d9d28c3 2018-03-11 stsp if (err)
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
365 6d9d28c3 2018-03-11 stsp goto done;
366 6d9d28c3 2018-03-11 stsp }
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 6d9d28c3 2018-03-11 stsp
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
375 6d9d28c3 2018-03-11 stsp goto done;
376 6d9d28c3 2018-03-11 stsp }
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
378 6d9d28c3 2018-03-11 stsp
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
382 6d9d28c3 2018-03-11 stsp goto done;
383 6d9d28c3 2018-03-11 stsp }
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
386 6d9d28c3 2018-03-11 stsp if (err)
387 6d9d28c3 2018-03-11 stsp goto done;
388 eaccb85f 2018-12-25 stsp
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
391 93a30277 2018-12-24 stsp if (err)
392 93a30277 2018-12-24 stsp goto done;
393 93a30277 2018-12-24 stsp
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
396 c442a90d 2019-03-10 stsp if (err)
397 c442a90d 2019-03-10 stsp goto done;
398 c442a90d 2019-03-10 stsp
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 eaccb85f 2018-12-25 stsp if (err)
401 c442a90d 2019-03-10 stsp goto done;
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
405 eaccb85f 2018-12-25 stsp goto done;
406 c442a90d 2019-03-10 stsp }
407 eaccb85f 2018-12-25 stsp
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 eaccb85f 2018-12-25 stsp if (err)
410 eaccb85f 2018-12-25 stsp goto done;
411 eaccb85f 2018-12-25 stsp
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
414 f5baf295 2018-03-11 stsp if (err)
415 6d9d28c3 2018-03-11 stsp goto done;
416 6d9d28c3 2018-03-11 stsp
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
419 6d9d28c3 2018-03-11 stsp done:
420 eaccb85f 2018-12-25 stsp if (repo)
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
427 6d9d28c3 2018-03-11 stsp if (err) {
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
429 6d9d28c3 2018-03-11 stsp close(fd);
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
433 6d9d28c3 2018-03-11 stsp } else
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
435 6d9d28c3 2018-03-11 stsp
436 6d9d28c3 2018-03-11 stsp return err;
437 86c3caaf 2018-03-09 stsp }
438 247140b2 2019-02-05 stsp
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
441 247140b2 2019-02-05 stsp {
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
443 86c3caaf 2018-03-09 stsp
444 247140b2 2019-02-05 stsp do {
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
454 247140b2 2019-02-05 stsp
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
456 247140b2 2019-02-05 stsp }
457 247140b2 2019-02-05 stsp
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
460 86c3caaf 2018-03-09 stsp {
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
473 86c3caaf 2018-03-09 stsp }
474 86c3caaf 2018-03-09 stsp
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
477 c7f4312f 2019-02-05 stsp {
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
479 c7f4312f 2019-02-05 stsp }
480 c7f4312f 2019-02-05 stsp
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
483 86c3caaf 2018-03-09 stsp {
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
485 49520a32 2018-12-29 stsp }
486 49520a32 2018-12-29 stsp
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
489 49520a32 2018-12-29 stsp {
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
491 e5dc7198 2018-12-29 stsp }
492 e5dc7198 2018-12-29 stsp
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
496 e5dc7198 2018-12-29 stsp {
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
498 e5dc7198 2018-12-29 stsp
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
502 e5dc7198 2018-12-29 stsp }
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
507 86c3caaf 2018-03-09 stsp }
508 86c3caaf 2018-03-09 stsp
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
511 86c3caaf 2018-03-09 stsp {
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
513 024e9686 2019-05-14 stsp }
514 024e9686 2019-05-14 stsp
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
518 024e9686 2019-05-14 stsp {
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
521 024e9686 2019-05-14 stsp
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
526 024e9686 2019-05-14 stsp goto done;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp }
534 024e9686 2019-05-14 stsp
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
536 024e9686 2019-05-14 stsp if (err)
537 024e9686 2019-05-14 stsp goto done;
538 024e9686 2019-05-14 stsp
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
541 024e9686 2019-05-14 stsp done:
542 024e9686 2019-05-14 stsp free(path_got);
543 024e9686 2019-05-14 stsp if (err)
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
546 507dc3bb 2018-12-29 stsp }
547 507dc3bb 2018-12-29 stsp
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
550 507dc3bb 2018-12-29 stsp {
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
552 86c3caaf 2018-03-09 stsp }
553 86c3caaf 2018-03-09 stsp
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
557 507dc3bb 2018-12-29 stsp {
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
562 507dc3bb 2018-12-29 stsp
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
567 507dc3bb 2018-12-29 stsp goto done;
568 507dc3bb 2018-12-29 stsp }
569 507dc3bb 2018-12-29 stsp
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
571 507dc3bb 2018-12-29 stsp if (err)
572 507dc3bb 2018-12-29 stsp return err;
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
576 507dc3bb 2018-12-29 stsp goto done;
577 507dc3bb 2018-12-29 stsp }
578 507dc3bb 2018-12-29 stsp
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
581 507dc3bb 2018-12-29 stsp if (err)
582 507dc3bb 2018-12-29 stsp goto done;
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 507dc3bb 2018-12-29 stsp if (err)
585 507dc3bb 2018-12-29 stsp goto done;
586 507dc3bb 2018-12-29 stsp
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
591 507dc3bb 2018-12-29 stsp goto done;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp done:
594 507dc3bb 2018-12-29 stsp if (obj)
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
599 507dc3bb 2018-12-29 stsp }
600 507dc3bb 2018-12-29 stsp
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
603 86c3caaf 2018-03-09 stsp {
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
609 21908da4 2019-01-13 stsp }
610 21908da4 2019-01-13 stsp
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
613 4a1ddfc2 2019-01-12 stsp {
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
616 4a1ddfc2 2019-01-12 stsp
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
619 4a1ddfc2 2019-01-12 stsp
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
629 ddcd8544 2019-03-11 stsp }
630 ddcd8544 2019-03-11 stsp }
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
633 68c76935 2019-02-19 stsp }
634 68c76935 2019-02-19 stsp
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
637 68c76935 2019-02-19 stsp {
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
642 68c76935 2019-02-19 stsp
643 68c76935 2019-02-19 stsp *same = 1;
644 68c76935 2019-02-19 stsp
645 230a42bd 2019-05-11 jcs for (;;) {
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp }
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
658 68c76935 2019-02-19 stsp *same = 0;
659 68c76935 2019-02-19 stsp break;
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
662 68c76935 2019-02-19 stsp *same = 0;
663 68c76935 2019-02-19 stsp break;
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 68c76935 2019-02-19 stsp *same = 0;
667 68c76935 2019-02-19 stsp break;
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp } else {
670 68c76935 2019-02-19 stsp *same = 0;
671 68c76935 2019-02-19 stsp break;
672 68c76935 2019-02-19 stsp }
673 68c76935 2019-02-19 stsp }
674 68c76935 2019-02-19 stsp
675 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp }
677 68c76935 2019-02-19 stsp
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
680 68c76935 2019-02-19 stsp {
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
685 68c76935 2019-02-19 stsp
686 68c76935 2019-02-19 stsp *same = 1;
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
690 68c76935 2019-02-19 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
693 68c76935 2019-02-19 stsp
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
696 68c76935 2019-02-19 stsp goto done;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp if (size1 != size2) {
701 68c76935 2019-02-19 stsp *same = 0;
702 68c76935 2019-02-19 stsp return NULL;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
712 68c76935 2019-02-19 stsp goto done;
713 68c76935 2019-02-19 stsp }
714 68c76935 2019-02-19 stsp
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
716 68c76935 2019-02-19 stsp done:
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
721 68c76935 2019-02-19 stsp
722 6353ad76 2019-02-08 stsp return err;
723 6353ad76 2019-02-08 stsp }
724 6353ad76 2019-02-08 stsp
725 6353ad76 2019-02-08 stsp /*
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
729 6353ad76 2019-02-08 stsp */
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
737 6353ad76 2019-02-08 stsp {
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
745 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
746 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
747 6353ad76 2019-02-08 stsp
748 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
749 234035bc 2019-06-01 stsp
750 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
751 af54ae4a 2019-02-19 stsp if (parent == NULL)
752 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
753 af54ae4a 2019-02-19 stsp
754 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
756 af54ae4a 2019-02-19 stsp
757 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 6353ad76 2019-02-08 stsp if (err)
759 6353ad76 2019-02-08 stsp goto done;
760 6353ad76 2019-02-08 stsp
761 af54ae4a 2019-02-19 stsp free(base_path);
762 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
764 af54ae4a 2019-02-19 stsp base_path = NULL;
765 af54ae4a 2019-02-19 stsp goto done;
766 af54ae4a 2019-02-19 stsp }
767 af54ae4a 2019-02-19 stsp
768 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 6353ad76 2019-02-08 stsp if (err)
770 6353ad76 2019-02-08 stsp goto done;
771 46b6ee73 2019-06-04 stsp if (blob_orig) {
772 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 46b6ee73 2019-06-04 stsp blob_orig);
774 1430b4e0 2019-03-27 stsp if (err)
775 1430b4e0 2019-03-27 stsp goto done;
776 1430b4e0 2019-03-27 stsp } else {
777 1430b4e0 2019-03-27 stsp /*
778 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
779 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
780 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
781 1430b4e0 2019-03-27 stsp */
782 1430b4e0 2019-03-27 stsp }
783 6353ad76 2019-02-08 stsp
784 3b9f0f87 2020-07-23 stsp /*
785 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
786 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
787 3b9f0f87 2020-07-23 stsp */
788 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
789 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
790 3b9f0f87 2020-07-23 stsp ssize_t target_len;
791 3b9f0f87 2020-07-23 stsp size_t n;
792 3b9f0f87 2020-07-23 stsp
793 3b9f0f87 2020-07-23 stsp free(base_path);
794 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
795 3b9f0f87 2020-07-23 stsp parent) == -1) {
796 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
797 3b9f0f87 2020-07-23 stsp base_path = NULL;
798 3b9f0f87 2020-07-23 stsp goto done;
799 3b9f0f87 2020-07-23 stsp }
800 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 3b9f0f87 2020-07-23 stsp if (err)
802 3b9f0f87 2020-07-23 stsp goto done;
803 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
804 3b9f0f87 2020-07-23 stsp sizeof(target_path));
805 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
806 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
807 3b9f0f87 2020-07-23 stsp goto done;
808 3b9f0f87 2020-07-23 stsp }
809 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
810 3b9f0f87 2020-07-23 stsp if (n != target_len) {
811 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
812 3b9f0f87 2020-07-23 stsp goto done;
813 3b9f0f87 2020-07-23 stsp }
814 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
815 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
816 3b9f0f87 2020-07-23 stsp goto done;
817 3b9f0f87 2020-07-23 stsp }
818 3b9f0f87 2020-07-23 stsp }
819 3b9f0f87 2020-07-23 stsp
820 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
823 6353ad76 2019-02-08 stsp if (err)
824 6353ad76 2019-02-08 stsp goto done;
825 6353ad76 2019-02-08 stsp
826 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
827 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 1ee397ad 2019-07-12 stsp if (err)
829 1ee397ad 2019-07-12 stsp goto done;
830 6353ad76 2019-02-08 stsp
831 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
832 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
833 816dc654 2019-02-16 stsp goto done;
834 68c76935 2019-02-19 stsp }
835 68c76935 2019-02-19 stsp
836 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
837 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
838 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
839 68c76935 2019-02-19 stsp merged_path);
840 68c76935 2019-02-19 stsp if (err)
841 68c76935 2019-02-19 stsp goto done;
842 816dc654 2019-02-16 stsp }
843 6353ad76 2019-02-08 stsp
844 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
845 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
846 70a0c8ec 2019-02-20 stsp goto done;
847 70a0c8ec 2019-02-20 stsp }
848 70a0c8ec 2019-02-20 stsp
849 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
850 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
851 230a42bd 2019-05-11 jcs ondisk_path);
852 6353ad76 2019-02-08 stsp goto done;
853 6353ad76 2019-02-08 stsp }
854 6353ad76 2019-02-08 stsp done:
855 fdcb7daf 2019-12-15 stsp if (err) {
856 fdcb7daf 2019-12-15 stsp if (merged_path)
857 fdcb7daf 2019-12-15 stsp unlink(merged_path);
858 3b9f0f87 2020-07-23 stsp }
859 3b9f0f87 2020-07-23 stsp if (symlink_path) {
860 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
861 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
862 fdcb7daf 2019-12-15 stsp }
863 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
865 3b9f0f87 2020-07-23 stsp free(symlink_path);
866 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
868 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
870 6353ad76 2019-02-08 stsp free(merged_path);
871 af54ae4a 2019-02-19 stsp free(base_path);
872 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
873 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
874 46b6ee73 2019-06-04 stsp free(blob_orig_path);
875 af57b12a 2020-07-23 stsp }
876 af57b12a 2020-07-23 stsp return err;
877 af57b12a 2020-07-23 stsp }
878 af57b12a 2020-07-23 stsp
879 af57b12a 2020-07-23 stsp static const struct got_error *
880 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
881 af57b12a 2020-07-23 stsp size_t target_len)
882 af57b12a 2020-07-23 stsp {
883 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
884 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
885 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
886 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
887 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
888 af57b12a 2020-07-23 stsp ondisk_path);
889 af57b12a 2020-07-23 stsp return NULL;
890 af57b12a 2020-07-23 stsp }
891 af57b12a 2020-07-23 stsp
892 af57b12a 2020-07-23 stsp /*
893 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
895 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
896 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
897 993e2a1b 2020-07-23 stsp *
898 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
899 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
900 993e2a1b 2020-07-23 stsp *
901 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
902 993e2a1b 2020-07-23 stsp * has deleted this symlink.
903 11cc08c1 2020-07-23 stsp */
904 11cc08c1 2020-07-23 stsp static const struct got_error *
905 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
906 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
908 11cc08c1 2020-07-23 stsp {
909 11cc08c1 2020-07-23 stsp const struct got_error *err;
910 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 11cc08c1 2020-07-23 stsp FILE *f = NULL;
912 11cc08c1 2020-07-23 stsp
913 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
914 11cc08c1 2020-07-23 stsp if (err)
915 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
916 11cc08c1 2020-07-23 stsp
917 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
918 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
920 11cc08c1 2020-07-23 stsp goto done;
921 11cc08c1 2020-07-23 stsp }
922 11cc08c1 2020-07-23 stsp
923 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 11cc08c1 2020-07-23 stsp if (err)
925 11cc08c1 2020-07-23 stsp goto done;
926 11cc08c1 2020-07-23 stsp
927 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
928 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
929 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
930 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
931 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
932 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
933 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
934 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
935 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
936 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
937 11cc08c1 2020-07-23 stsp goto done;
938 11cc08c1 2020-07-23 stsp }
939 11cc08c1 2020-07-23 stsp
940 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
941 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
942 11cc08c1 2020-07-23 stsp goto done;
943 11cc08c1 2020-07-23 stsp }
944 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
945 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
946 11cc08c1 2020-07-23 stsp goto done;
947 11cc08c1 2020-07-23 stsp }
948 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
949 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
950 11cc08c1 2020-07-23 stsp goto done;
951 11cc08c1 2020-07-23 stsp }
952 11cc08c1 2020-07-23 stsp done:
953 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
954 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
955 11cc08c1 2020-07-23 stsp free(path);
956 11cc08c1 2020-07-23 stsp free(id_str);
957 11cc08c1 2020-07-23 stsp free(label_deriv);
958 11cc08c1 2020-07-23 stsp return err;
959 11cc08c1 2020-07-23 stsp }
960 d219f183 2020-07-23 stsp
961 d219f183 2020-07-23 stsp /* forward declaration */
962 d219f183 2020-07-23 stsp static const struct got_error *
963 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
964 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
965 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
966 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
967 11cc08c1 2020-07-23 stsp
968 11cc08c1 2020-07-23 stsp /*
969 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
970 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
971 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
972 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
973 af57b12a 2020-07-23 stsp */
974 af57b12a 2020-07-23 stsp static const struct got_error *
975 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
976 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
977 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
978 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
980 af57b12a 2020-07-23 stsp {
981 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
982 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
983 af57b12a 2020-07-23 stsp struct stat sb;
984 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
985 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
986 11cc08c1 2020-07-23 stsp int have_local_change = 0;
987 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
988 af57b12a 2020-07-23 stsp
989 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
990 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
991 af57b12a 2020-07-23 stsp
992 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
993 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
994 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
995 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
996 af57b12a 2020-07-23 stsp ondisk_path);
997 af57b12a 2020-07-23 stsp goto done;
998 af57b12a 2020-07-23 stsp }
999 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1000 af57b12a 2020-07-23 stsp
1001 526a746f 2020-07-23 stsp if (blob_orig) {
1002 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1003 526a746f 2020-07-23 stsp if (err)
1004 526a746f 2020-07-23 stsp goto done;
1005 526a746f 2020-07-23 stsp }
1006 af57b12a 2020-07-23 stsp
1007 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1008 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1009 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1010 11cc08c1 2020-07-23 stsp have_local_change = 1;
1011 11cc08c1 2020-07-23 stsp
1012 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1013 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1014 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1015 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1016 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1017 11cc08c1 2020-07-23 stsp
1018 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1019 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1020 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1021 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1022 11cc08c1 2020-07-23 stsp path);
1023 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1024 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1025 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1026 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1027 11cc08c1 2020-07-23 stsp path);
1028 11cc08c1 2020-07-23 stsp } else {
1029 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1030 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1031 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1032 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1033 11cc08c1 2020-07-23 stsp if (err)
1034 11cc08c1 2020-07-23 stsp goto done;
1035 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1036 11cc08c1 2020-07-23 stsp path);
1037 11cc08c1 2020-07-23 stsp }
1038 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1039 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1040 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1041 af57b12a 2020-07-23 stsp strlen(deriv_target));
1042 af57b12a 2020-07-23 stsp if (err)
1043 af57b12a 2020-07-23 stsp goto done;
1044 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1045 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1046 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1047 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1048 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1049 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 ea7786be 2020-07-23 stsp path);
1051 ea7786be 2020-07-23 stsp } else {
1052 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1053 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1054 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1055 ea7786be 2020-07-23 stsp if (err)
1056 ea7786be 2020-07-23 stsp goto done;
1057 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1058 ea7786be 2020-07-23 stsp path);
1059 ea7786be 2020-07-23 stsp }
1060 6353ad76 2019-02-08 stsp }
1061 11cc08c1 2020-07-23 stsp
1062 af57b12a 2020-07-23 stsp done:
1063 af57b12a 2020-07-23 stsp free(ancestor_target);
1064 14c901f1 2019-08-08 stsp return err;
1065 14c901f1 2019-08-08 stsp }
1066 14c901f1 2019-08-08 stsp
1067 14c901f1 2019-08-08 stsp /*
1068 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1069 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1070 14c901f1 2019-08-08 stsp * acts as the second derived version.
1071 14c901f1 2019-08-08 stsp */
1072 14c901f1 2019-08-08 stsp static const struct got_error *
1073 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1074 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1075 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1076 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1077 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1078 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1079 14c901f1 2019-08-08 stsp {
1080 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1081 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1082 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1083 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1084 14c901f1 2019-08-08 stsp
1085 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1086 14c901f1 2019-08-08 stsp
1087 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1088 14c901f1 2019-08-08 stsp if (parent == NULL)
1089 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1090 14c901f1 2019-08-08 stsp
1091 14c901f1 2019-08-08 stsp free(base_path);
1092 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1093 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1094 14c901f1 2019-08-08 stsp base_path = NULL;
1095 14c901f1 2019-08-08 stsp goto done;
1096 14c901f1 2019-08-08 stsp }
1097 14c901f1 2019-08-08 stsp
1098 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1099 14c901f1 2019-08-08 stsp if (err)
1100 14c901f1 2019-08-08 stsp goto done;
1101 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1102 14c901f1 2019-08-08 stsp blob_deriv);
1103 14c901f1 2019-08-08 stsp if (err)
1104 14c901f1 2019-08-08 stsp goto done;
1105 14c901f1 2019-08-08 stsp
1106 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1107 14c901f1 2019-08-08 stsp if (err)
1108 14c901f1 2019-08-08 stsp goto done;
1109 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1110 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1111 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1112 14c901f1 2019-08-08 stsp goto done;
1113 14c901f1 2019-08-08 stsp }
1114 14c901f1 2019-08-08 stsp
1115 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1116 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1117 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1118 14c901f1 2019-08-08 stsp done:
1119 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1120 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1121 14c901f1 2019-08-08 stsp free(base_path);
1122 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1123 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1124 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1125 14c901f1 2019-08-08 stsp }
1126 6353ad76 2019-02-08 stsp free(id_str);
1127 818c7501 2019-07-11 stsp free(label_deriv);
1128 4a1ddfc2 2019-01-12 stsp return err;
1129 4a1ddfc2 2019-01-12 stsp }
1130 4a1ddfc2 2019-01-12 stsp
1131 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1132 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1133 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1134 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1135 13d9040b 2019-03-27 stsp {
1136 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1137 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1138 13d9040b 2019-03-27 stsp
1139 65b05cec 2020-07-23 stsp *new_iep = NULL;
1140 65b05cec 2020-07-23 stsp
1141 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1142 054041d0 2020-06-24 stsp if (err)
1143 054041d0 2020-06-24 stsp return err;
1144 054041d0 2020-06-24 stsp
1145 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1146 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1147 054041d0 2020-06-24 stsp if (err)
1148 054041d0 2020-06-24 stsp goto done;
1149 054041d0 2020-06-24 stsp
1150 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1151 054041d0 2020-06-24 stsp done:
1152 054041d0 2020-06-24 stsp if (err)
1153 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1154 65b05cec 2020-07-23 stsp else
1155 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1156 13d9040b 2019-03-27 stsp return err;
1157 1ebedb77 2019-10-19 stsp }
1158 1ebedb77 2019-10-19 stsp
1159 1ebedb77 2019-10-19 stsp static mode_t
1160 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1161 1ebedb77 2019-10-19 stsp {
1162 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1163 1ebedb77 2019-10-19 stsp
1164 1ebedb77 2019-10-19 stsp if (executable) {
1165 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1166 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1167 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1168 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1169 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1170 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1171 1ebedb77 2019-10-19 stsp }
1172 1ebedb77 2019-10-19 stsp
1173 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1174 13d9040b 2019-03-27 stsp }
1175 13d9040b 2019-03-27 stsp
1176 8ba819a3 2020-07-23 stsp /* forward declaration */
1177 13d9040b 2019-03-27 stsp static const struct got_error *
1178 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1179 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1180 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1181 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1182 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1183 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1184 8ba819a3 2020-07-23 stsp
1185 5a1fbc73 2020-07-23 stsp /*
1186 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1187 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1188 5a1fbc73 2020-07-23 stsp */
1189 8ba819a3 2020-07-23 stsp static const struct got_error *
1190 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1191 5a1fbc73 2020-07-23 stsp size_t target_len)
1192 5a1fbc73 2020-07-23 stsp {
1193 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1194 5a1fbc73 2020-07-23 stsp ssize_t elen;
1195 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1196 5a1fbc73 2020-07-23 stsp int fd;
1197 5a1fbc73 2020-07-23 stsp
1198 5a1fbc73 2020-07-23 stsp /*
1199 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1200 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1201 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1202 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1203 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1204 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1205 5a1fbc73 2020-07-23 stsp */
1206 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1207 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1208 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1209 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1210 5a1fbc73 2020-07-23 stsp
1211 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1212 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1213 5a1fbc73 2020-07-23 stsp if (elen == -1)
1214 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1215 5a1fbc73 2020-07-23 stsp
1216 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1217 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1218 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1219 5a1fbc73 2020-07-23 stsp }
1220 5a1fbc73 2020-07-23 stsp
1221 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1222 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1223 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1224 5a1fbc73 2020-07-23 stsp return err;
1225 3c1ec782 2020-07-23 stsp }
1226 3c1ec782 2020-07-23 stsp
1227 3c1ec782 2020-07-23 stsp static const struct got_error *
1228 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1229 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1230 3c1ec782 2020-07-23 stsp {
1231 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1232 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1233 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1234 3c1ec782 2020-07-23 stsp
1235 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1236 3c1ec782 2020-07-23 stsp
1237 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1238 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1239 3c1ec782 2020-07-23 stsp return NULL;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /*
1243 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1244 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1245 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1246 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1247 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1248 3c1ec782 2020-07-23 stsp */
1249 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1250 3c1ec782 2020-07-23 stsp char *abspath;
1251 3c1ec782 2020-07-23 stsp char *parent = dirname(ondisk_path);
1252 3c1ec782 2020-07-23 stsp if (parent == NULL)
1253 3c1ec782 2020-07-23 stsp return got_error_from_errno2("dirname", ondisk_path);
1254 3c1ec782 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1255 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1256 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1257 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1258 3c1ec782 2020-07-23 stsp free(abspath);
1259 3c1ec782 2020-07-23 stsp return err;
1260 3c1ec782 2020-07-23 stsp }
1261 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1262 3c1ec782 2020-07-23 stsp free(abspath);
1263 3c1ec782 2020-07-23 stsp if (err)
1264 3c1ec782 2020-07-23 stsp return err;
1265 3c1ec782 2020-07-23 stsp } else {
1266 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1267 3c1ec782 2020-07-23 stsp if (err)
1268 3c1ec782 2020-07-23 stsp return err;
1269 3c1ec782 2020-07-23 stsp }
1270 3c1ec782 2020-07-23 stsp
1271 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1272 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1273 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1274 3c1ec782 2020-07-23 stsp return NULL;
1275 3c1ec782 2020-07-23 stsp }
1276 3c1ec782 2020-07-23 stsp
1277 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1278 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1279 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1280 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1281 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1282 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1283 3c1ec782 2020-07-23 stsp
1284 3c1ec782 2020-07-23 stsp free(path_got);
1285 3c1ec782 2020-07-23 stsp return NULL;
1286 5a1fbc73 2020-07-23 stsp }
1287 5a1fbc73 2020-07-23 stsp
1288 5a1fbc73 2020-07-23 stsp static const struct got_error *
1289 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1290 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1291 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1292 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1293 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1294 9d31a1d8 2018-03-11 stsp {
1295 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1296 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1297 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1298 906c123b 2020-07-23 stsp char *path_got = NULL;
1299 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1301 8ba819a3 2020-07-23 stsp
1302 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1303 2e1fa222 2020-07-23 stsp
1304 8ba819a3 2020-07-23 stsp /*
1305 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1306 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1307 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1308 8ba819a3 2020-07-23 stsp * in the blob object.
1309 8ba819a3 2020-07-23 stsp */
1310 8ba819a3 2020-07-23 stsp do {
1311 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1312 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1313 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1314 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1315 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1316 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1317 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1319 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1320 3b9f0f87 2020-07-23 stsp progress_arg);
1321 8ba819a3 2020-07-23 stsp }
1322 8ba819a3 2020-07-23 stsp if (len > 0) {
1323 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1324 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1325 8ba819a3 2020-07-23 stsp len - hdrlen);
1326 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1327 8ba819a3 2020-07-23 stsp hdrlen = 0;
1328 8ba819a3 2020-07-23 stsp }
1329 8ba819a3 2020-07-23 stsp } while (len != 0);
1330 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1331 8ba819a3 2020-07-23 stsp
1332 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1333 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1334 3c1ec782 2020-07-23 stsp if (err)
1335 3c1ec782 2020-07-23 stsp return err;
1336 8ba819a3 2020-07-23 stsp
1337 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1338 906c123b 2020-07-23 stsp /* install as a regular file */
1339 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1340 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1341 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1342 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1343 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1344 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1345 906c123b 2020-07-23 stsp goto done;
1346 906c123b 2020-07-23 stsp }
1347 906c123b 2020-07-23 stsp
1348 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1349 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1350 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1351 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1352 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1353 c90c8ce3 2020-07-23 stsp goto done;
1354 c90c8ce3 2020-07-23 stsp }
1355 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1356 5a1fbc73 2020-07-23 stsp target_path, target_len);
1357 5a1fbc73 2020-07-23 stsp if (err)
1358 f35fa46a 2020-07-23 stsp goto done;
1359 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1360 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1361 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1362 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1363 6e1eade5 2020-07-23 stsp path);
1364 f35fa46a 2020-07-23 stsp }
1365 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1366 f35fa46a 2020-07-23 stsp }
1367 f35fa46a 2020-07-23 stsp
1368 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1369 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1370 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1371 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1372 f35fa46a 2020-07-23 stsp ondisk_path);
1373 f35fa46a 2020-07-23 stsp goto done;
1374 f35fa46a 2020-07-23 stsp }
1375 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1376 f35fa46a 2020-07-23 stsp if (err)
1377 f35fa46a 2020-07-23 stsp goto done;
1378 f35fa46a 2020-07-23 stsp /*
1379 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1380 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1381 f35fa46a 2020-07-23 stsp */
1382 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1383 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1384 f35fa46a 2020-07-23 stsp goto done;
1385 f35fa46a 2020-07-23 stsp }
1386 f35fa46a 2020-07-23 stsp }
1387 f35fa46a 2020-07-23 stsp
1388 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1389 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1390 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1391 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1392 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1393 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1394 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1395 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1396 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1397 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1398 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1399 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1400 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1401 8ba819a3 2020-07-23 stsp } else {
1402 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1403 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1404 8ba819a3 2020-07-23 stsp }
1405 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1406 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1407 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1408 8ba819a3 2020-07-23 stsp done:
1409 906c123b 2020-07-23 stsp free(path_got);
1410 8ba819a3 2020-07-23 stsp return err;
1411 8ba819a3 2020-07-23 stsp }
1412 8ba819a3 2020-07-23 stsp
1413 8ba819a3 2020-07-23 stsp static const struct got_error *
1414 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1415 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1416 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1417 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1418 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1419 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1420 8ba819a3 2020-07-23 stsp {
1421 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1422 507dc3bb 2018-12-29 stsp int fd = -1;
1423 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1424 507dc3bb 2018-12-29 stsp int update = 0;
1425 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1426 8ba819a3 2020-07-23 stsp
1427 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1428 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1429 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1430 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1431 21908da4 2019-01-13 stsp char *parent = dirname(path);
1432 21908da4 2019-01-13 stsp if (parent == NULL)
1433 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1434 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1435 21908da4 2019-01-13 stsp if (err)
1436 21908da4 2019-01-13 stsp return err;
1437 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1438 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1440 21908da4 2019-01-13 stsp if (fd == -1)
1441 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1442 230a42bd 2019-05-11 jcs ondisk_path);
1443 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1444 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1445 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1446 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1447 3b9f0f87 2020-07-23 stsp goto done;
1448 3b9f0f87 2020-07-23 stsp }
1449 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1450 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1451 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1452 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1453 507dc3bb 2018-12-29 stsp goto done;
1454 d70b8e30 2018-12-27 stsp } else {
1455 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1456 507dc3bb 2018-12-29 stsp ondisk_path);
1457 507dc3bb 2018-12-29 stsp if (err)
1458 507dc3bb 2018-12-29 stsp goto done;
1459 507dc3bb 2018-12-29 stsp update = 1;
1460 9d31a1d8 2018-03-11 stsp }
1461 507dc3bb 2018-12-29 stsp } else
1462 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1463 9d31a1d8 2018-03-11 stsp }
1464 9d31a1d8 2018-03-11 stsp
1465 bd6aa359 2020-07-23 stsp if (progress_cb) {
1466 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1467 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 bd6aa359 2020-07-23 stsp path);
1469 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1470 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 bd6aa359 2020-07-23 stsp path);
1472 bd6aa359 2020-07-23 stsp else
1473 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1474 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 bd6aa359 2020-07-23 stsp if (err)
1476 bd6aa359 2020-07-23 stsp goto done;
1477 bd6aa359 2020-07-23 stsp }
1478 d7b62c98 2018-12-27 stsp
1479 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1480 9d31a1d8 2018-03-11 stsp do {
1481 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1483 9d31a1d8 2018-03-11 stsp if (err)
1484 9d31a1d8 2018-03-11 stsp break;
1485 9d31a1d8 2018-03-11 stsp if (len > 0) {
1486 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1487 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1490 b87c6f83 2018-12-24 stsp goto done;
1491 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1492 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1493 b87c6f83 2018-12-24 stsp goto done;
1494 9d31a1d8 2018-03-11 stsp }
1495 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1496 9d31a1d8 2018-03-11 stsp }
1497 9d31a1d8 2018-03-11 stsp } while (len != 0);
1498 9d31a1d8 2018-03-11 stsp
1499 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1501 816dc654 2019-02-16 stsp goto done;
1502 816dc654 2019-02-16 stsp }
1503 9d31a1d8 2018-03-11 stsp
1504 507dc3bb 2018-12-29 stsp if (update) {
1505 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1506 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1507 230a42bd 2019-05-11 jcs ondisk_path);
1508 2a57020b 2019-02-20 stsp unlink(tmppath);
1509 68ed9ba5 2019-02-10 stsp goto done;
1510 68ed9ba5 2019-02-10 stsp }
1511 68ed9ba5 2019-02-10 stsp }
1512 ba8a0d4d 2019-02-10 stsp
1513 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1514 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1515 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1516 1ebedb77 2019-10-19 stsp goto done;
1517 507dc3bb 2018-12-29 stsp }
1518 507dc3bb 2018-12-29 stsp
1519 9d31a1d8 2018-03-11 stsp done:
1520 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1521 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1522 507dc3bb 2018-12-29 stsp free(tmppath);
1523 6353ad76 2019-02-08 stsp return err;
1524 6353ad76 2019-02-08 stsp }
1525 6353ad76 2019-02-08 stsp
1526 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1527 6353ad76 2019-02-08 stsp static const struct got_error *
1528 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1529 7154f6ce 2019-03-27 stsp {
1530 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1531 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1532 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1533 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1534 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1535 7154f6ce 2019-03-27 stsp };
1536 7154f6ce 2019-03-27 stsp int i = 0;
1537 7154f6ce 2019-03-27 stsp char *line;
1538 7154f6ce 2019-03-27 stsp size_t len;
1539 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1540 7154f6ce 2019-03-27 stsp
1541 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1542 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1543 7154f6ce 2019-03-27 stsp if (line == NULL) {
1544 7154f6ce 2019-03-27 stsp if (feof(f))
1545 7154f6ce 2019-03-27 stsp break;
1546 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1547 7154f6ce 2019-03-27 stsp break;
1548 7154f6ce 2019-03-27 stsp }
1549 7154f6ce 2019-03-27 stsp
1550 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1551 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1552 19332e6d 2019-05-13 stsp == 0)
1553 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1554 7154f6ce 2019-03-27 stsp else
1555 7154f6ce 2019-03-27 stsp i++;
1556 7154f6ce 2019-03-27 stsp }
1557 7154f6ce 2019-03-27 stsp }
1558 7154f6ce 2019-03-27 stsp
1559 7154f6ce 2019-03-27 stsp return err;
1560 e2b1e152 2019-07-13 stsp }
1561 e2b1e152 2019-07-13 stsp
1562 e2b1e152 2019-07-13 stsp static int
1563 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1564 1ebedb77 2019-10-19 stsp {
1565 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1566 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1567 1ebedb77 2019-10-19 stsp }
1568 1ebedb77 2019-10-19 stsp
1569 1ebedb77 2019-10-19 stsp static int
1570 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1571 e2b1e152 2019-07-13 stsp {
1572 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1573 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1574 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1575 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1576 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1577 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1578 c363b2c1 2019-08-03 stsp }
1579 c363b2c1 2019-08-03 stsp
1580 c363b2c1 2019-08-03 stsp static unsigned char
1581 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1582 c363b2c1 2019-08-03 stsp {
1583 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1584 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1585 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1586 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1587 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1588 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1589 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1590 c363b2c1 2019-08-03 stsp default:
1591 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1592 a919d5c4 2020-07-23 stsp }
1593 a919d5c4 2020-07-23 stsp }
1594 a919d5c4 2020-07-23 stsp
1595 a919d5c4 2020-07-23 stsp static const struct got_error *
1596 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1597 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1598 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1599 a919d5c4 2020-07-23 stsp {
1600 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1601 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1602 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1603 a919d5c4 2020-07-23 stsp ssize_t elen;
1604 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1605 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1606 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1607 a919d5c4 2020-07-23 stsp
1608 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1609 a919d5c4 2020-07-23 stsp
1610 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1611 a919d5c4 2020-07-23 stsp do {
1612 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1613 a919d5c4 2020-07-23 stsp if (err)
1614 a919d5c4 2020-07-23 stsp return err;
1615 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1616 a919d5c4 2020-07-23 stsp /*
1617 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1618 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1619 a919d5c4 2020-07-23 stsp */
1620 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1621 a919d5c4 2020-07-23 stsp }
1622 a919d5c4 2020-07-23 stsp if (len > 0) {
1623 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1624 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1625 a919d5c4 2020-07-23 stsp len - hdrlen);
1626 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1627 a919d5c4 2020-07-23 stsp hdrlen = 0;
1628 a919d5c4 2020-07-23 stsp }
1629 a919d5c4 2020-07-23 stsp } while (len != 0);
1630 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1631 a919d5c4 2020-07-23 stsp
1632 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1633 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1634 a919d5c4 2020-07-23 stsp if (elen == -1)
1635 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1636 a919d5c4 2020-07-23 stsp } else {
1637 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1638 a919d5c4 2020-07-23 stsp if (elen == -1)
1639 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1640 c363b2c1 2019-08-03 stsp }
1641 a919d5c4 2020-07-23 stsp
1642 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1643 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1644 a919d5c4 2020-07-23 stsp
1645 a919d5c4 2020-07-23 stsp return NULL;
1646 7154f6ce 2019-03-27 stsp }
1647 7154f6ce 2019-03-27 stsp
1648 7154f6ce 2019-03-27 stsp static const struct got_error *
1649 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1650 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1651 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1652 6353ad76 2019-02-08 stsp {
1653 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1654 6353ad76 2019-02-08 stsp struct got_object_id id;
1655 6353ad76 2019-02-08 stsp size_t hdrlen;
1656 1338848f 2019-12-13 stsp int fd = -1;
1657 6353ad76 2019-02-08 stsp FILE *f = NULL;
1658 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1659 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1660 6353ad76 2019-02-08 stsp size_t flen, blen;
1661 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1662 6353ad76 2019-02-08 stsp
1663 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1664 6353ad76 2019-02-08 stsp
1665 7f91a133 2019-12-13 stsp /*
1666 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1667 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1668 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1669 7f91a133 2019-12-13 stsp */
1670 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1671 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1672 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1673 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1674 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1675 882ef1b9 2019-12-13 stsp else
1676 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1677 882ef1b9 2019-12-13 stsp goto done;
1678 882ef1b9 2019-12-13 stsp }
1679 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1680 3d35a492 2019-12-13 stsp goto done;
1681 3d35a492 2019-12-13 stsp }
1682 7f91a133 2019-12-13 stsp } else {
1683 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1684 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1685 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1686 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1687 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1688 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1689 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1690 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1691 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1692 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1693 3d35a492 2019-12-13 stsp else
1694 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1695 3d35a492 2019-12-13 stsp goto done;
1696 3d35a492 2019-12-13 stsp }
1697 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1698 1338848f 2019-12-13 stsp goto done;
1699 a378724f 2019-02-10 stsp }
1700 a378724f 2019-02-10 stsp }
1701 6353ad76 2019-02-08 stsp
1702 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1703 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1704 1338848f 2019-12-13 stsp goto done;
1705 3f148bc6 2019-07-27 stsp }
1706 efdd40df 2019-07-27 stsp
1707 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1708 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1709 1338848f 2019-12-13 stsp goto done;
1710 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1711 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1712 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1713 1338848f 2019-12-13 stsp goto done;
1714 d00136be 2019-03-26 stsp }
1715 b8f41171 2019-02-10 stsp
1716 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1717 f179e66d 2020-07-23 stsp goto done;
1718 f179e66d 2020-07-23 stsp
1719 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1720 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1721 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1722 1338848f 2019-12-13 stsp goto done;
1723 f179e66d 2020-07-23 stsp }
1724 6353ad76 2019-02-08 stsp
1725 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1726 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1727 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1728 c363b2c1 2019-08-03 stsp else
1729 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1730 c363b2c1 2019-08-03 stsp
1731 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1732 6353ad76 2019-02-08 stsp if (err)
1733 1338848f 2019-12-13 stsp goto done;
1734 6353ad76 2019-02-08 stsp
1735 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1736 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1737 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1738 a919d5c4 2020-07-23 stsp goto done;
1739 a919d5c4 2020-07-23 stsp }
1740 a919d5c4 2020-07-23 stsp
1741 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1742 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1743 ab0d4361 2019-12-13 stsp if (fd == -1) {
1744 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1745 ab0d4361 2019-12-13 stsp goto done;
1746 ab0d4361 2019-12-13 stsp }
1747 3d35a492 2019-12-13 stsp }
1748 3d35a492 2019-12-13 stsp
1749 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1750 6353ad76 2019-02-08 stsp if (f == NULL) {
1751 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1752 6353ad76 2019-02-08 stsp goto done;
1753 6353ad76 2019-02-08 stsp }
1754 1338848f 2019-12-13 stsp fd = -1;
1755 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1756 656b1f76 2019-05-11 jcs for (;;) {
1757 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1759 6353ad76 2019-02-08 stsp if (err)
1760 7154f6ce 2019-03-27 stsp goto done;
1761 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1762 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1764 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1765 7154f6ce 2019-03-27 stsp goto done;
1766 80c5c120 2019-02-19 stsp }
1767 6353ad76 2019-02-08 stsp if (blen == 0) {
1768 6353ad76 2019-02-08 stsp if (flen != 0)
1769 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1770 6353ad76 2019-02-08 stsp break;
1771 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1772 6353ad76 2019-02-08 stsp if (blen != 0)
1773 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1774 6353ad76 2019-02-08 stsp break;
1775 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1776 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1777 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1779 6353ad76 2019-02-08 stsp break;
1780 6353ad76 2019-02-08 stsp }
1781 6353ad76 2019-02-08 stsp } else {
1782 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1783 6353ad76 2019-02-08 stsp break;
1784 6353ad76 2019-02-08 stsp }
1785 6353ad76 2019-02-08 stsp hdrlen = 0;
1786 6353ad76 2019-02-08 stsp }
1787 7154f6ce 2019-03-27 stsp
1788 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1789 7154f6ce 2019-03-27 stsp rewind(f);
1790 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1791 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1792 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1793 6353ad76 2019-02-08 stsp done:
1794 6353ad76 2019-02-08 stsp if (blob)
1795 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1796 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1797 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1798 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1799 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1800 9d31a1d8 2018-03-11 stsp return err;
1801 e2b1e152 2019-07-13 stsp }
1802 e2b1e152 2019-07-13 stsp
1803 e2b1e152 2019-07-13 stsp /*
1804 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1805 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1806 e2b1e152 2019-07-13 stsp */
1807 e2b1e152 2019-07-13 stsp static const struct got_error *
1808 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1809 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1810 e2b1e152 2019-07-13 stsp {
1811 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1813 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1814 e2b1e152 2019-07-13 stsp
1815 e2b1e152 2019-07-13 stsp return NULL;
1816 9d31a1d8 2018-03-11 stsp }
1817 9d31a1d8 2018-03-11 stsp
1818 9d31a1d8 2018-03-11 stsp static const struct got_error *
1819 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1820 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1822 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 0584f854 2019-04-06 stsp void *progress_arg)
1824 9d31a1d8 2018-03-11 stsp {
1825 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1826 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1827 6353ad76 2019-02-08 stsp char *ondisk_path;
1828 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1829 b8f41171 2019-02-10 stsp struct stat sb;
1830 9d31a1d8 2018-03-11 stsp
1831 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1833 6353ad76 2019-02-08 stsp
1834 abb4604f 2019-07-27 stsp if (ie) {
1835 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 a76c42e6 2019-08-03 stsp goto done;
1838 a76c42e6 2019-08-03 stsp }
1839 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 7f91a133 2019-12-13 stsp repo);
1841 abb4604f 2019-07-27 stsp if (err)
1842 abb4604f 2019-07-27 stsp goto done;
1843 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1845 c90c8ce3 2020-07-23 stsp } else {
1846 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1847 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1848 c90c8ce3 2020-07-23 stsp }
1849 6353ad76 2019-02-08 stsp
1850 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1851 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1852 b8f41171 2019-02-10 stsp goto done;
1853 b8f41171 2019-02-10 stsp }
1854 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1855 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1856 5036ab18 2020-04-18 stsp path);
1857 5036ab18 2020-04-18 stsp goto done;
1858 5036ab18 2020-04-18 stsp }
1859 b8f41171 2019-02-10 stsp
1860 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1861 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1862 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1863 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1864 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1865 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1866 e2b1e152 2019-07-13 stsp if (err)
1867 e2b1e152 2019-07-13 stsp goto done;
1868 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 b8f41171 2019-02-10 stsp path);
1870 6353ad76 2019-02-08 stsp goto done;
1871 a378724f 2019-02-10 stsp }
1872 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1873 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1874 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1875 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1876 b8f41171 2019-02-10 stsp goto done;
1877 e2b1e152 2019-07-13 stsp }
1878 9d31a1d8 2018-03-11 stsp }
1879 9d31a1d8 2018-03-11 stsp
1880 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1881 8da9e5f4 2019-01-12 stsp if (err)
1882 6353ad76 2019-02-08 stsp goto done;
1883 512f0d0e 2019-01-02 stsp
1884 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1885 234035bc 2019-06-01 stsp int update_timestamps;
1886 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1887 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1888 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1889 234035bc 2019-06-01 stsp struct got_object_id id2;
1890 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1891 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1892 234035bc 2019-06-01 stsp if (err)
1893 f69721c3 2019-10-21 stsp goto done;
1894 f69721c3 2019-10-21 stsp }
1895 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1896 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1897 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1898 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1899 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1900 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1901 f69721c3 2019-10-21 stsp goto done;
1902 f69721c3 2019-10-21 stsp }
1903 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1904 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1905 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1906 234035bc 2019-06-01 stsp goto done;
1907 f69721c3 2019-10-21 stsp }
1908 234035bc 2019-06-01 stsp }
1909 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1910 36bf999c 2020-07-23 stsp char *link_target;
1911 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1912 36bf999c 2020-07-23 stsp if (err)
1913 36bf999c 2020-07-23 stsp goto done;
1914 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1915 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1916 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1917 36bf999c 2020-07-23 stsp free(link_target);
1918 993e2a1b 2020-07-23 stsp } else {
1919 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1920 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1921 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1922 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1923 993e2a1b 2020-07-23 stsp }
1924 f69721c3 2019-10-21 stsp free(label_orig);
1925 234035bc 2019-06-01 stsp if (blob2)
1926 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1927 909d120e 2019-09-22 stsp if (err)
1928 909d120e 2019-09-22 stsp goto done;
1929 234035bc 2019-06-01 stsp /*
1930 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1931 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1932 234035bc 2019-06-01 stsp * unmodified files again.
1933 234035bc 2019-06-01 stsp */
1934 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1935 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1936 234035bc 2019-06-01 stsp update_timestamps);
1937 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1938 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1939 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1940 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1941 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1942 1ee397ad 2019-07-12 stsp if (err)
1943 1ee397ad 2019-07-12 stsp goto done;
1944 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1945 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 13d9040b 2019-03-27 stsp if (err)
1947 13d9040b 2019-03-27 stsp goto done;
1948 13d9040b 2019-03-27 stsp } else {
1949 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1950 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1951 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1952 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1953 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1954 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1955 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1956 2e1fa222 2020-07-23 stsp } else {
1957 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1958 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1959 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1960 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1961 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1962 2e1fa222 2020-07-23 stsp }
1963 13d9040b 2019-03-27 stsp if (err)
1964 13d9040b 2019-03-27 stsp goto done;
1965 65b05cec 2020-07-23 stsp
1966 054041d0 2020-06-24 stsp if (ie) {
1967 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1968 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1969 054041d0 2020-06-24 stsp } else {
1970 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1971 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1972 054041d0 2020-06-24 stsp &blob->id);
1973 054041d0 2020-06-24 stsp }
1974 13d9040b 2019-03-27 stsp if (err)
1975 13d9040b 2019-03-27 stsp goto done;
1976 65b05cec 2020-07-23 stsp
1977 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1978 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1979 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1980 2e1fa222 2020-07-23 stsp }
1981 13d9040b 2019-03-27 stsp }
1982 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1983 6353ad76 2019-02-08 stsp done:
1984 6353ad76 2019-02-08 stsp free(ondisk_path);
1985 8da9e5f4 2019-01-12 stsp return err;
1986 90285c3b 2019-01-08 stsp }
1987 90285c3b 2019-01-08 stsp
1988 90285c3b 2019-01-08 stsp static const struct got_error *
1989 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1990 90285c3b 2019-01-08 stsp {
1991 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1992 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1993 90285c3b 2019-01-08 stsp
1994 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1995 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1996 90285c3b 2019-01-08 stsp
1997 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1998 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1999 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2000 c97665ae 2019-01-13 stsp } else {
2001 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2002 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2003 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2004 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2005 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2006 230a42bd 2019-05-11 jcs parent);
2007 90285c3b 2019-01-08 stsp break;
2008 90285c3b 2019-01-08 stsp }
2009 90285c3b 2019-01-08 stsp parent = dirname(parent);
2010 90285c3b 2019-01-08 stsp }
2011 90285c3b 2019-01-08 stsp }
2012 90285c3b 2019-01-08 stsp free(ondisk_path);
2013 90285c3b 2019-01-08 stsp return err;
2014 512f0d0e 2019-01-02 stsp }
2015 512f0d0e 2019-01-02 stsp
2016 708d8e67 2019-03-27 stsp static const struct got_error *
2017 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2018 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2019 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2020 708d8e67 2019-03-27 stsp {
2021 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2022 708d8e67 2019-03-27 stsp unsigned char status;
2023 708d8e67 2019-03-27 stsp struct stat sb;
2024 708d8e67 2019-03-27 stsp char *ondisk_path;
2025 708d8e67 2019-03-27 stsp
2026 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2027 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2028 a76c42e6 2019-08-03 stsp
2029 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2030 708d8e67 2019-03-27 stsp == -1)
2031 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2032 708d8e67 2019-03-27 stsp
2033 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2034 708d8e67 2019-03-27 stsp if (err)
2035 5a58a424 2020-06-23 stsp goto done;
2036 708d8e67 2019-03-27 stsp
2037 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2038 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2039 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2040 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2041 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2042 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2043 993e2a1b 2020-07-23 stsp goto done;
2044 993e2a1b 2020-07-23 stsp }
2045 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2046 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2047 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2048 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2049 993e2a1b 2020-07-23 stsp if (err)
2050 993e2a1b 2020-07-23 stsp goto done;
2051 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2052 993e2a1b 2020-07-23 stsp ie->path);
2053 993e2a1b 2020-07-23 stsp goto done;
2054 993e2a1b 2020-07-23 stsp }
2055 993e2a1b 2020-07-23 stsp
2056 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2057 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2058 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2059 1ee397ad 2019-07-12 stsp if (err)
2060 5a58a424 2020-06-23 stsp goto done;
2061 fc6346c4 2019-03-27 stsp /*
2062 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2063 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2064 fc6346c4 2019-03-27 stsp */
2065 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2066 fc6346c4 2019-03-27 stsp 0);
2067 fc6346c4 2019-03-27 stsp } else {
2068 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2069 1ee397ad 2019-07-12 stsp if (err)
2070 5a58a424 2020-06-23 stsp goto done;
2071 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2072 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2073 fc6346c4 2019-03-27 stsp if (err)
2074 5a58a424 2020-06-23 stsp goto done;
2075 fc6346c4 2019-03-27 stsp }
2076 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2077 708d8e67 2019-03-27 stsp }
2078 5a58a424 2020-06-23 stsp done:
2079 5a58a424 2020-06-23 stsp free(ondisk_path);
2080 fc6346c4 2019-03-27 stsp return err;
2081 708d8e67 2019-03-27 stsp }
2082 708d8e67 2019-03-27 stsp
2083 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2084 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2085 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2086 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2087 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2088 8da9e5f4 2019-01-12 stsp void *progress_arg;
2089 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2090 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2091 8da9e5f4 2019-01-12 stsp };
2092 8da9e5f4 2019-01-12 stsp
2093 512f0d0e 2019-01-02 stsp static const struct got_error *
2094 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2095 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2096 512f0d0e 2019-01-02 stsp {
2097 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2098 0584f854 2019-04-06 stsp
2099 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2100 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2101 512f0d0e 2019-01-02 stsp
2102 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2103 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2104 8da9e5f4 2019-01-12 stsp }
2105 512f0d0e 2019-01-02 stsp
2106 8da9e5f4 2019-01-12 stsp static const struct got_error *
2107 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2108 8da9e5f4 2019-01-12 stsp {
2109 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2110 7a9df742 2019-01-08 stsp
2111 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2112 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2113 0584f854 2019-04-06 stsp
2114 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2115 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2116 9d31a1d8 2018-03-11 stsp }
2117 9d31a1d8 2018-03-11 stsp
2118 9d31a1d8 2018-03-11 stsp static const struct got_error *
2119 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2120 9d31a1d8 2018-03-11 stsp {
2121 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2122 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2123 8da9e5f4 2019-01-12 stsp char *path;
2124 9d31a1d8 2018-03-11 stsp
2125 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2126 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2127 0584f854 2019-04-06 stsp
2128 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2129 63c5ca5d 2019-08-24 stsp return NULL;
2130 63c5ca5d 2019-08-24 stsp
2131 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2132 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2133 8da9e5f4 2019-01-12 stsp == -1)
2134 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2135 9d31a1d8 2018-03-11 stsp
2136 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2137 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2138 8da9e5f4 2019-01-12 stsp else
2139 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2140 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2141 9d31a1d8 2018-03-11 stsp
2142 8da9e5f4 2019-01-12 stsp free(path);
2143 0cd1c46a 2019-03-11 stsp return err;
2144 0cd1c46a 2019-03-11 stsp }
2145 0cd1c46a 2019-03-11 stsp
2146 818c7501 2019-07-11 stsp static const struct got_error *
2147 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2148 0cd1c46a 2019-03-11 stsp {
2149 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2150 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2151 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2152 0cd1c46a 2019-03-11 stsp
2153 517bab73 2019-03-11 stsp *refname = NULL;
2154 517bab73 2019-03-11 stsp
2155 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2156 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2157 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2158 0cd1c46a 2019-03-11 stsp
2159 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2160 0647c563 2019-03-11 stsp == -1) {
2161 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2162 0647c563 2019-03-11 stsp *refname = NULL;
2163 0cd1c46a 2019-03-11 stsp }
2164 517bab73 2019-03-11 stsp free(uuidstr);
2165 517bab73 2019-03-11 stsp return err;
2166 517bab73 2019-03-11 stsp }
2167 517bab73 2019-03-11 stsp
2168 818c7501 2019-07-11 stsp const struct got_error *
2169 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2170 818c7501 2019-07-11 stsp {
2171 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2172 818c7501 2019-07-11 stsp }
2173 818c7501 2019-07-11 stsp
2174 818c7501 2019-07-11 stsp static const struct got_error *
2175 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2176 818c7501 2019-07-11 stsp {
2177 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2178 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2179 818c7501 2019-07-11 stsp }
2180 818c7501 2019-07-11 stsp
2181 818c7501 2019-07-11 stsp static const struct got_error *
2182 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2183 818c7501 2019-07-11 stsp {
2184 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2185 818c7501 2019-07-11 stsp }
2186 818c7501 2019-07-11 stsp
2187 818c7501 2019-07-11 stsp static const struct got_error *
2188 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2189 818c7501 2019-07-11 stsp {
2190 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2191 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2192 818c7501 2019-07-11 stsp }
2193 818c7501 2019-07-11 stsp
2194 818c7501 2019-07-11 stsp static const struct got_error *
2195 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2196 818c7501 2019-07-11 stsp {
2197 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2198 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2199 0ebf8283 2019-07-24 stsp }
2200 0ebf8283 2019-07-24 stsp
2201 0ebf8283 2019-07-24 stsp static const struct got_error *
2202 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2203 0ebf8283 2019-07-24 stsp {
2204 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2205 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2206 0ebf8283 2019-07-24 stsp }
2207 0ebf8283 2019-07-24 stsp
2208 0ebf8283 2019-07-24 stsp static const struct got_error *
2209 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2210 0ebf8283 2019-07-24 stsp {
2211 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2212 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2213 0ebf8283 2019-07-24 stsp }
2214 0ebf8283 2019-07-24 stsp
2215 0ebf8283 2019-07-24 stsp static const struct got_error *
2216 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2217 0ebf8283 2019-07-24 stsp {
2218 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2219 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2220 818c7501 2019-07-11 stsp }
2221 818c7501 2019-07-11 stsp
2222 0ebf8283 2019-07-24 stsp static const struct got_error *
2223 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2224 0ebf8283 2019-07-24 stsp {
2225 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2226 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2227 0ebf8283 2019-07-24 stsp }
2228 818c7501 2019-07-11 stsp
2229 0ebf8283 2019-07-24 stsp const struct got_error *
2230 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2231 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2232 0ebf8283 2019-07-24 stsp {
2233 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2234 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2235 0ebf8283 2019-07-24 stsp *path = NULL;
2236 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2237 0ebf8283 2019-07-24 stsp }
2238 0ebf8283 2019-07-24 stsp return NULL;
2239 0ebf8283 2019-07-24 stsp }
2240 0ebf8283 2019-07-24 stsp
2241 517bab73 2019-03-11 stsp /*
2242 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2243 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2244 517bab73 2019-03-11 stsp */
2245 517bab73 2019-03-11 stsp static const struct got_error *
2246 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2247 517bab73 2019-03-11 stsp {
2248 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2249 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2250 517bab73 2019-03-11 stsp char *refname;
2251 517bab73 2019-03-11 stsp
2252 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2253 517bab73 2019-03-11 stsp if (err)
2254 517bab73 2019-03-11 stsp return err;
2255 0cd1c46a 2019-03-11 stsp
2256 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2257 0cd1c46a 2019-03-11 stsp if (err)
2258 0cd1c46a 2019-03-11 stsp goto done;
2259 0cd1c46a 2019-03-11 stsp
2260 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2261 0cd1c46a 2019-03-11 stsp done:
2262 0cd1c46a 2019-03-11 stsp free(refname);
2263 0cd1c46a 2019-03-11 stsp if (ref)
2264 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2265 3e3a69f1 2019-07-25 stsp return err;
2266 3e3a69f1 2019-07-25 stsp }
2267 3e3a69f1 2019-07-25 stsp
2268 3e3a69f1 2019-07-25 stsp static const struct got_error *
2269 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2270 3e3a69f1 2019-07-25 stsp {
2271 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2272 3e3a69f1 2019-07-25 stsp
2273 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2274 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2275 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2276 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2277 3e3a69f1 2019-07-25 stsp }
2278 9d31a1d8 2018-03-11 stsp return err;
2279 9d31a1d8 2018-03-11 stsp }
2280 9d31a1d8 2018-03-11 stsp
2281 3e3a69f1 2019-07-25 stsp
2282 ebf99748 2019-05-09 stsp static const struct got_error *
2283 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2284 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2285 ebf99748 2019-05-09 stsp {
2286 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2287 ebf99748 2019-05-09 stsp FILE *index = NULL;
2288 0cd1c46a 2019-03-11 stsp
2289 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2290 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2291 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2292 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2293 ebf99748 2019-05-09 stsp
2294 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2295 3e3a69f1 2019-07-25 stsp if (err)
2296 ebf99748 2019-05-09 stsp goto done;
2297 ebf99748 2019-05-09 stsp
2298 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2299 ebf99748 2019-05-09 stsp if (index == NULL) {
2300 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2301 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2302 ebf99748 2019-05-09 stsp } else {
2303 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2304 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2305 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2306 ebf99748 2019-05-09 stsp }
2307 ebf99748 2019-05-09 stsp done:
2308 ebf99748 2019-05-09 stsp if (err) {
2309 ebf99748 2019-05-09 stsp free(*fileindex_path);
2310 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2311 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2312 ebf99748 2019-05-09 stsp *fileindex = NULL;
2313 ebf99748 2019-05-09 stsp }
2314 ebf99748 2019-05-09 stsp return err;
2315 ebf99748 2019-05-09 stsp }
2316 c932eeeb 2019-05-22 stsp
2317 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2318 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2319 c932eeeb 2019-05-22 stsp const char *path;
2320 c932eeeb 2019-05-22 stsp size_t path_len;
2321 c932eeeb 2019-05-22 stsp const char *entry_name;
2322 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2323 a484d721 2019-06-10 stsp void *progress_arg;
2324 c932eeeb 2019-05-22 stsp };
2325 ebf99748 2019-05-09 stsp
2326 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2327 c932eeeb 2019-05-22 stsp static const struct got_error *
2328 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2329 c932eeeb 2019-05-22 stsp {
2330 1ee397ad 2019-07-12 stsp const struct got_error *err;
2331 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2332 c932eeeb 2019-05-22 stsp
2333 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2334 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2335 c932eeeb 2019-05-22 stsp return NULL;
2336 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2337 102ff934 2019-06-11 stsp return NULL;
2338 102ff934 2019-06-11 stsp
2339 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2340 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2341 c932eeeb 2019-05-22 stsp return NULL;
2342 c932eeeb 2019-05-22 stsp
2343 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2344 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2345 edd02c5e 2019-07-11 stsp ie->path);
2346 1ee397ad 2019-07-12 stsp if (err)
2347 1ee397ad 2019-07-12 stsp return err;
2348 1ee397ad 2019-07-12 stsp }
2349 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2350 c932eeeb 2019-05-22 stsp return NULL;
2351 9c6338c4 2019-06-02 stsp }
2352 9c6338c4 2019-06-02 stsp
2353 9c6338c4 2019-06-02 stsp static const struct got_error *
2354 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2355 9c6338c4 2019-06-02 stsp {
2356 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2357 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2358 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2359 867630bb 2020-01-17 stsp struct timespec timeout;
2360 9c6338c4 2019-06-02 stsp
2361 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2362 9c6338c4 2019-06-02 stsp fileindex_path);
2363 9c6338c4 2019-06-02 stsp if (err)
2364 9c6338c4 2019-06-02 stsp goto done;
2365 9c6338c4 2019-06-02 stsp
2366 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2367 9c6338c4 2019-06-02 stsp if (err)
2368 9c6338c4 2019-06-02 stsp goto done;
2369 9c6338c4 2019-06-02 stsp
2370 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2371 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2372 9c6338c4 2019-06-02 stsp fileindex_path);
2373 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2374 9c6338c4 2019-06-02 stsp }
2375 867630bb 2020-01-17 stsp
2376 867630bb 2020-01-17 stsp /*
2377 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2378 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2379 867630bb 2020-01-17 stsp * was recorded in the file index.
2380 867630bb 2020-01-17 stsp */
2381 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2382 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2383 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2384 9c6338c4 2019-06-02 stsp done:
2385 9c6338c4 2019-06-02 stsp if (new_index)
2386 9c6338c4 2019-06-02 stsp fclose(new_index);
2387 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2388 9c6338c4 2019-06-02 stsp return err;
2389 c932eeeb 2019-05-22 stsp }
2390 6ced4176 2019-07-12 stsp
2391 6ced4176 2019-07-12 stsp static const struct got_error *
2392 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2393 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2394 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2395 6ced4176 2019-07-12 stsp {
2396 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2397 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2398 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2399 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2400 6ced4176 2019-07-12 stsp
2401 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2402 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2403 6ced4176 2019-07-12 stsp *tree_id = NULL;
2404 6ced4176 2019-07-12 stsp
2405 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2406 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2407 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2408 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2409 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2410 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2411 6ced4176 2019-07-12 stsp goto done;
2412 6ced4176 2019-07-12 stsp }
2413 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2414 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2415 6ced4176 2019-07-12 stsp if (err)
2416 6ced4176 2019-07-12 stsp goto done;
2417 6ced4176 2019-07-12 stsp return NULL;
2418 6ced4176 2019-07-12 stsp }
2419 6ced4176 2019-07-12 stsp
2420 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2421 6ced4176 2019-07-12 stsp
2422 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2423 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2424 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2425 6ced4176 2019-07-12 stsp goto done;
2426 6ced4176 2019-07-12 stsp }
2427 6ced4176 2019-07-12 stsp
2428 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2429 6ced4176 2019-07-12 stsp in_repo_path);
2430 6ced4176 2019-07-12 stsp if (err)
2431 6ced4176 2019-07-12 stsp goto done;
2432 6ced4176 2019-07-12 stsp
2433 6ced4176 2019-07-12 stsp free(in_repo_path);
2434 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2435 6ced4176 2019-07-12 stsp
2436 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2437 6ced4176 2019-07-12 stsp if (err)
2438 6ced4176 2019-07-12 stsp goto done;
2439 c932eeeb 2019-05-22 stsp
2440 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2441 6ced4176 2019-07-12 stsp /* Check out a single file. */
2442 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2443 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2444 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2445 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2446 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2447 6ced4176 2019-07-12 stsp goto done;
2448 6ced4176 2019-07-12 stsp }
2449 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2450 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2451 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2452 6ced4176 2019-07-12 stsp goto done;
2453 6ced4176 2019-07-12 stsp }
2454 6ced4176 2019-07-12 stsp } else {
2455 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2456 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2457 6ced4176 2019-07-12 stsp if (err)
2458 6ced4176 2019-07-12 stsp return err;
2459 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2460 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2461 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2462 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2463 6ced4176 2019-07-12 stsp goto done;
2464 6ced4176 2019-07-12 stsp }
2465 6ced4176 2019-07-12 stsp }
2466 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2467 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2468 6ced4176 2019-07-12 stsp } else {
2469 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2470 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2471 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2472 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2473 6ced4176 2019-07-12 stsp goto done;
2474 6ced4176 2019-07-12 stsp }
2475 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2476 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2477 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2478 6ced4176 2019-07-12 stsp goto done;
2479 6ced4176 2019-07-12 stsp }
2480 6ced4176 2019-07-12 stsp }
2481 6ced4176 2019-07-12 stsp done:
2482 6ced4176 2019-07-12 stsp free(id);
2483 6ced4176 2019-07-12 stsp free(in_repo_path);
2484 6ced4176 2019-07-12 stsp if (err) {
2485 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2486 6ced4176 2019-07-12 stsp free(*tree_relpath);
2487 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2488 6ced4176 2019-07-12 stsp free(*tree_id);
2489 6ced4176 2019-07-12 stsp *tree_id = NULL;
2490 6ced4176 2019-07-12 stsp }
2491 07ed472d 2019-07-12 stsp return err;
2492 07ed472d 2019-07-12 stsp }
2493 07ed472d 2019-07-12 stsp
2494 07ed472d 2019-07-12 stsp static const struct got_error *
2495 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2496 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2497 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2498 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2499 07ed472d 2019-07-12 stsp {
2500 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2501 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2502 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2503 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2504 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2505 07ed472d 2019-07-12 stsp
2506 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2507 7f47418f 2019-12-20 stsp if (err) {
2508 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2509 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2510 7f47418f 2019-12-20 stsp goto done;
2511 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2512 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2513 7f47418f 2019-12-20 stsp if (err)
2514 7f47418f 2019-12-20 stsp return err;
2515 7f47418f 2019-12-20 stsp }
2516 07ed472d 2019-07-12 stsp
2517 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2518 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2519 07ed472d 2019-07-12 stsp if (err)
2520 07ed472d 2019-07-12 stsp goto done;
2521 07ed472d 2019-07-12 stsp
2522 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2523 07ed472d 2019-07-12 stsp if (err)
2524 07ed472d 2019-07-12 stsp goto done;
2525 07ed472d 2019-07-12 stsp
2526 07ed472d 2019-07-12 stsp if (entry_name &&
2527 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2528 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2529 07ed472d 2019-07-12 stsp goto done;
2530 07ed472d 2019-07-12 stsp }
2531 07ed472d 2019-07-12 stsp
2532 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2533 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2534 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2535 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2536 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2537 07ed472d 2019-07-12 stsp arg.repo = repo;
2538 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2539 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2540 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2541 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2542 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2543 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2544 07ed472d 2019-07-12 stsp done:
2545 07ed472d 2019-07-12 stsp if (tree)
2546 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2547 07ed472d 2019-07-12 stsp if (commit)
2548 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2549 6ced4176 2019-07-12 stsp return err;
2550 6ced4176 2019-07-12 stsp }
2551 6ced4176 2019-07-12 stsp
2552 9d31a1d8 2018-03-11 stsp const struct got_error *
2553 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2554 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2555 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2556 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2557 9d31a1d8 2018-03-11 stsp {
2558 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2559 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2560 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2561 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2562 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2563 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2564 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2565 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2566 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2567 f2ea84fa 2019-07-27 stsp int entry_type;
2568 f2ea84fa 2019-07-27 stsp char *relpath;
2569 f2ea84fa 2019-07-27 stsp char *entry_name;
2570 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2571 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2572 9d31a1d8 2018-03-11 stsp
2573 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2574 f2ea84fa 2019-07-27 stsp
2575 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2576 9d31a1d8 2018-03-11 stsp if (err)
2577 9d31a1d8 2018-03-11 stsp return err;
2578 93a30277 2018-12-24 stsp
2579 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2580 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2581 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2582 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2583 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2584 f2ea84fa 2019-07-27 stsp goto done;
2585 f2ea84fa 2019-07-27 stsp }
2586 6ced4176 2019-07-12 stsp
2587 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2588 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2589 f2ea84fa 2019-07-27 stsp if (err) {
2590 f2ea84fa 2019-07-27 stsp free(tpd);
2591 6ced4176 2019-07-12 stsp goto done;
2592 6ced4176 2019-07-12 stsp }
2593 f2ea84fa 2019-07-27 stsp
2594 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2595 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2596 f2ea84fa 2019-07-27 stsp if (err) {
2597 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2598 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2599 f2ea84fa 2019-07-27 stsp free(tpd);
2600 f2ea84fa 2019-07-27 stsp goto done;
2601 f2ea84fa 2019-07-27 stsp }
2602 f2ea84fa 2019-07-27 stsp } else
2603 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2604 f2ea84fa 2019-07-27 stsp
2605 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2606 6ced4176 2019-07-12 stsp }
2607 6ced4176 2019-07-12 stsp
2608 51514078 2018-12-25 stsp /*
2609 51514078 2018-12-25 stsp * Read the file index.
2610 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2611 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2612 51514078 2018-12-25 stsp */
2613 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2614 8da9e5f4 2019-01-12 stsp if (err)
2615 c4cdcb68 2019-04-03 stsp goto done;
2616 c4cdcb68 2019-04-03 stsp
2617 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2618 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2619 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2620 f2ea84fa 2019-07-27 stsp
2621 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2622 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2623 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2624 f2ea84fa 2019-07-27 stsp if (err)
2625 f2ea84fa 2019-07-27 stsp break;
2626 f2ea84fa 2019-07-27 stsp
2627 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2628 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2629 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2630 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2631 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2632 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2633 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2634 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2635 f2ea84fa 2019-07-27 stsp if (err)
2636 f2ea84fa 2019-07-27 stsp break;
2637 f2ea84fa 2019-07-27 stsp
2638 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2639 711d9cd9 2019-07-12 stsp }
2640 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2641 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2642 af12c6b9 2019-06-04 stsp err = sync_err;
2643 9d31a1d8 2018-03-11 stsp done:
2644 9c6338c4 2019-06-02 stsp free(fileindex_path);
2645 edfa7d7f 2018-09-11 stsp if (tree)
2646 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2647 9d31a1d8 2018-03-11 stsp if (commit)
2648 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2649 5ade8233 2019-07-12 stsp if (fileindex)
2650 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2651 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2652 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2653 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2654 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2655 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2656 f2ea84fa 2019-07-27 stsp free(tpd);
2657 f2ea84fa 2019-07-27 stsp }
2658 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2659 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2660 9d31a1d8 2018-03-11 stsp err = unlockerr;
2661 f8d1f275 2019-02-04 stsp return err;
2662 f8d1f275 2019-02-04 stsp }
2663 f8d1f275 2019-02-04 stsp
2664 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2665 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2666 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2667 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2668 234035bc 2019-06-01 stsp void *progress_arg;
2669 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2670 234035bc 2019-06-01 stsp void *cancel_arg;
2671 f69721c3 2019-10-21 stsp const char *label_orig;
2672 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2673 234035bc 2019-06-01 stsp };
2674 234035bc 2019-06-01 stsp
2675 234035bc 2019-06-01 stsp static const struct got_error *
2676 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2677 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2678 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2679 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2680 234035bc 2019-06-01 stsp {
2681 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2682 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2683 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2684 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2685 234035bc 2019-06-01 stsp struct stat sb;
2686 234035bc 2019-06-01 stsp unsigned char status;
2687 234035bc 2019-06-01 stsp int local_changes_subsumed;
2688 234035bc 2019-06-01 stsp
2689 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2690 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2691 d6c87207 2019-08-02 stsp strlen(path2));
2692 1ee397ad 2019-07-12 stsp if (ie == NULL)
2693 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2694 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2695 234035bc 2019-06-01 stsp
2696 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2697 234035bc 2019-06-01 stsp path2) == -1)
2698 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2699 234035bc 2019-06-01 stsp
2700 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2701 7f91a133 2019-12-13 stsp repo);
2702 234035bc 2019-06-01 stsp if (err)
2703 234035bc 2019-06-01 stsp goto done;
2704 234035bc 2019-06-01 stsp
2705 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2706 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2707 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2708 234035bc 2019-06-01 stsp goto done;
2709 234035bc 2019-06-01 stsp }
2710 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2711 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2712 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2713 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2714 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2715 234035bc 2019-06-01 stsp goto done;
2716 234035bc 2019-06-01 stsp }
2717 234035bc 2019-06-01 stsp
2718 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2719 36bf999c 2020-07-23 stsp char *link_target2;
2720 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2721 36bf999c 2020-07-23 stsp if (err)
2722 36bf999c 2020-07-23 stsp goto done;
2723 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2724 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2725 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2726 36bf999c 2020-07-23 stsp free(link_target2);
2727 af57b12a 2020-07-23 stsp } else {
2728 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2729 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2730 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2731 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2732 af57b12a 2020-07-23 stsp }
2733 234035bc 2019-06-01 stsp } else if (blob1) {
2734 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2735 d6c87207 2019-08-02 stsp strlen(path1));
2736 1ee397ad 2019-07-12 stsp if (ie == NULL)
2737 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2738 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2739 2b92fad7 2019-06-02 stsp
2740 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2741 2b92fad7 2019-06-02 stsp path1) == -1)
2742 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2743 2b92fad7 2019-06-02 stsp
2744 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2745 7f91a133 2019-12-13 stsp repo);
2746 2b92fad7 2019-06-02 stsp if (err)
2747 2b92fad7 2019-06-02 stsp goto done;
2748 2b92fad7 2019-06-02 stsp
2749 2b92fad7 2019-06-02 stsp switch (status) {
2750 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2751 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2752 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2753 1ee397ad 2019-07-12 stsp if (err)
2754 1ee397ad 2019-07-12 stsp goto done;
2755 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2756 2b92fad7 2019-06-02 stsp if (err)
2757 2b92fad7 2019-06-02 stsp goto done;
2758 2b92fad7 2019-06-02 stsp if (ie)
2759 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2760 2b92fad7 2019-06-02 stsp break;
2761 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2762 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2763 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2764 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2765 1ee397ad 2019-07-12 stsp if (err)
2766 1ee397ad 2019-07-12 stsp goto done;
2767 2b92fad7 2019-06-02 stsp if (ie)
2768 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2769 2b92fad7 2019-06-02 stsp break;
2770 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2771 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2772 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2773 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2774 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2775 1ee397ad 2019-07-12 stsp if (err)
2776 1ee397ad 2019-07-12 stsp goto done;
2777 2b92fad7 2019-06-02 stsp break;
2778 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2779 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2780 1ee397ad 2019-07-12 stsp if (err)
2781 1ee397ad 2019-07-12 stsp goto done;
2782 2b92fad7 2019-06-02 stsp break;
2783 2b92fad7 2019-06-02 stsp default:
2784 2b92fad7 2019-06-02 stsp break;
2785 2b92fad7 2019-06-02 stsp }
2786 234035bc 2019-06-01 stsp } else if (blob2) {
2787 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2788 234035bc 2019-06-01 stsp path2) == -1)
2789 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2790 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2791 d6c87207 2019-08-02 stsp strlen(path2));
2792 234035bc 2019-06-01 stsp if (ie) {
2793 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2794 7f91a133 2019-12-13 stsp -1, NULL, repo);
2795 234035bc 2019-06-01 stsp if (err)
2796 234035bc 2019-06-01 stsp goto done;
2797 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2798 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2799 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2800 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2801 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2802 1ee397ad 2019-07-12 stsp status, path2);
2803 234035bc 2019-06-01 stsp goto done;
2804 234035bc 2019-06-01 stsp }
2805 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2806 36bf999c 2020-07-23 stsp char *link_target2;
2807 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2808 36bf999c 2020-07-23 stsp blob2);
2809 36bf999c 2020-07-23 stsp if (err)
2810 36bf999c 2020-07-23 stsp goto done;
2811 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2812 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2813 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2814 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2815 36bf999c 2020-07-23 stsp free(link_target2);
2816 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2817 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2818 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2819 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2820 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2821 526a746f 2020-07-23 stsp a->progress_arg);
2822 dfe9fba0 2020-07-23 stsp } else {
2823 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2824 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2825 526a746f 2020-07-23 stsp }
2826 dfe9fba0 2020-07-23 stsp if (err)
2827 dfe9fba0 2020-07-23 stsp goto done;
2828 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2829 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2830 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2831 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2832 234035bc 2019-06-01 stsp if (err)
2833 234035bc 2019-06-01 stsp goto done;
2834 234035bc 2019-06-01 stsp }
2835 234035bc 2019-06-01 stsp } else {
2836 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2837 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2838 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2839 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2840 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2841 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2842 2e1fa222 2020-07-23 stsp } else {
2843 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2844 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2845 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2846 2e1fa222 2020-07-23 stsp }
2847 234035bc 2019-06-01 stsp if (err)
2848 234035bc 2019-06-01 stsp goto done;
2849 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2850 234035bc 2019-06-01 stsp if (err)
2851 234035bc 2019-06-01 stsp goto done;
2852 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2853 3969253a 2020-03-07 stsp NULL, NULL, 1);
2854 3969253a 2020-03-07 stsp if (err) {
2855 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2856 3969253a 2020-03-07 stsp goto done;
2857 3969253a 2020-03-07 stsp }
2858 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2859 2b92fad7 2019-06-02 stsp if (err) {
2860 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2861 2b92fad7 2019-06-02 stsp goto done;
2862 2b92fad7 2019-06-02 stsp }
2863 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2864 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2865 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2866 2e1fa222 2020-07-23 stsp }
2867 234035bc 2019-06-01 stsp }
2868 234035bc 2019-06-01 stsp }
2869 234035bc 2019-06-01 stsp done:
2870 234035bc 2019-06-01 stsp free(ondisk_path);
2871 234035bc 2019-06-01 stsp return err;
2872 234035bc 2019-06-01 stsp }
2873 234035bc 2019-06-01 stsp
2874 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2875 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2876 234035bc 2019-06-01 stsp struct got_repository *repo;
2877 234035bc 2019-06-01 stsp };
2878 234035bc 2019-06-01 stsp
2879 234035bc 2019-06-01 stsp static const struct got_error *
2880 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2881 234035bc 2019-06-01 stsp {
2882 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2883 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2884 234035bc 2019-06-01 stsp unsigned char status;
2885 234035bc 2019-06-01 stsp struct stat sb;
2886 234035bc 2019-06-01 stsp char *ondisk_path;
2887 234035bc 2019-06-01 stsp
2888 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2889 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2890 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2891 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2892 234035bc 2019-06-01 stsp
2893 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2894 234035bc 2019-06-01 stsp == -1)
2895 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2896 234035bc 2019-06-01 stsp
2897 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2898 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2899 234035bc 2019-06-01 stsp if (err)
2900 234035bc 2019-06-01 stsp return err;
2901 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2902 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2903 234035bc 2019-06-01 stsp
2904 234035bc 2019-06-01 stsp return NULL;
2905 234035bc 2019-06-01 stsp }
2906 234035bc 2019-06-01 stsp
2907 818c7501 2019-07-11 stsp static const struct got_error *
2908 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2909 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2910 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2911 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2912 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2913 234035bc 2019-06-01 stsp {
2914 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2915 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2916 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2917 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2918 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2919 234035bc 2019-06-01 stsp
2920 03415a1a 2019-06-02 stsp if (commit_id1) {
2921 f69721c3 2019-10-21 stsp char *id_str;
2922 f69721c3 2019-10-21 stsp
2923 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2924 03415a1a 2019-06-02 stsp worktree->path_prefix);
2925 03415a1a 2019-06-02 stsp if (err)
2926 03415a1a 2019-06-02 stsp goto done;
2927 03415a1a 2019-06-02 stsp
2928 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2929 03415a1a 2019-06-02 stsp if (err)
2930 03415a1a 2019-06-02 stsp goto done;
2931 f69721c3 2019-10-21 stsp
2932 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2933 f69721c3 2019-10-21 stsp if (err)
2934 f69721c3 2019-10-21 stsp goto done;
2935 f69721c3 2019-10-21 stsp
2936 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2937 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2938 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2939 f69721c3 2019-10-21 stsp free(id_str);
2940 f69721c3 2019-10-21 stsp goto done;
2941 f69721c3 2019-10-21 stsp }
2942 f69721c3 2019-10-21 stsp free(id_str);
2943 03415a1a 2019-06-02 stsp }
2944 234035bc 2019-06-01 stsp
2945 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2946 234035bc 2019-06-01 stsp worktree->path_prefix);
2947 234035bc 2019-06-01 stsp if (err)
2948 234035bc 2019-06-01 stsp goto done;
2949 234035bc 2019-06-01 stsp
2950 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2951 234035bc 2019-06-01 stsp if (err)
2952 234035bc 2019-06-01 stsp goto done;
2953 234035bc 2019-06-01 stsp
2954 234035bc 2019-06-01 stsp arg.worktree = worktree;
2955 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2956 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2957 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2958 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2959 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2960 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2961 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2962 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2963 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2964 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2965 af12c6b9 2019-06-04 stsp err = sync_err;
2966 234035bc 2019-06-01 stsp done:
2967 234035bc 2019-06-01 stsp if (tree1)
2968 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2969 234035bc 2019-06-01 stsp if (tree2)
2970 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2971 f69721c3 2019-10-21 stsp free(label_orig);
2972 818c7501 2019-07-11 stsp return err;
2973 818c7501 2019-07-11 stsp }
2974 234035bc 2019-06-01 stsp
2975 818c7501 2019-07-11 stsp const struct got_error *
2976 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2977 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2978 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2979 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2980 818c7501 2019-07-11 stsp {
2981 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2982 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2983 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2984 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2985 818c7501 2019-07-11 stsp
2986 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2987 818c7501 2019-07-11 stsp if (err)
2988 818c7501 2019-07-11 stsp return err;
2989 818c7501 2019-07-11 stsp
2990 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2991 818c7501 2019-07-11 stsp if (err)
2992 818c7501 2019-07-11 stsp goto done;
2993 818c7501 2019-07-11 stsp
2994 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2995 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2996 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2997 818c7501 2019-07-11 stsp &mok_arg);
2998 818c7501 2019-07-11 stsp if (err)
2999 818c7501 2019-07-11 stsp goto done;
3000 818c7501 2019-07-11 stsp
3001 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3002 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3003 818c7501 2019-07-11 stsp done:
3004 818c7501 2019-07-11 stsp if (fileindex)
3005 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3006 818c7501 2019-07-11 stsp free(fileindex_path);
3007 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3008 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3009 234035bc 2019-06-01 stsp err = unlockerr;
3010 234035bc 2019-06-01 stsp return err;
3011 234035bc 2019-06-01 stsp }
3012 234035bc 2019-06-01 stsp
3013 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3014 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3015 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3016 927df6b7 2019-02-10 stsp const char *status_path;
3017 927df6b7 2019-02-10 stsp size_t status_path_len;
3018 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3019 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3020 f8d1f275 2019-02-04 stsp void *status_arg;
3021 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3022 f8d1f275 2019-02-04 stsp void *cancel_arg;
3023 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3024 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3025 f2a9dc41 2019-12-13 tracey int report_unchanged;
3026 3143d852 2020-06-25 stsp int no_ignores;
3027 f8d1f275 2019-02-04 stsp };
3028 88d0e355 2019-08-03 stsp
3029 f8d1f275 2019-02-04 stsp static const struct got_error *
3030 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3031 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3032 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3033 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3034 927df6b7 2019-02-10 stsp {
3035 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3036 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3037 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3038 927df6b7 2019-02-10 stsp struct stat sb;
3039 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3040 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3041 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3042 927df6b7 2019-02-10 stsp
3043 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3044 98eaaa12 2019-08-03 stsp if (err)
3045 98eaaa12 2019-08-03 stsp return err;
3046 98eaaa12 2019-08-03 stsp
3047 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3048 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3049 98eaaa12 2019-08-03 stsp return NULL;
3050 98eaaa12 2019-08-03 stsp
3051 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3052 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3053 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3054 98eaaa12 2019-08-03 stsp }
3055 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3056 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3057 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3058 927df6b7 2019-02-10 stsp }
3059 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3060 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3061 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3062 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3063 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3064 98eaaa12 2019-08-03 stsp }
3065 98eaaa12 2019-08-03 stsp
3066 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3067 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3068 927df6b7 2019-02-10 stsp }
3069 927df6b7 2019-02-10 stsp
3070 927df6b7 2019-02-10 stsp static const struct got_error *
3071 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3072 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3073 f8d1f275 2019-02-04 stsp {
3074 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3075 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3076 f8d1f275 2019-02-04 stsp char *abspath;
3077 f8d1f275 2019-02-04 stsp
3078 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3079 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3080 0584f854 2019-04-06 stsp
3081 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3082 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3083 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3084 927df6b7 2019-02-10 stsp return NULL;
3085 927df6b7 2019-02-10 stsp
3086 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3087 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3088 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3089 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3090 f8d1f275 2019-02-04 stsp } else {
3091 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3092 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3093 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3094 f8d1f275 2019-02-04 stsp }
3095 f8d1f275 2019-02-04 stsp
3096 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3097 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3098 f8d1f275 2019-02-04 stsp free(abspath);
3099 9d31a1d8 2018-03-11 stsp return err;
3100 9d31a1d8 2018-03-11 stsp }
3101 f8d1f275 2019-02-04 stsp
3102 f8d1f275 2019-02-04 stsp static const struct got_error *
3103 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3104 f8d1f275 2019-02-04 stsp {
3105 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3106 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3107 2ec1f75b 2019-03-26 stsp unsigned char status;
3108 927df6b7 2019-02-10 stsp
3109 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3110 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3111 0584f854 2019-04-06 stsp
3112 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3113 927df6b7 2019-02-10 stsp return NULL;
3114 927df6b7 2019-02-10 stsp
3115 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3116 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3117 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3118 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3119 2ec1f75b 2019-03-26 stsp else
3120 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3121 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3122 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3123 6841da00 2019-08-08 stsp }
3124 6841da00 2019-08-08 stsp
3125 6841da00 2019-08-08 stsp void
3126 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3127 6841da00 2019-08-08 stsp {
3128 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3129 6841da00 2019-08-08 stsp
3130 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3131 6841da00 2019-08-08 stsp free((char *)pe->path);
3132 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3133 6841da00 2019-08-08 stsp }
3134 6841da00 2019-08-08 stsp
3135 6841da00 2019-08-08 stsp void
3136 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3137 6841da00 2019-08-08 stsp {
3138 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3139 6841da00 2019-08-08 stsp
3140 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3141 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3142 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3143 6841da00 2019-08-08 stsp free((char *)pe->path);
3144 6841da00 2019-08-08 stsp }
3145 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3146 6841da00 2019-08-08 stsp }
3147 6841da00 2019-08-08 stsp
3148 6841da00 2019-08-08 stsp static const struct got_error *
3149 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3150 6841da00 2019-08-08 stsp {
3151 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3152 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3153 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3154 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3155 6841da00 2019-08-08 stsp size_t linesize = 0;
3156 6841da00 2019-08-08 stsp ssize_t linelen;
3157 6841da00 2019-08-08 stsp
3158 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3159 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3160 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3161 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3162 6841da00 2019-08-08 stsp
3163 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3164 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3165 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3166 bd8de430 2019-10-04 stsp
3167 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3168 bd8de430 2019-10-04 stsp if (line[0] == '#')
3169 bd8de430 2019-10-04 stsp continue;
3170 bd8de430 2019-10-04 stsp
3171 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3172 bd8de430 2019-10-04 stsp if (line[0] == '!')
3173 bd8de430 2019-10-04 stsp continue;
3174 bd8de430 2019-10-04 stsp
3175 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3176 6841da00 2019-08-08 stsp line) == -1) {
3177 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3178 6841da00 2019-08-08 stsp goto done;
3179 6841da00 2019-08-08 stsp }
3180 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3181 6841da00 2019-08-08 stsp if (err)
3182 6841da00 2019-08-08 stsp goto done;
3183 6841da00 2019-08-08 stsp }
3184 6841da00 2019-08-08 stsp if (ferror(f)) {
3185 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3186 6841da00 2019-08-08 stsp goto done;
3187 6841da00 2019-08-08 stsp }
3188 6841da00 2019-08-08 stsp
3189 6841da00 2019-08-08 stsp dirpath = strdup(path);
3190 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3191 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3192 6841da00 2019-08-08 stsp goto done;
3193 6841da00 2019-08-08 stsp }
3194 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3195 6841da00 2019-08-08 stsp done:
3196 6841da00 2019-08-08 stsp free(line);
3197 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3198 6841da00 2019-08-08 stsp free(dirpath);
3199 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3200 6841da00 2019-08-08 stsp }
3201 6841da00 2019-08-08 stsp return err;
3202 6841da00 2019-08-08 stsp }
3203 6841da00 2019-08-08 stsp
3204 6841da00 2019-08-08 stsp int
3205 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3206 6841da00 2019-08-08 stsp {
3207 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3208 bd8de430 2019-10-04 stsp
3209 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3210 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3211 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3212 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3213 bd8de430 2019-10-04 stsp
3214 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3215 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3216 6841da00 2019-08-08 stsp
3217 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3218 bd8de430 2019-10-04 stsp continue;
3219 bd8de430 2019-10-04 stsp pattern += 3;
3220 bd8de430 2019-10-04 stsp p = path;
3221 bd8de430 2019-10-04 stsp while (*p) {
3222 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3223 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3224 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3225 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3226 bd8de430 2019-10-04 stsp p++;
3227 bd8de430 2019-10-04 stsp while (*p == '/')
3228 bd8de430 2019-10-04 stsp p++;
3229 bd8de430 2019-10-04 stsp continue;
3230 bd8de430 2019-10-04 stsp }
3231 bd8de430 2019-10-04 stsp return 1;
3232 bd8de430 2019-10-04 stsp }
3233 bd8de430 2019-10-04 stsp }
3234 bd8de430 2019-10-04 stsp }
3235 bd8de430 2019-10-04 stsp
3236 6841da00 2019-08-08 stsp /*
3237 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3238 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3239 6841da00 2019-08-08 stsp * ignores backwards.
3240 6841da00 2019-08-08 stsp */
3241 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3242 6841da00 2019-08-08 stsp while (pe) {
3243 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3244 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3245 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3246 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3247 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3248 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3249 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3250 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3251 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3252 6841da00 2019-08-08 stsp continue;
3253 6841da00 2019-08-08 stsp return 1;
3254 6841da00 2019-08-08 stsp }
3255 6841da00 2019-08-08 stsp }
3256 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3257 6841da00 2019-08-08 stsp }
3258 6841da00 2019-08-08 stsp
3259 6841da00 2019-08-08 stsp return 0;
3260 6841da00 2019-08-08 stsp }
3261 6841da00 2019-08-08 stsp
3262 6841da00 2019-08-08 stsp static const struct got_error *
3263 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3264 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3265 6841da00 2019-08-08 stsp {
3266 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3267 6841da00 2019-08-08 stsp char *ignorespath;
3268 886cec17 2019-12-15 stsp int fd = -1;
3269 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3270 6841da00 2019-08-08 stsp
3271 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3272 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3273 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3274 6841da00 2019-08-08 stsp
3275 886cec17 2019-12-15 stsp if (dirfd != -1) {
3276 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3277 886cec17 2019-12-15 stsp if (fd == -1) {
3278 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3279 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3280 886cec17 2019-12-15 stsp ignorespath);
3281 886cec17 2019-12-15 stsp } else {
3282 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3283 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3284 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3285 886cec17 2019-12-15 stsp ignorespath);
3286 886cec17 2019-12-15 stsp else {
3287 886cec17 2019-12-15 stsp fd = -1;
3288 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3289 886cec17 2019-12-15 stsp }
3290 886cec17 2019-12-15 stsp }
3291 886cec17 2019-12-15 stsp } else {
3292 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3293 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3294 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3295 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3296 886cec17 2019-12-15 stsp ignorespath);
3297 886cec17 2019-12-15 stsp } else
3298 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3299 886cec17 2019-12-15 stsp }
3300 6841da00 2019-08-08 stsp
3301 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3302 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3303 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3304 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3305 6841da00 2019-08-08 stsp free(ignorespath);
3306 6841da00 2019-08-08 stsp return err;
3307 f8d1f275 2019-02-04 stsp }
3308 f8d1f275 2019-02-04 stsp
3309 f8d1f275 2019-02-04 stsp static const struct got_error *
3310 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3311 f8d1f275 2019-02-04 stsp {
3312 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3313 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3314 f8d1f275 2019-02-04 stsp char *path = NULL;
3315 f8d1f275 2019-02-04 stsp
3316 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3317 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3318 0584f854 2019-04-06 stsp
3319 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3320 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3321 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3322 f8d1f275 2019-02-04 stsp } else {
3323 f8d1f275 2019-02-04 stsp path = de->d_name;
3324 f8d1f275 2019-02-04 stsp }
3325 f8d1f275 2019-02-04 stsp
3326 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3327 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3328 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3329 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3330 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3331 f8d1f275 2019-02-04 stsp if (parent_path[0])
3332 f8d1f275 2019-02-04 stsp free(path);
3333 b72f483a 2019-02-05 stsp return err;
3334 f8d1f275 2019-02-04 stsp }
3335 f8d1f275 2019-02-04 stsp
3336 347d1d3e 2019-07-12 stsp static const struct got_error *
3337 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3338 3143d852 2020-06-25 stsp {
3339 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3340 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3341 3143d852 2020-06-25 stsp
3342 3143d852 2020-06-25 stsp if (a->no_ignores)
3343 3143d852 2020-06-25 stsp return NULL;
3344 3143d852 2020-06-25 stsp
3345 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3346 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3347 3143d852 2020-06-25 stsp if (err)
3348 3143d852 2020-06-25 stsp return err;
3349 3143d852 2020-06-25 stsp
3350 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3351 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3352 3143d852 2020-06-25 stsp
3353 3143d852 2020-06-25 stsp return err;
3354 3143d852 2020-06-25 stsp }
3355 3143d852 2020-06-25 stsp
3356 3143d852 2020-06-25 stsp static const struct got_error *
3357 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3358 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3359 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3360 abb4604f 2019-07-27 stsp {
3361 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3362 abb4604f 2019-07-27 stsp struct stat sb;
3363 abb4604f 2019-07-27 stsp
3364 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3365 abb4604f 2019-07-27 stsp if (ie)
3366 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3367 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3368 abb4604f 2019-07-27 stsp
3369 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3370 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3371 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3372 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3373 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3374 abb4604f 2019-07-27 stsp return NULL;
3375 abb4604f 2019-07-27 stsp }
3376 abb4604f 2019-07-27 stsp
3377 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3378 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3379 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3380 abb4604f 2019-07-27 stsp
3381 abb4604f 2019-07-27 stsp return NULL;
3382 3143d852 2020-06-25 stsp }
3383 3143d852 2020-06-25 stsp
3384 3143d852 2020-06-25 stsp static const struct got_error *
3385 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3386 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3387 3143d852 2020-06-25 stsp {
3388 3143d852 2020-06-25 stsp const struct got_error *err;
3389 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3390 3143d852 2020-06-25 stsp
3391 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3392 3143d852 2020-06-25 stsp ".cvsignore");
3393 3143d852 2020-06-25 stsp if (err)
3394 3143d852 2020-06-25 stsp return err;
3395 3143d852 2020-06-25 stsp
3396 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3397 3143d852 2020-06-25 stsp ".gitignore");
3398 3143d852 2020-06-25 stsp if (err)
3399 3143d852 2020-06-25 stsp return err;
3400 3143d852 2020-06-25 stsp
3401 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3402 3143d852 2020-06-25 stsp if (err) {
3403 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3404 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3405 3143d852 2020-06-25 stsp return err;
3406 3143d852 2020-06-25 stsp }
3407 3143d852 2020-06-25 stsp for (;;) {
3408 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3409 3143d852 2020-06-25 stsp ".cvsignore");
3410 3143d852 2020-06-25 stsp if (err)
3411 3143d852 2020-06-25 stsp break;
3412 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3413 3143d852 2020-06-25 stsp ".gitignore");
3414 3143d852 2020-06-25 stsp if (err)
3415 3143d852 2020-06-25 stsp break;
3416 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3417 3143d852 2020-06-25 stsp if (err) {
3418 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3419 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3420 3143d852 2020-06-25 stsp break;
3421 3143d852 2020-06-25 stsp }
3422 b737c85a 2020-06-26 stsp free(parent_path);
3423 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3424 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3425 3143d852 2020-06-25 stsp }
3426 3143d852 2020-06-25 stsp
3427 b737c85a 2020-06-26 stsp free(parent_path);
3428 b737c85a 2020-06-26 stsp free(next_parent_path);
3429 3143d852 2020-06-25 stsp return err;
3430 abb4604f 2019-07-27 stsp }
3431 abb4604f 2019-07-27 stsp
3432 abb4604f 2019-07-27 stsp static const struct got_error *
3433 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3434 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3435 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3436 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3437 f2a9dc41 2019-12-13 tracey int report_unchanged)
3438 f8d1f275 2019-02-04 stsp {
3439 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3440 6fc93f37 2019-12-13 stsp int fd = -1;
3441 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3442 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3443 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3444 3143d852 2020-06-25 stsp
3445 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3446 f8d1f275 2019-02-04 stsp
3447 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3448 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3449 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3450 8dc303cc 2019-07-27 stsp
3451 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3452 6fc93f37 2019-12-13 stsp if (fd == -1) {
3453 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3454 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3455 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3456 abb4604f 2019-07-27 stsp else
3457 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3458 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3459 f2a9dc41 2019-12-13 tracey report_unchanged);
3460 abb4604f 2019-07-27 stsp } else {
3461 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3462 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3463 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3464 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3465 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3466 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3467 abb4604f 2019-07-27 stsp arg.status_path = path;
3468 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3469 abb4604f 2019-07-27 stsp arg.repo = repo;
3470 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3471 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3472 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3473 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3474 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3475 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3476 022fae89 2019-12-06 tracey if (!no_ignores) {
3477 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3478 3143d852 2020-06-25 stsp worktree->root_path, path);
3479 3143d852 2020-06-25 stsp if (err)
3480 3143d852 2020-06-25 stsp goto done;
3481 022fae89 2019-12-06 tracey }
3482 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3483 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3484 927df6b7 2019-02-10 stsp }
3485 3143d852 2020-06-25 stsp done:
3486 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3487 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3488 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3489 927df6b7 2019-02-10 stsp free(ondisk_path);
3490 347d1d3e 2019-07-12 stsp return err;
3491 347d1d3e 2019-07-12 stsp }
3492 347d1d3e 2019-07-12 stsp
3493 347d1d3e 2019-07-12 stsp const struct got_error *
3494 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3495 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3496 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3497 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3498 347d1d3e 2019-07-12 stsp {
3499 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3500 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3501 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3502 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3503 347d1d3e 2019-07-12 stsp
3504 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3505 347d1d3e 2019-07-12 stsp if (err)
3506 347d1d3e 2019-07-12 stsp return err;
3507 347d1d3e 2019-07-12 stsp
3508 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3509 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3510 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3511 72ea6654 2019-07-27 stsp if (err)
3512 72ea6654 2019-07-27 stsp break;
3513 72ea6654 2019-07-27 stsp }
3514 f8d1f275 2019-02-04 stsp free(fileindex_path);
3515 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3516 f8d1f275 2019-02-04 stsp return err;
3517 f8d1f275 2019-02-04 stsp }
3518 6c7ab921 2019-03-18 stsp
3519 6c7ab921 2019-03-18 stsp const struct got_error *
3520 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3521 6c7ab921 2019-03-18 stsp const char *arg)
3522 6c7ab921 2019-03-18 stsp {
3523 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3524 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3525 6c7ab921 2019-03-18 stsp size_t len;
3526 00bb5ea0 2020-07-23 stsp struct stat sb;
3527 6c7ab921 2019-03-18 stsp
3528 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3529 6c7ab921 2019-03-18 stsp
3530 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3531 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3532 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3533 00bb5ea0 2020-07-23 stsp
3534 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3535 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3536 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3537 00bb5ea0 2020-07-23 stsp goto done;
3538 00bb5ea0 2020-07-23 stsp }
3539 00bb5ea0 2020-07-23 stsp }
3540 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3541 00bb5ea0 2020-07-23 stsp /*
3542 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3543 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3544 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3545 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3546 00bb5ea0 2020-07-23 stsp */
3547 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3548 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3549 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3550 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3551 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3552 00bb5ea0 2020-07-23 stsp goto done;
3553 00bb5ea0 2020-07-23 stsp }
3554 00bb5ea0 2020-07-23 stsp
3555 00bb5ea0 2020-07-23 stsp }
3556 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3557 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3558 00bb5ea0 2020-07-23 stsp if (err)
3559 d0710d08 2019-07-22 stsp goto done;
3560 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3561 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3562 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3563 00bb5ea0 2020-07-23 stsp goto done;
3564 00bb5ea0 2020-07-23 stsp }
3565 00bb5ea0 2020-07-23 stsp } else {
3566 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3567 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3568 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3569 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3570 00bb5ea0 2020-07-23 stsp goto done;
3571 00bb5ea0 2020-07-23 stsp }
3572 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3573 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3574 00bb5ea0 2020-07-23 stsp goto done;
3575 00bb5ea0 2020-07-23 stsp }
3576 d0710d08 2019-07-22 stsp }
3577 d0710d08 2019-07-22 stsp }
3578 6c7ab921 2019-03-18 stsp
3579 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3580 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3581 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3582 6c7ab921 2019-03-18 stsp goto done;
3583 6c7ab921 2019-03-18 stsp }
3584 6c7ab921 2019-03-18 stsp
3585 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3586 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3587 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3588 f6d88e1a 2019-05-29 stsp if (err)
3589 f6d88e1a 2019-05-29 stsp goto done;
3590 f6d88e1a 2019-05-29 stsp } else {
3591 f6d88e1a 2019-05-29 stsp path = strdup("");
3592 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3593 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3594 f6d88e1a 2019-05-29 stsp goto done;
3595 f6d88e1a 2019-05-29 stsp }
3596 6c7ab921 2019-03-18 stsp }
3597 6c7ab921 2019-03-18 stsp
3598 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3599 6c7ab921 2019-03-18 stsp len = strlen(path);
3600 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3601 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3602 6c7ab921 2019-03-18 stsp len--;
3603 6c7ab921 2019-03-18 stsp }
3604 6c7ab921 2019-03-18 stsp done:
3605 6c7ab921 2019-03-18 stsp free(resolved);
3606 d0710d08 2019-07-22 stsp free(cwd);
3607 6c7ab921 2019-03-18 stsp if (err == NULL)
3608 6c7ab921 2019-03-18 stsp *wt_path = path;
3609 6c7ab921 2019-03-18 stsp else
3610 6c7ab921 2019-03-18 stsp free(path);
3611 d00136be 2019-03-26 stsp return err;
3612 d00136be 2019-03-26 stsp }
3613 d00136be 2019-03-26 stsp
3614 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3615 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3616 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3617 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3618 4e68cba3 2019-11-23 stsp void *progress_arg;
3619 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3620 4e68cba3 2019-11-23 stsp };
3621 4e68cba3 2019-11-23 stsp
3622 4e68cba3 2019-11-23 stsp static const struct got_error *
3623 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3624 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3625 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3626 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3627 07f5b47a 2019-06-02 stsp {
3628 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3629 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3630 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3631 6d022e97 2019-08-04 stsp struct stat sb;
3632 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3633 07f5b47a 2019-06-02 stsp
3634 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3635 dbb83fbd 2019-12-12 stsp relpath) == -1)
3636 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3637 dbb83fbd 2019-12-12 stsp
3638 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3639 6d022e97 2019-08-04 stsp if (ie) {
3640 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3641 12463d8b 2019-12-13 stsp de_name, a->repo);
3642 6d022e97 2019-08-04 stsp if (err)
3643 dbb83fbd 2019-12-12 stsp goto done;
3644 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3645 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3646 dbb83fbd 2019-12-12 stsp goto done;
3647 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3648 dbb83fbd 2019-12-12 stsp if (err)
3649 dbb83fbd 2019-12-12 stsp goto done;
3650 6d022e97 2019-08-04 stsp }
3651 07f5b47a 2019-06-02 stsp
3652 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3653 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3654 dbb83fbd 2019-12-12 stsp goto done;
3655 4e68cba3 2019-11-23 stsp }
3656 07f5b47a 2019-06-02 stsp
3657 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3658 4e68cba3 2019-11-23 stsp if (err)
3659 4e68cba3 2019-11-23 stsp goto done;
3660 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3661 3969253a 2020-03-07 stsp if (err) {
3662 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3663 3969253a 2020-03-07 stsp goto done;
3664 3969253a 2020-03-07 stsp }
3665 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3666 07f5b47a 2019-06-02 stsp if (err) {
3667 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3668 4e68cba3 2019-11-23 stsp goto done;
3669 07f5b47a 2019-06-02 stsp }
3670 4e68cba3 2019-11-23 stsp done:
3671 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3672 dbb83fbd 2019-12-12 stsp if (err)
3673 dbb83fbd 2019-12-12 stsp return err;
3674 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3675 dbb83fbd 2019-12-12 stsp return NULL;
3676 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3677 07f5b47a 2019-06-02 stsp }
3678 07f5b47a 2019-06-02 stsp
3679 d00136be 2019-03-26 stsp const struct got_error *
3680 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3681 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3682 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3683 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3684 d00136be 2019-03-26 stsp {
3685 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3686 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3687 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3688 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3689 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3690 d00136be 2019-03-26 stsp
3691 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3692 d00136be 2019-03-26 stsp if (err)
3693 d00136be 2019-03-26 stsp return err;
3694 d00136be 2019-03-26 stsp
3695 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3696 d00136be 2019-03-26 stsp if (err)
3697 d00136be 2019-03-26 stsp goto done;
3698 d00136be 2019-03-26 stsp
3699 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3700 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3701 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3702 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3703 4e68cba3 2019-11-23 stsp saa.repo = repo;
3704 4e68cba3 2019-11-23 stsp
3705 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3706 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3707 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3708 1dd54920 2019-05-11 stsp if (err)
3709 af12c6b9 2019-06-04 stsp break;
3710 1dd54920 2019-05-11 stsp }
3711 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3712 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3713 af12c6b9 2019-06-04 stsp err = sync_err;
3714 d00136be 2019-03-26 stsp done:
3715 fb399478 2019-07-12 stsp free(fileindex_path);
3716 d00136be 2019-03-26 stsp if (fileindex)
3717 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3718 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3719 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3720 d00136be 2019-03-26 stsp err = unlockerr;
3721 6c7ab921 2019-03-18 stsp return err;
3722 6c7ab921 2019-03-18 stsp }
3723 17ed4618 2019-06-02 stsp
3724 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3725 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3726 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3727 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3728 f2a9dc41 2019-12-13 tracey void *progress_arg;
3729 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3730 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3731 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3732 f2a9dc41 2019-12-13 tracey };
3733 f2a9dc41 2019-12-13 tracey
3734 f2a9dc41 2019-12-13 tracey static const struct got_error *
3735 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3736 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3737 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3738 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3739 17ed4618 2019-06-02 stsp {
3740 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3741 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3742 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3743 17ed4618 2019-06-02 stsp struct stat sb;
3744 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3745 17ed4618 2019-06-02 stsp
3746 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3747 17ed4618 2019-06-02 stsp if (ie == NULL)
3748 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3749 2ec1f75b 2019-03-26 stsp
3750 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3751 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3752 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3753 9acbc4fa 2019-08-03 stsp return NULL;
3754 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3755 9acbc4fa 2019-08-03 stsp }
3756 9acbc4fa 2019-08-03 stsp
3757 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3758 f2a9dc41 2019-12-13 tracey relpath) == -1)
3759 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3760 f2a9dc41 2019-12-13 tracey
3761 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3762 12463d8b 2019-12-13 stsp a->repo);
3763 17ed4618 2019-06-02 stsp if (err)
3764 f2a9dc41 2019-12-13 tracey goto done;
3765 17ed4618 2019-06-02 stsp
3766 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3767 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3768 f2a9dc41 2019-12-13 tracey goto done;
3769 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3770 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3771 f2a9dc41 2019-12-13 tracey goto done;
3772 f2a9dc41 2019-12-13 tracey }
3773 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3774 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3775 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3776 f2a9dc41 2019-12-13 tracey goto done;
3777 f2a9dc41 2019-12-13 tracey }
3778 17ed4618 2019-06-02 stsp }
3779 17ed4618 2019-06-02 stsp
3780 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3781 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3782 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3783 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3784 12463d8b 2019-12-13 stsp ondisk_path);
3785 12463d8b 2019-12-13 stsp goto done;
3786 12463d8b 2019-12-13 stsp }
3787 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3788 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3789 12463d8b 2019-12-13 stsp goto done;
3790 15341bfd 2020-03-05 tracey }
3791 15341bfd 2020-03-05 tracey
3792 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3793 15341bfd 2020-03-05 tracey
3794 15341bfd 2020-03-05 tracey if (parent == NULL) {
3795 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3796 15341bfd 2020-03-05 tracey goto done;
3797 15341bfd 2020-03-05 tracey }
3798 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3799 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3800 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3801 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3802 15341bfd 2020-03-05 tracey parent);
3803 15341bfd 2020-03-05 tracey break;
3804 15341bfd 2020-03-05 tracey }
3805 15341bfd 2020-03-05 tracey parent = dirname(parent);
3806 15341bfd 2020-03-05 tracey if (parent == NULL) {
3807 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3808 15341bfd 2020-03-05 tracey goto done;
3809 15341bfd 2020-03-05 tracey }
3810 12463d8b 2019-12-13 stsp }
3811 f2a9dc41 2019-12-13 tracey }
3812 17ed4618 2019-06-02 stsp
3813 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3814 f2a9dc41 2019-12-13 tracey done:
3815 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3816 f2a9dc41 2019-12-13 tracey if (err)
3817 f2a9dc41 2019-12-13 tracey return err;
3818 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3819 f2a9dc41 2019-12-13 tracey return NULL;
3820 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3821 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3822 17ed4618 2019-06-02 stsp }
3823 17ed4618 2019-06-02 stsp
3824 2ec1f75b 2019-03-26 stsp const struct got_error *
3825 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3826 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3827 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3828 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3829 2ec1f75b 2019-03-26 stsp {
3830 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3831 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3832 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3833 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3834 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3835 2ec1f75b 2019-03-26 stsp
3836 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3837 2ec1f75b 2019-03-26 stsp if (err)
3838 2ec1f75b 2019-03-26 stsp return err;
3839 2ec1f75b 2019-03-26 stsp
3840 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3841 2ec1f75b 2019-03-26 stsp if (err)
3842 2ec1f75b 2019-03-26 stsp goto done;
3843 f2a9dc41 2019-12-13 tracey
3844 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3845 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3846 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3847 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3848 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3849 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3850 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3851 2ec1f75b 2019-03-26 stsp
3852 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3853 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3854 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3855 17ed4618 2019-06-02 stsp if (err)
3856 af12c6b9 2019-06-04 stsp break;
3857 2ec1f75b 2019-03-26 stsp }
3858 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3859 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3860 af12c6b9 2019-06-04 stsp err = sync_err;
3861 2ec1f75b 2019-03-26 stsp done:
3862 fb399478 2019-07-12 stsp free(fileindex_path);
3863 2ec1f75b 2019-03-26 stsp if (fileindex)
3864 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3865 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3866 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3867 2ec1f75b 2019-03-26 stsp err = unlockerr;
3868 33aa809d 2019-08-08 stsp return err;
3869 33aa809d 2019-08-08 stsp }
3870 33aa809d 2019-08-08 stsp
3871 33aa809d 2019-08-08 stsp static const struct got_error *
3872 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3873 33aa809d 2019-08-08 stsp {
3874 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3875 33aa809d 2019-08-08 stsp char *line = NULL;
3876 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3877 33aa809d 2019-08-08 stsp ssize_t linelen;
3878 33aa809d 2019-08-08 stsp
3879 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3880 33aa809d 2019-08-08 stsp if (linelen == -1) {
3881 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3882 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3883 33aa809d 2019-08-08 stsp goto done;
3884 33aa809d 2019-08-08 stsp }
3885 33aa809d 2019-08-08 stsp return NULL;
3886 33aa809d 2019-08-08 stsp }
3887 33aa809d 2019-08-08 stsp if (outfile) {
3888 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3889 33aa809d 2019-08-08 stsp if (n != linelen) {
3890 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3891 33aa809d 2019-08-08 stsp goto done;
3892 33aa809d 2019-08-08 stsp }
3893 33aa809d 2019-08-08 stsp }
3894 33aa809d 2019-08-08 stsp if (rejectfile) {
3895 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3896 33aa809d 2019-08-08 stsp if (n != linelen)
3897 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3898 33aa809d 2019-08-08 stsp }
3899 33aa809d 2019-08-08 stsp done:
3900 33aa809d 2019-08-08 stsp free(line);
3901 2ec1f75b 2019-03-26 stsp return err;
3902 2ec1f75b 2019-03-26 stsp }
3903 1f1abb7e 2019-08-08 stsp
3904 33aa809d 2019-08-08 stsp static const struct got_error *
3905 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3906 33aa809d 2019-08-08 stsp {
3907 33aa809d 2019-08-08 stsp char *line = NULL;
3908 33aa809d 2019-08-08 stsp size_t linesize = 0;
3909 33aa809d 2019-08-08 stsp ssize_t linelen;
3910 33aa809d 2019-08-08 stsp
3911 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3912 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3913 500467ff 2019-09-25 hiltjo if (ferror(f))
3914 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3915 500467ff 2019-09-25 hiltjo return NULL;
3916 500467ff 2019-09-25 hiltjo }
3917 33aa809d 2019-08-08 stsp free(line);
3918 33aa809d 2019-08-08 stsp return NULL;
3919 33aa809d 2019-08-08 stsp }
3920 33aa809d 2019-08-08 stsp
3921 33aa809d 2019-08-08 stsp static const struct got_error *
3922 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3923 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3924 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3925 33aa809d 2019-08-08 stsp {
3926 33aa809d 2019-08-08 stsp const struct got_error *err;
3927 33aa809d 2019-08-08 stsp
3928 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3929 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3930 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3931 33aa809d 2019-08-08 stsp if (err)
3932 33aa809d 2019-08-08 stsp return err;
3933 33aa809d 2019-08-08 stsp (*line_cur1)++;
3934 33aa809d 2019-08-08 stsp }
3935 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3936 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3937 33aa809d 2019-08-08 stsp if (rejectfile)
3938 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3939 33aa809d 2019-08-08 stsp else
3940 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3941 33aa809d 2019-08-08 stsp if (err)
3942 33aa809d 2019-08-08 stsp return err;
3943 33aa809d 2019-08-08 stsp (*line_cur2)++;
3944 33aa809d 2019-08-08 stsp }
3945 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3946 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3947 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3948 33aa809d 2019-08-08 stsp if (err)
3949 33aa809d 2019-08-08 stsp return err;
3950 33aa809d 2019-08-08 stsp (*line_cur2)++;
3951 33aa809d 2019-08-08 stsp }
3952 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3953 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3954 33aa809d 2019-08-08 stsp if (rejectfile)
3955 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3956 33aa809d 2019-08-08 stsp else
3957 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3958 33aa809d 2019-08-08 stsp if (err)
3959 33aa809d 2019-08-08 stsp return err;
3960 33aa809d 2019-08-08 stsp (*line_cur1)++;
3961 33aa809d 2019-08-08 stsp }
3962 f1e81a05 2019-08-10 stsp
3963 f1e81a05 2019-08-10 stsp return NULL;
3964 f1e81a05 2019-08-10 stsp }
3965 f1e81a05 2019-08-10 stsp
3966 f1e81a05 2019-08-10 stsp static const struct got_error *
3967 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3968 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3969 f1e81a05 2019-08-10 stsp {
3970 f1e81a05 2019-08-10 stsp const struct got_error *err;
3971 f1e81a05 2019-08-10 stsp
3972 f1e81a05 2019-08-10 stsp if (outfile) {
3973 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3974 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3975 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3976 f1e81a05 2019-08-10 stsp if (err)
3977 f1e81a05 2019-08-10 stsp return err;
3978 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3979 f1e81a05 2019-08-10 stsp }
3980 f1e81a05 2019-08-10 stsp }
3981 f1e81a05 2019-08-10 stsp if (rejectfile) {
3982 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3983 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3984 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3985 f1e81a05 2019-08-10 stsp if (err)
3986 f1e81a05 2019-08-10 stsp return err;
3987 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3988 f1e81a05 2019-08-10 stsp }
3989 33aa809d 2019-08-08 stsp }
3990 33aa809d 2019-08-08 stsp
3991 33aa809d 2019-08-08 stsp return NULL;
3992 33aa809d 2019-08-08 stsp }
3993 33aa809d 2019-08-08 stsp
3994 33aa809d 2019-08-08 stsp static const struct got_error *
3995 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3996 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3997 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3998 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3999 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4000 33aa809d 2019-08-08 stsp {
4001 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4002 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4003 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4004 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4005 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4006 33aa809d 2019-08-08 stsp long pos1, pos2;
4007 33aa809d 2019-08-08 stsp FILE *hunkfile;
4008 33aa809d 2019-08-08 stsp
4009 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4010 33aa809d 2019-08-08 stsp
4011 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4012 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4013 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4014 33aa809d 2019-08-08 stsp
4015 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4016 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4017 33aa809d 2019-08-08 stsp
4018 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4019 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4020 33aa809d 2019-08-08 stsp
4021 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4022 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4023 33aa809d 2019-08-08 stsp goto done;
4024 33aa809d 2019-08-08 stsp }
4025 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4026 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4027 33aa809d 2019-08-08 stsp goto done;
4028 33aa809d 2019-08-08 stsp }
4029 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4030 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4031 33aa809d 2019-08-08 stsp goto done;
4032 33aa809d 2019-08-08 stsp }
4033 33aa809d 2019-08-08 stsp
4034 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4035 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4036 33aa809d 2019-08-08 stsp if (err)
4037 33aa809d 2019-08-08 stsp goto done;
4038 33aa809d 2019-08-08 stsp
4039 33aa809d 2019-08-08 stsp switch (*choice) {
4040 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4041 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4042 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4043 33aa809d 2019-08-08 stsp break;
4044 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4045 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4046 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4047 33aa809d 2019-08-08 stsp break;
4048 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4049 33aa809d 2019-08-08 stsp break;
4050 33aa809d 2019-08-08 stsp default:
4051 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4052 33aa809d 2019-08-08 stsp break;
4053 33aa809d 2019-08-08 stsp }
4054 33aa809d 2019-08-08 stsp done:
4055 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4056 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4057 33aa809d 2019-08-08 stsp return err;
4058 33aa809d 2019-08-08 stsp }
4059 33aa809d 2019-08-08 stsp
4060 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4061 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4062 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4063 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4064 1f1abb7e 2019-08-08 stsp void *progress_arg;
4065 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4066 33aa809d 2019-08-08 stsp void *patch_arg;
4067 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4068 1f1abb7e 2019-08-08 stsp };
4069 a129376b 2019-03-28 stsp
4070 e20a8b6f 2019-06-04 stsp static const struct got_error *
4071 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4072 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4073 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4074 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4075 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4076 33aa809d 2019-08-08 stsp {
4077 33aa809d 2019-08-08 stsp const struct got_error *err;
4078 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4079 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4080 1ebedb77 2019-10-19 stsp int fd2 = -1;
4081 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4082 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4083 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4084 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4085 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4086 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4087 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4088 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4089 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4090 33aa809d 2019-08-08 stsp int n = 0;
4091 33aa809d 2019-08-08 stsp
4092 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4093 33aa809d 2019-08-08 stsp
4094 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4095 33aa809d 2019-08-08 stsp if (err)
4096 33aa809d 2019-08-08 stsp return err;
4097 33aa809d 2019-08-08 stsp
4098 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4099 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4100 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4101 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4102 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4103 fa3cef63 2020-07-23 stsp goto done;
4104 fa3cef63 2020-07-23 stsp }
4105 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4106 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4107 fa3cef63 2020-07-23 stsp if (link_len == -1)
4108 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4109 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4110 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4111 12463d8b 2019-12-13 stsp }
4112 12463d8b 2019-12-13 stsp } else {
4113 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4114 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4115 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4116 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4117 fa3cef63 2020-07-23 stsp goto done;
4118 fa3cef63 2020-07-23 stsp }
4119 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4120 fa3cef63 2020-07-23 stsp sizeof(link_target));
4121 fa3cef63 2020-07-23 stsp if (link_len == -1)
4122 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4123 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4124 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4125 12463d8b 2019-12-13 stsp }
4126 1ebedb77 2019-10-19 stsp }
4127 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4128 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4129 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4130 fa3cef63 2020-07-23 stsp goto done;
4131 fa3cef63 2020-07-23 stsp }
4132 1ebedb77 2019-10-19 stsp
4133 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4134 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4135 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4136 fa3cef63 2020-07-23 stsp goto done;
4137 fa3cef63 2020-07-23 stsp }
4138 fa3cef63 2020-07-23 stsp fd2 = -1;
4139 fa3cef63 2020-07-23 stsp } else {
4140 fa3cef63 2020-07-23 stsp size_t n;
4141 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4142 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4143 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4144 fa3cef63 2020-07-23 stsp goto done;
4145 fa3cef63 2020-07-23 stsp }
4146 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4147 fa3cef63 2020-07-23 stsp if (n != link_len) {
4148 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4149 fa3cef63 2020-07-23 stsp goto done;
4150 fa3cef63 2020-07-23 stsp }
4151 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4152 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4153 fa3cef63 2020-07-23 stsp goto done;
4154 fa3cef63 2020-07-23 stsp }
4155 fa3cef63 2020-07-23 stsp rewind(f2);
4156 33aa809d 2019-08-08 stsp }
4157 33aa809d 2019-08-08 stsp
4158 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4159 33aa809d 2019-08-08 stsp if (err)
4160 33aa809d 2019-08-08 stsp goto done;
4161 33aa809d 2019-08-08 stsp
4162 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4163 33aa809d 2019-08-08 stsp if (err)
4164 33aa809d 2019-08-08 stsp goto done;
4165 33aa809d 2019-08-08 stsp
4166 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4167 33aa809d 2019-08-08 stsp if (err)
4168 33aa809d 2019-08-08 stsp goto done;
4169 33aa809d 2019-08-08 stsp
4170 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4171 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4172 33aa809d 2019-08-08 stsp goto done;
4173 33aa809d 2019-08-08 stsp }
4174 33aa809d 2019-08-08 stsp
4175 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4176 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4177 33aa809d 2019-08-08 stsp if (err)
4178 33aa809d 2019-08-08 stsp goto done;
4179 33aa809d 2019-08-08 stsp
4180 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4181 33aa809d 2019-08-08 stsp if (err)
4182 33aa809d 2019-08-08 stsp goto done;
4183 33aa809d 2019-08-08 stsp
4184 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4185 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4186 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4187 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4188 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4189 33aa809d 2019-08-08 stsp int choice;
4190 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4191 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4192 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4193 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4194 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4195 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4196 33aa809d 2019-08-08 stsp if (err)
4197 33aa809d 2019-08-08 stsp goto done;
4198 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4199 33aa809d 2019-08-08 stsp have_content = 1;
4200 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4201 33aa809d 2019-08-08 stsp break;
4202 33aa809d 2019-08-08 stsp }
4203 1ebedb77 2019-10-19 stsp if (have_content) {
4204 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4205 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4206 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4207 1ebedb77 2019-10-19 stsp if (err)
4208 1ebedb77 2019-10-19 stsp goto done;
4209 1ebedb77 2019-10-19 stsp
4210 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4211 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4212 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4213 fa3cef63 2020-07-23 stsp goto done;
4214 fa3cef63 2020-07-23 stsp }
4215 1ebedb77 2019-10-19 stsp }
4216 1ebedb77 2019-10-19 stsp }
4217 33aa809d 2019-08-08 stsp done:
4218 33aa809d 2019-08-08 stsp free(id_str);
4219 33aa809d 2019-08-08 stsp if (blob)
4220 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4221 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4222 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4223 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4224 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4225 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4226 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4227 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4228 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4229 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4230 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4231 33aa809d 2019-08-08 stsp if (err || !have_content) {
4232 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4233 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4234 33aa809d 2019-08-08 stsp free(*path_outfile);
4235 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4236 33aa809d 2019-08-08 stsp }
4237 33aa809d 2019-08-08 stsp free(args);
4238 33aa809d 2019-08-08 stsp if (ds) {
4239 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4240 33aa809d 2019-08-08 stsp free(ds);
4241 33aa809d 2019-08-08 stsp }
4242 33aa809d 2019-08-08 stsp if (changes)
4243 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4244 33aa809d 2019-08-08 stsp free(path1);
4245 33aa809d 2019-08-08 stsp return err;
4246 33aa809d 2019-08-08 stsp }
4247 33aa809d 2019-08-08 stsp
4248 33aa809d 2019-08-08 stsp static const struct got_error *
4249 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4250 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4251 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4252 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4253 a129376b 2019-03-28 stsp {
4254 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4255 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4256 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4257 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4258 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4259 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4260 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4261 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4262 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4263 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4264 a129376b 2019-03-28 stsp
4265 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4266 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4267 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4268 d3bcc3d1 2019-08-08 stsp return NULL;
4269 3d69ad8d 2019-08-17 semarie
4270 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4271 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4272 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4273 d3bcc3d1 2019-08-08 stsp
4274 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4275 65084dad 2019-08-08 stsp if (ie == NULL)
4276 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4277 a129376b 2019-03-28 stsp
4278 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4279 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4280 a129376b 2019-03-28 stsp if (err) {
4281 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4282 a129376b 2019-03-28 stsp goto done;
4283 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4284 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4285 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4286 e20a8b6f 2019-06-04 stsp goto done;
4287 e20a8b6f 2019-06-04 stsp }
4288 a129376b 2019-03-28 stsp }
4289 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4290 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4291 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4292 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4293 a129376b 2019-03-28 stsp goto done;
4294 a129376b 2019-03-28 stsp }
4295 a129376b 2019-03-28 stsp } else {
4296 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4297 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4298 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4299 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4300 a129376b 2019-03-28 stsp goto done;
4301 a129376b 2019-03-28 stsp }
4302 a129376b 2019-03-28 stsp } else {
4303 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4304 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4306 a129376b 2019-03-28 stsp goto done;
4307 a129376b 2019-03-28 stsp }
4308 a129376b 2019-03-28 stsp }
4309 a129376b 2019-03-28 stsp }
4310 a129376b 2019-03-28 stsp
4311 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4312 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4313 a9fa2909 2019-07-27 stsp if (err) {
4314 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4315 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4316 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4317 a9fa2909 2019-07-27 stsp goto done;
4318 a9fa2909 2019-07-27 stsp } else {
4319 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4320 a9fa2909 2019-07-27 stsp if (err)
4321 a9fa2909 2019-07-27 stsp goto done;
4322 a9fa2909 2019-07-27 stsp
4323 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4324 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4325 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4326 a9fa2909 2019-07-27 stsp goto done;
4327 a9fa2909 2019-07-27 stsp }
4328 a9fa2909 2019-07-27 stsp
4329 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4330 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4331 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4332 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4333 a9fa2909 2019-07-27 stsp goto done;
4334 a9fa2909 2019-07-27 stsp }
4335 a129376b 2019-03-28 stsp }
4336 a129376b 2019-03-28 stsp
4337 a129376b 2019-03-28 stsp switch (status) {
4338 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4339 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4340 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4341 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4342 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4343 33aa809d 2019-08-08 stsp if (err)
4344 33aa809d 2019-08-08 stsp goto done;
4345 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4346 33aa809d 2019-08-08 stsp break;
4347 33aa809d 2019-08-08 stsp }
4348 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4349 1f1abb7e 2019-08-08 stsp ie->path);
4350 1ee397ad 2019-07-12 stsp if (err)
4351 1ee397ad 2019-07-12 stsp goto done;
4352 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4353 a129376b 2019-03-28 stsp break;
4354 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4355 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4356 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4357 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4358 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4359 33aa809d 2019-08-08 stsp if (err)
4360 33aa809d 2019-08-08 stsp goto done;
4361 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4362 33aa809d 2019-08-08 stsp break;
4363 33aa809d 2019-08-08 stsp }
4364 33aa809d 2019-08-08 stsp /* fall through */
4365 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4366 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4367 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4368 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4369 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4370 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4371 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4372 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4373 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4374 24278f30 2019-08-03 stsp } else
4375 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4376 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4377 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4378 a129376b 2019-03-28 stsp if (err)
4379 65084dad 2019-08-08 stsp goto done;
4380 65084dad 2019-08-08 stsp
4381 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4382 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4383 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4384 a129376b 2019-03-28 stsp goto done;
4385 65084dad 2019-08-08 stsp }
4386 65084dad 2019-08-08 stsp
4387 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4388 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4389 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4390 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4391 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4392 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4393 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4394 33aa809d 2019-08-08 stsp break;
4395 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4396 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4397 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4398 369fd7e5 2020-07-23 stsp path_content);
4399 369fd7e5 2020-07-23 stsp break;
4400 369fd7e5 2020-07-23 stsp }
4401 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4402 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4403 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4404 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4405 369fd7e5 2020-07-23 stsp } else {
4406 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4407 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4408 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4409 369fd7e5 2020-07-23 stsp goto done;
4410 369fd7e5 2020-07-23 stsp }
4411 33aa809d 2019-08-08 stsp }
4412 33aa809d 2019-08-08 stsp } else {
4413 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4414 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4415 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4416 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4417 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4418 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4419 2e1fa222 2020-07-23 stsp } else {
4420 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4421 2e1fa222 2020-07-23 stsp ie->path,
4422 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4423 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4424 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4425 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4426 2e1fa222 2020-07-23 stsp }
4427 a129376b 2019-03-28 stsp if (err)
4428 a129376b 2019-03-28 stsp goto done;
4429 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4430 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4431 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4432 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4433 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4434 2e1fa222 2020-07-23 stsp if (err)
4435 2e1fa222 2020-07-23 stsp goto done;
4436 2e1fa222 2020-07-23 stsp }
4437 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4438 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4439 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4440 33aa809d 2019-08-08 stsp }
4441 a129376b 2019-03-28 stsp }
4442 a129376b 2019-03-28 stsp break;
4443 e20a8b6f 2019-06-04 stsp }
4444 a129376b 2019-03-28 stsp default:
4445 1f1abb7e 2019-08-08 stsp break;
4446 a129376b 2019-03-28 stsp }
4447 a129376b 2019-03-28 stsp done:
4448 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4449 33aa809d 2019-08-08 stsp free(path_content);
4450 e20a8b6f 2019-06-04 stsp free(parent_path);
4451 a129376b 2019-03-28 stsp free(tree_path);
4452 a129376b 2019-03-28 stsp if (blob)
4453 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4454 a129376b 2019-03-28 stsp if (tree)
4455 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4456 a129376b 2019-03-28 stsp free(tree_id);
4457 e20a8b6f 2019-06-04 stsp return err;
4458 e20a8b6f 2019-06-04 stsp }
4459 e20a8b6f 2019-06-04 stsp
4460 e20a8b6f 2019-06-04 stsp const struct got_error *
4461 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4462 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4463 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4464 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4465 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4466 e20a8b6f 2019-06-04 stsp {
4467 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4468 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4469 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4470 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4471 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4472 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4473 e20a8b6f 2019-06-04 stsp
4474 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4475 e20a8b6f 2019-06-04 stsp if (err)
4476 e20a8b6f 2019-06-04 stsp return err;
4477 e20a8b6f 2019-06-04 stsp
4478 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4479 e20a8b6f 2019-06-04 stsp if (err)
4480 e20a8b6f 2019-06-04 stsp goto done;
4481 e20a8b6f 2019-06-04 stsp
4482 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4483 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4484 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4485 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4486 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4487 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4488 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4489 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4490 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4491 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4492 e20a8b6f 2019-06-04 stsp if (err)
4493 af12c6b9 2019-06-04 stsp break;
4494 e20a8b6f 2019-06-04 stsp }
4495 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4496 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4497 e20a8b6f 2019-06-04 stsp err = sync_err;
4498 af12c6b9 2019-06-04 stsp done:
4499 fb399478 2019-07-12 stsp free(fileindex_path);
4500 a129376b 2019-03-28 stsp if (fileindex)
4501 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4502 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4503 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4504 a129376b 2019-03-28 stsp err = unlockerr;
4505 c4296144 2019-05-09 stsp return err;
4506 c4296144 2019-05-09 stsp }
4507 c4296144 2019-05-09 stsp
4508 cf066bf8 2019-05-09 stsp static void
4509 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4510 cf066bf8 2019-05-09 stsp {
4511 24519714 2019-05-09 stsp free(ct->path);
4512 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4513 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4514 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4515 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4516 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4517 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4518 cf066bf8 2019-05-09 stsp free(ct);
4519 cf066bf8 2019-05-09 stsp }
4520 24519714 2019-05-09 stsp
4521 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4522 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4523 24519714 2019-05-09 stsp struct got_repository *repo;
4524 24519714 2019-05-09 stsp struct got_worktree *worktree;
4525 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4526 5f8a88c6 2019-08-03 stsp int have_staged_files;
4527 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4528 24519714 2019-05-09 stsp };
4529 cf066bf8 2019-05-09 stsp
4530 c4296144 2019-05-09 stsp static const struct got_error *
4531 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4532 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4533 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4534 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4535 c4296144 2019-05-09 stsp {
4536 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4537 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4538 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4539 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4540 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4541 768aea60 2019-05-09 stsp struct stat sb;
4542 c4296144 2019-05-09 stsp
4543 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4544 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4545 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4546 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4547 5f8a88c6 2019-08-03 stsp return NULL;
4548 5f8a88c6 2019-08-03 stsp } else {
4549 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4550 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4551 c4296144 2019-05-09 stsp
4552 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4553 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4554 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4555 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4556 5f8a88c6 2019-08-03 stsp return NULL;
4557 5f8a88c6 2019-08-03 stsp }
4558 0b5cc0d6 2019-05-09 stsp
4559 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4561 036813ee 2019-05-09 stsp goto done;
4562 036813ee 2019-05-09 stsp }
4563 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4564 036813ee 2019-05-09 stsp parent_path = strdup("");
4565 69960a46 2019-05-09 stsp if (parent_path == NULL)
4566 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4567 69960a46 2019-05-09 stsp } else {
4568 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4569 69960a46 2019-05-09 stsp if (err)
4570 69960a46 2019-05-09 stsp return err;
4571 69960a46 2019-05-09 stsp }
4572 c4296144 2019-05-09 stsp
4573 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4574 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4575 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4576 c4296144 2019-05-09 stsp goto done;
4577 768aea60 2019-05-09 stsp }
4578 768aea60 2019-05-09 stsp
4579 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4580 768aea60 2019-05-09 stsp relpath) == -1) {
4581 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4582 768aea60 2019-05-09 stsp goto done;
4583 768aea60 2019-05-09 stsp }
4584 0aeb8099 2020-07-23 stsp
4585 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4586 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4587 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4588 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4589 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4590 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4591 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4592 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4593 0aeb8099 2020-07-23 stsp break;
4594 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4595 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4596 0aeb8099 2020-07-23 stsp break;
4597 0aeb8099 2020-07-23 stsp default:
4598 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4599 0aeb8099 2020-07-23 stsp goto done;
4600 0aeb8099 2020-07-23 stsp }
4601 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4602 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4603 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4604 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4605 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4606 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4607 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4608 12463d8b 2019-12-13 stsp ct->ondisk_path);
4609 12463d8b 2019-12-13 stsp goto done;
4610 12463d8b 2019-12-13 stsp }
4611 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4612 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4613 768aea60 2019-05-09 stsp goto done;
4614 768aea60 2019-05-09 stsp }
4615 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4616 c4296144 2019-05-09 stsp }
4617 c4296144 2019-05-09 stsp
4618 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4619 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4620 44d03001 2019-05-09 stsp relpath) == -1) {
4621 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4622 44d03001 2019-05-09 stsp goto done;
4623 44d03001 2019-05-09 stsp }
4624 44d03001 2019-05-09 stsp
4625 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4626 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4627 35213c7c 2020-07-23 stsp int is_bad_symlink;
4628 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4629 35213c7c 2020-07-23 stsp ssize_t target_len;
4630 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4631 35213c7c 2020-07-23 stsp sizeof(target_path));
4632 35213c7c 2020-07-23 stsp if (target_len == -1) {
4633 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4634 35213c7c 2020-07-23 stsp ct->ondisk_path);
4635 35213c7c 2020-07-23 stsp goto done;
4636 35213c7c 2020-07-23 stsp }
4637 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4638 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4639 35213c7c 2020-07-23 stsp if (err)
4640 35213c7c 2020-07-23 stsp goto done;
4641 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4642 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4643 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4644 35213c7c 2020-07-23 stsp goto done;
4645 35213c7c 2020-07-23 stsp }
4646 35213c7c 2020-07-23 stsp }
4647 35213c7c 2020-07-23 stsp
4648 35213c7c 2020-07-23 stsp
4649 cf066bf8 2019-05-09 stsp ct->status = status;
4650 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4651 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4652 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4653 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4654 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4655 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4656 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4657 b416585c 2019-05-13 stsp goto done;
4658 b416585c 2019-05-13 stsp }
4659 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4660 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4661 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4662 f0b75401 2019-08-03 stsp goto done;
4663 f0b75401 2019-08-03 stsp }
4664 f0b75401 2019-08-03 stsp }
4665 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4666 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4667 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4668 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4669 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4670 036813ee 2019-05-09 stsp goto done;
4671 036813ee 2019-05-09 stsp }
4672 ca2503ea 2019-05-09 stsp }
4673 24519714 2019-05-09 stsp ct->path = strdup(path);
4674 24519714 2019-05-09 stsp if (ct->path == NULL) {
4675 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4676 24519714 2019-05-09 stsp goto done;
4677 24519714 2019-05-09 stsp }
4678 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4679 c4296144 2019-05-09 stsp done:
4680 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4681 ed175427 2019-05-09 stsp free_commitable(ct);
4682 24519714 2019-05-09 stsp free(parent_path);
4683 036813ee 2019-05-09 stsp free(path);
4684 a129376b 2019-03-28 stsp return err;
4685 a129376b 2019-03-28 stsp }
4686 c4296144 2019-05-09 stsp
4687 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4688 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4689 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4690 036813ee 2019-05-09 stsp struct got_repository *);
4691 ed175427 2019-05-09 stsp
4692 ed175427 2019-05-09 stsp static const struct got_error *
4693 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4694 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4695 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4696 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4697 afa376bf 2019-05-09 stsp struct got_repository *repo)
4698 ed175427 2019-05-09 stsp {
4699 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4700 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4701 036813ee 2019-05-09 stsp char *subpath;
4702 ed175427 2019-05-09 stsp
4703 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4704 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4705 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4706 ed175427 2019-05-09 stsp
4707 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4708 ed175427 2019-05-09 stsp if (err)
4709 ed175427 2019-05-09 stsp return err;
4710 ed175427 2019-05-09 stsp
4711 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4712 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4713 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4714 036813ee 2019-05-09 stsp free(subpath);
4715 036813ee 2019-05-09 stsp return err;
4716 036813ee 2019-05-09 stsp }
4717 ed175427 2019-05-09 stsp
4718 036813ee 2019-05-09 stsp static const struct got_error *
4719 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4720 036813ee 2019-05-09 stsp {
4721 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4722 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4723 ed175427 2019-05-09 stsp
4724 036813ee 2019-05-09 stsp *match = 0;
4725 ed175427 2019-05-09 stsp
4726 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4727 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4728 0f63689d 2019-05-10 stsp return NULL;
4729 036813ee 2019-05-09 stsp }
4730 0b5cc0d6 2019-05-09 stsp
4731 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4732 0f63689d 2019-05-10 stsp if (err)
4733 0f63689d 2019-05-10 stsp return err;
4734 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4735 036813ee 2019-05-09 stsp free(ct_parent_path);
4736 036813ee 2019-05-09 stsp return err;
4737 036813ee 2019-05-09 stsp }
4738 0b5cc0d6 2019-05-09 stsp
4739 768aea60 2019-05-09 stsp static mode_t
4740 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4741 768aea60 2019-05-09 stsp {
4742 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4743 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4744 3d9a4ec4 2020-07-23 stsp
4745 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4746 768aea60 2019-05-09 stsp }
4747 768aea60 2019-05-09 stsp
4748 036813ee 2019-05-09 stsp static const struct got_error *
4749 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4750 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4751 036813ee 2019-05-09 stsp {
4752 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4753 ca2503ea 2019-05-09 stsp
4754 036813ee 2019-05-09 stsp *new_te = NULL;
4755 0b5cc0d6 2019-05-09 stsp
4756 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4757 036813ee 2019-05-09 stsp if (err)
4758 036813ee 2019-05-09 stsp goto done;
4759 0b5cc0d6 2019-05-09 stsp
4760 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4761 036813ee 2019-05-09 stsp
4762 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4763 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4764 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4765 5f8a88c6 2019-08-03 stsp else
4766 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4767 036813ee 2019-05-09 stsp done:
4768 036813ee 2019-05-09 stsp if (err && *new_te) {
4769 56e0773d 2019-11-28 stsp free(*new_te);
4770 036813ee 2019-05-09 stsp *new_te = NULL;
4771 036813ee 2019-05-09 stsp }
4772 036813ee 2019-05-09 stsp return err;
4773 036813ee 2019-05-09 stsp }
4774 036813ee 2019-05-09 stsp
4775 036813ee 2019-05-09 stsp static const struct got_error *
4776 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4777 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4778 036813ee 2019-05-09 stsp {
4779 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4780 036813ee 2019-05-09 stsp char *ct_name;
4781 036813ee 2019-05-09 stsp
4782 036813ee 2019-05-09 stsp *new_te = NULL;
4783 036813ee 2019-05-09 stsp
4784 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4785 036813ee 2019-05-09 stsp if (*new_te == NULL)
4786 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4787 036813ee 2019-05-09 stsp
4788 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4789 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4790 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4791 036813ee 2019-05-09 stsp goto done;
4792 036813ee 2019-05-09 stsp }
4793 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4794 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4795 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4796 036813ee 2019-05-09 stsp goto done;
4797 036813ee 2019-05-09 stsp }
4798 036813ee 2019-05-09 stsp
4799 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4800 036813ee 2019-05-09 stsp
4801 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4802 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4803 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4804 5f8a88c6 2019-08-03 stsp else
4805 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4806 036813ee 2019-05-09 stsp done:
4807 036813ee 2019-05-09 stsp if (err && *new_te) {
4808 56e0773d 2019-11-28 stsp free(*new_te);
4809 036813ee 2019-05-09 stsp *new_te = NULL;
4810 036813ee 2019-05-09 stsp }
4811 036813ee 2019-05-09 stsp return err;
4812 036813ee 2019-05-09 stsp }
4813 036813ee 2019-05-09 stsp
4814 036813ee 2019-05-09 stsp static const struct got_error *
4815 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4816 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4817 036813ee 2019-05-09 stsp {
4818 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4819 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4820 036813ee 2019-05-09 stsp
4821 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4822 036813ee 2019-05-09 stsp if (err)
4823 036813ee 2019-05-09 stsp return err;
4824 036813ee 2019-05-09 stsp if (new_pe == NULL)
4825 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4826 036813ee 2019-05-09 stsp return NULL;
4827 afa376bf 2019-05-09 stsp }
4828 afa376bf 2019-05-09 stsp
4829 afa376bf 2019-05-09 stsp static const struct got_error *
4830 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4831 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4832 afa376bf 2019-05-09 stsp {
4833 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4834 5f8a88c6 2019-08-03 stsp unsigned char status;
4835 5f8a88c6 2019-08-03 stsp
4836 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4837 afa376bf 2019-05-09 stsp ct_path++;
4838 5f8a88c6 2019-08-03 stsp
4839 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4840 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4841 5f8a88c6 2019-08-03 stsp else
4842 5f8a88c6 2019-08-03 stsp status = ct->status;
4843 5f8a88c6 2019-08-03 stsp
4844 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4845 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4846 036813ee 2019-05-09 stsp }
4847 036813ee 2019-05-09 stsp
4848 036813ee 2019-05-09 stsp static const struct got_error *
4849 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4850 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4851 44d03001 2019-05-09 stsp {
4852 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4853 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4854 44d03001 2019-05-09 stsp char *te_path;
4855 44d03001 2019-05-09 stsp
4856 44d03001 2019-05-09 stsp *modified = 0;
4857 44d03001 2019-05-09 stsp
4858 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4859 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4860 44d03001 2019-05-09 stsp te->name) == -1)
4861 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4862 44d03001 2019-05-09 stsp
4863 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4864 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4865 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4866 44d03001 2019-05-09 stsp strlen(te_path));
4867 44d03001 2019-05-09 stsp if (*modified)
4868 44d03001 2019-05-09 stsp break;
4869 44d03001 2019-05-09 stsp }
4870 44d03001 2019-05-09 stsp
4871 44d03001 2019-05-09 stsp free(te_path);
4872 44d03001 2019-05-09 stsp return err;
4873 44d03001 2019-05-09 stsp }
4874 44d03001 2019-05-09 stsp
4875 44d03001 2019-05-09 stsp static const struct got_error *
4876 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4877 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4878 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4879 036813ee 2019-05-09 stsp {
4880 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4881 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4882 036813ee 2019-05-09 stsp
4883 036813ee 2019-05-09 stsp *ctp = NULL;
4884 036813ee 2019-05-09 stsp
4885 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4886 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4887 036813ee 2019-05-09 stsp char *ct_name = NULL;
4888 036813ee 2019-05-09 stsp int path_matches;
4889 036813ee 2019-05-09 stsp
4890 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4891 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4892 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4893 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4894 5f8a88c6 2019-08-03 stsp continue;
4895 5f8a88c6 2019-08-03 stsp } else {
4896 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4897 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4898 5f8a88c6 2019-08-03 stsp continue;
4899 5f8a88c6 2019-08-03 stsp }
4900 036813ee 2019-05-09 stsp
4901 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4902 036813ee 2019-05-09 stsp continue;
4903 036813ee 2019-05-09 stsp
4904 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4905 036813ee 2019-05-09 stsp if (err)
4906 036813ee 2019-05-09 stsp return err;
4907 036813ee 2019-05-09 stsp if (!path_matches)
4908 036813ee 2019-05-09 stsp continue;
4909 036813ee 2019-05-09 stsp
4910 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4911 036813ee 2019-05-09 stsp if (ct_name == NULL)
4912 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4913 036813ee 2019-05-09 stsp
4914 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4915 036813ee 2019-05-09 stsp continue;
4916 036813ee 2019-05-09 stsp
4917 036813ee 2019-05-09 stsp *ctp = ct;
4918 036813ee 2019-05-09 stsp break;
4919 036813ee 2019-05-09 stsp }
4920 2b496619 2019-07-10 stsp
4921 2b496619 2019-07-10 stsp return err;
4922 2b496619 2019-07-10 stsp }
4923 2b496619 2019-07-10 stsp
4924 2b496619 2019-07-10 stsp static const struct got_error *
4925 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4926 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4927 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4928 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4929 2b496619 2019-07-10 stsp struct got_repository *repo)
4930 2b496619 2019-07-10 stsp {
4931 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4932 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4933 2b496619 2019-07-10 stsp char *subtree_path;
4934 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4935 ba580f68 2020-03-22 stsp int nentries;
4936 2b496619 2019-07-10 stsp
4937 2b496619 2019-07-10 stsp *new_tep = NULL;
4938 2b496619 2019-07-10 stsp
4939 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4940 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4941 2b496619 2019-07-10 stsp child_path) == -1)
4942 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4943 036813ee 2019-05-09 stsp
4944 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4945 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4946 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4947 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4948 56e0773d 2019-11-28 stsp
4949 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4950 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4951 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4952 2b496619 2019-07-10 stsp goto done;
4953 2b496619 2019-07-10 stsp }
4954 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4955 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4956 2b496619 2019-07-10 stsp if (err) {
4957 56e0773d 2019-11-28 stsp free(new_te);
4958 2b496619 2019-07-10 stsp goto done;
4959 2b496619 2019-07-10 stsp }
4960 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4961 2b496619 2019-07-10 stsp done:
4962 56e0773d 2019-11-28 stsp free(id);
4963 2b496619 2019-07-10 stsp free(subtree_path);
4964 2b496619 2019-07-10 stsp if (err == NULL)
4965 2b496619 2019-07-10 stsp *new_tep = new_te;
4966 036813ee 2019-05-09 stsp return err;
4967 036813ee 2019-05-09 stsp }
4968 036813ee 2019-05-09 stsp
4969 036813ee 2019-05-09 stsp static const struct got_error *
4970 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4971 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4972 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4973 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4974 036813ee 2019-05-09 stsp struct got_repository *repo)
4975 036813ee 2019-05-09 stsp {
4976 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4977 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4978 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4979 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4980 036813ee 2019-05-09 stsp
4981 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4982 ba580f68 2020-03-22 stsp *nentries = 0;
4983 036813ee 2019-05-09 stsp
4984 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4985 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4986 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4987 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4988 036813ee 2019-05-09 stsp
4989 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4990 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4991 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4992 036813ee 2019-05-09 stsp continue;
4993 036813ee 2019-05-09 stsp
4994 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4995 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4996 036813ee 2019-05-09 stsp continue;
4997 036813ee 2019-05-09 stsp
4998 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4999 036813ee 2019-05-09 stsp pe->path);
5000 036813ee 2019-05-09 stsp if (err)
5001 036813ee 2019-05-09 stsp goto done;
5002 036813ee 2019-05-09 stsp
5003 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5004 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5005 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5006 036813ee 2019-05-09 stsp if (err)
5007 036813ee 2019-05-09 stsp goto done;
5008 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5009 afa376bf 2019-05-09 stsp if (err)
5010 afa376bf 2019-05-09 stsp goto done;
5011 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5012 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5013 2b496619 2019-07-10 stsp if (err)
5014 2f51b5b3 2019-05-09 stsp goto done;
5015 ba580f68 2020-03-22 stsp (*nentries)++;
5016 2b496619 2019-07-10 stsp } else {
5017 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5018 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5019 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5020 2b496619 2019-07-10 stsp == NULL) {
5021 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5022 2b496619 2019-07-10 stsp child_path, path_base_tree,
5023 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5024 2b496619 2019-07-10 stsp repo);
5025 2b496619 2019-07-10 stsp if (err)
5026 2b496619 2019-07-10 stsp goto done;
5027 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5028 2b496619 2019-07-10 stsp if (err)
5029 2b496619 2019-07-10 stsp goto done;
5030 ba580f68 2020-03-22 stsp (*nentries)++;
5031 9ba0479c 2019-05-10 stsp }
5032 ed175427 2019-05-09 stsp }
5033 2f51b5b3 2019-05-09 stsp }
5034 2f51b5b3 2019-05-09 stsp
5035 2f51b5b3 2019-05-09 stsp if (base_tree) {
5036 56e0773d 2019-11-28 stsp int i, nbase_entries;
5037 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5038 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5039 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5040 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5041 63c5ca5d 2019-08-24 stsp
5042 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5043 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5044 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5045 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5046 63c5ca5d 2019-08-24 stsp if (err)
5047 63c5ca5d 2019-08-24 stsp goto done;
5048 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5049 63c5ca5d 2019-08-24 stsp if (err)
5050 63c5ca5d 2019-08-24 stsp goto done;
5051 ba580f68 2020-03-22 stsp (*nentries)++;
5052 63c5ca5d 2019-08-24 stsp continue;
5053 63c5ca5d 2019-08-24 stsp }
5054 2f51b5b3 2019-05-09 stsp
5055 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5056 44d03001 2019-05-09 stsp int modified;
5057 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5058 036813ee 2019-05-09 stsp if (err)
5059 036813ee 2019-05-09 stsp goto done;
5060 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5061 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5062 2f51b5b3 2019-05-09 stsp if (err)
5063 2f51b5b3 2019-05-09 stsp goto done;
5064 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5065 44d03001 2019-05-09 stsp if (modified) {
5066 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5067 ba580f68 2020-03-22 stsp int nsubentries;
5068 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5069 ba580f68 2020-03-22 stsp &nsubentries, te,
5070 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5071 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5072 44d03001 2019-05-09 stsp if (err)
5073 44d03001 2019-05-09 stsp goto done;
5074 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5075 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5076 ba580f68 2020-03-22 stsp free(new_id);
5077 ba580f68 2020-03-22 stsp continue;
5078 ba580f68 2020-03-22 stsp }
5079 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5080 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5081 56e0773d 2019-11-28 stsp free(new_id);
5082 44d03001 2019-05-09 stsp }
5083 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5084 036813ee 2019-05-09 stsp if (err)
5085 036813ee 2019-05-09 stsp goto done;
5086 ba580f68 2020-03-22 stsp (*nentries)++;
5087 2f51b5b3 2019-05-09 stsp continue;
5088 036813ee 2019-05-09 stsp }
5089 2f51b5b3 2019-05-09 stsp
5090 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5091 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5092 f66c734c 2019-09-22 stsp if (err)
5093 f66c734c 2019-09-22 stsp goto done;
5094 2f51b5b3 2019-05-09 stsp if (ct) {
5095 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5096 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5097 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5098 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5099 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5100 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5101 2f51b5b3 2019-05-09 stsp if (err)
5102 2f51b5b3 2019-05-09 stsp goto done;
5103 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5104 2f51b5b3 2019-05-09 stsp if (err)
5105 2f51b5b3 2019-05-09 stsp goto done;
5106 ba580f68 2020-03-22 stsp (*nentries)++;
5107 2f51b5b3 2019-05-09 stsp }
5108 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5109 b416585c 2019-05-13 stsp status_arg);
5110 afa376bf 2019-05-09 stsp if (err)
5111 afa376bf 2019-05-09 stsp goto done;
5112 2f51b5b3 2019-05-09 stsp } else {
5113 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5114 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5115 2f51b5b3 2019-05-09 stsp if (err)
5116 2f51b5b3 2019-05-09 stsp goto done;
5117 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5118 2f51b5b3 2019-05-09 stsp if (err)
5119 2f51b5b3 2019-05-09 stsp goto done;
5120 ba580f68 2020-03-22 stsp (*nentries)++;
5121 2f51b5b3 2019-05-09 stsp }
5122 ca2503ea 2019-05-09 stsp }
5123 ed175427 2019-05-09 stsp }
5124 0b5cc0d6 2019-05-09 stsp
5125 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5126 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5127 ed175427 2019-05-09 stsp done:
5128 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5129 2e1fa222 2020-07-23 stsp return err;
5130 2e1fa222 2020-07-23 stsp }
5131 2e1fa222 2020-07-23 stsp
5132 2e1fa222 2020-07-23 stsp static const struct got_error *
5133 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5134 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5135 72fd46fa 2019-09-06 stsp int have_staged_files)
5136 ebf99748 2019-05-09 stsp {
5137 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5138 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5139 ebf99748 2019-05-09 stsp
5140 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5141 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5142 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5143 ebf99748 2019-05-09 stsp
5144 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5145 ebf99748 2019-05-09 stsp if (ie) {
5146 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5147 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5148 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5149 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5150 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5151 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5152 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5153 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5154 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5155 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5156 72fd46fa 2019-09-06 stsp !have_staged_files);
5157 ebf99748 2019-05-09 stsp } else
5158 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5159 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5160 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5161 72fd46fa 2019-09-06 stsp !have_staged_files);
5162 ebf99748 2019-05-09 stsp } else {
5163 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5164 ebf99748 2019-05-09 stsp if (err)
5165 af12c6b9 2019-06-04 stsp break;
5166 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5167 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5168 3969253a 2020-03-07 stsp if (err) {
5169 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5170 3969253a 2020-03-07 stsp break;
5171 3969253a 2020-03-07 stsp }
5172 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5173 3969253a 2020-03-07 stsp if (err) {
5174 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5175 af12c6b9 2019-06-04 stsp break;
5176 3969253a 2020-03-07 stsp }
5177 ebf99748 2019-05-09 stsp }
5178 ebf99748 2019-05-09 stsp }
5179 d56d26ce 2019-05-10 stsp return err;
5180 d56d26ce 2019-05-10 stsp }
5181 735ef5ac 2019-08-03 stsp
5182 d56d26ce 2019-05-10 stsp
5183 d56d26ce 2019-05-10 stsp static const struct got_error *
5184 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5185 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5186 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5187 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5188 735ef5ac 2019-08-03 stsp int ood_errcode)
5189 d56d26ce 2019-05-10 stsp {
5190 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5191 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5192 1a36436d 2019-06-10 stsp
5193 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5194 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5195 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5196 1a36436d 2019-06-10 stsp return NULL;
5197 9bead371 2019-07-28 stsp /*
5198 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5199 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5200 9bead371 2019-07-28 stsp */
5201 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5202 735ef5ac 2019-08-03 stsp in_repo_path);
5203 9bead371 2019-07-28 stsp if (err) {
5204 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5205 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5206 1a36436d 2019-06-10 stsp goto done;
5207 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5208 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5209 1a36436d 2019-06-10 stsp } else {
5210 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5211 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5212 735ef5ac 2019-08-03 stsp in_repo_path);
5213 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5214 1a36436d 2019-06-10 stsp goto done;
5215 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5216 1a36436d 2019-06-10 stsp }
5217 1a36436d 2019-06-10 stsp done:
5218 1a36436d 2019-06-10 stsp free(id);
5219 1a36436d 2019-06-10 stsp return err;
5220 ebf99748 2019-05-09 stsp }
5221 ebf99748 2019-05-09 stsp
5222 c4296144 2019-05-09 stsp const struct got_error *
5223 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5224 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5225 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5226 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5227 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5228 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5229 afa376bf 2019-05-09 stsp struct got_repository *repo)
5230 c4296144 2019-05-09 stsp {
5231 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5232 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5233 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5234 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5235 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5236 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5237 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5238 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5239 ba580f68 2020-03-22 stsp int nentries;
5240 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5241 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5242 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5243 c4296144 2019-05-09 stsp
5244 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5245 c4296144 2019-05-09 stsp
5246 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5247 675c7539 2019-05-09 stsp
5248 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5249 588edf97 2019-05-10 stsp if (err)
5250 588edf97 2019-05-10 stsp goto done;
5251 de18fc63 2019-05-09 stsp
5252 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5253 588edf97 2019-05-10 stsp if (err)
5254 588edf97 2019-05-10 stsp goto done;
5255 588edf97 2019-05-10 stsp
5256 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5257 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5258 33ad4cbe 2019-05-12 jcs if (err)
5259 33ad4cbe 2019-05-12 jcs goto done;
5260 33ad4cbe 2019-05-12 jcs }
5261 c4296144 2019-05-09 stsp
5262 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5263 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5264 33ad4cbe 2019-05-12 jcs goto done;
5265 33ad4cbe 2019-05-12 jcs }
5266 33ad4cbe 2019-05-12 jcs
5267 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5268 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5269 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5270 cf066bf8 2019-05-09 stsp char *ondisk_path;
5271 cf066bf8 2019-05-09 stsp
5272 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5273 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5274 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5275 5f8a88c6 2019-08-03 stsp continue;
5276 5f8a88c6 2019-08-03 stsp
5277 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5278 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5279 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5280 cf066bf8 2019-05-09 stsp continue;
5281 cf066bf8 2019-05-09 stsp
5282 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5283 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5284 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5285 cf066bf8 2019-05-09 stsp goto done;
5286 cf066bf8 2019-05-09 stsp }
5287 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5288 2e1fa222 2020-07-23 stsp free(ondisk_path);
5289 2e1fa222 2020-07-23 stsp if (err)
5290 cf066bf8 2019-05-09 stsp goto done;
5291 cf066bf8 2019-05-09 stsp }
5292 036813ee 2019-05-09 stsp
5293 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5294 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5295 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5296 036813ee 2019-05-09 stsp if (err)
5297 036813ee 2019-05-09 stsp goto done;
5298 036813ee 2019-05-09 stsp
5299 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5300 de18fc63 2019-05-09 stsp if (err)
5301 de18fc63 2019-05-09 stsp goto done;
5302 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5303 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5304 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5305 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5306 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5307 33ad4cbe 2019-05-12 jcs free(logmsg);
5308 9d40349a 2019-05-09 stsp if (err)
5309 9d40349a 2019-05-09 stsp goto done;
5310 9d40349a 2019-05-09 stsp
5311 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5312 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5313 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5314 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5315 09f5bd90 2019-05-09 stsp goto done;
5316 09f5bd90 2019-05-09 stsp }
5317 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5318 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5319 09f5bd90 2019-05-09 stsp if (err)
5320 09f5bd90 2019-05-09 stsp goto done;
5321 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5322 ebf99748 2019-05-09 stsp if (err)
5323 ebf99748 2019-05-09 stsp goto done;
5324 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5325 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5326 09f5bd90 2019-05-09 stsp goto done;
5327 09f5bd90 2019-05-09 stsp }
5328 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5329 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5330 f2c16586 2019-05-09 stsp if (err)
5331 f2c16586 2019-05-09 stsp goto done;
5332 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5333 f2c16586 2019-05-09 stsp if (err)
5334 f2c16586 2019-05-09 stsp goto done;
5335 f2c16586 2019-05-09 stsp
5336 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5337 09f5bd90 2019-05-09 stsp if (err)
5338 09f5bd90 2019-05-09 stsp goto done;
5339 09f5bd90 2019-05-09 stsp
5340 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5341 ebf99748 2019-05-09 stsp if (err)
5342 ebf99748 2019-05-09 stsp goto done;
5343 c4296144 2019-05-09 stsp done:
5344 588edf97 2019-05-10 stsp if (head_tree)
5345 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5346 588edf97 2019-05-10 stsp if (head_commit)
5347 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5348 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5349 2f17228e 2019-05-12 stsp if (head_ref2) {
5350 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5351 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5352 2f17228e 2019-05-12 stsp err = unlockerr;
5353 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5354 39cd0ff6 2019-07-12 stsp }
5355 39cd0ff6 2019-07-12 stsp return err;
5356 39cd0ff6 2019-07-12 stsp }
5357 5c1e53bc 2019-07-28 stsp
5358 5c1e53bc 2019-07-28 stsp static const struct got_error *
5359 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5360 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5361 5c1e53bc 2019-07-28 stsp {
5362 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5363 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5364 39cd0ff6 2019-07-12 stsp
5365 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5366 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5367 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5368 5c1e53bc 2019-07-28 stsp
5369 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5370 5c1e53bc 2019-07-28 stsp ct_path++;
5371 5c1e53bc 2019-07-28 stsp
5372 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5373 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5374 5c1e53bc 2019-07-28 stsp break;
5375 5c1e53bc 2019-07-28 stsp }
5376 5c1e53bc 2019-07-28 stsp
5377 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5378 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5379 5c1e53bc 2019-07-28 stsp
5380 5c1e53bc 2019-07-28 stsp return NULL;
5381 5c1e53bc 2019-07-28 stsp }
5382 5c1e53bc 2019-07-28 stsp
5383 f0b75401 2019-08-03 stsp static const struct got_error *
5384 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5385 f0b75401 2019-08-03 stsp {
5386 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5387 f0b75401 2019-08-03 stsp
5388 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5389 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5390 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5391 f0b75401 2019-08-03 stsp }
5392 f0b75401 2019-08-03 stsp
5393 f0b75401 2019-08-03 stsp return NULL;
5394 f0b75401 2019-08-03 stsp }
5395 f0b75401 2019-08-03 stsp
5396 5f8a88c6 2019-08-03 stsp static const struct got_error *
5397 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5398 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5399 5f8a88c6 2019-08-03 stsp {
5400 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5401 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5402 5f8a88c6 2019-08-03 stsp
5403 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5404 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5405 5f8a88c6 2019-08-03 stsp continue;
5406 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5407 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5408 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5409 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5410 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5411 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5412 5f8a88c6 2019-08-03 stsp }
5413 5f8a88c6 2019-08-03 stsp
5414 5f8a88c6 2019-08-03 stsp return NULL;
5415 5f8a88c6 2019-08-03 stsp }
5416 5f8a88c6 2019-08-03 stsp
5417 39cd0ff6 2019-07-12 stsp const struct got_error *
5418 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5419 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5420 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5421 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5422 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5423 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5424 39cd0ff6 2019-07-12 stsp {
5425 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5426 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5427 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5428 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5429 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5430 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5431 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5432 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5433 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5434 39cd0ff6 2019-07-12 stsp
5435 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5436 39cd0ff6 2019-07-12 stsp
5437 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5438 39cd0ff6 2019-07-12 stsp
5439 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5440 39cd0ff6 2019-07-12 stsp if (err)
5441 39cd0ff6 2019-07-12 stsp goto done;
5442 39cd0ff6 2019-07-12 stsp
5443 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5444 39cd0ff6 2019-07-12 stsp if (err)
5445 39cd0ff6 2019-07-12 stsp goto done;
5446 39cd0ff6 2019-07-12 stsp
5447 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5448 39cd0ff6 2019-07-12 stsp if (err)
5449 39cd0ff6 2019-07-12 stsp goto done;
5450 39cd0ff6 2019-07-12 stsp
5451 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5452 39cd0ff6 2019-07-12 stsp if (err)
5453 39cd0ff6 2019-07-12 stsp goto done;
5454 39cd0ff6 2019-07-12 stsp
5455 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5456 5f8a88c6 2019-08-03 stsp &have_staged_files);
5457 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5458 5f8a88c6 2019-08-03 stsp goto done;
5459 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5460 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5461 5f8a88c6 2019-08-03 stsp if (err)
5462 5f8a88c6 2019-08-03 stsp goto done;
5463 5f8a88c6 2019-08-03 stsp }
5464 5f8a88c6 2019-08-03 stsp
5465 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5466 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5467 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5468 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5469 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5470 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5471 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5472 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5473 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5474 5c1e53bc 2019-07-28 stsp if (err)
5475 5c1e53bc 2019-07-28 stsp goto done;
5476 5c1e53bc 2019-07-28 stsp }
5477 39cd0ff6 2019-07-12 stsp
5478 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5479 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5480 39cd0ff6 2019-07-12 stsp goto done;
5481 5c1e53bc 2019-07-28 stsp }
5482 5c1e53bc 2019-07-28 stsp
5483 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5484 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5485 5c1e53bc 2019-07-28 stsp if (err)
5486 5c1e53bc 2019-07-28 stsp goto done;
5487 39cd0ff6 2019-07-12 stsp }
5488 f0b75401 2019-08-03 stsp
5489 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5490 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5491 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5492 f0b75401 2019-08-03 stsp
5493 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5494 f0b75401 2019-08-03 stsp ct_path++;
5495 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5496 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5497 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5498 b50cabdf 2019-07-12 stsp if (err)
5499 b50cabdf 2019-07-12 stsp goto done;
5500 f0b75401 2019-08-03 stsp
5501 b50cabdf 2019-07-12 stsp }
5502 b50cabdf 2019-07-12 stsp
5503 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5504 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5505 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5506 39cd0ff6 2019-07-12 stsp if (err)
5507 39cd0ff6 2019-07-12 stsp goto done;
5508 39cd0ff6 2019-07-12 stsp
5509 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5510 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5511 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5512 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5513 39cd0ff6 2019-07-12 stsp err = sync_err;
5514 39cd0ff6 2019-07-12 stsp done:
5515 39cd0ff6 2019-07-12 stsp if (fileindex)
5516 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5517 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5518 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5519 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5520 39cd0ff6 2019-07-12 stsp err = unlockerr;
5521 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5522 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5523 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5524 39cd0ff6 2019-07-12 stsp }
5525 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5526 c4296144 2019-05-09 stsp return err;
5527 8656d6c4 2019-05-20 stsp }
5528 8656d6c4 2019-05-20 stsp
5529 8656d6c4 2019-05-20 stsp const char *
5530 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5531 8656d6c4 2019-05-20 stsp {
5532 8656d6c4 2019-05-20 stsp return ct->path;
5533 8656d6c4 2019-05-20 stsp }
5534 8656d6c4 2019-05-20 stsp
5535 8656d6c4 2019-05-20 stsp unsigned int
5536 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5537 8656d6c4 2019-05-20 stsp {
5538 8656d6c4 2019-05-20 stsp return ct->status;
5539 818c7501 2019-07-11 stsp }
5540 818c7501 2019-07-11 stsp
5541 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5542 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5543 818c7501 2019-07-11 stsp struct got_repository *repo;
5544 818c7501 2019-07-11 stsp };
5545 818c7501 2019-07-11 stsp
5546 818c7501 2019-07-11 stsp static const struct got_error *
5547 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5548 818c7501 2019-07-11 stsp {
5549 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5550 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5551 818c7501 2019-07-11 stsp unsigned char status;
5552 818c7501 2019-07-11 stsp struct stat sb;
5553 818c7501 2019-07-11 stsp char *ondisk_path;
5554 818c7501 2019-07-11 stsp
5555 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5556 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5557 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5558 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5559 818c7501 2019-07-11 stsp
5560 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5561 818c7501 2019-07-11 stsp == -1)
5562 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5563 818c7501 2019-07-11 stsp
5564 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5565 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5566 818c7501 2019-07-11 stsp free(ondisk_path);
5567 818c7501 2019-07-11 stsp if (err)
5568 818c7501 2019-07-11 stsp return err;
5569 818c7501 2019-07-11 stsp
5570 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5571 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5572 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5573 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5574 818c7501 2019-07-11 stsp
5575 818c7501 2019-07-11 stsp return NULL;
5576 818c7501 2019-07-11 stsp }
5577 818c7501 2019-07-11 stsp
5578 818c7501 2019-07-11 stsp const struct got_error *
5579 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5580 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5581 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5582 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5583 818c7501 2019-07-11 stsp {
5584 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5585 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5586 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5587 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5588 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5589 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5590 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5591 818c7501 2019-07-11 stsp
5592 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5593 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5594 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5595 818c7501 2019-07-11 stsp
5596 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5597 818c7501 2019-07-11 stsp if (err)
5598 818c7501 2019-07-11 stsp return err;
5599 818c7501 2019-07-11 stsp
5600 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5601 818c7501 2019-07-11 stsp if (err)
5602 818c7501 2019-07-11 stsp goto done;
5603 818c7501 2019-07-11 stsp
5604 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5605 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5606 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5607 818c7501 2019-07-11 stsp &ok_arg);
5608 818c7501 2019-07-11 stsp if (err)
5609 818c7501 2019-07-11 stsp goto done;
5610 818c7501 2019-07-11 stsp
5611 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5612 818c7501 2019-07-11 stsp if (err)
5613 818c7501 2019-07-11 stsp goto done;
5614 818c7501 2019-07-11 stsp
5615 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5616 818c7501 2019-07-11 stsp if (err)
5617 818c7501 2019-07-11 stsp goto done;
5618 818c7501 2019-07-11 stsp
5619 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5620 818c7501 2019-07-11 stsp if (err)
5621 818c7501 2019-07-11 stsp goto done;
5622 818c7501 2019-07-11 stsp
5623 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5624 818c7501 2019-07-11 stsp 0);
5625 e51d7b55 2020-01-04 stsp if (err)
5626 e51d7b55 2020-01-04 stsp goto done;
5627 e51d7b55 2020-01-04 stsp
5628 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5629 818c7501 2019-07-11 stsp if (err)
5630 818c7501 2019-07-11 stsp goto done;
5631 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5632 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5633 e51d7b55 2020-01-04 stsp goto done;
5634 e51d7b55 2020-01-04 stsp }
5635 818c7501 2019-07-11 stsp
5636 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5637 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5638 818c7501 2019-07-11 stsp if (err)
5639 818c7501 2019-07-11 stsp goto done;
5640 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5641 818c7501 2019-07-11 stsp if (err)
5642 818c7501 2019-07-11 stsp goto done;
5643 818c7501 2019-07-11 stsp
5644 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5645 818c7501 2019-07-11 stsp
5646 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5647 818c7501 2019-07-11 stsp if (err)
5648 818c7501 2019-07-11 stsp goto done;
5649 818c7501 2019-07-11 stsp
5650 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5651 818c7501 2019-07-11 stsp if (err)
5652 818c7501 2019-07-11 stsp goto done;
5653 818c7501 2019-07-11 stsp
5654 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5655 818c7501 2019-07-11 stsp worktree->base_commit_id);
5656 818c7501 2019-07-11 stsp if (err)
5657 818c7501 2019-07-11 stsp goto done;
5658 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5659 818c7501 2019-07-11 stsp if (err)
5660 818c7501 2019-07-11 stsp goto done;
5661 818c7501 2019-07-11 stsp
5662 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5663 818c7501 2019-07-11 stsp if (err)
5664 818c7501 2019-07-11 stsp goto done;
5665 818c7501 2019-07-11 stsp done:
5666 818c7501 2019-07-11 stsp free(fileindex_path);
5667 818c7501 2019-07-11 stsp free(tmp_branch_name);
5668 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5669 818c7501 2019-07-11 stsp free(branch_ref_name);
5670 818c7501 2019-07-11 stsp if (branch_ref)
5671 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5672 818c7501 2019-07-11 stsp if (wt_branch)
5673 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5674 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5675 818c7501 2019-07-11 stsp if (err) {
5676 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5677 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5678 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5679 818c7501 2019-07-11 stsp }
5680 818c7501 2019-07-11 stsp if (*tmp_branch) {
5681 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5682 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5683 818c7501 2019-07-11 stsp }
5684 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5685 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5686 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5687 3e3a69f1 2019-07-25 stsp }
5688 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5689 818c7501 2019-07-11 stsp }
5690 818c7501 2019-07-11 stsp return err;
5691 818c7501 2019-07-11 stsp }
5692 818c7501 2019-07-11 stsp
5693 818c7501 2019-07-11 stsp const struct got_error *
5694 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5695 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5696 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5697 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5698 818c7501 2019-07-11 stsp {
5699 818c7501 2019-07-11 stsp const struct got_error *err;
5700 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5701 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5702 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5703 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5704 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5705 818c7501 2019-07-11 stsp
5706 818c7501 2019-07-11 stsp *commit_id = NULL;
5707 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5708 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5709 3e3a69f1 2019-07-25 stsp *branch = NULL;
5710 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5711 3e3a69f1 2019-07-25 stsp
5712 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5713 3e3a69f1 2019-07-25 stsp if (err)
5714 3e3a69f1 2019-07-25 stsp return err;
5715 818c7501 2019-07-11 stsp
5716 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5717 3e3a69f1 2019-07-25 stsp if (err)
5718 f032f1f7 2019-08-04 stsp goto done;
5719 f032f1f7 2019-08-04 stsp
5720 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5721 f032f1f7 2019-08-04 stsp &have_staged_files);
5722 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5723 f032f1f7 2019-08-04 stsp goto done;
5724 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5725 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5726 3e3a69f1 2019-07-25 stsp goto done;
5727 f032f1f7 2019-08-04 stsp }
5728 3e3a69f1 2019-07-25 stsp
5729 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5730 818c7501 2019-07-11 stsp if (err)
5731 f032f1f7 2019-08-04 stsp goto done;
5732 818c7501 2019-07-11 stsp
5733 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5734 818c7501 2019-07-11 stsp if (err)
5735 818c7501 2019-07-11 stsp goto done;
5736 818c7501 2019-07-11 stsp
5737 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5738 818c7501 2019-07-11 stsp if (err)
5739 818c7501 2019-07-11 stsp goto done;
5740 818c7501 2019-07-11 stsp
5741 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5742 818c7501 2019-07-11 stsp if (err)
5743 818c7501 2019-07-11 stsp goto done;
5744 818c7501 2019-07-11 stsp
5745 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5746 818c7501 2019-07-11 stsp if (err)
5747 818c7501 2019-07-11 stsp goto done;
5748 818c7501 2019-07-11 stsp
5749 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5750 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5751 818c7501 2019-07-11 stsp if (err)
5752 818c7501 2019-07-11 stsp goto done;
5753 818c7501 2019-07-11 stsp
5754 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5755 818c7501 2019-07-11 stsp if (err)
5756 818c7501 2019-07-11 stsp goto done;
5757 818c7501 2019-07-11 stsp
5758 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5759 818c7501 2019-07-11 stsp if (err)
5760 818c7501 2019-07-11 stsp goto done;
5761 818c7501 2019-07-11 stsp
5762 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5763 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5764 818c7501 2019-07-11 stsp if (err)
5765 818c7501 2019-07-11 stsp goto done;
5766 818c7501 2019-07-11 stsp
5767 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5768 818c7501 2019-07-11 stsp if (err)
5769 818c7501 2019-07-11 stsp goto done;
5770 818c7501 2019-07-11 stsp done:
5771 818c7501 2019-07-11 stsp free(commit_ref_name);
5772 818c7501 2019-07-11 stsp free(branch_ref_name);
5773 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5774 818c7501 2019-07-11 stsp if (commit_ref)
5775 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5776 818c7501 2019-07-11 stsp if (branch_ref)
5777 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5778 818c7501 2019-07-11 stsp if (err) {
5779 818c7501 2019-07-11 stsp free(*commit_id);
5780 818c7501 2019-07-11 stsp *commit_id = NULL;
5781 818c7501 2019-07-11 stsp if (*tmp_branch) {
5782 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5783 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5784 818c7501 2019-07-11 stsp }
5785 818c7501 2019-07-11 stsp if (*new_base_branch) {
5786 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5787 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5788 818c7501 2019-07-11 stsp }
5789 818c7501 2019-07-11 stsp if (*branch) {
5790 818c7501 2019-07-11 stsp got_ref_close(*branch);
5791 818c7501 2019-07-11 stsp *branch = NULL;
5792 818c7501 2019-07-11 stsp }
5793 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5794 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5795 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5796 3e3a69f1 2019-07-25 stsp }
5797 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5798 818c7501 2019-07-11 stsp }
5799 818c7501 2019-07-11 stsp return err;
5800 c4296144 2019-05-09 stsp }
5801 818c7501 2019-07-11 stsp
5802 818c7501 2019-07-11 stsp const struct got_error *
5803 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5804 818c7501 2019-07-11 stsp {
5805 818c7501 2019-07-11 stsp const struct got_error *err;
5806 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5807 818c7501 2019-07-11 stsp
5808 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5809 818c7501 2019-07-11 stsp if (err)
5810 818c7501 2019-07-11 stsp return err;
5811 818c7501 2019-07-11 stsp
5812 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5813 818c7501 2019-07-11 stsp free(tmp_branch_name);
5814 818c7501 2019-07-11 stsp return NULL;
5815 818c7501 2019-07-11 stsp }
5816 818c7501 2019-07-11 stsp
5817 818c7501 2019-07-11 stsp static const struct got_error *
5818 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5819 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5820 818c7501 2019-07-11 stsp {
5821 0ebf8283 2019-07-24 stsp *logmsg = arg;
5822 818c7501 2019-07-11 stsp return NULL;
5823 818c7501 2019-07-11 stsp }
5824 818c7501 2019-07-11 stsp
5825 818c7501 2019-07-11 stsp static const struct got_error *
5826 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5827 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5828 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5829 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5830 818c7501 2019-07-11 stsp {
5831 818c7501 2019-07-11 stsp return NULL;
5832 01757395 2019-07-12 stsp }
5833 01757395 2019-07-12 stsp
5834 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5835 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5836 01757395 2019-07-12 stsp void *progress_arg;
5837 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5838 01757395 2019-07-12 stsp };
5839 01757395 2019-07-12 stsp
5840 01757395 2019-07-12 stsp static const struct got_error *
5841 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5842 01757395 2019-07-12 stsp {
5843 01757395 2019-07-12 stsp const struct got_error *err;
5844 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5845 01757395 2019-07-12 stsp char *p;
5846 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5847 01757395 2019-07-12 stsp
5848 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5849 01757395 2019-07-12 stsp if (err)
5850 01757395 2019-07-12 stsp return err;
5851 01757395 2019-07-12 stsp
5852 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5853 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5854 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5855 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5856 01757395 2019-07-12 stsp return NULL;
5857 01757395 2019-07-12 stsp
5858 01757395 2019-07-12 stsp p = strdup(path);
5859 01757395 2019-07-12 stsp if (p == NULL)
5860 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5861 01757395 2019-07-12 stsp
5862 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5863 01757395 2019-07-12 stsp if (err || new == NULL)
5864 01757395 2019-07-12 stsp free(p);
5865 01757395 2019-07-12 stsp return err;
5866 01757395 2019-07-12 stsp }
5867 01757395 2019-07-12 stsp
5868 01757395 2019-07-12 stsp void
5869 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5870 01757395 2019-07-12 stsp {
5871 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5872 01757395 2019-07-12 stsp
5873 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5874 01757395 2019-07-12 stsp free((char *)pe->path);
5875 01757395 2019-07-12 stsp
5876 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5877 818c7501 2019-07-11 stsp }
5878 818c7501 2019-07-11 stsp
5879 0ebf8283 2019-07-24 stsp static const struct got_error *
5880 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5881 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5882 818c7501 2019-07-11 stsp {
5883 818c7501 2019-07-11 stsp const struct got_error *err;
5884 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5885 818c7501 2019-07-11 stsp
5886 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5887 818c7501 2019-07-11 stsp if (err) {
5888 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5889 818c7501 2019-07-11 stsp goto done;
5890 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5891 818c7501 2019-07-11 stsp if (err)
5892 818c7501 2019-07-11 stsp goto done;
5893 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5894 818c7501 2019-07-11 stsp if (err)
5895 818c7501 2019-07-11 stsp goto done;
5896 de05890f 2020-03-05 stsp } else if (is_rebase) {
5897 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5898 818c7501 2019-07-11 stsp int cmp;
5899 818c7501 2019-07-11 stsp
5900 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5901 818c7501 2019-07-11 stsp if (err)
5902 818c7501 2019-07-11 stsp goto done;
5903 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5904 818c7501 2019-07-11 stsp free(stored_id);
5905 818c7501 2019-07-11 stsp if (cmp != 0) {
5906 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5907 818c7501 2019-07-11 stsp goto done;
5908 818c7501 2019-07-11 stsp }
5909 818c7501 2019-07-11 stsp }
5910 0ebf8283 2019-07-24 stsp done:
5911 0ebf8283 2019-07-24 stsp if (commit_ref)
5912 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5913 0ebf8283 2019-07-24 stsp return err;
5914 0ebf8283 2019-07-24 stsp }
5915 0ebf8283 2019-07-24 stsp
5916 0ebf8283 2019-07-24 stsp static const struct got_error *
5917 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5918 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5919 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5920 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5921 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5922 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5923 0ebf8283 2019-07-24 stsp {
5924 0ebf8283 2019-07-24 stsp const struct got_error *err;
5925 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5926 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5927 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5928 818c7501 2019-07-11 stsp
5929 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5930 0ebf8283 2019-07-24 stsp
5931 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5932 0ebf8283 2019-07-24 stsp if (err)
5933 0ebf8283 2019-07-24 stsp return err;
5934 0ebf8283 2019-07-24 stsp
5935 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5936 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5937 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5938 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5939 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5940 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5941 818c7501 2019-07-11 stsp if (commit_ref)
5942 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5943 818c7501 2019-07-11 stsp return err;
5944 818c7501 2019-07-11 stsp }
5945 818c7501 2019-07-11 stsp
5946 818c7501 2019-07-11 stsp const struct got_error *
5947 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5948 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5949 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5950 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5951 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5952 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5953 818c7501 2019-07-11 stsp {
5954 0ebf8283 2019-07-24 stsp const struct got_error *err;
5955 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5956 0ebf8283 2019-07-24 stsp
5957 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5958 0ebf8283 2019-07-24 stsp if (err)
5959 0ebf8283 2019-07-24 stsp return err;
5960 0ebf8283 2019-07-24 stsp
5961 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5962 0ebf8283 2019-07-24 stsp if (err)
5963 0ebf8283 2019-07-24 stsp goto done;
5964 0ebf8283 2019-07-24 stsp
5965 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5966 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5967 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5968 0ebf8283 2019-07-24 stsp done:
5969 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5970 0ebf8283 2019-07-24 stsp return err;
5971 0ebf8283 2019-07-24 stsp }
5972 0ebf8283 2019-07-24 stsp
5973 0ebf8283 2019-07-24 stsp const struct got_error *
5974 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5975 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5976 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5977 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5978 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5979 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5980 0ebf8283 2019-07-24 stsp {
5981 0ebf8283 2019-07-24 stsp const struct got_error *err;
5982 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5983 0ebf8283 2019-07-24 stsp
5984 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5985 0ebf8283 2019-07-24 stsp if (err)
5986 0ebf8283 2019-07-24 stsp return err;
5987 0ebf8283 2019-07-24 stsp
5988 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5989 0ebf8283 2019-07-24 stsp if (err)
5990 0ebf8283 2019-07-24 stsp goto done;
5991 0ebf8283 2019-07-24 stsp
5992 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5993 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5994 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5995 0ebf8283 2019-07-24 stsp done:
5996 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5997 0ebf8283 2019-07-24 stsp return err;
5998 0ebf8283 2019-07-24 stsp }
5999 0ebf8283 2019-07-24 stsp
6000 0ebf8283 2019-07-24 stsp static const struct got_error *
6001 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6002 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6003 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6004 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6005 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6006 0ebf8283 2019-07-24 stsp {
6007 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6008 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6009 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6010 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6011 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6012 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6013 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6014 818c7501 2019-07-11 stsp
6015 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6016 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6017 a0e95631 2019-07-12 stsp
6018 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6019 818c7501 2019-07-11 stsp
6020 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6021 a0e95631 2019-07-12 stsp if (err)
6022 3e3a69f1 2019-07-25 stsp return err;
6023 a0e95631 2019-07-12 stsp
6024 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6025 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6026 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6027 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6028 01757395 2019-07-12 stsp /*
6029 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6030 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6031 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6032 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6033 01757395 2019-07-12 stsp */
6034 01757395 2019-07-12 stsp if (merged_paths) {
6035 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6036 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6037 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6038 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6039 f2a9dc41 2019-12-13 tracey 0);
6040 01757395 2019-07-12 stsp if (err)
6041 01757395 2019-07-12 stsp goto done;
6042 01757395 2019-07-12 stsp }
6043 01757395 2019-07-12 stsp } else {
6044 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6045 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6046 01757395 2019-07-12 stsp if (err)
6047 01757395 2019-07-12 stsp goto done;
6048 01757395 2019-07-12 stsp }
6049 a0e95631 2019-07-12 stsp
6050 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6051 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6052 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6053 a0e95631 2019-07-12 stsp if (err)
6054 a0e95631 2019-07-12 stsp goto done;
6055 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6056 a0e95631 2019-07-12 stsp goto done;
6057 ff0d2220 2019-07-11 stsp }
6058 818c7501 2019-07-11 stsp
6059 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6060 edd02c5e 2019-07-11 stsp if (err)
6061 edd02c5e 2019-07-11 stsp goto done;
6062 edd02c5e 2019-07-11 stsp
6063 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6064 818c7501 2019-07-11 stsp if (err)
6065 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp
6067 5943eee2 2019-08-13 stsp if (new_logmsg) {
6068 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6069 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6070 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6071 5943eee2 2019-08-13 stsp goto done;
6072 5943eee2 2019-08-13 stsp }
6073 5943eee2 2019-08-13 stsp } else {
6074 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6075 5943eee2 2019-08-13 stsp if (err)
6076 5943eee2 2019-08-13 stsp goto done;
6077 5943eee2 2019-08-13 stsp }
6078 0ebf8283 2019-07-24 stsp
6079 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6080 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6081 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6082 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6083 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6084 a0e95631 2019-07-12 stsp if (err)
6085 a0e95631 2019-07-12 stsp goto done;
6086 a0e95631 2019-07-12 stsp
6087 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6088 a0e95631 2019-07-12 stsp if (err)
6089 a0e95631 2019-07-12 stsp goto done;
6090 a0e95631 2019-07-12 stsp
6091 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6092 a0e95631 2019-07-12 stsp if (err)
6093 a0e95631 2019-07-12 stsp goto done;
6094 a0e95631 2019-07-12 stsp
6095 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6096 72fd46fa 2019-09-06 stsp fileindex, 0);
6097 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6098 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6099 a0e95631 2019-07-12 stsp err = sync_err;
6100 818c7501 2019-07-11 stsp done:
6101 a0e95631 2019-07-12 stsp free(fileindex_path);
6102 a0e95631 2019-07-12 stsp free(head_commit_id);
6103 a0e95631 2019-07-12 stsp if (head_ref)
6104 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6105 818c7501 2019-07-11 stsp if (err) {
6106 818c7501 2019-07-11 stsp free(*new_commit_id);
6107 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6108 818c7501 2019-07-11 stsp }
6109 818c7501 2019-07-11 stsp return err;
6110 818c7501 2019-07-11 stsp }
6111 818c7501 2019-07-11 stsp
6112 818c7501 2019-07-11 stsp const struct got_error *
6113 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6114 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6115 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6116 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6117 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6118 0ebf8283 2019-07-24 stsp {
6119 0ebf8283 2019-07-24 stsp const struct got_error *err;
6120 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6121 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6122 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6123 0ebf8283 2019-07-24 stsp
6124 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6125 0ebf8283 2019-07-24 stsp if (err)
6126 0ebf8283 2019-07-24 stsp return err;
6127 0ebf8283 2019-07-24 stsp
6128 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6129 0ebf8283 2019-07-24 stsp if (err)
6130 0ebf8283 2019-07-24 stsp goto done;
6131 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6132 0ebf8283 2019-07-24 stsp if (err)
6133 0ebf8283 2019-07-24 stsp goto done;
6134 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6135 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6136 0ebf8283 2019-07-24 stsp goto done;
6137 0ebf8283 2019-07-24 stsp }
6138 0ebf8283 2019-07-24 stsp
6139 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6140 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6141 0ebf8283 2019-07-24 stsp done:
6142 0ebf8283 2019-07-24 stsp if (commit_ref)
6143 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6144 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6145 0ebf8283 2019-07-24 stsp free(commit_id);
6146 0ebf8283 2019-07-24 stsp return err;
6147 0ebf8283 2019-07-24 stsp }
6148 0ebf8283 2019-07-24 stsp
6149 0ebf8283 2019-07-24 stsp const struct got_error *
6150 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6151 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6152 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6153 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6154 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6155 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6156 0ebf8283 2019-07-24 stsp {
6157 0ebf8283 2019-07-24 stsp const struct got_error *err;
6158 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6159 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6160 0ebf8283 2019-07-24 stsp
6161 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6162 0ebf8283 2019-07-24 stsp if (err)
6163 0ebf8283 2019-07-24 stsp return err;
6164 0ebf8283 2019-07-24 stsp
6165 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6166 0ebf8283 2019-07-24 stsp if (err)
6167 0ebf8283 2019-07-24 stsp goto done;
6168 0ebf8283 2019-07-24 stsp
6169 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6170 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6171 0ebf8283 2019-07-24 stsp done:
6172 0ebf8283 2019-07-24 stsp if (commit_ref)
6173 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6174 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6175 0ebf8283 2019-07-24 stsp return err;
6176 0ebf8283 2019-07-24 stsp }
6177 0ebf8283 2019-07-24 stsp
6178 0ebf8283 2019-07-24 stsp const struct got_error *
6179 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6180 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6181 818c7501 2019-07-11 stsp {
6182 3e3a69f1 2019-07-25 stsp if (fileindex)
6183 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6184 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6185 69844fba 2019-07-11 stsp }
6186 69844fba 2019-07-11 stsp
6187 69844fba 2019-07-11 stsp static const struct got_error *
6188 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6189 69844fba 2019-07-11 stsp {
6190 69844fba 2019-07-11 stsp const struct got_error *err;
6191 69844fba 2019-07-11 stsp struct got_reference *ref;
6192 69844fba 2019-07-11 stsp
6193 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6194 69844fba 2019-07-11 stsp if (err) {
6195 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6196 69844fba 2019-07-11 stsp return NULL;
6197 69844fba 2019-07-11 stsp return err;
6198 69844fba 2019-07-11 stsp }
6199 69844fba 2019-07-11 stsp
6200 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6201 69844fba 2019-07-11 stsp got_ref_close(ref);
6202 69844fba 2019-07-11 stsp return err;
6203 818c7501 2019-07-11 stsp }
6204 818c7501 2019-07-11 stsp
6205 69844fba 2019-07-11 stsp static const struct got_error *
6206 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6207 69844fba 2019-07-11 stsp {
6208 69844fba 2019-07-11 stsp const struct got_error *err;
6209 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6210 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6211 69844fba 2019-07-11 stsp
6212 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6213 69844fba 2019-07-11 stsp if (err)
6214 69844fba 2019-07-11 stsp goto done;
6215 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6216 69844fba 2019-07-11 stsp if (err)
6217 69844fba 2019-07-11 stsp goto done;
6218 69844fba 2019-07-11 stsp
6219 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6220 69844fba 2019-07-11 stsp if (err)
6221 69844fba 2019-07-11 stsp goto done;
6222 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6223 69844fba 2019-07-11 stsp if (err)
6224 69844fba 2019-07-11 stsp goto done;
6225 69844fba 2019-07-11 stsp
6226 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6227 69844fba 2019-07-11 stsp if (err)
6228 69844fba 2019-07-11 stsp goto done;
6229 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6230 69844fba 2019-07-11 stsp if (err)
6231 69844fba 2019-07-11 stsp goto done;
6232 69844fba 2019-07-11 stsp
6233 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6234 69844fba 2019-07-11 stsp if (err)
6235 69844fba 2019-07-11 stsp goto done;
6236 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6237 69844fba 2019-07-11 stsp if (err)
6238 69844fba 2019-07-11 stsp goto done;
6239 69844fba 2019-07-11 stsp
6240 69844fba 2019-07-11 stsp done:
6241 69844fba 2019-07-11 stsp free(tmp_branch_name);
6242 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6243 69844fba 2019-07-11 stsp free(branch_ref_name);
6244 69844fba 2019-07-11 stsp free(commit_ref_name);
6245 69844fba 2019-07-11 stsp return err;
6246 69844fba 2019-07-11 stsp }
6247 69844fba 2019-07-11 stsp
6248 818c7501 2019-07-11 stsp const struct got_error *
6249 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6250 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6251 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6252 818c7501 2019-07-11 stsp struct got_repository *repo)
6253 818c7501 2019-07-11 stsp {
6254 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6255 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6256 818c7501 2019-07-11 stsp
6257 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6258 818c7501 2019-07-11 stsp if (err)
6259 818c7501 2019-07-11 stsp return err;
6260 818c7501 2019-07-11 stsp
6261 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6262 818c7501 2019-07-11 stsp if (err)
6263 818c7501 2019-07-11 stsp goto done;
6264 818c7501 2019-07-11 stsp
6265 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6266 818c7501 2019-07-11 stsp if (err)
6267 818c7501 2019-07-11 stsp goto done;
6268 818c7501 2019-07-11 stsp
6269 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6270 818c7501 2019-07-11 stsp if (err)
6271 818c7501 2019-07-11 stsp goto done;
6272 818c7501 2019-07-11 stsp
6273 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6274 818c7501 2019-07-11 stsp done:
6275 3e3a69f1 2019-07-25 stsp if (fileindex)
6276 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6277 818c7501 2019-07-11 stsp free(new_head_commit_id);
6278 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6279 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6280 818c7501 2019-07-11 stsp err = unlockerr;
6281 818c7501 2019-07-11 stsp return err;
6282 818c7501 2019-07-11 stsp }
6283 818c7501 2019-07-11 stsp
6284 818c7501 2019-07-11 stsp const struct got_error *
6285 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6286 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6287 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6288 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6289 818c7501 2019-07-11 stsp {
6290 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6291 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6292 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6293 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6294 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6295 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6296 818c7501 2019-07-11 stsp
6297 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6298 818c7501 2019-07-11 stsp if (err)
6299 818c7501 2019-07-11 stsp return err;
6300 818c7501 2019-07-11 stsp
6301 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6302 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6303 818c7501 2019-07-11 stsp if (err)
6304 818c7501 2019-07-11 stsp goto done;
6305 818c7501 2019-07-11 stsp
6306 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6307 818c7501 2019-07-11 stsp if (err)
6308 818c7501 2019-07-11 stsp goto done;
6309 818c7501 2019-07-11 stsp
6310 818c7501 2019-07-11 stsp /*
6311 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6312 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6313 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6314 818c7501 2019-07-11 stsp */
6315 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6316 818c7501 2019-07-11 stsp if (err)
6317 818c7501 2019-07-11 stsp goto done;
6318 818c7501 2019-07-11 stsp
6319 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6320 818c7501 2019-07-11 stsp if (err)
6321 818c7501 2019-07-11 stsp goto done;
6322 818c7501 2019-07-11 stsp
6323 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6324 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6325 ca355955 2019-07-12 stsp if (err)
6326 ca355955 2019-07-12 stsp goto done;
6327 ca355955 2019-07-12 stsp
6328 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6329 ca355955 2019-07-12 stsp if (err)
6330 ca355955 2019-07-12 stsp goto done;
6331 ca355955 2019-07-12 stsp
6332 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6333 818c7501 2019-07-11 stsp if (err)
6334 818c7501 2019-07-11 stsp goto done;
6335 818c7501 2019-07-11 stsp
6336 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6337 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6338 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6339 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6340 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6341 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6342 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6343 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6344 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6345 55bd499d 2019-07-12 stsp if (err)
6346 1f1abb7e 2019-08-08 stsp goto sync;
6347 55bd499d 2019-07-12 stsp
6348 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6349 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6350 ca355955 2019-07-12 stsp sync:
6351 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6352 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6353 ca355955 2019-07-12 stsp err = sync_err;
6354 818c7501 2019-07-11 stsp done:
6355 818c7501 2019-07-11 stsp got_ref_close(resolved);
6356 a3a2faf2 2019-07-12 stsp free(tree_id);
6357 818c7501 2019-07-11 stsp free(commit_id);
6358 0ebf8283 2019-07-24 stsp if (fileindex)
6359 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6360 0ebf8283 2019-07-24 stsp free(fileindex_path);
6361 0ebf8283 2019-07-24 stsp
6362 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6363 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6364 0ebf8283 2019-07-24 stsp err = unlockerr;
6365 0ebf8283 2019-07-24 stsp return err;
6366 0ebf8283 2019-07-24 stsp }
6367 0ebf8283 2019-07-24 stsp
6368 0ebf8283 2019-07-24 stsp const struct got_error *
6369 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6370 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6371 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6372 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6373 0ebf8283 2019-07-24 stsp {
6374 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6375 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6376 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6377 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6378 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6379 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6380 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6381 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6382 0ebf8283 2019-07-24 stsp
6383 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6384 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6385 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6386 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6387 0ebf8283 2019-07-24 stsp
6388 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6389 0ebf8283 2019-07-24 stsp if (err)
6390 0ebf8283 2019-07-24 stsp return err;
6391 0ebf8283 2019-07-24 stsp
6392 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6393 0ebf8283 2019-07-24 stsp if (err)
6394 0ebf8283 2019-07-24 stsp goto done;
6395 0ebf8283 2019-07-24 stsp
6396 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6397 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6398 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6399 0ebf8283 2019-07-24 stsp &ok_arg);
6400 0ebf8283 2019-07-24 stsp if (err)
6401 0ebf8283 2019-07-24 stsp goto done;
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6404 0ebf8283 2019-07-24 stsp if (err)
6405 0ebf8283 2019-07-24 stsp goto done;
6406 0ebf8283 2019-07-24 stsp
6407 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6408 0ebf8283 2019-07-24 stsp if (err)
6409 0ebf8283 2019-07-24 stsp goto done;
6410 0ebf8283 2019-07-24 stsp
6411 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6412 0ebf8283 2019-07-24 stsp worktree);
6413 0ebf8283 2019-07-24 stsp if (err)
6414 0ebf8283 2019-07-24 stsp goto done;
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6417 0ebf8283 2019-07-24 stsp 0);
6418 0ebf8283 2019-07-24 stsp if (err)
6419 0ebf8283 2019-07-24 stsp goto done;
6420 0ebf8283 2019-07-24 stsp
6421 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6422 0ebf8283 2019-07-24 stsp if (err)
6423 0ebf8283 2019-07-24 stsp goto done;
6424 0ebf8283 2019-07-24 stsp
6425 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6426 0ebf8283 2019-07-24 stsp if (err)
6427 0ebf8283 2019-07-24 stsp goto done;
6428 0ebf8283 2019-07-24 stsp
6429 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6430 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6431 0ebf8283 2019-07-24 stsp if (err)
6432 0ebf8283 2019-07-24 stsp goto done;
6433 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6434 0ebf8283 2019-07-24 stsp if (err)
6435 0ebf8283 2019-07-24 stsp goto done;
6436 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6437 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6438 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6439 0ebf8283 2019-07-24 stsp goto done;
6440 0ebf8283 2019-07-24 stsp }
6441 0ebf8283 2019-07-24 stsp
6442 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6443 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6444 0ebf8283 2019-07-24 stsp if (err)
6445 0ebf8283 2019-07-24 stsp goto done;
6446 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6447 0ebf8283 2019-07-24 stsp if (err)
6448 0ebf8283 2019-07-24 stsp goto done;
6449 0ebf8283 2019-07-24 stsp
6450 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6451 0ebf8283 2019-07-24 stsp if (err)
6452 0ebf8283 2019-07-24 stsp goto done;
6453 0ebf8283 2019-07-24 stsp done:
6454 0ebf8283 2019-07-24 stsp free(fileindex_path);
6455 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6456 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6457 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6458 0ebf8283 2019-07-24 stsp if (wt_branch)
6459 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6460 0ebf8283 2019-07-24 stsp if (err) {
6461 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6462 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6463 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6464 0ebf8283 2019-07-24 stsp }
6465 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6466 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6467 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6468 0ebf8283 2019-07-24 stsp }
6469 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6470 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6471 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6472 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6473 3e3a69f1 2019-07-25 stsp }
6474 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6475 0ebf8283 2019-07-24 stsp }
6476 0ebf8283 2019-07-24 stsp return err;
6477 0ebf8283 2019-07-24 stsp }
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp const struct got_error *
6480 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6481 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6482 0ebf8283 2019-07-24 stsp {
6483 3e3a69f1 2019-07-25 stsp if (fileindex)
6484 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6485 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6486 0ebf8283 2019-07-24 stsp }
6487 0ebf8283 2019-07-24 stsp
6488 0ebf8283 2019-07-24 stsp const struct got_error *
6489 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6490 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6491 0ebf8283 2019-07-24 stsp {
6492 0ebf8283 2019-07-24 stsp const struct got_error *err;
6493 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6494 0ebf8283 2019-07-24 stsp
6495 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6496 0ebf8283 2019-07-24 stsp if (err)
6497 0ebf8283 2019-07-24 stsp return err;
6498 0ebf8283 2019-07-24 stsp
6499 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6500 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6501 0ebf8283 2019-07-24 stsp return NULL;
6502 0ebf8283 2019-07-24 stsp }
6503 0ebf8283 2019-07-24 stsp
6504 0ebf8283 2019-07-24 stsp const struct got_error *
6505 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6506 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6507 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6508 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6509 0ebf8283 2019-07-24 stsp {
6510 0ebf8283 2019-07-24 stsp const struct got_error *err;
6511 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6512 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6513 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6514 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6515 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6516 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6517 0ebf8283 2019-07-24 stsp
6518 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6519 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6520 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6521 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6522 0ebf8283 2019-07-24 stsp
6523 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6524 3e3a69f1 2019-07-25 stsp if (err)
6525 3e3a69f1 2019-07-25 stsp return err;
6526 3e3a69f1 2019-07-25 stsp
6527 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6528 3e3a69f1 2019-07-25 stsp if (err)
6529 f032f1f7 2019-08-04 stsp goto done;
6530 f032f1f7 2019-08-04 stsp
6531 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6532 f032f1f7 2019-08-04 stsp &have_staged_files);
6533 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6534 f032f1f7 2019-08-04 stsp goto done;
6535 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6536 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6537 3e3a69f1 2019-07-25 stsp goto done;
6538 f032f1f7 2019-08-04 stsp }
6539 3e3a69f1 2019-07-25 stsp
6540 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6541 0ebf8283 2019-07-24 stsp if (err)
6542 f032f1f7 2019-08-04 stsp goto done;
6543 0ebf8283 2019-07-24 stsp
6544 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6545 0ebf8283 2019-07-24 stsp if (err)
6546 0ebf8283 2019-07-24 stsp goto done;
6547 0ebf8283 2019-07-24 stsp
6548 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6549 0ebf8283 2019-07-24 stsp if (err)
6550 0ebf8283 2019-07-24 stsp goto done;
6551 0ebf8283 2019-07-24 stsp
6552 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6553 0ebf8283 2019-07-24 stsp worktree);
6554 0ebf8283 2019-07-24 stsp if (err)
6555 0ebf8283 2019-07-24 stsp goto done;
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6558 0ebf8283 2019-07-24 stsp if (err)
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp
6561 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6562 0ebf8283 2019-07-24 stsp if (err)
6563 0ebf8283 2019-07-24 stsp goto done;
6564 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6565 0ebf8283 2019-07-24 stsp if (err)
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp
6568 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6569 0ebf8283 2019-07-24 stsp if (err)
6570 0ebf8283 2019-07-24 stsp goto done;
6571 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6572 0ebf8283 2019-07-24 stsp if (err)
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6576 0ebf8283 2019-07-24 stsp if (err)
6577 0ebf8283 2019-07-24 stsp goto done;
6578 0ebf8283 2019-07-24 stsp done:
6579 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6580 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6581 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6582 0ebf8283 2019-07-24 stsp if (commit_ref)
6583 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6584 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6585 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6586 0ebf8283 2019-07-24 stsp if (err) {
6587 0ebf8283 2019-07-24 stsp free(*commit_id);
6588 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6589 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6590 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6591 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6592 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6593 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6594 0ebf8283 2019-07-24 stsp }
6595 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6596 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6597 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6598 3e3a69f1 2019-07-25 stsp }
6599 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6600 0ebf8283 2019-07-24 stsp }
6601 0ebf8283 2019-07-24 stsp return err;
6602 0ebf8283 2019-07-24 stsp }
6603 0ebf8283 2019-07-24 stsp
6604 0ebf8283 2019-07-24 stsp static const struct got_error *
6605 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6606 0ebf8283 2019-07-24 stsp {
6607 0ebf8283 2019-07-24 stsp const struct got_error *err;
6608 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6609 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6610 0ebf8283 2019-07-24 stsp
6611 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6612 0ebf8283 2019-07-24 stsp if (err)
6613 0ebf8283 2019-07-24 stsp goto done;
6614 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6615 0ebf8283 2019-07-24 stsp if (err)
6616 0ebf8283 2019-07-24 stsp goto done;
6617 0ebf8283 2019-07-24 stsp
6618 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6619 0ebf8283 2019-07-24 stsp worktree);
6620 0ebf8283 2019-07-24 stsp if (err)
6621 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6623 0ebf8283 2019-07-24 stsp if (err)
6624 0ebf8283 2019-07-24 stsp goto done;
6625 0ebf8283 2019-07-24 stsp
6626 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6627 0ebf8283 2019-07-24 stsp if (err)
6628 0ebf8283 2019-07-24 stsp goto done;
6629 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6630 0ebf8283 2019-07-24 stsp if (err)
6631 0ebf8283 2019-07-24 stsp goto done;
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6634 0ebf8283 2019-07-24 stsp if (err)
6635 0ebf8283 2019-07-24 stsp goto done;
6636 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6637 0ebf8283 2019-07-24 stsp if (err)
6638 0ebf8283 2019-07-24 stsp goto done;
6639 0ebf8283 2019-07-24 stsp done:
6640 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6641 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6642 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6643 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6644 0ebf8283 2019-07-24 stsp return err;
6645 0ebf8283 2019-07-24 stsp }
6646 0ebf8283 2019-07-24 stsp
6647 0ebf8283 2019-07-24 stsp const struct got_error *
6648 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6649 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6650 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6651 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6652 0ebf8283 2019-07-24 stsp {
6653 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6654 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6655 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6656 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6657 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6658 0ebf8283 2019-07-24 stsp
6659 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6660 0ebf8283 2019-07-24 stsp if (err)
6661 0ebf8283 2019-07-24 stsp return err;
6662 0ebf8283 2019-07-24 stsp
6663 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6664 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6665 0ebf8283 2019-07-24 stsp if (err)
6666 0ebf8283 2019-07-24 stsp goto done;
6667 0ebf8283 2019-07-24 stsp
6668 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6669 0ebf8283 2019-07-24 stsp if (err)
6670 0ebf8283 2019-07-24 stsp goto done;
6671 0ebf8283 2019-07-24 stsp
6672 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6673 0ebf8283 2019-07-24 stsp if (err)
6674 0ebf8283 2019-07-24 stsp goto done;
6675 0ebf8283 2019-07-24 stsp
6676 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6677 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6678 0ebf8283 2019-07-24 stsp if (err)
6679 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp
6681 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6682 0ebf8283 2019-07-24 stsp if (err)
6683 0ebf8283 2019-07-24 stsp goto done;
6684 0ebf8283 2019-07-24 stsp
6685 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6686 0ebf8283 2019-07-24 stsp if (err)
6687 0ebf8283 2019-07-24 stsp goto done;
6688 0ebf8283 2019-07-24 stsp
6689 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6690 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6691 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6692 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6693 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6694 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6695 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6696 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6697 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6698 0ebf8283 2019-07-24 stsp if (err)
6699 1f1abb7e 2019-08-08 stsp goto sync;
6700 0ebf8283 2019-07-24 stsp
6701 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6702 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6703 0ebf8283 2019-07-24 stsp sync:
6704 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6705 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6706 0ebf8283 2019-07-24 stsp err = sync_err;
6707 0ebf8283 2019-07-24 stsp done:
6708 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6709 0ebf8283 2019-07-24 stsp free(tree_id);
6710 818c7501 2019-07-11 stsp free(fileindex_path);
6711 818c7501 2019-07-11 stsp
6712 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6713 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6714 818c7501 2019-07-11 stsp err = unlockerr;
6715 818c7501 2019-07-11 stsp return err;
6716 818c7501 2019-07-11 stsp }
6717 0ebf8283 2019-07-24 stsp
6718 0ebf8283 2019-07-24 stsp const struct got_error *
6719 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6720 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6721 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6722 0ebf8283 2019-07-24 stsp {
6723 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6724 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6725 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6726 0ebf8283 2019-07-24 stsp
6727 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6728 0ebf8283 2019-07-24 stsp if (err)
6729 0ebf8283 2019-07-24 stsp return err;
6730 0ebf8283 2019-07-24 stsp
6731 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6732 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6733 0ebf8283 2019-07-24 stsp if (err)
6734 0ebf8283 2019-07-24 stsp goto done;
6735 0ebf8283 2019-07-24 stsp
6736 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6737 0ebf8283 2019-07-24 stsp if (err)
6738 0ebf8283 2019-07-24 stsp goto done;
6739 0ebf8283 2019-07-24 stsp
6740 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6741 0ebf8283 2019-07-24 stsp if (err)
6742 0ebf8283 2019-07-24 stsp goto done;
6743 0ebf8283 2019-07-24 stsp
6744 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6745 0ebf8283 2019-07-24 stsp if (err)
6746 0ebf8283 2019-07-24 stsp goto done;
6747 0ebf8283 2019-07-24 stsp
6748 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6749 0ebf8283 2019-07-24 stsp done:
6750 3e3a69f1 2019-07-25 stsp if (fileindex)
6751 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6752 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6753 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6754 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6755 0ebf8283 2019-07-24 stsp err = unlockerr;
6756 0ebf8283 2019-07-24 stsp return err;
6757 0ebf8283 2019-07-24 stsp }
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp const struct got_error *
6760 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6761 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6762 0ebf8283 2019-07-24 stsp {
6763 0ebf8283 2019-07-24 stsp const struct got_error *err;
6764 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6765 0ebf8283 2019-07-24 stsp
6766 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6767 0ebf8283 2019-07-24 stsp if (err)
6768 0ebf8283 2019-07-24 stsp return err;
6769 0ebf8283 2019-07-24 stsp
6770 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6771 0ebf8283 2019-07-24 stsp if (err)
6772 0ebf8283 2019-07-24 stsp goto done;
6773 0ebf8283 2019-07-24 stsp
6774 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6775 0ebf8283 2019-07-24 stsp done:
6776 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6777 2822a352 2019-10-15 stsp return err;
6778 2822a352 2019-10-15 stsp }
6779 2822a352 2019-10-15 stsp
6780 2822a352 2019-10-15 stsp const struct got_error *
6781 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6782 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6783 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6784 2822a352 2019-10-15 stsp struct got_repository *repo)
6785 2822a352 2019-10-15 stsp {
6786 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6787 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6788 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6789 2822a352 2019-10-15 stsp
6790 2822a352 2019-10-15 stsp *fileindex = NULL;
6791 2822a352 2019-10-15 stsp *branch_ref = NULL;
6792 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6793 2822a352 2019-10-15 stsp
6794 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6795 2822a352 2019-10-15 stsp if (err)
6796 2822a352 2019-10-15 stsp return err;
6797 2822a352 2019-10-15 stsp
6798 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6799 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6800 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6801 2822a352 2019-10-15 stsp "update -b or different branch name required");
6802 2822a352 2019-10-15 stsp goto done;
6803 2822a352 2019-10-15 stsp }
6804 2822a352 2019-10-15 stsp
6805 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6806 2822a352 2019-10-15 stsp if (err)
6807 2822a352 2019-10-15 stsp goto done;
6808 2822a352 2019-10-15 stsp
6809 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6810 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6811 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6812 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6813 2822a352 2019-10-15 stsp &ok_arg);
6814 2822a352 2019-10-15 stsp if (err)
6815 2822a352 2019-10-15 stsp goto done;
6816 2822a352 2019-10-15 stsp
6817 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6818 2822a352 2019-10-15 stsp if (err)
6819 2822a352 2019-10-15 stsp goto done;
6820 2822a352 2019-10-15 stsp
6821 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6822 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6823 2822a352 2019-10-15 stsp done:
6824 2822a352 2019-10-15 stsp if (err) {
6825 2822a352 2019-10-15 stsp if (*branch_ref) {
6826 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6827 2822a352 2019-10-15 stsp *branch_ref = NULL;
6828 2822a352 2019-10-15 stsp }
6829 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6830 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6831 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6832 2822a352 2019-10-15 stsp }
6833 2822a352 2019-10-15 stsp if (*fileindex) {
6834 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6835 2822a352 2019-10-15 stsp *fileindex = NULL;
6836 2822a352 2019-10-15 stsp }
6837 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6838 2822a352 2019-10-15 stsp }
6839 0cb83759 2019-08-03 stsp return err;
6840 0cb83759 2019-08-03 stsp }
6841 0cb83759 2019-08-03 stsp
6842 2822a352 2019-10-15 stsp const struct got_error *
6843 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6844 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6845 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6846 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6847 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6848 2822a352 2019-10-15 stsp {
6849 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6850 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6851 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6852 2822a352 2019-10-15 stsp
6853 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6854 2822a352 2019-10-15 stsp if (err)
6855 2822a352 2019-10-15 stsp goto done;
6856 2822a352 2019-10-15 stsp
6857 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6858 2822a352 2019-10-15 stsp if (err)
6859 2822a352 2019-10-15 stsp goto done;
6860 2822a352 2019-10-15 stsp
6861 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6862 2822a352 2019-10-15 stsp worktree->path_prefix);
6863 2822a352 2019-10-15 stsp if (err)
6864 2822a352 2019-10-15 stsp goto done;
6865 2822a352 2019-10-15 stsp
6866 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6867 2822a352 2019-10-15 stsp if (err)
6868 2822a352 2019-10-15 stsp goto done;
6869 2822a352 2019-10-15 stsp
6870 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6871 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6872 2822a352 2019-10-15 stsp if (err)
6873 2822a352 2019-10-15 stsp goto sync;
6874 2822a352 2019-10-15 stsp
6875 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6876 2822a352 2019-10-15 stsp if (err)
6877 2822a352 2019-10-15 stsp goto sync;
6878 2822a352 2019-10-15 stsp
6879 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6880 2822a352 2019-10-15 stsp sync:
6881 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6882 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6883 2822a352 2019-10-15 stsp err = sync_err;
6884 2822a352 2019-10-15 stsp
6885 2822a352 2019-10-15 stsp done:
6886 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6887 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6888 2822a352 2019-10-15 stsp err = unlockerr;
6889 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6890 2822a352 2019-10-15 stsp
6891 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6892 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6893 2822a352 2019-10-15 stsp err = unlockerr;
6894 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6895 2822a352 2019-10-15 stsp
6896 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6897 2822a352 2019-10-15 stsp free(fileindex_path);
6898 2822a352 2019-10-15 stsp free(tree_id);
6899 2822a352 2019-10-15 stsp
6900 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6901 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6902 2822a352 2019-10-15 stsp err = unlockerr;
6903 2822a352 2019-10-15 stsp return err;
6904 2822a352 2019-10-15 stsp }
6905 2822a352 2019-10-15 stsp
6906 2822a352 2019-10-15 stsp const struct got_error *
6907 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6908 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6909 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6910 2822a352 2019-10-15 stsp {
6911 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6912 8b692cd0 2019-10-21 stsp
6913 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6914 8b692cd0 2019-10-21 stsp
6915 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6916 8b692cd0 2019-10-21 stsp
6917 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6918 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6919 8b692cd0 2019-10-21 stsp err = unlockerr;
6920 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6921 8b692cd0 2019-10-21 stsp
6922 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6923 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6924 8b692cd0 2019-10-21 stsp err = unlockerr;
6925 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6926 8b692cd0 2019-10-21 stsp
6927 8b692cd0 2019-10-21 stsp return err;
6928 2822a352 2019-10-15 stsp }
6929 2822a352 2019-10-15 stsp
6930 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6931 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6932 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6933 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6934 2db2652d 2019-08-07 stsp struct got_repository *repo;
6935 2db2652d 2019-08-07 stsp int have_changes;
6936 2db2652d 2019-08-07 stsp };
6937 2db2652d 2019-08-07 stsp
6938 2db2652d 2019-08-07 stsp const struct got_error *
6939 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6940 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6941 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6942 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6943 735ef5ac 2019-08-03 stsp {
6944 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6945 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6946 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6947 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6948 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6949 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6950 8b13ce36 2019-08-08 stsp
6951 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6952 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6953 8b13ce36 2019-08-08 stsp return NULL;
6954 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6955 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6956 735ef5ac 2019-08-03 stsp
6957 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6958 735ef5ac 2019-08-03 stsp if (ie == NULL)
6959 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6960 735ef5ac 2019-08-03 stsp
6961 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6962 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6963 735ef5ac 2019-08-03 stsp relpath) == -1)
6964 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6965 735ef5ac 2019-08-03 stsp
6966 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6967 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6968 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6969 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6970 735ef5ac 2019-08-03 stsp }
6971 735ef5ac 2019-08-03 stsp
6972 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6973 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6974 3aa5969e 2019-08-06 stsp goto done;
6975 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6976 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6977 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6978 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6979 735ef5ac 2019-08-03 stsp goto done;
6980 3aa5969e 2019-08-06 stsp }
6981 735ef5ac 2019-08-03 stsp
6982 2db2652d 2019-08-07 stsp a->have_changes = 1;
6983 2db2652d 2019-08-07 stsp
6984 735ef5ac 2019-08-03 stsp p = in_repo_path;
6985 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6986 735ef5ac 2019-08-03 stsp p++;
6987 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6988 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6989 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6990 735ef5ac 2019-08-03 stsp done:
6991 735ef5ac 2019-08-03 stsp free(in_repo_path);
6992 dc424a06 2019-08-07 stsp return err;
6993 dc424a06 2019-08-07 stsp }
6994 dc424a06 2019-08-07 stsp
6995 2db2652d 2019-08-07 stsp struct stage_path_arg {
6996 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6997 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6998 2db2652d 2019-08-07 stsp struct got_repository *repo;
6999 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7000 2db2652d 2019-08-07 stsp void *status_arg;
7001 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7002 2db2652d 2019-08-07 stsp void *patch_arg;
7003 7b5dc508 2019-10-28 stsp int staged_something;
7004 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7005 2db2652d 2019-08-07 stsp };
7006 2db2652d 2019-08-07 stsp
7007 2db2652d 2019-08-07 stsp static const struct got_error *
7008 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7009 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7010 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7011 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7012 0cb83759 2019-08-03 stsp {
7013 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7014 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7015 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7016 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7017 0cb83759 2019-08-03 stsp uint32_t stage;
7018 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7019 0aeb8099 2020-07-23 stsp struct stat sb;
7020 8b13ce36 2019-08-08 stsp
7021 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7022 8b13ce36 2019-08-08 stsp return NULL;
7023 0cb83759 2019-08-03 stsp
7024 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7025 d3e7c587 2019-08-03 stsp if (ie == NULL)
7026 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7027 0cb83759 2019-08-03 stsp
7028 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7029 2db2652d 2019-08-07 stsp relpath)== -1)
7030 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7031 0cb83759 2019-08-03 stsp
7032 0cb83759 2019-08-03 stsp switch (status) {
7033 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7034 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7035 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7036 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7037 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7038 0aeb8099 2020-07-23 stsp break;
7039 0aeb8099 2020-07-23 stsp }
7040 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7041 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7042 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7043 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7044 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7045 dc424a06 2019-08-07 stsp if (err)
7046 dc424a06 2019-08-07 stsp break;
7047 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7048 dc424a06 2019-08-07 stsp break;
7049 dc424a06 2019-08-07 stsp } else {
7050 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7051 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7052 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7053 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7054 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7055 dc424a06 2019-08-07 stsp break;
7056 dc424a06 2019-08-07 stsp }
7057 dc424a06 2019-08-07 stsp }
7058 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7059 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7060 0cb83759 2019-08-03 stsp if (err)
7061 dc424a06 2019-08-07 stsp break;
7062 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7063 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7064 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7065 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7066 0cb83759 2019-08-03 stsp else
7067 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7068 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7069 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7070 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7071 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7072 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7073 35213c7c 2020-07-23 stsp ssize_t target_len;
7074 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7075 35213c7c 2020-07-23 stsp sizeof(target_path));
7076 35213c7c 2020-07-23 stsp if (target_len == -1) {
7077 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7078 35213c7c 2020-07-23 stsp ondisk_path);
7079 35213c7c 2020-07-23 stsp break;
7080 35213c7c 2020-07-23 stsp }
7081 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7082 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7083 35213c7c 2020-07-23 stsp a->worktree->root_path);
7084 35213c7c 2020-07-23 stsp if (err)
7085 35213c7c 2020-07-23 stsp break;
7086 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7087 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7088 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7089 35213c7c 2020-07-23 stsp break;
7090 35213c7c 2020-07-23 stsp }
7091 35213c7c 2020-07-23 stsp }
7092 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7093 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7094 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7095 35213c7c 2020-07-23 stsp else
7096 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7097 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7098 0aeb8099 2020-07-23 stsp } else {
7099 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7100 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7101 0aeb8099 2020-07-23 stsp }
7102 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7103 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7104 dc424a06 2019-08-07 stsp break;
7105 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7106 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7107 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7108 0cb83759 2019-08-03 stsp break;
7109 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7110 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7111 d3e7c587 2019-08-03 stsp break;
7112 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7113 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7114 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7115 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7116 dc424a06 2019-08-07 stsp if (err)
7117 dc424a06 2019-08-07 stsp break;
7118 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7119 88f33a19 2019-08-08 stsp break;
7120 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7121 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7122 dc424a06 2019-08-07 stsp break;
7123 88f33a19 2019-08-08 stsp }
7124 dc424a06 2019-08-07 stsp }
7125 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7126 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7127 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7128 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7129 dc424a06 2019-08-07 stsp break;
7130 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7131 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7132 12463d8b 2019-12-13 stsp de_name);
7133 0cb83759 2019-08-03 stsp break;
7134 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7135 d3e7c587 2019-08-03 stsp break;
7136 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7137 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7138 ebf48fd5 2019-08-03 stsp break;
7139 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7140 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7141 2a06fe5f 2019-08-24 stsp break;
7142 0cb83759 2019-08-03 stsp default:
7143 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7144 537ac44b 2019-08-03 stsp break;
7145 0cb83759 2019-08-03 stsp }
7146 dc424a06 2019-08-07 stsp
7147 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7148 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7149 dc424a06 2019-08-07 stsp free(path_content);
7150 2db2652d 2019-08-07 stsp free(ondisk_path);
7151 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7152 0ebf8283 2019-07-24 stsp return err;
7153 0ebf8283 2019-07-24 stsp }
7154 0cb83759 2019-08-03 stsp
7155 0cb83759 2019-08-03 stsp const struct got_error *
7156 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7157 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7158 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7159 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7160 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7161 0cb83759 2019-08-03 stsp {
7162 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7163 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7164 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7165 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7166 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7167 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7168 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7169 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7170 0cb83759 2019-08-03 stsp
7171 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7172 0cb83759 2019-08-03 stsp if (err)
7173 0cb83759 2019-08-03 stsp return err;
7174 0cb83759 2019-08-03 stsp
7175 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7176 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7177 735ef5ac 2019-08-03 stsp if (err)
7178 735ef5ac 2019-08-03 stsp goto done;
7179 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7180 735ef5ac 2019-08-03 stsp if (err)
7181 735ef5ac 2019-08-03 stsp goto done;
7182 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7183 0cb83759 2019-08-03 stsp if (err)
7184 0cb83759 2019-08-03 stsp goto done;
7185 0cb83759 2019-08-03 stsp
7186 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7187 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7188 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7189 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7190 2db2652d 2019-08-07 stsp oka.repo = repo;
7191 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7192 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7193 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7194 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7195 735ef5ac 2019-08-03 stsp if (err)
7196 735ef5ac 2019-08-03 stsp goto done;
7197 735ef5ac 2019-08-03 stsp }
7198 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7199 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7200 2db2652d 2019-08-07 stsp goto done;
7201 2db2652d 2019-08-07 stsp }
7202 735ef5ac 2019-08-03 stsp
7203 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7204 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7205 2db2652d 2019-08-07 stsp spa.repo = repo;
7206 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7207 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7208 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7209 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7210 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7211 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7212 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7213 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7214 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7215 42005733 2019-08-03 stsp if (err)
7216 2db2652d 2019-08-07 stsp goto done;
7217 7b5dc508 2019-10-28 stsp }
7218 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7219 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7220 7b5dc508 2019-10-28 stsp goto done;
7221 0cb83759 2019-08-03 stsp }
7222 0cb83759 2019-08-03 stsp
7223 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7224 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7225 0cb83759 2019-08-03 stsp err = sync_err;
7226 0cb83759 2019-08-03 stsp done:
7227 735ef5ac 2019-08-03 stsp if (head_ref)
7228 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7229 735ef5ac 2019-08-03 stsp free(head_commit_id);
7230 0cb83759 2019-08-03 stsp free(fileindex_path);
7231 0cb83759 2019-08-03 stsp if (fileindex)
7232 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7233 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7234 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7235 0cb83759 2019-08-03 stsp err = unlockerr;
7236 0cb83759 2019-08-03 stsp return err;
7237 0cb83759 2019-08-03 stsp }
7238 ad493afc 2019-08-03 stsp
7239 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7240 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7241 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7242 ad493afc 2019-08-03 stsp struct got_repository *repo;
7243 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7244 ad493afc 2019-08-03 stsp void *progress_arg;
7245 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7246 2e1f37b0 2019-08-08 stsp void *patch_arg;
7247 ad493afc 2019-08-03 stsp };
7248 2e1f37b0 2019-08-08 stsp
7249 2e1f37b0 2019-08-08 stsp static const struct got_error *
7250 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7251 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7252 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7253 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7254 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7255 2e1f37b0 2019-08-08 stsp {
7256 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7257 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7258 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7259 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7260 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7261 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7262 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7263 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7264 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7265 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7266 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7267 2e1f37b0 2019-08-08 stsp
7268 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7269 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7270 2e1f37b0 2019-08-08 stsp
7271 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7272 2e1f37b0 2019-08-08 stsp if (err)
7273 2e1f37b0 2019-08-08 stsp return err;
7274 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7275 2e1f37b0 2019-08-08 stsp if (err)
7276 2e1f37b0 2019-08-08 stsp goto done;
7277 2e1f37b0 2019-08-08 stsp
7278 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7279 2e1f37b0 2019-08-08 stsp if (err)
7280 2e1f37b0 2019-08-08 stsp goto done;
7281 2e1f37b0 2019-08-08 stsp
7282 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7283 2e1f37b0 2019-08-08 stsp if (err)
7284 2e1f37b0 2019-08-08 stsp goto done;
7285 2e1f37b0 2019-08-08 stsp
7286 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7287 2e1f37b0 2019-08-08 stsp if (err)
7288 2e1f37b0 2019-08-08 stsp goto done;
7289 2e1f37b0 2019-08-08 stsp
7290 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7291 2e1f37b0 2019-08-08 stsp if (err)
7292 2e1f37b0 2019-08-08 stsp goto done;
7293 ad493afc 2019-08-03 stsp
7294 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7295 2e1f37b0 2019-08-08 stsp if (err)
7296 2e1f37b0 2019-08-08 stsp goto done;
7297 2e1f37b0 2019-08-08 stsp
7298 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7299 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7300 2e1f37b0 2019-08-08 stsp goto done;
7301 2e1f37b0 2019-08-08 stsp }
7302 2e1f37b0 2019-08-08 stsp
7303 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7304 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7305 2e1f37b0 2019-08-08 stsp goto done;
7306 2e1f37b0 2019-08-08 stsp }
7307 2e1f37b0 2019-08-08 stsp
7308 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7309 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7310 2e1f37b0 2019-08-08 stsp if (err)
7311 2e1f37b0 2019-08-08 stsp goto done;
7312 2e1f37b0 2019-08-08 stsp
7313 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7314 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7315 2e1f37b0 2019-08-08 stsp if (err)
7316 2e1f37b0 2019-08-08 stsp goto done;
7317 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7318 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7319 2e1f37b0 2019-08-08 stsp if (err)
7320 2e1f37b0 2019-08-08 stsp goto done;
7321 2e1f37b0 2019-08-08 stsp
7322 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7323 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7324 2e1f37b0 2019-08-08 stsp goto done;
7325 2e1f37b0 2019-08-08 stsp }
7326 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7327 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7328 2e1f37b0 2019-08-08 stsp goto done;
7329 2e1f37b0 2019-08-08 stsp }
7330 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7331 2e1f37b0 2019-08-08 stsp int choice;
7332 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7333 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7334 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7335 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7336 2e1f37b0 2019-08-08 stsp if (err)
7337 2e1f37b0 2019-08-08 stsp goto done;
7338 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7339 2e1f37b0 2019-08-08 stsp have_content = 1;
7340 19e4b907 2019-08-08 stsp else
7341 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7342 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7343 2e1f37b0 2019-08-08 stsp break;
7344 2e1f37b0 2019-08-08 stsp }
7345 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7346 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7347 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7348 2e1f37b0 2019-08-08 stsp done:
7349 2e1f37b0 2019-08-08 stsp free(label1);
7350 2e1f37b0 2019-08-08 stsp if (blob)
7351 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7352 2e1f37b0 2019-08-08 stsp if (staged_blob)
7353 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7354 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7355 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7356 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7357 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7358 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7359 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7360 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7361 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7362 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7363 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7364 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7365 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7366 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7367 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7368 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7369 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7370 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7371 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7372 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7373 2e1f37b0 2019-08-08 stsp }
7374 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7375 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7376 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7377 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7378 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7379 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7380 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7381 2e1f37b0 2019-08-08 stsp }
7382 2e1f37b0 2019-08-08 stsp free(args);
7383 2e1f37b0 2019-08-08 stsp if (ds) {
7384 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7385 2e1f37b0 2019-08-08 stsp free(ds);
7386 2e1f37b0 2019-08-08 stsp }
7387 2e1f37b0 2019-08-08 stsp if (changes)
7388 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7389 2e1f37b0 2019-08-08 stsp free(path1);
7390 2e1f37b0 2019-08-08 stsp free(path2);
7391 fda8017d 2020-07-23 stsp return err;
7392 fda8017d 2020-07-23 stsp }
7393 fda8017d 2020-07-23 stsp
7394 fda8017d 2020-07-23 stsp static const struct got_error *
7395 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7396 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7397 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7398 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7399 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7400 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7401 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7402 fda8017d 2020-07-23 stsp {
7403 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7404 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7405 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7406 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7407 fda8017d 2020-07-23 stsp FILE *f = NULL;
7408 fda8017d 2020-07-23 stsp struct stat sb;
7409 fda8017d 2020-07-23 stsp
7410 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7411 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7412 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7413 fda8017d 2020-07-23 stsp if (err)
7414 fda8017d 2020-07-23 stsp return err;
7415 fda8017d 2020-07-23 stsp
7416 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7417 fda8017d 2020-07-23 stsp return NULL;
7418 fda8017d 2020-07-23 stsp
7419 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7420 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7421 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7422 fda8017d 2020-07-23 stsp if (err)
7423 fda8017d 2020-07-23 stsp goto done;
7424 fda8017d 2020-07-23 stsp }
7425 fda8017d 2020-07-23 stsp
7426 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7427 fda8017d 2020-07-23 stsp if (f == NULL) {
7428 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7429 fda8017d 2020-07-23 stsp path_unstaged_content);
7430 fda8017d 2020-07-23 stsp goto done;
7431 fda8017d 2020-07-23 stsp }
7432 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7433 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7434 fda8017d 2020-07-23 stsp goto done;
7435 fda8017d 2020-07-23 stsp }
7436 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7437 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7438 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7439 fda8017d 2020-07-23 stsp size_t r;
7440 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7441 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7442 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7443 fda8017d 2020-07-23 stsp goto done;
7444 fda8017d 2020-07-23 stsp }
7445 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7446 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7447 fda8017d 2020-07-23 stsp goto done;
7448 fda8017d 2020-07-23 stsp }
7449 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7450 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7451 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7452 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7453 fda8017d 2020-07-23 stsp progress_arg);
7454 fda8017d 2020-07-23 stsp } else {
7455 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7456 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7457 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7458 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7459 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7460 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7461 fda8017d 2020-07-23 stsp }
7462 fda8017d 2020-07-23 stsp if (err)
7463 fda8017d 2020-07-23 stsp goto done;
7464 fda8017d 2020-07-23 stsp
7465 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7466 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7467 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7468 fda8017d 2020-07-23 stsp } else
7469 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7470 fda8017d 2020-07-23 stsp done:
7471 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7472 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7473 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7474 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7475 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7476 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7477 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7478 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7479 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7480 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7481 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7482 2e1f37b0 2019-08-08 stsp return err;
7483 2e1f37b0 2019-08-08 stsp }
7484 2e1f37b0 2019-08-08 stsp
7485 ad493afc 2019-08-03 stsp static const struct got_error *
7486 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7487 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7488 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7489 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7490 ad493afc 2019-08-03 stsp {
7491 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7492 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7493 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7494 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7495 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7496 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7497 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7498 9bc94a15 2019-08-03 stsp struct stat sb;
7499 ad493afc 2019-08-03 stsp
7500 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7501 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7502 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7503 2e1f37b0 2019-08-08 stsp return NULL;
7504 2e1f37b0 2019-08-08 stsp
7505 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7506 ad493afc 2019-08-03 stsp if (ie == NULL)
7507 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7508 9bc94a15 2019-08-03 stsp
7509 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7510 9bc94a15 2019-08-03 stsp == -1)
7511 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7512 ad493afc 2019-08-03 stsp
7513 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7514 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7515 f69721c3 2019-10-21 stsp if (err)
7516 f69721c3 2019-10-21 stsp goto done;
7517 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7518 f69721c3 2019-10-21 stsp id_str) == -1) {
7519 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7520 f69721c3 2019-10-21 stsp goto done;
7521 f69721c3 2019-10-21 stsp }
7522 f69721c3 2019-10-21 stsp
7523 ad493afc 2019-08-03 stsp switch (staged_status) {
7524 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7525 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7526 ad493afc 2019-08-03 stsp blob_id, 8192);
7527 ad493afc 2019-08-03 stsp if (err)
7528 ad493afc 2019-08-03 stsp break;
7529 ad493afc 2019-08-03 stsp /* fall through */
7530 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7531 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7532 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7533 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7534 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7535 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7536 2e1f37b0 2019-08-08 stsp if (err)
7537 2e1f37b0 2019-08-08 stsp break;
7538 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7539 2e1f37b0 2019-08-08 stsp break;
7540 2e1f37b0 2019-08-08 stsp } else {
7541 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7542 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7543 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7544 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7545 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7546 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7547 2e1f37b0 2019-08-08 stsp }
7548 2e1f37b0 2019-08-08 stsp }
7549 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7550 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7551 ad493afc 2019-08-03 stsp if (err)
7552 ad493afc 2019-08-03 stsp break;
7553 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7554 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7555 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7556 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7557 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7558 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7559 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7560 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7561 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7562 ea7786be 2020-07-23 stsp break;
7563 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7564 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7565 36bf999c 2020-07-23 stsp char *staged_target;
7566 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7567 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7568 36bf999c 2020-07-23 stsp if (err)
7569 36bf999c 2020-07-23 stsp goto done;
7570 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7571 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7572 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7573 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7574 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7575 36bf999c 2020-07-23 stsp free(staged_target);
7576 dfe9fba0 2020-07-23 stsp } else {
7577 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7578 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7579 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7580 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7581 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7582 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7583 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7584 dfe9fba0 2020-07-23 stsp }
7585 ea7786be 2020-07-23 stsp break;
7586 ea7786be 2020-07-23 stsp default:
7587 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7588 ea7786be 2020-07-23 stsp break;
7589 ea7786be 2020-07-23 stsp }
7590 ad493afc 2019-08-03 stsp if (err == NULL)
7591 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7592 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7593 ad493afc 2019-08-03 stsp break;
7594 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7595 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7596 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7597 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7598 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7599 2e1f37b0 2019-08-08 stsp if (err)
7600 2e1f37b0 2019-08-08 stsp break;
7601 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7602 2e1f37b0 2019-08-08 stsp break;
7603 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7604 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7605 2e1f37b0 2019-08-08 stsp break;
7606 2e1f37b0 2019-08-08 stsp }
7607 2e1f37b0 2019-08-08 stsp }
7608 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7609 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7610 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7611 9bc94a15 2019-08-03 stsp if (err)
7612 9bc94a15 2019-08-03 stsp break;
7613 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7614 ad493afc 2019-08-03 stsp break;
7615 ad493afc 2019-08-03 stsp }
7616 f69721c3 2019-10-21 stsp done:
7617 ad493afc 2019-08-03 stsp free(ondisk_path);
7618 ad493afc 2019-08-03 stsp if (blob_base)
7619 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7620 ad493afc 2019-08-03 stsp if (blob_staged)
7621 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7622 f69721c3 2019-10-21 stsp free(id_str);
7623 f69721c3 2019-10-21 stsp free(label_orig);
7624 ad493afc 2019-08-03 stsp return err;
7625 ad493afc 2019-08-03 stsp }
7626 ad493afc 2019-08-03 stsp
7627 ad493afc 2019-08-03 stsp const struct got_error *
7628 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7629 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7630 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7631 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7632 ad493afc 2019-08-03 stsp struct got_repository *repo)
7633 ad493afc 2019-08-03 stsp {
7634 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7635 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7636 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7637 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7638 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7639 ad493afc 2019-08-03 stsp
7640 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7641 ad493afc 2019-08-03 stsp if (err)
7642 ad493afc 2019-08-03 stsp return err;
7643 ad493afc 2019-08-03 stsp
7644 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7645 ad493afc 2019-08-03 stsp if (err)
7646 ad493afc 2019-08-03 stsp goto done;
7647 ad493afc 2019-08-03 stsp
7648 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7649 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7650 ad493afc 2019-08-03 stsp upa.repo = repo;
7651 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7652 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7653 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7654 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7655 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7656 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7657 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7658 ad493afc 2019-08-03 stsp if (err)
7659 ad493afc 2019-08-03 stsp goto done;
7660 ad493afc 2019-08-03 stsp }
7661 ad493afc 2019-08-03 stsp
7662 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7663 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7664 ad493afc 2019-08-03 stsp err = sync_err;
7665 ad493afc 2019-08-03 stsp done:
7666 ad493afc 2019-08-03 stsp free(fileindex_path);
7667 ad493afc 2019-08-03 stsp if (fileindex)
7668 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7669 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7670 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7671 ad493afc 2019-08-03 stsp err = unlockerr;
7672 ad493afc 2019-08-03 stsp return err;
7673 ad493afc 2019-08-03 stsp }