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 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 1d0f4054 2021-06-17 stsp if (repo) {
440 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
441 1d0f4054 2021-06-17 stsp if (err == NULL)
442 1d0f4054 2021-06-17 stsp err = close_err;
443 1d0f4054 2021-06-17 stsp }
444 7ac97322 2018-03-11 stsp free(path_got);
445 056e7441 2018-03-11 stsp free(path_lock);
446 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
447 c442a90d 2019-03-10 stsp free(uuidstr);
448 bd165944 2019-03-10 stsp free(formatstr);
449 6d9d28c3 2018-03-11 stsp if (err) {
450 6d9d28c3 2018-03-11 stsp if (fd != -1)
451 6d9d28c3 2018-03-11 stsp close(fd);
452 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
453 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
454 6d9d28c3 2018-03-11 stsp *worktree = NULL;
455 6d9d28c3 2018-03-11 stsp } else
456 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
457 6d9d28c3 2018-03-11 stsp
458 6d9d28c3 2018-03-11 stsp return err;
459 86c3caaf 2018-03-09 stsp }
460 247140b2 2019-02-05 stsp
461 247140b2 2019-02-05 stsp const struct got_error *
462 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
463 247140b2 2019-02-05 stsp {
464 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
465 aedea96d 2020-10-20 stsp char *worktree_path;
466 86c3caaf 2018-03-09 stsp
467 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
468 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
469 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp for (;;) {
472 aedea96d 2020-10-20 stsp char *parent_path;
473 aedea96d 2020-10-20 stsp
474 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
475 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 aedea96d 2020-10-20 stsp free(worktree_path);
477 247140b2 2019-02-05 stsp return err;
478 aedea96d 2020-10-20 stsp }
479 aedea96d 2020-10-20 stsp if (*worktree) {
480 aedea96d 2020-10-20 stsp free(worktree_path);
481 aedea96d 2020-10-20 stsp return NULL;
482 aedea96d 2020-10-20 stsp }
483 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 aedea96d 2020-10-20 stsp break;
485 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
486 aedea96d 2020-10-20 stsp if (err) {
487 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 aedea96d 2020-10-20 stsp return err;
490 aedea96d 2020-10-20 stsp }
491 aedea96d 2020-10-20 stsp break;
492 aedea96d 2020-10-20 stsp }
493 aedea96d 2020-10-20 stsp free(worktree_path);
494 aedea96d 2020-10-20 stsp worktree_path = parent_path;
495 aedea96d 2020-10-20 stsp }
496 247140b2 2019-02-05 stsp
497 aedea96d 2020-10-20 stsp free(worktree_path);
498 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
499 247140b2 2019-02-05 stsp }
500 247140b2 2019-02-05 stsp
501 3a6ce05a 2019-02-11 stsp const struct got_error *
502 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
503 86c3caaf 2018-03-09 stsp {
504 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
505 60e40e95 2021-01-22 stsp
506 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
507 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
510 a6b21eef 2021-01-21 stsp }
511 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
512 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
513 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
514 60e40e95 2021-01-22 stsp free(worktree->repo_path);
515 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
516 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
517 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
518 60e40e95 2021-01-22 stsp free(worktree->root_path);
519 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
520 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
521 a06ff56f 2021-01-21 naddy free(worktree);
522 3a6ce05a 2019-02-11 stsp return err;
523 86c3caaf 2018-03-09 stsp }
524 86c3caaf 2018-03-09 stsp
525 2fbdb5ae 2018-12-29 stsp const char *
526 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
527 c7f4312f 2019-02-05 stsp {
528 c7f4312f 2019-02-05 stsp return worktree->root_path;
529 c7f4312f 2019-02-05 stsp }
530 c7f4312f 2019-02-05 stsp
531 c7f4312f 2019-02-05 stsp const char *
532 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
533 86c3caaf 2018-03-09 stsp {
534 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
535 49520a32 2018-12-29 stsp }
536 49520a32 2018-12-29 stsp const char *
537 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
538 49520a32 2018-12-29 stsp {
539 f609be2e 2018-12-29 stsp return worktree->path_prefix;
540 e5dc7198 2018-12-29 stsp }
541 e5dc7198 2018-12-29 stsp
542 e5dc7198 2018-12-29 stsp const struct got_error *
543 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 e5dc7198 2018-12-29 stsp const char *path_prefix)
545 e5dc7198 2018-12-29 stsp {
546 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
547 e5dc7198 2018-12-29 stsp
548 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
549 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
551 e5dc7198 2018-12-29 stsp }
552 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
553 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
554 e5dc7198 2018-12-29 stsp free(absprefix);
555 e5dc7198 2018-12-29 stsp return NULL;
556 86c3caaf 2018-03-09 stsp }
557 86c3caaf 2018-03-09 stsp
558 bc70eb79 2019-05-09 stsp const char *
559 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
560 86c3caaf 2018-03-09 stsp {
561 36a38700 2019-05-10 stsp return worktree->head_ref_name;
562 024e9686 2019-05-14 stsp }
563 024e9686 2019-05-14 stsp
564 024e9686 2019-05-14 stsp const struct got_error *
565 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
566 024e9686 2019-05-14 stsp struct got_reference *head_ref)
567 024e9686 2019-05-14 stsp {
568 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
569 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
570 024e9686 2019-05-14 stsp
571 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
573 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
574 024e9686 2019-05-14 stsp path_got = NULL;
575 024e9686 2019-05-14 stsp goto done;
576 024e9686 2019-05-14 stsp }
577 024e9686 2019-05-14 stsp
578 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
579 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
580 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
581 024e9686 2019-05-14 stsp goto done;
582 024e9686 2019-05-14 stsp }
583 024e9686 2019-05-14 stsp
584 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
585 024e9686 2019-05-14 stsp if (err)
586 024e9686 2019-05-14 stsp goto done;
587 024e9686 2019-05-14 stsp
588 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
589 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
590 024e9686 2019-05-14 stsp done:
591 024e9686 2019-05-14 stsp free(path_got);
592 024e9686 2019-05-14 stsp if (err)
593 024e9686 2019-05-14 stsp free(head_ref_name);
594 024e9686 2019-05-14 stsp return err;
595 507dc3bb 2018-12-29 stsp }
596 507dc3bb 2018-12-29 stsp
597 b72f483a 2019-02-05 stsp struct got_object_id *
598 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
599 507dc3bb 2018-12-29 stsp {
600 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
601 86c3caaf 2018-03-09 stsp }
602 86c3caaf 2018-03-09 stsp
603 507dc3bb 2018-12-29 stsp const struct got_error *
604 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
606 507dc3bb 2018-12-29 stsp {
607 507dc3bb 2018-12-29 stsp const struct got_error *err;
608 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
609 507dc3bb 2018-12-29 stsp char *id_str = NULL;
610 507dc3bb 2018-12-29 stsp char *path_got = NULL;
611 507dc3bb 2018-12-29 stsp
612 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
615 507dc3bb 2018-12-29 stsp path_got = NULL;
616 507dc3bb 2018-12-29 stsp goto done;
617 507dc3bb 2018-12-29 stsp }
618 507dc3bb 2018-12-29 stsp
619 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
620 507dc3bb 2018-12-29 stsp if (err)
621 507dc3bb 2018-12-29 stsp return err;
622 507dc3bb 2018-12-29 stsp
623 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
625 507dc3bb 2018-12-29 stsp goto done;
626 507dc3bb 2018-12-29 stsp }
627 507dc3bb 2018-12-29 stsp
628 507dc3bb 2018-12-29 stsp /* Record our base commit. */
629 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
630 507dc3bb 2018-12-29 stsp if (err)
631 507dc3bb 2018-12-29 stsp goto done;
632 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 507dc3bb 2018-12-29 stsp if (err)
634 507dc3bb 2018-12-29 stsp goto done;
635 507dc3bb 2018-12-29 stsp
636 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
637 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
638 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
640 507dc3bb 2018-12-29 stsp goto done;
641 507dc3bb 2018-12-29 stsp }
642 507dc3bb 2018-12-29 stsp done:
643 507dc3bb 2018-12-29 stsp if (obj)
644 507dc3bb 2018-12-29 stsp got_object_close(obj);
645 507dc3bb 2018-12-29 stsp free(id_str);
646 507dc3bb 2018-12-29 stsp free(path_got);
647 507dc3bb 2018-12-29 stsp return err;
648 50b0790e 2020-09-11 stsp }
649 50b0790e 2020-09-11 stsp
650 50b0790e 2020-09-11 stsp const struct got_gotconfig *
651 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
652 50b0790e 2020-09-11 stsp {
653 50b0790e 2020-09-11 stsp return worktree->gotconfig;
654 507dc3bb 2018-12-29 stsp }
655 507dc3bb 2018-12-29 stsp
656 9d31a1d8 2018-03-11 stsp static const struct got_error *
657 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
658 86c3caaf 2018-03-09 stsp {
659 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
662 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
663 86c3caaf 2018-03-09 stsp return NULL;
664 21908da4 2019-01-13 stsp }
665 21908da4 2019-01-13 stsp
666 21908da4 2019-01-13 stsp static const struct got_error *
667 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
668 4a1ddfc2 2019-01-12 stsp {
669 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
670 4a1ddfc2 2019-01-12 stsp char *abspath;
671 4a1ddfc2 2019-01-12 stsp
672 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
674 4a1ddfc2 2019-01-12 stsp
675 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
676 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 ddcd8544 2019-03-11 stsp struct stat sb;
678 ddcd8544 2019-03-11 stsp err = NULL;
679 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
680 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
681 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
682 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
683 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
684 ddcd8544 2019-03-11 stsp }
685 ddcd8544 2019-03-11 stsp }
686 4a1ddfc2 2019-01-12 stsp free(abspath);
687 68c76935 2019-02-19 stsp return err;
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp
690 68c76935 2019-02-19 stsp static const struct got_error *
691 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
692 68c76935 2019-02-19 stsp {
693 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
694 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
695 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
696 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
697 68c76935 2019-02-19 stsp
698 68c76935 2019-02-19 stsp *same = 1;
699 68c76935 2019-02-19 stsp
700 230a42bd 2019-05-11 jcs for (;;) {
701 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
703 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
704 68c76935 2019-02-19 stsp break;
705 68c76935 2019-02-19 stsp }
706 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
708 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
709 68c76935 2019-02-19 stsp break;
710 68c76935 2019-02-19 stsp }
711 68c76935 2019-02-19 stsp if (flen1 == 0) {
712 68c76935 2019-02-19 stsp if (flen2 != 0)
713 68c76935 2019-02-19 stsp *same = 0;
714 68c76935 2019-02-19 stsp break;
715 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
716 68c76935 2019-02-19 stsp if (flen1 != 0)
717 68c76935 2019-02-19 stsp *same = 0;
718 68c76935 2019-02-19 stsp break;
719 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
720 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 68c76935 2019-02-19 stsp *same = 0;
722 68c76935 2019-02-19 stsp break;
723 68c76935 2019-02-19 stsp }
724 68c76935 2019-02-19 stsp } else {
725 68c76935 2019-02-19 stsp *same = 0;
726 68c76935 2019-02-19 stsp break;
727 68c76935 2019-02-19 stsp }
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp return err;
731 68c76935 2019-02-19 stsp }
732 68c76935 2019-02-19 stsp
733 68c76935 2019-02-19 stsp static const struct got_error *
734 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
735 68c76935 2019-02-19 stsp {
736 68c76935 2019-02-19 stsp struct stat sb;
737 68c76935 2019-02-19 stsp size_t size1, size2;
738 68c76935 2019-02-19 stsp
739 68c76935 2019-02-19 stsp *same = 1;
740 68c76935 2019-02-19 stsp
741 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
742 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
743 68c76935 2019-02-19 stsp size1 = sb.st_size;
744 68c76935 2019-02-19 stsp
745 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
746 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
747 68c76935 2019-02-19 stsp size2 = sb.st_size;
748 68c76935 2019-02-19 stsp
749 68c76935 2019-02-19 stsp if (size1 != size2) {
750 68c76935 2019-02-19 stsp *same = 0;
751 68c76935 2019-02-19 stsp return NULL;
752 68c76935 2019-02-19 stsp }
753 68c76935 2019-02-19 stsp
754 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
755 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
756 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
757 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
758 68c76935 2019-02-19 stsp
759 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
760 6353ad76 2019-02-08 stsp }
761 6353ad76 2019-02-08 stsp
762 6353ad76 2019-02-08 stsp /*
763 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
764 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
765 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
766 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
767 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
768 6353ad76 2019-02-08 stsp */
769 6353ad76 2019-02-08 stsp static const struct got_error *
770 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
773 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
776 6353ad76 2019-02-08 stsp {
777 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
778 6353ad76 2019-02-08 stsp int merged_fd = -1;
779 db590691 2021-06-02 stsp FILE *f_merged = NULL;
780 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
781 234035bc 2019-06-01 stsp int overlapcnt = 0;
782 3524bbf9 2020-10-20 stsp char *parent = NULL;
783 6353ad76 2019-02-08 stsp
784 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
785 234035bc 2019-06-01 stsp
786 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
787 3524bbf9 2020-10-20 stsp if (err)
788 3524bbf9 2020-10-20 stsp return err;
789 af54ae4a 2019-02-19 stsp
790 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
792 3524bbf9 2020-10-20 stsp goto done;
793 3524bbf9 2020-10-20 stsp }
794 af54ae4a 2019-02-19 stsp
795 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 6353ad76 2019-02-08 stsp if (err)
797 6353ad76 2019-02-08 stsp goto done;
798 3b9f0f87 2020-07-23 stsp
799 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 6353ad76 2019-02-08 stsp if (err)
802 6353ad76 2019-02-08 stsp goto done;
803 6353ad76 2019-02-08 stsp
804 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
805 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 1ee397ad 2019-07-12 stsp if (err)
807 1ee397ad 2019-07-12 stsp goto done;
808 6353ad76 2019-02-08 stsp
809 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
811 816dc654 2019-02-16 stsp goto done;
812 68c76935 2019-02-19 stsp }
813 68c76935 2019-02-19 stsp
814 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
815 db590691 2021-06-02 stsp if (f_merged == NULL) {
816 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
817 db590691 2021-06-02 stsp goto done;
818 db590691 2021-06-02 stsp }
819 db590691 2021-06-02 stsp merged_fd = -1;
820 db590691 2021-06-02 stsp
821 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
822 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
823 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
824 db590691 2021-06-02 stsp f_merged);
825 68c76935 2019-02-19 stsp if (err)
826 68c76935 2019-02-19 stsp goto done;
827 816dc654 2019-02-16 stsp }
828 6353ad76 2019-02-08 stsp
829 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
830 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
831 70a0c8ec 2019-02-20 stsp goto done;
832 70a0c8ec 2019-02-20 stsp }
833 70a0c8ec 2019-02-20 stsp
834 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
835 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
836 230a42bd 2019-05-11 jcs ondisk_path);
837 6353ad76 2019-02-08 stsp goto done;
838 6353ad76 2019-02-08 stsp }
839 6353ad76 2019-02-08 stsp done:
840 fdcb7daf 2019-12-15 stsp if (err) {
841 fdcb7daf 2019-12-15 stsp if (merged_path)
842 fdcb7daf 2019-12-15 stsp unlink(merged_path);
843 3b9f0f87 2020-07-23 stsp }
844 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
846 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
848 6353ad76 2019-02-08 stsp free(merged_path);
849 af54ae4a 2019-02-19 stsp free(base_path);
850 3524bbf9 2020-10-20 stsp free(parent);
851 af57b12a 2020-07-23 stsp return err;
852 af57b12a 2020-07-23 stsp }
853 af57b12a 2020-07-23 stsp
854 af57b12a 2020-07-23 stsp static const struct got_error *
855 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
856 af57b12a 2020-07-23 stsp size_t target_len)
857 af57b12a 2020-07-23 stsp {
858 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
859 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
860 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
861 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
862 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
863 af57b12a 2020-07-23 stsp ondisk_path);
864 af57b12a 2020-07-23 stsp return NULL;
865 af57b12a 2020-07-23 stsp }
866 af57b12a 2020-07-23 stsp
867 af57b12a 2020-07-23 stsp /*
868 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
870 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
871 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
872 993e2a1b 2020-07-23 stsp *
873 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
874 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
875 993e2a1b 2020-07-23 stsp *
876 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
877 993e2a1b 2020-07-23 stsp * has deleted this symlink.
878 11cc08c1 2020-07-23 stsp */
879 11cc08c1 2020-07-23 stsp static const struct got_error *
880 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
881 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
883 11cc08c1 2020-07-23 stsp {
884 11cc08c1 2020-07-23 stsp const struct got_error *err;
885 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 11cc08c1 2020-07-23 stsp FILE *f = NULL;
887 11cc08c1 2020-07-23 stsp
888 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
889 11cc08c1 2020-07-23 stsp if (err)
890 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
891 11cc08c1 2020-07-23 stsp
892 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
893 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
895 11cc08c1 2020-07-23 stsp goto done;
896 11cc08c1 2020-07-23 stsp }
897 11cc08c1 2020-07-23 stsp
898 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 11cc08c1 2020-07-23 stsp if (err)
900 3818e3c4 2020-11-01 naddy goto done;
901 3818e3c4 2020-11-01 naddy
902 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
904 11cc08c1 2020-07-23 stsp goto done;
905 3818e3c4 2020-11-01 naddy }
906 11cc08c1 2020-07-23 stsp
907 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
910 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
911 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
912 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
913 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
914 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
915 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
917 11cc08c1 2020-07-23 stsp goto done;
918 11cc08c1 2020-07-23 stsp }
919 11cc08c1 2020-07-23 stsp
920 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
921 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
922 11cc08c1 2020-07-23 stsp goto done;
923 11cc08c1 2020-07-23 stsp }
924 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
925 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
926 11cc08c1 2020-07-23 stsp goto done;
927 11cc08c1 2020-07-23 stsp }
928 11cc08c1 2020-07-23 stsp done:
929 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
930 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
931 11cc08c1 2020-07-23 stsp free(path);
932 11cc08c1 2020-07-23 stsp free(id_str);
933 11cc08c1 2020-07-23 stsp free(label_deriv);
934 11cc08c1 2020-07-23 stsp return err;
935 11cc08c1 2020-07-23 stsp }
936 d219f183 2020-07-23 stsp
937 d219f183 2020-07-23 stsp /* forward declaration */
938 d219f183 2020-07-23 stsp static const struct got_error *
939 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
941 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
942 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
943 11cc08c1 2020-07-23 stsp
944 11cc08c1 2020-07-23 stsp /*
945 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
946 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
947 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
948 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
949 af57b12a 2020-07-23 stsp */
950 af57b12a 2020-07-23 stsp static const struct got_error *
951 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
952 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
953 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
954 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
956 af57b12a 2020-07-23 stsp {
957 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
958 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
959 af57b12a 2020-07-23 stsp struct stat sb;
960 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
961 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
962 11cc08c1 2020-07-23 stsp int have_local_change = 0;
963 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
964 af57b12a 2020-07-23 stsp
965 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
966 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
967 af57b12a 2020-07-23 stsp
968 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
969 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
970 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
971 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
972 af57b12a 2020-07-23 stsp ondisk_path);
973 af57b12a 2020-07-23 stsp goto done;
974 af57b12a 2020-07-23 stsp }
975 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
976 af57b12a 2020-07-23 stsp
977 526a746f 2020-07-23 stsp if (blob_orig) {
978 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 526a746f 2020-07-23 stsp if (err)
980 526a746f 2020-07-23 stsp goto done;
981 526a746f 2020-07-23 stsp }
982 af57b12a 2020-07-23 stsp
983 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
984 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
985 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 11cc08c1 2020-07-23 stsp have_local_change = 1;
987 11cc08c1 2020-07-23 stsp
988 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
993 11cc08c1 2020-07-23 stsp
994 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
995 11cc08c1 2020-07-23 stsp if (ancestor_target) {
996 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
997 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 11cc08c1 2020-07-23 stsp path);
999 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1000 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1002 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 11cc08c1 2020-07-23 stsp path);
1004 11cc08c1 2020-07-23 stsp } else {
1005 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1006 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1007 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1008 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1009 11cc08c1 2020-07-23 stsp if (err)
1010 11cc08c1 2020-07-23 stsp goto done;
1011 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 11cc08c1 2020-07-23 stsp path);
1013 11cc08c1 2020-07-23 stsp }
1014 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1015 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1016 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1017 af57b12a 2020-07-23 stsp strlen(deriv_target));
1018 af57b12a 2020-07-23 stsp if (err)
1019 af57b12a 2020-07-23 stsp goto done;
1020 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1022 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1023 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1025 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 ea7786be 2020-07-23 stsp path);
1027 ea7786be 2020-07-23 stsp } else {
1028 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1031 ea7786be 2020-07-23 stsp if (err)
1032 ea7786be 2020-07-23 stsp goto done;
1033 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 ea7786be 2020-07-23 stsp path);
1035 ea7786be 2020-07-23 stsp }
1036 6353ad76 2019-02-08 stsp }
1037 11cc08c1 2020-07-23 stsp
1038 af57b12a 2020-07-23 stsp done:
1039 af57b12a 2020-07-23 stsp free(ancestor_target);
1040 eec2f5a9 2021-06-03 stsp return err;
1041 eec2f5a9 2021-06-03 stsp }
1042 eec2f5a9 2021-06-03 stsp
1043 eec2f5a9 2021-06-03 stsp static const struct got_error *
1044 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1045 eec2f5a9 2021-06-03 stsp {
1046 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1047 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1048 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1049 eec2f5a9 2021-06-03 stsp size_t n;
1050 eec2f5a9 2021-06-03 stsp FILE *f;
1051 eec2f5a9 2021-06-03 stsp
1052 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1053 eec2f5a9 2021-06-03 stsp
1054 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1055 eec2f5a9 2021-06-03 stsp if (f == NULL)
1056 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1057 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1059 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp }
1062 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1063 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1064 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1065 eec2f5a9 2021-06-03 stsp goto done;
1066 eec2f5a9 2021-06-03 stsp }
1067 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1069 eec2f5a9 2021-06-03 stsp goto done;
1070 eec2f5a9 2021-06-03 stsp }
1071 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1072 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1073 eec2f5a9 2021-06-03 stsp goto done;
1074 eec2f5a9 2021-06-03 stsp }
1075 eec2f5a9 2021-06-03 stsp done:
1076 eec2f5a9 2021-06-03 stsp if (err)
1077 eec2f5a9 2021-06-03 stsp fclose(f);
1078 eec2f5a9 2021-06-03 stsp else
1079 eec2f5a9 2021-06-03 stsp *outfile = f;
1080 14c901f1 2019-08-08 stsp return err;
1081 14c901f1 2019-08-08 stsp }
1082 14c901f1 2019-08-08 stsp
1083 14c901f1 2019-08-08 stsp /*
1084 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1086 14c901f1 2019-08-08 stsp * acts as the second derived version.
1087 14c901f1 2019-08-08 stsp */
1088 14c901f1 2019-08-08 stsp static const struct got_error *
1089 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1091 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1092 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1093 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1095 14c901f1 2019-08-08 stsp {
1096 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1097 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1099 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1101 14c901f1 2019-08-08 stsp
1102 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1103 14c901f1 2019-08-08 stsp
1104 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1105 ed6b5030 2020-10-20 stsp if (err)
1106 ed6b5030 2020-10-20 stsp return err;
1107 14c901f1 2019-08-08 stsp
1108 67a66647 2021-05-31 stsp if (blob_orig) {
1109 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 67a66647 2021-05-31 stsp parent) == -1) {
1111 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1112 67a66647 2021-05-31 stsp base_path = NULL;
1113 67a66647 2021-05-31 stsp goto done;
1114 67a66647 2021-05-31 stsp }
1115 67a66647 2021-05-31 stsp
1116 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 67a66647 2021-05-31 stsp if (err)
1118 67a66647 2021-05-31 stsp goto done;
1119 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 67a66647 2021-05-31 stsp blob_orig);
1121 67a66647 2021-05-31 stsp if (err)
1122 67a66647 2021-05-31 stsp goto done;
1123 67a66647 2021-05-31 stsp free(base_path);
1124 db590691 2021-06-02 stsp } else {
1125 db590691 2021-06-02 stsp /*
1126 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1127 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1128 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1129 db590691 2021-06-02 stsp */
1130 db590691 2021-06-02 stsp f_orig = got_opentemp();
1131 db590691 2021-06-02 stsp if (f_orig == NULL) {
1132 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1133 db590691 2021-06-02 stsp goto done;
1134 db590691 2021-06-02 stsp }
1135 67a66647 2021-05-31 stsp }
1136 67a66647 2021-05-31 stsp
1137 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1139 14c901f1 2019-08-08 stsp base_path = NULL;
1140 14c901f1 2019-08-08 stsp goto done;
1141 14c901f1 2019-08-08 stsp }
1142 14c901f1 2019-08-08 stsp
1143 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 14c901f1 2019-08-08 stsp if (err)
1145 14c901f1 2019-08-08 stsp goto done;
1146 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 14c901f1 2019-08-08 stsp blob_deriv);
1148 14c901f1 2019-08-08 stsp if (err)
1149 14c901f1 2019-08-08 stsp goto done;
1150 14c901f1 2019-08-08 stsp
1151 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 14c901f1 2019-08-08 stsp if (err)
1153 14c901f1 2019-08-08 stsp goto done;
1154 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1155 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1157 14c901f1 2019-08-08 stsp goto done;
1158 eec2f5a9 2021-06-03 stsp }
1159 eec2f5a9 2021-06-03 stsp
1160 eec2f5a9 2021-06-03 stsp /*
1161 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1162 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1163 eec2f5a9 2021-06-03 stsp */
1164 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1165 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 eec2f5a9 2021-06-03 stsp if (err)
1167 eec2f5a9 2021-06-03 stsp goto done;
1168 eec2f5a9 2021-06-03 stsp } else {
1169 eec2f5a9 2021-06-03 stsp int fd;
1170 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1172 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1173 eec2f5a9 2021-06-03 stsp goto done;
1174 eec2f5a9 2021-06-03 stsp }
1175 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1176 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1177 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1178 eec2f5a9 2021-06-03 stsp close(fd);
1179 eec2f5a9 2021-06-03 stsp goto done;
1180 eec2f5a9 2021-06-03 stsp }
1181 14c901f1 2019-08-08 stsp }
1182 14c901f1 2019-08-08 stsp
1183 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 14c901f1 2019-08-08 stsp done:
1187 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1189 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1191 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1193 14c901f1 2019-08-08 stsp free(base_path);
1194 67a66647 2021-05-31 stsp if (blob_orig_path) {
1195 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1196 67a66647 2021-05-31 stsp free(blob_orig_path);
1197 67a66647 2021-05-31 stsp }
1198 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1199 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1200 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1201 14c901f1 2019-08-08 stsp }
1202 6353ad76 2019-02-08 stsp free(id_str);
1203 818c7501 2019-07-11 stsp free(label_deriv);
1204 ed6b5030 2020-10-20 stsp free(parent);
1205 4a1ddfc2 2019-01-12 stsp return err;
1206 4a1ddfc2 2019-01-12 stsp }
1207 4a1ddfc2 2019-01-12 stsp
1208 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1209 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1212 13d9040b 2019-03-27 stsp {
1213 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1214 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1215 13d9040b 2019-03-27 stsp
1216 65b05cec 2020-07-23 stsp *new_iep = NULL;
1217 65b05cec 2020-07-23 stsp
1218 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1219 054041d0 2020-06-24 stsp if (err)
1220 054041d0 2020-06-24 stsp return err;
1221 054041d0 2020-06-24 stsp
1222 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1224 054041d0 2020-06-24 stsp if (err)
1225 054041d0 2020-06-24 stsp goto done;
1226 054041d0 2020-06-24 stsp
1227 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1228 054041d0 2020-06-24 stsp done:
1229 054041d0 2020-06-24 stsp if (err)
1230 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1231 65b05cec 2020-07-23 stsp else
1232 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1233 13d9040b 2019-03-27 stsp return err;
1234 1ebedb77 2019-10-19 stsp }
1235 1ebedb77 2019-10-19 stsp
1236 1ebedb77 2019-10-19 stsp static mode_t
1237 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1238 1ebedb77 2019-10-19 stsp {
1239 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1240 1ebedb77 2019-10-19 stsp
1241 1ebedb77 2019-10-19 stsp if (executable) {
1242 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1243 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1244 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1245 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1246 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1247 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1248 1ebedb77 2019-10-19 stsp }
1249 1ebedb77 2019-10-19 stsp
1250 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1251 13d9040b 2019-03-27 stsp }
1252 13d9040b 2019-03-27 stsp
1253 8ba819a3 2020-07-23 stsp /* forward declaration */
1254 13d9040b 2019-03-27 stsp static const struct got_error *
1255 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1257 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1258 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1259 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1260 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1261 8ba819a3 2020-07-23 stsp
1262 5a1fbc73 2020-07-23 stsp /*
1263 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1264 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1265 5a1fbc73 2020-07-23 stsp */
1266 8ba819a3 2020-07-23 stsp static const struct got_error *
1267 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1269 5a1fbc73 2020-07-23 stsp {
1270 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1271 5a1fbc73 2020-07-23 stsp ssize_t elen;
1272 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1273 5a1fbc73 2020-07-23 stsp int fd;
1274 5a1fbc73 2020-07-23 stsp
1275 c6e8a826 2021-04-05 stsp *did_something = 0;
1276 c6e8a826 2021-04-05 stsp
1277 5a1fbc73 2020-07-23 stsp /*
1278 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1279 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1280 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1281 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1282 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1283 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1284 5a1fbc73 2020-07-23 stsp */
1285 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1287 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1288 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1289 5a1fbc73 2020-07-23 stsp
1290 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1291 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 5a1fbc73 2020-07-23 stsp if (elen == -1)
1293 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1294 5a1fbc73 2020-07-23 stsp
1295 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1296 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1297 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1298 5a1fbc73 2020-07-23 stsp }
1299 5a1fbc73 2020-07-23 stsp
1300 c6e8a826 2021-04-05 stsp *did_something = 1;
1301 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1302 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1303 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1304 5a1fbc73 2020-07-23 stsp return err;
1305 3c1ec782 2020-07-23 stsp }
1306 3c1ec782 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp static const struct got_error *
1308 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1310 3c1ec782 2020-07-23 stsp {
1311 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1312 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1313 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1314 3c1ec782 2020-07-23 stsp
1315 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1316 3c1ec782 2020-07-23 stsp
1317 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1318 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1319 3c1ec782 2020-07-23 stsp return NULL;
1320 3c1ec782 2020-07-23 stsp }
1321 3c1ec782 2020-07-23 stsp
1322 3c1ec782 2020-07-23 stsp /*
1323 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1324 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1325 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1326 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1327 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1328 3c1ec782 2020-07-23 stsp */
1329 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1330 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1331 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1332 ce031e9e 2020-10-20 stsp if (err)
1333 ce031e9e 2020-10-20 stsp return err;
1334 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 ce031e9e 2020-10-20 stsp free(parent);
1336 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1337 ce031e9e 2020-10-20 stsp }
1338 ce031e9e 2020-10-20 stsp free(parent);
1339 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1340 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 3c1ec782 2020-07-23 stsp free(abspath);
1342 3c1ec782 2020-07-23 stsp return err;
1343 3c1ec782 2020-07-23 stsp }
1344 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 3c1ec782 2020-07-23 stsp free(abspath);
1346 3c1ec782 2020-07-23 stsp if (err)
1347 3c1ec782 2020-07-23 stsp return err;
1348 3c1ec782 2020-07-23 stsp } else {
1349 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 3c1ec782 2020-07-23 stsp if (err)
1351 3c1ec782 2020-07-23 stsp return err;
1352 3c1ec782 2020-07-23 stsp }
1353 3c1ec782 2020-07-23 stsp
1354 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1355 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1357 3c1ec782 2020-07-23 stsp return NULL;
1358 3c1ec782 2020-07-23 stsp }
1359 3c1ec782 2020-07-23 stsp
1360 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1361 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1363 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1364 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1366 3c1ec782 2020-07-23 stsp
1367 3c1ec782 2020-07-23 stsp free(path_got);
1368 3c1ec782 2020-07-23 stsp return NULL;
1369 5a1fbc73 2020-07-23 stsp }
1370 5a1fbc73 2020-07-23 stsp
1371 5a1fbc73 2020-07-23 stsp static const struct got_error *
1372 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1375 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1376 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1377 9d31a1d8 2018-03-11 stsp {
1378 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1379 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1380 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1381 906c123b 2020-07-23 stsp char *path_got = NULL;
1382 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1384 8ba819a3 2020-07-23 stsp
1385 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1386 2e1fa222 2020-07-23 stsp
1387 8ba819a3 2020-07-23 stsp /*
1388 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1389 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1390 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1391 8ba819a3 2020-07-23 stsp * in the blob object.
1392 8ba819a3 2020-07-23 stsp */
1393 8ba819a3 2020-07-23 stsp do {
1394 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1395 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1396 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1397 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1398 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1399 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1400 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1402 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1403 3b9f0f87 2020-07-23 stsp progress_arg);
1404 8ba819a3 2020-07-23 stsp }
1405 8ba819a3 2020-07-23 stsp if (len > 0) {
1406 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1407 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1408 8ba819a3 2020-07-23 stsp len - hdrlen);
1409 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1410 8ba819a3 2020-07-23 stsp hdrlen = 0;
1411 8ba819a3 2020-07-23 stsp }
1412 8ba819a3 2020-07-23 stsp } while (len != 0);
1413 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1414 8ba819a3 2020-07-23 stsp
1415 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1417 3c1ec782 2020-07-23 stsp if (err)
1418 3c1ec782 2020-07-23 stsp return err;
1419 8ba819a3 2020-07-23 stsp
1420 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1421 906c123b 2020-07-23 stsp /* install as a regular file */
1422 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1423 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1424 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1426 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1427 906c123b 2020-07-23 stsp goto done;
1428 906c123b 2020-07-23 stsp }
1429 906c123b 2020-07-23 stsp
1430 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1431 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1432 c6e8a826 2021-04-05 stsp int symlink_replaced;
1433 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1434 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1435 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1436 c90c8ce3 2020-07-23 stsp goto done;
1437 c90c8ce3 2020-07-23 stsp }
1438 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1439 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1440 5a1fbc73 2020-07-23 stsp if (err)
1441 f35fa46a 2020-07-23 stsp goto done;
1442 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1443 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1444 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1445 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1446 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1447 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1448 c6e8a826 2021-04-05 stsp } else {
1449 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1450 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1451 c6e8a826 2021-04-05 stsp }
1452 f35fa46a 2020-07-23 stsp }
1453 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1454 f35fa46a 2020-07-23 stsp }
1455 f35fa46a 2020-07-23 stsp
1456 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1457 f4994adc 2020-10-20 stsp char *parent;
1458 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1459 f4994adc 2020-10-20 stsp if (err)
1460 f4994adc 2020-10-20 stsp goto done;
1461 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1462 f4994adc 2020-10-20 stsp free(parent);
1463 f35fa46a 2020-07-23 stsp if (err)
1464 f35fa46a 2020-07-23 stsp goto done;
1465 f35fa46a 2020-07-23 stsp /*
1466 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1467 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1468 f35fa46a 2020-07-23 stsp */
1469 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1470 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1471 f35fa46a 2020-07-23 stsp goto done;
1472 f35fa46a 2020-07-23 stsp }
1473 f35fa46a 2020-07-23 stsp }
1474 f35fa46a 2020-07-23 stsp
1475 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1476 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1477 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1478 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1479 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1480 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1481 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1483 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1484 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1485 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1486 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1487 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1488 8ba819a3 2020-07-23 stsp } else {
1489 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1490 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1491 8ba819a3 2020-07-23 stsp }
1492 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1493 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1495 8ba819a3 2020-07-23 stsp done:
1496 906c123b 2020-07-23 stsp free(path_got);
1497 8ba819a3 2020-07-23 stsp return err;
1498 8ba819a3 2020-07-23 stsp }
1499 8ba819a3 2020-07-23 stsp
1500 8ba819a3 2020-07-23 stsp static const struct got_error *
1501 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1503 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1504 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1505 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1506 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1507 8ba819a3 2020-07-23 stsp {
1508 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1509 507dc3bb 2018-12-29 stsp int fd = -1;
1510 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1511 507dc3bb 2018-12-29 stsp int update = 0;
1512 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1513 8ba819a3 2020-07-23 stsp
1514 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1516 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1517 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1518 f5375317 2020-10-20 stsp char *parent;
1519 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1520 f5375317 2020-10-20 stsp if (err)
1521 f5375317 2020-10-20 stsp return err;
1522 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1523 f5375317 2020-10-20 stsp free(parent);
1524 21908da4 2019-01-13 stsp if (err)
1525 21908da4 2019-01-13 stsp return err;
1526 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1527 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1529 21908da4 2019-01-13 stsp if (fd == -1)
1530 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1531 230a42bd 2019-05-11 jcs ondisk_path);
1532 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1533 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1534 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1535 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1536 3b9f0f87 2020-07-23 stsp goto done;
1537 3b9f0f87 2020-07-23 stsp }
1538 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1541 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1542 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1543 507dc3bb 2018-12-29 stsp goto done;
1544 d70b8e30 2018-12-27 stsp } else {
1545 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1546 507dc3bb 2018-12-29 stsp ondisk_path);
1547 507dc3bb 2018-12-29 stsp if (err)
1548 507dc3bb 2018-12-29 stsp goto done;
1549 507dc3bb 2018-12-29 stsp update = 1;
1550 9d31a1d8 2018-03-11 stsp }
1551 507dc3bb 2018-12-29 stsp } else
1552 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1553 9d31a1d8 2018-03-11 stsp }
1554 9d31a1d8 2018-03-11 stsp
1555 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1557 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1558 3818e3c4 2020-11-01 naddy goto done;
1559 3818e3c4 2020-11-01 naddy }
1560 3818e3c4 2020-11-01 naddy
1561 bd6aa359 2020-07-23 stsp if (progress_cb) {
1562 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1563 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1564 bd6aa359 2020-07-23 stsp path);
1565 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1566 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1567 bd6aa359 2020-07-23 stsp path);
1568 bd6aa359 2020-07-23 stsp else
1569 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1570 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1571 bd6aa359 2020-07-23 stsp if (err)
1572 bd6aa359 2020-07-23 stsp goto done;
1573 bd6aa359 2020-07-23 stsp }
1574 d7b62c98 2018-12-27 stsp
1575 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1576 9d31a1d8 2018-03-11 stsp do {
1577 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1579 9d31a1d8 2018-03-11 stsp if (err)
1580 9d31a1d8 2018-03-11 stsp break;
1581 9d31a1d8 2018-03-11 stsp if (len > 0) {
1582 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1583 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1585 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1586 b87c6f83 2018-12-24 stsp goto done;
1587 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1588 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1589 b87c6f83 2018-12-24 stsp goto done;
1590 9d31a1d8 2018-03-11 stsp }
1591 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1592 9d31a1d8 2018-03-11 stsp }
1593 9d31a1d8 2018-03-11 stsp } while (len != 0);
1594 9d31a1d8 2018-03-11 stsp
1595 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1597 816dc654 2019-02-16 stsp goto done;
1598 816dc654 2019-02-16 stsp }
1599 9d31a1d8 2018-03-11 stsp
1600 507dc3bb 2018-12-29 stsp if (update) {
1601 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1603 f6d8c0ac 2020-11-09 stsp goto done;
1604 f6d8c0ac 2020-11-09 stsp }
1605 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1606 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1607 230a42bd 2019-05-11 jcs ondisk_path);
1608 68ed9ba5 2019-02-10 stsp goto done;
1609 68ed9ba5 2019-02-10 stsp }
1610 63df146d 2020-11-09 stsp free(tmppath);
1611 63df146d 2020-11-09 stsp tmppath = NULL;
1612 507dc3bb 2018-12-29 stsp }
1613 507dc3bb 2018-12-29 stsp
1614 9d31a1d8 2018-03-11 stsp done:
1615 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1616 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1617 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1619 507dc3bb 2018-12-29 stsp free(tmppath);
1620 6353ad76 2019-02-08 stsp return err;
1621 6353ad76 2019-02-08 stsp }
1622 6353ad76 2019-02-08 stsp
1623 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 6353ad76 2019-02-08 stsp static const struct got_error *
1625 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1626 7154f6ce 2019-03-27 stsp {
1627 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1628 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1629 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1631 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1632 7154f6ce 2019-03-27 stsp };
1633 7154f6ce 2019-03-27 stsp int i = 0;
1634 9bdd68dd 2020-01-02 naddy char *line = NULL;
1635 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1636 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1637 7154f6ce 2019-03-27 stsp
1638 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1639 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1640 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1641 7154f6ce 2019-03-27 stsp if (feof(f))
1642 7154f6ce 2019-03-27 stsp break;
1643 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1644 7154f6ce 2019-03-27 stsp break;
1645 7154f6ce 2019-03-27 stsp }
1646 7154f6ce 2019-03-27 stsp
1647 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1649 19332e6d 2019-05-13 stsp == 0)
1650 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1651 7154f6ce 2019-03-27 stsp else
1652 7154f6ce 2019-03-27 stsp i++;
1653 7154f6ce 2019-03-27 stsp }
1654 7154f6ce 2019-03-27 stsp }
1655 9bdd68dd 2020-01-02 naddy free(line);
1656 7154f6ce 2019-03-27 stsp
1657 7154f6ce 2019-03-27 stsp return err;
1658 e2b1e152 2019-07-13 stsp }
1659 e2b1e152 2019-07-13 stsp
1660 e2b1e152 2019-07-13 stsp static int
1661 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1662 1ebedb77 2019-10-19 stsp {
1663 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1665 1ebedb77 2019-10-19 stsp }
1666 1ebedb77 2019-10-19 stsp
1667 1ebedb77 2019-10-19 stsp static int
1668 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1669 e2b1e152 2019-07-13 stsp {
1670 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1675 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1676 c363b2c1 2019-08-03 stsp }
1677 c363b2c1 2019-08-03 stsp
1678 c363b2c1 2019-08-03 stsp static unsigned char
1679 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1680 c363b2c1 2019-08-03 stsp {
1681 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1682 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1683 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1684 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1685 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1686 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1687 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1688 c363b2c1 2019-08-03 stsp default:
1689 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1690 a919d5c4 2020-07-23 stsp }
1691 a919d5c4 2020-07-23 stsp }
1692 a919d5c4 2020-07-23 stsp
1693 a919d5c4 2020-07-23 stsp static const struct got_error *
1694 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1695 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1696 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1697 a919d5c4 2020-07-23 stsp {
1698 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1699 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1700 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1701 a919d5c4 2020-07-23 stsp ssize_t elen;
1702 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1703 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1705 a919d5c4 2020-07-23 stsp
1706 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1707 a919d5c4 2020-07-23 stsp
1708 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1709 a919d5c4 2020-07-23 stsp do {
1710 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1711 a919d5c4 2020-07-23 stsp if (err)
1712 a919d5c4 2020-07-23 stsp return err;
1713 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1714 a919d5c4 2020-07-23 stsp /*
1715 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1716 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1717 a919d5c4 2020-07-23 stsp */
1718 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1719 a919d5c4 2020-07-23 stsp }
1720 a919d5c4 2020-07-23 stsp if (len > 0) {
1721 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1722 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1723 a919d5c4 2020-07-23 stsp len - hdrlen);
1724 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1725 a919d5c4 2020-07-23 stsp hdrlen = 0;
1726 a919d5c4 2020-07-23 stsp }
1727 a919d5c4 2020-07-23 stsp } while (len != 0);
1728 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1729 a919d5c4 2020-07-23 stsp
1730 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1731 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 a919d5c4 2020-07-23 stsp if (elen == -1)
1733 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1734 a919d5c4 2020-07-23 stsp } else {
1735 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1736 a919d5c4 2020-07-23 stsp if (elen == -1)
1737 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1738 c363b2c1 2019-08-03 stsp }
1739 a919d5c4 2020-07-23 stsp
1740 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1742 a919d5c4 2020-07-23 stsp
1743 a919d5c4 2020-07-23 stsp return NULL;
1744 7154f6ce 2019-03-27 stsp }
1745 7154f6ce 2019-03-27 stsp
1746 7154f6ce 2019-03-27 stsp static const struct got_error *
1747 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1748 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1749 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1750 6353ad76 2019-02-08 stsp {
1751 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1752 6353ad76 2019-02-08 stsp struct got_object_id id;
1753 6353ad76 2019-02-08 stsp size_t hdrlen;
1754 1338848f 2019-12-13 stsp int fd = -1;
1755 6353ad76 2019-02-08 stsp FILE *f = NULL;
1756 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1757 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1758 6353ad76 2019-02-08 stsp size_t flen, blen;
1759 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1760 6353ad76 2019-02-08 stsp
1761 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1762 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1763 6353ad76 2019-02-08 stsp
1764 7f91a133 2019-12-13 stsp /*
1765 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1766 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1767 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1768 7f91a133 2019-12-13 stsp */
1769 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1770 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1772 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1773 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1774 882ef1b9 2019-12-13 stsp else
1775 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1776 882ef1b9 2019-12-13 stsp goto done;
1777 882ef1b9 2019-12-13 stsp }
1778 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1779 3d35a492 2019-12-13 stsp goto done;
1780 3d35a492 2019-12-13 stsp }
1781 7f91a133 2019-12-13 stsp } else {
1782 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1785 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1786 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1787 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1788 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1790 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1791 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1792 3d35a492 2019-12-13 stsp else
1793 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1794 3d35a492 2019-12-13 stsp goto done;
1795 3d35a492 2019-12-13 stsp }
1796 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1797 1338848f 2019-12-13 stsp goto done;
1798 a378724f 2019-02-10 stsp }
1799 a378724f 2019-02-10 stsp }
1800 6353ad76 2019-02-08 stsp
1801 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1803 1338848f 2019-12-13 stsp goto done;
1804 3f148bc6 2019-07-27 stsp }
1805 efdd40df 2019-07-27 stsp
1806 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1808 1338848f 2019-12-13 stsp goto done;
1809 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1810 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1811 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1812 1338848f 2019-12-13 stsp goto done;
1813 d00136be 2019-03-26 stsp }
1814 b8f41171 2019-02-10 stsp
1815 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1816 f179e66d 2020-07-23 stsp goto done;
1817 f179e66d 2020-07-23 stsp
1818 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1819 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1821 1338848f 2019-12-13 stsp goto done;
1822 f179e66d 2020-07-23 stsp }
1823 6353ad76 2019-02-08 stsp
1824 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1825 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1826 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 c363b2c1 2019-08-03 stsp else
1828 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1829 c363b2c1 2019-08-03 stsp
1830 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 6353ad76 2019-02-08 stsp if (err)
1832 1338848f 2019-12-13 stsp goto done;
1833 6353ad76 2019-02-08 stsp
1834 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1835 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1836 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1837 a919d5c4 2020-07-23 stsp goto done;
1838 a919d5c4 2020-07-23 stsp }
1839 a919d5c4 2020-07-23 stsp
1840 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1841 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 ab0d4361 2019-12-13 stsp if (fd == -1) {
1843 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1844 ab0d4361 2019-12-13 stsp goto done;
1845 ab0d4361 2019-12-13 stsp }
1846 3d35a492 2019-12-13 stsp }
1847 3d35a492 2019-12-13 stsp
1848 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1849 6353ad76 2019-02-08 stsp if (f == NULL) {
1850 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1851 6353ad76 2019-02-08 stsp goto done;
1852 6353ad76 2019-02-08 stsp }
1853 1338848f 2019-12-13 stsp fd = -1;
1854 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1855 656b1f76 2019-05-11 jcs for (;;) {
1856 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1858 6353ad76 2019-02-08 stsp if (err)
1859 7154f6ce 2019-03-27 stsp goto done;
1860 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1861 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1863 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1864 7154f6ce 2019-03-27 stsp goto done;
1865 80c5c120 2019-02-19 stsp }
1866 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1867 6353ad76 2019-02-08 stsp if (flen != 0)
1868 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1869 6353ad76 2019-02-08 stsp break;
1870 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1871 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1872 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1873 6353ad76 2019-02-08 stsp break;
1874 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1875 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1876 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1878 6353ad76 2019-02-08 stsp break;
1879 6353ad76 2019-02-08 stsp }
1880 6353ad76 2019-02-08 stsp } else {
1881 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1882 6353ad76 2019-02-08 stsp break;
1883 6353ad76 2019-02-08 stsp }
1884 6353ad76 2019-02-08 stsp hdrlen = 0;
1885 6353ad76 2019-02-08 stsp }
1886 7154f6ce 2019-03-27 stsp
1887 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1888 7154f6ce 2019-03-27 stsp rewind(f);
1889 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1890 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1891 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1892 6353ad76 2019-02-08 stsp done:
1893 6353ad76 2019-02-08 stsp if (blob)
1894 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1895 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1896 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1897 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1898 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1899 9d31a1d8 2018-03-11 stsp return err;
1900 e2b1e152 2019-07-13 stsp }
1901 e2b1e152 2019-07-13 stsp
1902 e2b1e152 2019-07-13 stsp /*
1903 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1904 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1905 e2b1e152 2019-07-13 stsp */
1906 e2b1e152 2019-07-13 stsp static const struct got_error *
1907 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1909 e2b1e152 2019-07-13 stsp {
1910 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1912 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1913 e2b1e152 2019-07-13 stsp
1914 e2b1e152 2019-07-13 stsp return NULL;
1915 9d31a1d8 2018-03-11 stsp }
1916 9d31a1d8 2018-03-11 stsp
1917 9d31a1d8 2018-03-11 stsp static const struct got_error *
1918 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1919 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1921 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 0584f854 2019-04-06 stsp void *progress_arg)
1923 9d31a1d8 2018-03-11 stsp {
1924 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1925 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1926 6353ad76 2019-02-08 stsp char *ondisk_path;
1927 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1928 b8f41171 2019-02-10 stsp struct stat sb;
1929 9d31a1d8 2018-03-11 stsp
1930 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1932 6353ad76 2019-02-08 stsp
1933 abb4604f 2019-07-27 stsp if (ie) {
1934 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 a76c42e6 2019-08-03 stsp goto done;
1937 a76c42e6 2019-08-03 stsp }
1938 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 7f91a133 2019-12-13 stsp repo);
1940 abb4604f 2019-07-27 stsp if (err)
1941 abb4604f 2019-07-27 stsp goto done;
1942 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1944 c90c8ce3 2020-07-23 stsp } else {
1945 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1947 c90c8ce3 2020-07-23 stsp }
1948 6353ad76 2019-02-08 stsp
1949 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1950 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1951 b8f41171 2019-02-10 stsp goto done;
1952 b8f41171 2019-02-10 stsp }
1953 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1954 a769b60b 2021-06-27 stsp if (ie)
1955 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1956 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1957 5036ab18 2020-04-18 stsp path);
1958 5036ab18 2020-04-18 stsp goto done;
1959 5036ab18 2020-04-18 stsp }
1960 b8f41171 2019-02-10 stsp
1961 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1963 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1964 c6e8a826 2021-04-05 stsp /*
1965 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1966 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1967 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1968 c6e8a826 2021-04-05 stsp * updating contents of this file.
1969 c6e8a826 2021-04-05 stsp */
1970 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1971 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1973 c6e8a826 2021-04-05 stsp /* Same commit. */
1974 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1975 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1976 e2b1e152 2019-07-13 stsp if (err)
1977 e2b1e152 2019-07-13 stsp goto done;
1978 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 b8f41171 2019-02-10 stsp path);
1980 6353ad76 2019-02-08 stsp goto done;
1981 a378724f 2019-02-10 stsp }
1982 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1983 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1984 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1985 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1986 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1987 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1988 0f58026f 2021-04-05 stsp if (err)
1989 0f58026f 2021-04-05 stsp goto done;
1990 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 0f58026f 2021-04-05 stsp path);
1992 b8f41171 2019-02-10 stsp goto done;
1993 e2b1e152 2019-07-13 stsp }
1994 9d31a1d8 2018-03-11 stsp }
1995 9d31a1d8 2018-03-11 stsp
1996 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1997 8da9e5f4 2019-01-12 stsp if (err)
1998 6353ad76 2019-02-08 stsp goto done;
1999 512f0d0e 2019-01-02 stsp
2000 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 234035bc 2019-06-01 stsp int update_timestamps;
2002 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2003 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2004 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2005 234035bc 2019-06-01 stsp struct got_object_id id2;
2006 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2008 234035bc 2019-06-01 stsp if (err)
2009 f69721c3 2019-10-21 stsp goto done;
2010 f69721c3 2019-10-21 stsp }
2011 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2012 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2015 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2016 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2017 f69721c3 2019-10-21 stsp goto done;
2018 f69721c3 2019-10-21 stsp }
2019 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2020 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2022 234035bc 2019-06-01 stsp goto done;
2023 f69721c3 2019-10-21 stsp }
2024 234035bc 2019-06-01 stsp }
2025 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2026 36bf999c 2020-07-23 stsp char *link_target;
2027 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2028 36bf999c 2020-07-23 stsp if (err)
2029 36bf999c 2020-07-23 stsp goto done;
2030 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2032 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2033 36bf999c 2020-07-23 stsp free(link_target);
2034 993e2a1b 2020-07-23 stsp } else {
2035 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2036 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2037 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2038 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2039 993e2a1b 2020-07-23 stsp }
2040 f69721c3 2019-10-21 stsp free(label_orig);
2041 234035bc 2019-06-01 stsp if (blob2)
2042 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2043 909d120e 2019-09-22 stsp if (err)
2044 909d120e 2019-09-22 stsp goto done;
2045 234035bc 2019-06-01 stsp /*
2046 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2047 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2048 234035bc 2019-06-01 stsp * unmodified files again.
2049 234035bc 2019-06-01 stsp */
2050 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2052 234035bc 2019-06-01 stsp update_timestamps);
2053 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2057 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2058 1ee397ad 2019-07-12 stsp if (err)
2059 1ee397ad 2019-07-12 stsp goto done;
2060 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2062 13d9040b 2019-03-27 stsp if (err)
2063 13d9040b 2019-03-27 stsp goto done;
2064 13d9040b 2019-03-27 stsp } else {
2065 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2066 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2067 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2068 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2069 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2070 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2071 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2072 2e1fa222 2020-07-23 stsp } else {
2073 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2074 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2075 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2076 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2077 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2078 2e1fa222 2020-07-23 stsp }
2079 13d9040b 2019-03-27 stsp if (err)
2080 13d9040b 2019-03-27 stsp goto done;
2081 65b05cec 2020-07-23 stsp
2082 054041d0 2020-06-24 stsp if (ie) {
2083 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2084 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2085 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2086 054041d0 2020-06-24 stsp } else {
2087 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2088 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2089 054041d0 2020-06-24 stsp &blob->id);
2090 054041d0 2020-06-24 stsp }
2091 13d9040b 2019-03-27 stsp if (err)
2092 13d9040b 2019-03-27 stsp goto done;
2093 65b05cec 2020-07-23 stsp
2094 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2095 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2096 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 2e1fa222 2020-07-23 stsp }
2098 13d9040b 2019-03-27 stsp }
2099 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2100 6353ad76 2019-02-08 stsp done:
2101 6353ad76 2019-02-08 stsp free(ondisk_path);
2102 8da9e5f4 2019-01-12 stsp return err;
2103 90285c3b 2019-01-08 stsp }
2104 90285c3b 2019-01-08 stsp
2105 90285c3b 2019-01-08 stsp static const struct got_error *
2106 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2107 90285c3b 2019-01-08 stsp {
2108 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2109 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2110 90285c3b 2019-01-08 stsp
2111 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2113 90285c3b 2019-01-08 stsp
2114 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2115 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2116 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2117 c97665ae 2019-01-13 stsp } else {
2118 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2119 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2120 1c4cdd89 2021-06-20 stsp if (err)
2121 1c4cdd89 2021-06-20 stsp goto done;
2122 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2123 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2124 fddefe3b 2020-10-20 stsp free(ondisk_path);
2125 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2126 1c4cdd89 2021-06-20 stsp parent = NULL;
2127 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2128 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2129 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2130 fddefe3b 2020-10-20 stsp ondisk_path);
2131 90285c3b 2019-01-08 stsp break;
2132 90285c3b 2019-01-08 stsp }
2133 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2134 1c4cdd89 2021-06-20 stsp if (err)
2135 1c4cdd89 2021-06-20 stsp break;
2136 1c4cdd89 2021-06-20 stsp }
2137 90285c3b 2019-01-08 stsp }
2138 1c4cdd89 2021-06-20 stsp done:
2139 90285c3b 2019-01-08 stsp free(ondisk_path);
2140 1c4cdd89 2021-06-20 stsp free(parent);
2141 90285c3b 2019-01-08 stsp return err;
2142 512f0d0e 2019-01-02 stsp }
2143 512f0d0e 2019-01-02 stsp
2144 708d8e67 2019-03-27 stsp static const struct got_error *
2145 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2146 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2147 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2148 708d8e67 2019-03-27 stsp {
2149 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2150 708d8e67 2019-03-27 stsp unsigned char status;
2151 708d8e67 2019-03-27 stsp struct stat sb;
2152 708d8e67 2019-03-27 stsp char *ondisk_path;
2153 708d8e67 2019-03-27 stsp
2154 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2155 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2156 a76c42e6 2019-08-03 stsp
2157 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2158 708d8e67 2019-03-27 stsp == -1)
2159 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2160 708d8e67 2019-03-27 stsp
2161 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2162 708d8e67 2019-03-27 stsp if (err)
2163 5a58a424 2020-06-23 stsp goto done;
2164 708d8e67 2019-03-27 stsp
2165 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2166 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2167 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2168 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2169 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2170 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2171 993e2a1b 2020-07-23 stsp goto done;
2172 993e2a1b 2020-07-23 stsp }
2173 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2174 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2175 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2176 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2177 993e2a1b 2020-07-23 stsp if (err)
2178 993e2a1b 2020-07-23 stsp goto done;
2179 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2180 993e2a1b 2020-07-23 stsp ie->path);
2181 993e2a1b 2020-07-23 stsp goto done;
2182 993e2a1b 2020-07-23 stsp }
2183 993e2a1b 2020-07-23 stsp
2184 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2185 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2186 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2187 1ee397ad 2019-07-12 stsp if (err)
2188 5a58a424 2020-06-23 stsp goto done;
2189 fc6346c4 2019-03-27 stsp /*
2190 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2191 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2192 fc6346c4 2019-03-27 stsp */
2193 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2194 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2195 fc6346c4 2019-03-27 stsp } else {
2196 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2197 1ee397ad 2019-07-12 stsp if (err)
2198 5a58a424 2020-06-23 stsp goto done;
2199 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2200 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2201 fc6346c4 2019-03-27 stsp if (err)
2202 5a58a424 2020-06-23 stsp goto done;
2203 fc6346c4 2019-03-27 stsp }
2204 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2205 708d8e67 2019-03-27 stsp }
2206 5a58a424 2020-06-23 stsp done:
2207 5a58a424 2020-06-23 stsp free(ondisk_path);
2208 fc6346c4 2019-03-27 stsp return err;
2209 708d8e67 2019-03-27 stsp }
2210 708d8e67 2019-03-27 stsp
2211 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2212 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2213 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2214 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2215 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2216 8da9e5f4 2019-01-12 stsp void *progress_arg;
2217 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2218 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2219 8da9e5f4 2019-01-12 stsp };
2220 8da9e5f4 2019-01-12 stsp
2221 512f0d0e 2019-01-02 stsp static const struct got_error *
2222 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2223 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2224 512f0d0e 2019-01-02 stsp {
2225 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2226 0584f854 2019-04-06 stsp
2227 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2228 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2229 512f0d0e 2019-01-02 stsp
2230 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2231 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2232 8da9e5f4 2019-01-12 stsp }
2233 512f0d0e 2019-01-02 stsp
2234 8da9e5f4 2019-01-12 stsp static const struct got_error *
2235 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2236 8da9e5f4 2019-01-12 stsp {
2237 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2238 7a9df742 2019-01-08 stsp
2239 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2241 0584f854 2019-04-06 stsp
2242 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2243 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2244 9d31a1d8 2018-03-11 stsp }
2245 9d31a1d8 2018-03-11 stsp
2246 9d31a1d8 2018-03-11 stsp static const struct got_error *
2247 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2248 9d31a1d8 2018-03-11 stsp {
2249 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2250 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2251 8da9e5f4 2019-01-12 stsp char *path;
2252 9d31a1d8 2018-03-11 stsp
2253 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2255 0584f854 2019-04-06 stsp
2256 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2257 63c5ca5d 2019-08-24 stsp return NULL;
2258 63c5ca5d 2019-08-24 stsp
2259 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2260 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2261 8da9e5f4 2019-01-12 stsp == -1)
2262 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2263 9d31a1d8 2018-03-11 stsp
2264 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2265 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2266 8da9e5f4 2019-01-12 stsp else
2267 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2268 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2269 9d31a1d8 2018-03-11 stsp
2270 8da9e5f4 2019-01-12 stsp free(path);
2271 0cd1c46a 2019-03-11 stsp return err;
2272 0cd1c46a 2019-03-11 stsp }
2273 0cd1c46a 2019-03-11 stsp
2274 b2118c49 2020-07-28 stsp const struct got_error *
2275 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2276 b2118c49 2020-07-28 stsp {
2277 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2278 b2118c49 2020-07-28 stsp
2279 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2280 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2281 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2282 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2283 b2118c49 2020-07-28 stsp }
2284 b2118c49 2020-07-28 stsp
2285 b2118c49 2020-07-28 stsp return NULL;
2286 b2118c49 2020-07-28 stsp }
2287 b2118c49 2020-07-28 stsp
2288 818c7501 2019-07-11 stsp static const struct got_error *
2289 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2290 0cd1c46a 2019-03-11 stsp {
2291 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2292 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2293 0cd1c46a 2019-03-11 stsp
2294 517bab73 2019-03-11 stsp *refname = NULL;
2295 517bab73 2019-03-11 stsp
2296 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2297 b2118c49 2020-07-28 stsp if (err)
2298 b2118c49 2020-07-28 stsp return err;
2299 0cd1c46a 2019-03-11 stsp
2300 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2301 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2302 0647c563 2019-03-11 stsp *refname = NULL;
2303 0cd1c46a 2019-03-11 stsp }
2304 517bab73 2019-03-11 stsp free(uuidstr);
2305 517bab73 2019-03-11 stsp return err;
2306 517bab73 2019-03-11 stsp }
2307 517bab73 2019-03-11 stsp
2308 818c7501 2019-07-11 stsp const struct got_error *
2309 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2310 818c7501 2019-07-11 stsp {
2311 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2312 818c7501 2019-07-11 stsp }
2313 818c7501 2019-07-11 stsp
2314 818c7501 2019-07-11 stsp static const struct got_error *
2315 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2316 818c7501 2019-07-11 stsp {
2317 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2318 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2319 818c7501 2019-07-11 stsp }
2320 818c7501 2019-07-11 stsp
2321 818c7501 2019-07-11 stsp static const struct got_error *
2322 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2323 818c7501 2019-07-11 stsp {
2324 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2325 818c7501 2019-07-11 stsp }
2326 818c7501 2019-07-11 stsp
2327 818c7501 2019-07-11 stsp static const struct got_error *
2328 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2329 818c7501 2019-07-11 stsp {
2330 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2331 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2332 818c7501 2019-07-11 stsp }
2333 818c7501 2019-07-11 stsp
2334 818c7501 2019-07-11 stsp static const struct got_error *
2335 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2336 818c7501 2019-07-11 stsp {
2337 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2338 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2339 0ebf8283 2019-07-24 stsp }
2340 0ebf8283 2019-07-24 stsp
2341 0ebf8283 2019-07-24 stsp static const struct got_error *
2342 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2343 0ebf8283 2019-07-24 stsp {
2344 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2345 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2346 0ebf8283 2019-07-24 stsp }
2347 0ebf8283 2019-07-24 stsp
2348 0ebf8283 2019-07-24 stsp static const struct got_error *
2349 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2350 0ebf8283 2019-07-24 stsp {
2351 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2352 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2353 0ebf8283 2019-07-24 stsp }
2354 0ebf8283 2019-07-24 stsp
2355 0ebf8283 2019-07-24 stsp static const struct got_error *
2356 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2357 0ebf8283 2019-07-24 stsp {
2358 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2359 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2360 818c7501 2019-07-11 stsp }
2361 818c7501 2019-07-11 stsp
2362 0ebf8283 2019-07-24 stsp static const struct got_error *
2363 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2364 0ebf8283 2019-07-24 stsp {
2365 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2366 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2367 0ebf8283 2019-07-24 stsp }
2368 818c7501 2019-07-11 stsp
2369 0ebf8283 2019-07-24 stsp const struct got_error *
2370 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2371 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2372 0ebf8283 2019-07-24 stsp {
2373 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2374 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2375 0ebf8283 2019-07-24 stsp *path = NULL;
2376 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2377 0ebf8283 2019-07-24 stsp }
2378 0ebf8283 2019-07-24 stsp return NULL;
2379 0ebf8283 2019-07-24 stsp }
2380 0ebf8283 2019-07-24 stsp
2381 517bab73 2019-03-11 stsp /*
2382 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2383 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2384 517bab73 2019-03-11 stsp */
2385 517bab73 2019-03-11 stsp static const struct got_error *
2386 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2387 517bab73 2019-03-11 stsp {
2388 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2389 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2390 517bab73 2019-03-11 stsp char *refname;
2391 517bab73 2019-03-11 stsp
2392 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2393 517bab73 2019-03-11 stsp if (err)
2394 517bab73 2019-03-11 stsp return err;
2395 0cd1c46a 2019-03-11 stsp
2396 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2397 0cd1c46a 2019-03-11 stsp if (err)
2398 0cd1c46a 2019-03-11 stsp goto done;
2399 0cd1c46a 2019-03-11 stsp
2400 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2401 0cd1c46a 2019-03-11 stsp done:
2402 0cd1c46a 2019-03-11 stsp free(refname);
2403 0cd1c46a 2019-03-11 stsp if (ref)
2404 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2405 3e3a69f1 2019-07-25 stsp return err;
2406 3e3a69f1 2019-07-25 stsp }
2407 3e3a69f1 2019-07-25 stsp
2408 3e3a69f1 2019-07-25 stsp static const struct got_error *
2409 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2410 3e3a69f1 2019-07-25 stsp {
2411 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2412 3e3a69f1 2019-07-25 stsp
2413 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2414 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2415 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2416 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2417 3e3a69f1 2019-07-25 stsp }
2418 9d31a1d8 2018-03-11 stsp return err;
2419 9d31a1d8 2018-03-11 stsp }
2420 9d31a1d8 2018-03-11 stsp
2421 3e3a69f1 2019-07-25 stsp
2422 ebf99748 2019-05-09 stsp static const struct got_error *
2423 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2424 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2425 ebf99748 2019-05-09 stsp {
2426 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2427 ebf99748 2019-05-09 stsp FILE *index = NULL;
2428 0cd1c46a 2019-03-11 stsp
2429 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2430 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2431 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2432 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2433 ebf99748 2019-05-09 stsp
2434 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2435 3e3a69f1 2019-07-25 stsp if (err)
2436 ebf99748 2019-05-09 stsp goto done;
2437 ebf99748 2019-05-09 stsp
2438 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2439 ebf99748 2019-05-09 stsp if (index == NULL) {
2440 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2441 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2442 ebf99748 2019-05-09 stsp } else {
2443 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2444 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2445 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2446 ebf99748 2019-05-09 stsp }
2447 ebf99748 2019-05-09 stsp done:
2448 ebf99748 2019-05-09 stsp if (err) {
2449 ebf99748 2019-05-09 stsp free(*fileindex_path);
2450 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2451 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2452 ebf99748 2019-05-09 stsp *fileindex = NULL;
2453 ebf99748 2019-05-09 stsp }
2454 ebf99748 2019-05-09 stsp return err;
2455 ebf99748 2019-05-09 stsp }
2456 c932eeeb 2019-05-22 stsp
2457 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2458 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2459 c932eeeb 2019-05-22 stsp const char *path;
2460 c932eeeb 2019-05-22 stsp size_t path_len;
2461 c932eeeb 2019-05-22 stsp const char *entry_name;
2462 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2463 a484d721 2019-06-10 stsp void *progress_arg;
2464 c932eeeb 2019-05-22 stsp };
2465 ebf99748 2019-05-09 stsp
2466 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2467 c932eeeb 2019-05-22 stsp static const struct got_error *
2468 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2469 c932eeeb 2019-05-22 stsp {
2470 1ee397ad 2019-07-12 stsp const struct got_error *err;
2471 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2472 c932eeeb 2019-05-22 stsp
2473 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2474 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2475 c932eeeb 2019-05-22 stsp return NULL;
2476 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2477 102ff934 2019-06-11 stsp return NULL;
2478 102ff934 2019-06-11 stsp
2479 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2480 a769b60b 2021-06-27 stsp return NULL;
2481 a769b60b 2021-06-27 stsp
2482 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2483 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2484 c932eeeb 2019-05-22 stsp return NULL;
2485 c932eeeb 2019-05-22 stsp
2486 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2487 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2488 edd02c5e 2019-07-11 stsp ie->path);
2489 1ee397ad 2019-07-12 stsp if (err)
2490 1ee397ad 2019-07-12 stsp return err;
2491 1ee397ad 2019-07-12 stsp }
2492 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2493 c932eeeb 2019-05-22 stsp return NULL;
2494 a615e0e7 2020-12-16 stsp }
2495 a615e0e7 2020-12-16 stsp
2496 a615e0e7 2020-12-16 stsp static const struct got_error *
2497 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2498 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2499 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2500 a615e0e7 2020-12-16 stsp {
2501 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2502 a615e0e7 2020-12-16 stsp
2503 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2504 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2505 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2506 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2507 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2508 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2509 a615e0e7 2020-12-16 stsp
2510 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2511 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2512 9c6338c4 2019-06-02 stsp }
2513 9c6338c4 2019-06-02 stsp
2514 9c6338c4 2019-06-02 stsp static const struct got_error *
2515 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2516 9c6338c4 2019-06-02 stsp {
2517 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2518 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2519 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2520 867630bb 2020-01-17 stsp struct timespec timeout;
2521 9c6338c4 2019-06-02 stsp
2522 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2523 9c6338c4 2019-06-02 stsp fileindex_path);
2524 9c6338c4 2019-06-02 stsp if (err)
2525 9c6338c4 2019-06-02 stsp goto done;
2526 9c6338c4 2019-06-02 stsp
2527 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2528 9c6338c4 2019-06-02 stsp if (err)
2529 9c6338c4 2019-06-02 stsp goto done;
2530 9c6338c4 2019-06-02 stsp
2531 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2532 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2533 9c6338c4 2019-06-02 stsp fileindex_path);
2534 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2535 9c6338c4 2019-06-02 stsp }
2536 867630bb 2020-01-17 stsp
2537 867630bb 2020-01-17 stsp /*
2538 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2539 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2540 867630bb 2020-01-17 stsp * was recorded in the file index.
2541 867630bb 2020-01-17 stsp */
2542 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2543 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2544 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2545 9c6338c4 2019-06-02 stsp done:
2546 9c6338c4 2019-06-02 stsp if (new_index)
2547 9c6338c4 2019-06-02 stsp fclose(new_index);
2548 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2549 9c6338c4 2019-06-02 stsp return err;
2550 c932eeeb 2019-05-22 stsp }
2551 6ced4176 2019-07-12 stsp
2552 6ced4176 2019-07-12 stsp static const struct got_error *
2553 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2554 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2555 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2556 6ced4176 2019-07-12 stsp {
2557 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2558 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2559 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2560 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2561 6ced4176 2019-07-12 stsp
2562 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2563 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2564 6ced4176 2019-07-12 stsp *tree_id = NULL;
2565 6ced4176 2019-07-12 stsp
2566 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2567 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2568 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2569 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2570 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2571 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2572 6ced4176 2019-07-12 stsp goto done;
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2575 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2576 6ced4176 2019-07-12 stsp if (err)
2577 6ced4176 2019-07-12 stsp goto done;
2578 6ced4176 2019-07-12 stsp return NULL;
2579 6ced4176 2019-07-12 stsp }
2580 6ced4176 2019-07-12 stsp
2581 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2582 6ced4176 2019-07-12 stsp
2583 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2584 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2585 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2586 6ced4176 2019-07-12 stsp goto done;
2587 6ced4176 2019-07-12 stsp }
2588 6ced4176 2019-07-12 stsp
2589 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2590 6ced4176 2019-07-12 stsp in_repo_path);
2591 6ced4176 2019-07-12 stsp if (err)
2592 6ced4176 2019-07-12 stsp goto done;
2593 6ced4176 2019-07-12 stsp
2594 6ced4176 2019-07-12 stsp free(in_repo_path);
2595 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2596 6ced4176 2019-07-12 stsp
2597 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2598 6ced4176 2019-07-12 stsp if (err)
2599 6ced4176 2019-07-12 stsp goto done;
2600 c932eeeb 2019-05-22 stsp
2601 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2602 6ced4176 2019-07-12 stsp /* Check out a single file. */
2603 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2604 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2605 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2606 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2607 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2608 6ced4176 2019-07-12 stsp goto done;
2609 6ced4176 2019-07-12 stsp }
2610 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2611 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2612 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2613 6ced4176 2019-07-12 stsp goto done;
2614 6ced4176 2019-07-12 stsp }
2615 6ced4176 2019-07-12 stsp } else {
2616 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2617 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2618 6ced4176 2019-07-12 stsp if (err)
2619 6ced4176 2019-07-12 stsp return err;
2620 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2621 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2622 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2623 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2624 6ced4176 2019-07-12 stsp goto done;
2625 6ced4176 2019-07-12 stsp }
2626 6ced4176 2019-07-12 stsp }
2627 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2628 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2629 6ced4176 2019-07-12 stsp } else {
2630 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2631 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2632 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2633 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2634 6ced4176 2019-07-12 stsp goto done;
2635 6ced4176 2019-07-12 stsp }
2636 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2637 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2638 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2639 6ced4176 2019-07-12 stsp goto done;
2640 6ced4176 2019-07-12 stsp }
2641 6ced4176 2019-07-12 stsp }
2642 6ced4176 2019-07-12 stsp done:
2643 6ced4176 2019-07-12 stsp free(id);
2644 6ced4176 2019-07-12 stsp free(in_repo_path);
2645 6ced4176 2019-07-12 stsp if (err) {
2646 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2647 6ced4176 2019-07-12 stsp free(*tree_relpath);
2648 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2649 6ced4176 2019-07-12 stsp free(*tree_id);
2650 6ced4176 2019-07-12 stsp *tree_id = NULL;
2651 6ced4176 2019-07-12 stsp }
2652 07ed472d 2019-07-12 stsp return err;
2653 07ed472d 2019-07-12 stsp }
2654 07ed472d 2019-07-12 stsp
2655 07ed472d 2019-07-12 stsp static const struct got_error *
2656 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2657 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2658 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2659 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2660 07ed472d 2019-07-12 stsp {
2661 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2662 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2663 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2664 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2665 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2666 07ed472d 2019-07-12 stsp
2667 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2668 7f47418f 2019-12-20 stsp if (err) {
2669 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2670 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2671 7f47418f 2019-12-20 stsp goto done;
2672 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2673 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2674 7f47418f 2019-12-20 stsp if (err)
2675 7f47418f 2019-12-20 stsp return err;
2676 7f47418f 2019-12-20 stsp }
2677 07ed472d 2019-07-12 stsp
2678 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2679 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2680 07ed472d 2019-07-12 stsp if (err)
2681 07ed472d 2019-07-12 stsp goto done;
2682 07ed472d 2019-07-12 stsp
2683 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2684 07ed472d 2019-07-12 stsp if (err)
2685 07ed472d 2019-07-12 stsp goto done;
2686 07ed472d 2019-07-12 stsp
2687 07ed472d 2019-07-12 stsp if (entry_name &&
2688 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2689 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2690 07ed472d 2019-07-12 stsp goto done;
2691 07ed472d 2019-07-12 stsp }
2692 07ed472d 2019-07-12 stsp
2693 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2694 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2695 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2696 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2697 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2698 07ed472d 2019-07-12 stsp arg.repo = repo;
2699 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2700 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2701 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2702 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2703 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2704 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2705 07ed472d 2019-07-12 stsp done:
2706 07ed472d 2019-07-12 stsp if (tree)
2707 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2708 07ed472d 2019-07-12 stsp if (commit)
2709 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2710 6ced4176 2019-07-12 stsp return err;
2711 6ced4176 2019-07-12 stsp }
2712 6ced4176 2019-07-12 stsp
2713 9d31a1d8 2018-03-11 stsp const struct got_error *
2714 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2715 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2716 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2717 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2718 9d31a1d8 2018-03-11 stsp {
2719 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2720 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2721 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2722 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2723 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2724 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2725 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2726 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2727 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2728 f2ea84fa 2019-07-27 stsp int entry_type;
2729 f2ea84fa 2019-07-27 stsp char *relpath;
2730 f2ea84fa 2019-07-27 stsp char *entry_name;
2731 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2732 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2733 9d31a1d8 2018-03-11 stsp
2734 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2735 f2ea84fa 2019-07-27 stsp
2736 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2737 9d31a1d8 2018-03-11 stsp if (err)
2738 9d31a1d8 2018-03-11 stsp return err;
2739 93a30277 2018-12-24 stsp
2740 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2741 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2742 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2743 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2744 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2745 f2ea84fa 2019-07-27 stsp goto done;
2746 f2ea84fa 2019-07-27 stsp }
2747 6ced4176 2019-07-12 stsp
2748 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2749 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2750 f2ea84fa 2019-07-27 stsp if (err) {
2751 f2ea84fa 2019-07-27 stsp free(tpd);
2752 6ced4176 2019-07-12 stsp goto done;
2753 6ced4176 2019-07-12 stsp }
2754 f2ea84fa 2019-07-27 stsp
2755 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2756 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2757 f2ea84fa 2019-07-27 stsp if (err) {
2758 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2759 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2760 f2ea84fa 2019-07-27 stsp free(tpd);
2761 f2ea84fa 2019-07-27 stsp goto done;
2762 f2ea84fa 2019-07-27 stsp }
2763 f2ea84fa 2019-07-27 stsp } else
2764 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2765 f2ea84fa 2019-07-27 stsp
2766 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2767 6ced4176 2019-07-12 stsp }
2768 6ced4176 2019-07-12 stsp
2769 51514078 2018-12-25 stsp /*
2770 51514078 2018-12-25 stsp * Read the file index.
2771 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2772 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2773 51514078 2018-12-25 stsp */
2774 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2775 8da9e5f4 2019-01-12 stsp if (err)
2776 c4cdcb68 2019-04-03 stsp goto done;
2777 c4cdcb68 2019-04-03 stsp
2778 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2779 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2780 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2781 f2ea84fa 2019-07-27 stsp
2782 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2783 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2784 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2785 f2ea84fa 2019-07-27 stsp if (err)
2786 f2ea84fa 2019-07-27 stsp break;
2787 f2ea84fa 2019-07-27 stsp
2788 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2789 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2790 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2791 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2792 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2793 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2794 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2795 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2796 f2ea84fa 2019-07-27 stsp if (err)
2797 f2ea84fa 2019-07-27 stsp break;
2798 f2ea84fa 2019-07-27 stsp
2799 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2800 711d9cd9 2019-07-12 stsp }
2801 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2802 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2803 af12c6b9 2019-06-04 stsp err = sync_err;
2804 9d31a1d8 2018-03-11 stsp done:
2805 9c6338c4 2019-06-02 stsp free(fileindex_path);
2806 edfa7d7f 2018-09-11 stsp if (tree)
2807 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2808 9d31a1d8 2018-03-11 stsp if (commit)
2809 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2810 5ade8233 2019-07-12 stsp if (fileindex)
2811 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2812 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2813 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2814 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2815 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2816 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2817 f2ea84fa 2019-07-27 stsp free(tpd);
2818 f2ea84fa 2019-07-27 stsp }
2819 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2820 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2821 9d31a1d8 2018-03-11 stsp err = unlockerr;
2822 f8d1f275 2019-02-04 stsp return err;
2823 f8d1f275 2019-02-04 stsp }
2824 f8d1f275 2019-02-04 stsp
2825 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2826 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2827 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2828 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2829 234035bc 2019-06-01 stsp void *progress_arg;
2830 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2831 234035bc 2019-06-01 stsp void *cancel_arg;
2832 f69721c3 2019-10-21 stsp const char *label_orig;
2833 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2834 234035bc 2019-06-01 stsp };
2835 234035bc 2019-06-01 stsp
2836 234035bc 2019-06-01 stsp static const struct got_error *
2837 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2838 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2839 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2840 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2841 234035bc 2019-06-01 stsp {
2842 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2843 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2844 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2845 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2846 234035bc 2019-06-01 stsp struct stat sb;
2847 234035bc 2019-06-01 stsp unsigned char status;
2848 234035bc 2019-06-01 stsp int local_changes_subsumed;
2849 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2850 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2851 234035bc 2019-06-01 stsp
2852 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2853 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2854 d6c87207 2019-08-02 stsp strlen(path2));
2855 1ee397ad 2019-07-12 stsp if (ie == NULL)
2856 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2857 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2858 234035bc 2019-06-01 stsp
2859 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2860 234035bc 2019-06-01 stsp path2) == -1)
2861 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2862 234035bc 2019-06-01 stsp
2863 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2864 7f91a133 2019-12-13 stsp repo);
2865 234035bc 2019-06-01 stsp if (err)
2866 234035bc 2019-06-01 stsp goto done;
2867 234035bc 2019-06-01 stsp
2868 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2869 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2870 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2871 234035bc 2019-06-01 stsp goto done;
2872 234035bc 2019-06-01 stsp }
2873 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2874 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2875 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2876 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2877 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2878 234035bc 2019-06-01 stsp goto done;
2879 234035bc 2019-06-01 stsp }
2880 234035bc 2019-06-01 stsp
2881 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2882 36bf999c 2020-07-23 stsp char *link_target2;
2883 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2884 36bf999c 2020-07-23 stsp if (err)
2885 36bf999c 2020-07-23 stsp goto done;
2886 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2887 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2888 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2889 36bf999c 2020-07-23 stsp free(link_target2);
2890 af57b12a 2020-07-23 stsp } else {
2891 1af628f4 2021-06-03 stsp int fd;
2892 1af628f4 2021-06-03 stsp
2893 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2894 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2895 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2896 1af628f4 2021-06-03 stsp goto done;
2897 1af628f4 2021-06-03 stsp }
2898 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2899 1af628f4 2021-06-03 stsp f_orig, blob1);
2900 1af628f4 2021-06-03 stsp if (err)
2901 1af628f4 2021-06-03 stsp goto done;
2902 1af628f4 2021-06-03 stsp
2903 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2904 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2905 1af628f4 2021-06-03 stsp goto done;
2906 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2907 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2908 1af628f4 2021-06-03 stsp if (err)
2909 1af628f4 2021-06-03 stsp goto done;
2910 1af628f4 2021-06-03 stsp
2911 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2912 1af628f4 2021-06-03 stsp if (fd == -1) {
2913 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2914 1af628f4 2021-06-03 stsp ondisk_path);
2915 1af628f4 2021-06-03 stsp goto done;
2916 1af628f4 2021-06-03 stsp }
2917 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2918 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2919 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2920 1af628f4 2021-06-03 stsp ondisk_path);
2921 1af628f4 2021-06-03 stsp close(fd);
2922 1af628f4 2021-06-03 stsp goto done;
2923 1af628f4 2021-06-03 stsp }
2924 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2925 1af628f4 2021-06-03 stsp if (err)
2926 1af628f4 2021-06-03 stsp goto done;
2927 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2928 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2929 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2930 1af628f4 2021-06-03 stsp goto done;
2931 1af628f4 2021-06-03 stsp }
2932 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2933 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2934 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2935 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2936 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2937 af57b12a 2020-07-23 stsp }
2938 234035bc 2019-06-01 stsp } else if (blob1) {
2939 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2940 d6c87207 2019-08-02 stsp strlen(path1));
2941 1ee397ad 2019-07-12 stsp if (ie == NULL)
2942 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2943 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2944 2b92fad7 2019-06-02 stsp
2945 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2946 2b92fad7 2019-06-02 stsp path1) == -1)
2947 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2948 2b92fad7 2019-06-02 stsp
2949 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2950 7f91a133 2019-12-13 stsp repo);
2951 2b92fad7 2019-06-02 stsp if (err)
2952 2b92fad7 2019-06-02 stsp goto done;
2953 2b92fad7 2019-06-02 stsp
2954 2b92fad7 2019-06-02 stsp switch (status) {
2955 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2956 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2957 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2958 1ee397ad 2019-07-12 stsp if (err)
2959 1ee397ad 2019-07-12 stsp goto done;
2960 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2961 2b92fad7 2019-06-02 stsp if (err)
2962 2b92fad7 2019-06-02 stsp goto done;
2963 2b92fad7 2019-06-02 stsp if (ie)
2964 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2965 2b92fad7 2019-06-02 stsp break;
2966 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2967 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2968 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2969 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2970 1ee397ad 2019-07-12 stsp if (err)
2971 1ee397ad 2019-07-12 stsp goto done;
2972 2b92fad7 2019-06-02 stsp if (ie)
2973 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2974 2b92fad7 2019-06-02 stsp break;
2975 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2976 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2977 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2978 0a22ca1a 2020-09-23 stsp /*
2979 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2980 0a22ca1a 2020-09-23 stsp * exists in the repository.
2981 0a22ca1a 2020-09-23 stsp */
2982 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2983 0a22ca1a 2020-09-23 stsp if (err)
2984 0a22ca1a 2020-09-23 stsp goto done;
2985 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2986 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2987 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2988 0a22ca1a 2020-09-23 stsp if (err)
2989 0a22ca1a 2020-09-23 stsp goto done;
2990 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2991 0a22ca1a 2020-09-23 stsp path1);
2992 0a22ca1a 2020-09-23 stsp if (err)
2993 0a22ca1a 2020-09-23 stsp goto done;
2994 0a22ca1a 2020-09-23 stsp if (ie)
2995 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2996 0a22ca1a 2020-09-23 stsp ie);
2997 0a22ca1a 2020-09-23 stsp } else {
2998 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2999 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3000 0a22ca1a 2020-09-23 stsp }
3001 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3002 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3003 0a22ca1a 2020-09-23 stsp free(id);
3004 0a22ca1a 2020-09-23 stsp if (err)
3005 0a22ca1a 2020-09-23 stsp goto done;
3006 0a22ca1a 2020-09-23 stsp break;
3007 0a22ca1a 2020-09-23 stsp }
3008 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3009 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3010 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3011 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3012 1ee397ad 2019-07-12 stsp if (err)
3013 1ee397ad 2019-07-12 stsp goto done;
3014 2b92fad7 2019-06-02 stsp break;
3015 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3016 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3017 1ee397ad 2019-07-12 stsp if (err)
3018 1ee397ad 2019-07-12 stsp goto done;
3019 2b92fad7 2019-06-02 stsp break;
3020 2b92fad7 2019-06-02 stsp default:
3021 2b92fad7 2019-06-02 stsp break;
3022 2b92fad7 2019-06-02 stsp }
3023 234035bc 2019-06-01 stsp } else if (blob2) {
3024 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3025 234035bc 2019-06-01 stsp path2) == -1)
3026 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3027 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3028 d6c87207 2019-08-02 stsp strlen(path2));
3029 234035bc 2019-06-01 stsp if (ie) {
3030 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3031 7f91a133 2019-12-13 stsp -1, NULL, repo);
3032 234035bc 2019-06-01 stsp if (err)
3033 234035bc 2019-06-01 stsp goto done;
3034 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3037 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3039 1ee397ad 2019-07-12 stsp status, path2);
3040 234035bc 2019-06-01 stsp goto done;
3041 234035bc 2019-06-01 stsp }
3042 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3043 36bf999c 2020-07-23 stsp char *link_target2;
3044 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3045 36bf999c 2020-07-23 stsp blob2);
3046 36bf999c 2020-07-23 stsp if (err)
3047 36bf999c 2020-07-23 stsp goto done;
3048 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3049 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3050 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3051 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3052 36bf999c 2020-07-23 stsp free(link_target2);
3053 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3054 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3055 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3056 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3057 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3058 526a746f 2020-07-23 stsp a->progress_arg);
3059 dfe9fba0 2020-07-23 stsp } else {
3060 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3061 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3062 526a746f 2020-07-23 stsp }
3063 dfe9fba0 2020-07-23 stsp if (err)
3064 dfe9fba0 2020-07-23 stsp goto done;
3065 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3066 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3067 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3068 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3069 234035bc 2019-06-01 stsp if (err)
3070 234035bc 2019-06-01 stsp goto done;
3071 234035bc 2019-06-01 stsp }
3072 234035bc 2019-06-01 stsp } else {
3073 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3074 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3075 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3076 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3077 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3078 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3079 2e1fa222 2020-07-23 stsp } else {
3080 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3081 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3082 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3083 2e1fa222 2020-07-23 stsp }
3084 234035bc 2019-06-01 stsp if (err)
3085 234035bc 2019-06-01 stsp goto done;
3086 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3087 234035bc 2019-06-01 stsp if (err)
3088 234035bc 2019-06-01 stsp goto done;
3089 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3090 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3091 3969253a 2020-03-07 stsp if (err) {
3092 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3093 3969253a 2020-03-07 stsp goto done;
3094 3969253a 2020-03-07 stsp }
3095 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3096 2b92fad7 2019-06-02 stsp if (err) {
3097 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3098 2b92fad7 2019-06-02 stsp goto done;
3099 2b92fad7 2019-06-02 stsp }
3100 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3101 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3102 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3103 2e1fa222 2020-07-23 stsp }
3104 234035bc 2019-06-01 stsp }
3105 234035bc 2019-06-01 stsp }
3106 234035bc 2019-06-01 stsp done:
3107 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3108 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3109 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3110 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3111 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3112 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3113 1af628f4 2021-06-03 stsp free(id_str);
3114 54d5be07 2021-06-03 stsp free(label_deriv2);
3115 234035bc 2019-06-01 stsp free(ondisk_path);
3116 234035bc 2019-06-01 stsp return err;
3117 234035bc 2019-06-01 stsp }
3118 234035bc 2019-06-01 stsp
3119 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3120 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3121 234035bc 2019-06-01 stsp struct got_repository *repo;
3122 234035bc 2019-06-01 stsp };
3123 234035bc 2019-06-01 stsp
3124 234035bc 2019-06-01 stsp static const struct got_error *
3125 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3126 234035bc 2019-06-01 stsp {
3127 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3128 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3129 234035bc 2019-06-01 stsp unsigned char status;
3130 234035bc 2019-06-01 stsp struct stat sb;
3131 234035bc 2019-06-01 stsp char *ondisk_path;
3132 234035bc 2019-06-01 stsp
3133 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3134 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3135 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3136 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3137 234035bc 2019-06-01 stsp
3138 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3139 234035bc 2019-06-01 stsp == -1)
3140 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3141 234035bc 2019-06-01 stsp
3142 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3143 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3144 234035bc 2019-06-01 stsp if (err)
3145 234035bc 2019-06-01 stsp return err;
3146 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3147 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3148 234035bc 2019-06-01 stsp
3149 234035bc 2019-06-01 stsp return NULL;
3150 234035bc 2019-06-01 stsp }
3151 234035bc 2019-06-01 stsp
3152 818c7501 2019-07-11 stsp static const struct got_error *
3153 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3154 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3155 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3156 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3157 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3158 234035bc 2019-06-01 stsp {
3159 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3160 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3161 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3162 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3163 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3164 234035bc 2019-06-01 stsp
3165 03415a1a 2019-06-02 stsp if (commit_id1) {
3166 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3167 03415a1a 2019-06-02 stsp worktree->path_prefix);
3168 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3169 03415a1a 2019-06-02 stsp goto done;
3170 69d57f3d 2020-07-31 stsp }
3171 69d57f3d 2020-07-31 stsp if (tree_id1) {
3172 69d57f3d 2020-07-31 stsp char *id_str;
3173 03415a1a 2019-06-02 stsp
3174 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3175 03415a1a 2019-06-02 stsp if (err)
3176 03415a1a 2019-06-02 stsp goto done;
3177 f69721c3 2019-10-21 stsp
3178 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3179 f69721c3 2019-10-21 stsp if (err)
3180 f69721c3 2019-10-21 stsp goto done;
3181 f69721c3 2019-10-21 stsp
3182 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3183 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3184 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3185 f69721c3 2019-10-21 stsp free(id_str);
3186 f69721c3 2019-10-21 stsp goto done;
3187 f69721c3 2019-10-21 stsp }
3188 f69721c3 2019-10-21 stsp free(id_str);
3189 03415a1a 2019-06-02 stsp }
3190 234035bc 2019-06-01 stsp
3191 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3192 234035bc 2019-06-01 stsp worktree->path_prefix);
3193 234035bc 2019-06-01 stsp if (err)
3194 234035bc 2019-06-01 stsp goto done;
3195 234035bc 2019-06-01 stsp
3196 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3197 234035bc 2019-06-01 stsp if (err)
3198 234035bc 2019-06-01 stsp goto done;
3199 234035bc 2019-06-01 stsp
3200 234035bc 2019-06-01 stsp arg.worktree = worktree;
3201 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3202 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3203 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3204 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3205 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3206 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3207 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3208 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3209 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3210 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3211 af12c6b9 2019-06-04 stsp err = sync_err;
3212 234035bc 2019-06-01 stsp done:
3213 234035bc 2019-06-01 stsp if (tree1)
3214 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3215 234035bc 2019-06-01 stsp if (tree2)
3216 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3217 f69721c3 2019-10-21 stsp free(label_orig);
3218 818c7501 2019-07-11 stsp return err;
3219 818c7501 2019-07-11 stsp }
3220 234035bc 2019-06-01 stsp
3221 818c7501 2019-07-11 stsp const struct got_error *
3222 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3223 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3224 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3225 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3226 818c7501 2019-07-11 stsp {
3227 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3228 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3229 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3230 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3231 818c7501 2019-07-11 stsp
3232 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3233 818c7501 2019-07-11 stsp if (err)
3234 818c7501 2019-07-11 stsp return err;
3235 818c7501 2019-07-11 stsp
3236 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3237 818c7501 2019-07-11 stsp if (err)
3238 818c7501 2019-07-11 stsp goto done;
3239 818c7501 2019-07-11 stsp
3240 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3241 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3242 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3243 818c7501 2019-07-11 stsp &mok_arg);
3244 818c7501 2019-07-11 stsp if (err)
3245 818c7501 2019-07-11 stsp goto done;
3246 818c7501 2019-07-11 stsp
3247 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3248 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3249 818c7501 2019-07-11 stsp done:
3250 818c7501 2019-07-11 stsp if (fileindex)
3251 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3252 818c7501 2019-07-11 stsp free(fileindex_path);
3253 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3254 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3255 234035bc 2019-06-01 stsp err = unlockerr;
3256 234035bc 2019-06-01 stsp return err;
3257 234035bc 2019-06-01 stsp }
3258 234035bc 2019-06-01 stsp
3259 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3260 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3261 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3262 927df6b7 2019-02-10 stsp const char *status_path;
3263 927df6b7 2019-02-10 stsp size_t status_path_len;
3264 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3265 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3266 f8d1f275 2019-02-04 stsp void *status_arg;
3267 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3268 f8d1f275 2019-02-04 stsp void *cancel_arg;
3269 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3270 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3271 f2a9dc41 2019-12-13 tracey int report_unchanged;
3272 3143d852 2020-06-25 stsp int no_ignores;
3273 f8d1f275 2019-02-04 stsp };
3274 88d0e355 2019-08-03 stsp
3275 f8d1f275 2019-02-04 stsp static const struct got_error *
3276 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3277 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3278 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3279 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3280 927df6b7 2019-02-10 stsp {
3281 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3282 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3283 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3284 927df6b7 2019-02-10 stsp struct stat sb;
3285 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3286 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3287 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3288 927df6b7 2019-02-10 stsp
3289 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3290 98eaaa12 2019-08-03 stsp if (err)
3291 98eaaa12 2019-08-03 stsp return err;
3292 98eaaa12 2019-08-03 stsp
3293 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3294 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3295 98eaaa12 2019-08-03 stsp return NULL;
3296 98eaaa12 2019-08-03 stsp
3297 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3298 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3299 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3300 98eaaa12 2019-08-03 stsp }
3301 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3302 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3303 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3304 927df6b7 2019-02-10 stsp }
3305 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3306 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3307 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3308 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3309 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3310 98eaaa12 2019-08-03 stsp }
3311 98eaaa12 2019-08-03 stsp
3312 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3313 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3314 927df6b7 2019-02-10 stsp }
3315 927df6b7 2019-02-10 stsp
3316 927df6b7 2019-02-10 stsp static const struct got_error *
3317 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3318 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3319 f8d1f275 2019-02-04 stsp {
3320 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3321 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3322 f8d1f275 2019-02-04 stsp char *abspath;
3323 f8d1f275 2019-02-04 stsp
3324 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3325 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3326 0584f854 2019-04-06 stsp
3327 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3328 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3329 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3330 927df6b7 2019-02-10 stsp return NULL;
3331 927df6b7 2019-02-10 stsp
3332 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3333 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3334 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3335 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3336 f8d1f275 2019-02-04 stsp } else {
3337 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3338 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3339 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3340 f8d1f275 2019-02-04 stsp }
3341 f8d1f275 2019-02-04 stsp
3342 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3343 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3344 f8d1f275 2019-02-04 stsp free(abspath);
3345 9d31a1d8 2018-03-11 stsp return err;
3346 9d31a1d8 2018-03-11 stsp }
3347 f8d1f275 2019-02-04 stsp
3348 f8d1f275 2019-02-04 stsp static const struct got_error *
3349 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3350 f8d1f275 2019-02-04 stsp {
3351 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3352 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3353 2ec1f75b 2019-03-26 stsp unsigned char status;
3354 927df6b7 2019-02-10 stsp
3355 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3356 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3357 0584f854 2019-04-06 stsp
3358 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3359 927df6b7 2019-02-10 stsp return NULL;
3360 927df6b7 2019-02-10 stsp
3361 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3362 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3363 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3364 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3365 2ec1f75b 2019-03-26 stsp else
3366 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3367 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3368 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3369 6841da00 2019-08-08 stsp }
3370 6841da00 2019-08-08 stsp
3371 6841da00 2019-08-08 stsp void
3372 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3373 6841da00 2019-08-08 stsp {
3374 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3375 6841da00 2019-08-08 stsp
3376 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3377 6841da00 2019-08-08 stsp free((char *)pe->path);
3378 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3379 6841da00 2019-08-08 stsp }
3380 6841da00 2019-08-08 stsp
3381 6841da00 2019-08-08 stsp void
3382 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3383 6841da00 2019-08-08 stsp {
3384 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3385 6841da00 2019-08-08 stsp
3386 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3387 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3388 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3389 6841da00 2019-08-08 stsp free((char *)pe->path);
3390 6841da00 2019-08-08 stsp }
3391 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3392 6841da00 2019-08-08 stsp }
3393 6841da00 2019-08-08 stsp
3394 6841da00 2019-08-08 stsp static const struct got_error *
3395 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3396 6841da00 2019-08-08 stsp {
3397 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3398 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3399 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3400 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3401 6841da00 2019-08-08 stsp size_t linesize = 0;
3402 6841da00 2019-08-08 stsp ssize_t linelen;
3403 6841da00 2019-08-08 stsp
3404 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3405 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3406 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3407 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3408 6841da00 2019-08-08 stsp
3409 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3410 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3411 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3412 bd8de430 2019-10-04 stsp
3413 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3414 bd8de430 2019-10-04 stsp if (line[0] == '#')
3415 bd8de430 2019-10-04 stsp continue;
3416 bd8de430 2019-10-04 stsp
3417 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3418 bd8de430 2019-10-04 stsp if (line[0] == '!')
3419 bd8de430 2019-10-04 stsp continue;
3420 bd8de430 2019-10-04 stsp
3421 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3422 6841da00 2019-08-08 stsp line) == -1) {
3423 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3424 6841da00 2019-08-08 stsp goto done;
3425 6841da00 2019-08-08 stsp }
3426 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3427 6841da00 2019-08-08 stsp if (err)
3428 6841da00 2019-08-08 stsp goto done;
3429 6841da00 2019-08-08 stsp }
3430 6841da00 2019-08-08 stsp if (ferror(f)) {
3431 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3432 6841da00 2019-08-08 stsp goto done;
3433 6841da00 2019-08-08 stsp }
3434 6841da00 2019-08-08 stsp
3435 6841da00 2019-08-08 stsp dirpath = strdup(path);
3436 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3437 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3438 6841da00 2019-08-08 stsp goto done;
3439 6841da00 2019-08-08 stsp }
3440 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3441 6841da00 2019-08-08 stsp done:
3442 6841da00 2019-08-08 stsp free(line);
3443 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3444 6841da00 2019-08-08 stsp free(dirpath);
3445 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3446 6841da00 2019-08-08 stsp }
3447 6841da00 2019-08-08 stsp return err;
3448 6841da00 2019-08-08 stsp }
3449 6841da00 2019-08-08 stsp
3450 6841da00 2019-08-08 stsp int
3451 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3452 6841da00 2019-08-08 stsp {
3453 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3454 bd8de430 2019-10-04 stsp
3455 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3456 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3457 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3458 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3459 bd8de430 2019-10-04 stsp
3460 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3461 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3462 6841da00 2019-08-08 stsp
3463 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3464 bd8de430 2019-10-04 stsp continue;
3465 bd8de430 2019-10-04 stsp pattern += 3;
3466 bd8de430 2019-10-04 stsp p = path;
3467 bd8de430 2019-10-04 stsp while (*p) {
3468 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3469 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3470 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3471 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3472 bd8de430 2019-10-04 stsp p++;
3473 bd8de430 2019-10-04 stsp while (*p == '/')
3474 bd8de430 2019-10-04 stsp p++;
3475 bd8de430 2019-10-04 stsp continue;
3476 bd8de430 2019-10-04 stsp }
3477 bd8de430 2019-10-04 stsp return 1;
3478 bd8de430 2019-10-04 stsp }
3479 bd8de430 2019-10-04 stsp }
3480 bd8de430 2019-10-04 stsp }
3481 bd8de430 2019-10-04 stsp
3482 6841da00 2019-08-08 stsp /*
3483 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3484 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3485 6841da00 2019-08-08 stsp * ignores backwards.
3486 6841da00 2019-08-08 stsp */
3487 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3488 6841da00 2019-08-08 stsp while (pe) {
3489 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3490 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3491 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3492 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3493 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3494 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3495 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3496 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3497 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3498 6841da00 2019-08-08 stsp continue;
3499 6841da00 2019-08-08 stsp return 1;
3500 6841da00 2019-08-08 stsp }
3501 6841da00 2019-08-08 stsp }
3502 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3503 6841da00 2019-08-08 stsp }
3504 6841da00 2019-08-08 stsp
3505 6841da00 2019-08-08 stsp return 0;
3506 6841da00 2019-08-08 stsp }
3507 6841da00 2019-08-08 stsp
3508 6841da00 2019-08-08 stsp static const struct got_error *
3509 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3510 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3511 6841da00 2019-08-08 stsp {
3512 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3513 6841da00 2019-08-08 stsp char *ignorespath;
3514 886cec17 2019-12-15 stsp int fd = -1;
3515 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3516 6841da00 2019-08-08 stsp
3517 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3518 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3519 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3520 6841da00 2019-08-08 stsp
3521 886cec17 2019-12-15 stsp if (dirfd != -1) {
3522 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3523 886cec17 2019-12-15 stsp if (fd == -1) {
3524 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3525 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3526 886cec17 2019-12-15 stsp ignorespath);
3527 886cec17 2019-12-15 stsp } else {
3528 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3529 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3530 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3531 886cec17 2019-12-15 stsp ignorespath);
3532 886cec17 2019-12-15 stsp else {
3533 886cec17 2019-12-15 stsp fd = -1;
3534 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3535 886cec17 2019-12-15 stsp }
3536 886cec17 2019-12-15 stsp }
3537 886cec17 2019-12-15 stsp } else {
3538 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3539 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3540 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3541 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3542 886cec17 2019-12-15 stsp ignorespath);
3543 886cec17 2019-12-15 stsp } else
3544 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3545 886cec17 2019-12-15 stsp }
3546 6841da00 2019-08-08 stsp
3547 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3548 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3549 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3550 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3551 6841da00 2019-08-08 stsp free(ignorespath);
3552 6841da00 2019-08-08 stsp return err;
3553 f8d1f275 2019-02-04 stsp }
3554 f8d1f275 2019-02-04 stsp
3555 f8d1f275 2019-02-04 stsp static const struct got_error *
3556 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3557 f8d1f275 2019-02-04 stsp {
3558 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3559 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3560 f8d1f275 2019-02-04 stsp char *path = NULL;
3561 f8d1f275 2019-02-04 stsp
3562 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3563 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3564 0584f854 2019-04-06 stsp
3565 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3566 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3567 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3568 f8d1f275 2019-02-04 stsp } else {
3569 f8d1f275 2019-02-04 stsp path = de->d_name;
3570 f8d1f275 2019-02-04 stsp }
3571 f8d1f275 2019-02-04 stsp
3572 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3573 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3574 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3575 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3576 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3577 f8d1f275 2019-02-04 stsp if (parent_path[0])
3578 f8d1f275 2019-02-04 stsp free(path);
3579 b72f483a 2019-02-05 stsp return err;
3580 f8d1f275 2019-02-04 stsp }
3581 f8d1f275 2019-02-04 stsp
3582 347d1d3e 2019-07-12 stsp static const struct got_error *
3583 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3584 3143d852 2020-06-25 stsp {
3585 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3586 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3587 3143d852 2020-06-25 stsp
3588 3143d852 2020-06-25 stsp if (a->no_ignores)
3589 3143d852 2020-06-25 stsp return NULL;
3590 3143d852 2020-06-25 stsp
3591 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3592 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3593 3143d852 2020-06-25 stsp if (err)
3594 3143d852 2020-06-25 stsp return err;
3595 3143d852 2020-06-25 stsp
3596 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3597 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3598 3143d852 2020-06-25 stsp
3599 3143d852 2020-06-25 stsp return err;
3600 3143d852 2020-06-25 stsp }
3601 3143d852 2020-06-25 stsp
3602 3143d852 2020-06-25 stsp static const struct got_error *
3603 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3604 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3605 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3606 abb4604f 2019-07-27 stsp {
3607 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3608 abb4604f 2019-07-27 stsp struct stat sb;
3609 abb4604f 2019-07-27 stsp
3610 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3611 abb4604f 2019-07-27 stsp if (ie)
3612 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3613 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3614 abb4604f 2019-07-27 stsp
3615 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3616 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3617 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3618 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3619 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3620 abb4604f 2019-07-27 stsp return NULL;
3621 abb4604f 2019-07-27 stsp }
3622 abb4604f 2019-07-27 stsp
3623 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3624 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3625 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 abb4604f 2019-07-27 stsp
3627 abb4604f 2019-07-27 stsp return NULL;
3628 3143d852 2020-06-25 stsp }
3629 3143d852 2020-06-25 stsp
3630 3143d852 2020-06-25 stsp static const struct got_error *
3631 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3632 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3633 3143d852 2020-06-25 stsp {
3634 3143d852 2020-06-25 stsp const struct got_error *err;
3635 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3636 3143d852 2020-06-25 stsp
3637 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3638 3143d852 2020-06-25 stsp ".cvsignore");
3639 3143d852 2020-06-25 stsp if (err)
3640 3143d852 2020-06-25 stsp return err;
3641 3143d852 2020-06-25 stsp
3642 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3643 3143d852 2020-06-25 stsp ".gitignore");
3644 3143d852 2020-06-25 stsp if (err)
3645 3143d852 2020-06-25 stsp return err;
3646 3143d852 2020-06-25 stsp
3647 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3648 3143d852 2020-06-25 stsp if (err) {
3649 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3650 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3651 3143d852 2020-06-25 stsp return err;
3652 3143d852 2020-06-25 stsp }
3653 3143d852 2020-06-25 stsp for (;;) {
3654 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3655 3143d852 2020-06-25 stsp ".cvsignore");
3656 3143d852 2020-06-25 stsp if (err)
3657 3143d852 2020-06-25 stsp break;
3658 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3659 3143d852 2020-06-25 stsp ".gitignore");
3660 3143d852 2020-06-25 stsp if (err)
3661 3143d852 2020-06-25 stsp break;
3662 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3663 3143d852 2020-06-25 stsp if (err) {
3664 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3665 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3666 3143d852 2020-06-25 stsp break;
3667 3143d852 2020-06-25 stsp }
3668 b737c85a 2020-06-26 stsp free(parent_path);
3669 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3670 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3671 3143d852 2020-06-25 stsp }
3672 3143d852 2020-06-25 stsp
3673 b737c85a 2020-06-26 stsp free(parent_path);
3674 b737c85a 2020-06-26 stsp free(next_parent_path);
3675 3143d852 2020-06-25 stsp return err;
3676 abb4604f 2019-07-27 stsp }
3677 abb4604f 2019-07-27 stsp
3678 abb4604f 2019-07-27 stsp static const struct got_error *
3679 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3680 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3681 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3682 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3683 f2a9dc41 2019-12-13 tracey int report_unchanged)
3684 f8d1f275 2019-02-04 stsp {
3685 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3686 6fc93f37 2019-12-13 stsp int fd = -1;
3687 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3688 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3689 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3690 3143d852 2020-06-25 stsp
3691 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3692 f8d1f275 2019-02-04 stsp
3693 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3694 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3695 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3696 8dc303cc 2019-07-27 stsp
3697 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3698 6fc93f37 2019-12-13 stsp if (fd == -1) {
3699 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3700 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3701 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3702 abb4604f 2019-07-27 stsp else
3703 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3704 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3705 f2a9dc41 2019-12-13 tracey report_unchanged);
3706 abb4604f 2019-07-27 stsp } else {
3707 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3708 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3709 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3710 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3711 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3712 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3713 abb4604f 2019-07-27 stsp arg.status_path = path;
3714 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3715 abb4604f 2019-07-27 stsp arg.repo = repo;
3716 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3717 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3718 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3719 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3720 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3721 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3722 022fae89 2019-12-06 tracey if (!no_ignores) {
3723 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3724 3143d852 2020-06-25 stsp worktree->root_path, path);
3725 3143d852 2020-06-25 stsp if (err)
3726 3143d852 2020-06-25 stsp goto done;
3727 022fae89 2019-12-06 tracey }
3728 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3729 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3730 927df6b7 2019-02-10 stsp }
3731 3143d852 2020-06-25 stsp done:
3732 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3733 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3734 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3735 927df6b7 2019-02-10 stsp free(ondisk_path);
3736 347d1d3e 2019-07-12 stsp return err;
3737 347d1d3e 2019-07-12 stsp }
3738 347d1d3e 2019-07-12 stsp
3739 347d1d3e 2019-07-12 stsp const struct got_error *
3740 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3741 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3742 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3743 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3744 347d1d3e 2019-07-12 stsp {
3745 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3746 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3747 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3748 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3749 347d1d3e 2019-07-12 stsp
3750 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3751 347d1d3e 2019-07-12 stsp if (err)
3752 347d1d3e 2019-07-12 stsp return err;
3753 347d1d3e 2019-07-12 stsp
3754 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3755 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3756 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3757 f6343036 2021-06-22 stsp no_ignores, 0);
3758 72ea6654 2019-07-27 stsp if (err)
3759 72ea6654 2019-07-27 stsp break;
3760 72ea6654 2019-07-27 stsp }
3761 f8d1f275 2019-02-04 stsp free(fileindex_path);
3762 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3763 f8d1f275 2019-02-04 stsp return err;
3764 f8d1f275 2019-02-04 stsp }
3765 6c7ab921 2019-03-18 stsp
3766 6c7ab921 2019-03-18 stsp const struct got_error *
3767 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3768 6c7ab921 2019-03-18 stsp const char *arg)
3769 6c7ab921 2019-03-18 stsp {
3770 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3771 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3772 6c7ab921 2019-03-18 stsp size_t len;
3773 00bb5ea0 2020-07-23 stsp struct stat sb;
3774 01740607 2020-11-04 stsp char *abspath = NULL;
3775 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3776 6c7ab921 2019-03-18 stsp
3777 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3778 6c7ab921 2019-03-18 stsp
3779 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3780 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3781 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3782 00bb5ea0 2020-07-23 stsp
3783 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3784 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3785 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3786 00bb5ea0 2020-07-23 stsp goto done;
3787 00bb5ea0 2020-07-23 stsp }
3788 727173c3 2020-11-06 stsp sb.st_mode = 0;
3789 00bb5ea0 2020-07-23 stsp }
3790 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3791 00bb5ea0 2020-07-23 stsp /*
3792 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3793 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3794 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3795 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3796 00bb5ea0 2020-07-23 stsp */
3797 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3798 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3799 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3800 00bb5ea0 2020-07-23 stsp goto done;
3801 00bb5ea0 2020-07-23 stsp }
3802 00bb5ea0 2020-07-23 stsp
3803 00bb5ea0 2020-07-23 stsp }
3804 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3805 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3806 00bb5ea0 2020-07-23 stsp if (err)
3807 d0710d08 2019-07-22 stsp goto done;
3808 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3809 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3810 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3811 00bb5ea0 2020-07-23 stsp goto done;
3812 00bb5ea0 2020-07-23 stsp }
3813 00bb5ea0 2020-07-23 stsp } else {
3814 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3815 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3816 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3817 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3818 00bb5ea0 2020-07-23 stsp goto done;
3819 00bb5ea0 2020-07-23 stsp }
3820 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3821 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3822 01740607 2020-11-04 stsp goto done;
3823 01740607 2020-11-04 stsp }
3824 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3825 01740607 2020-11-04 stsp sizeof(canonpath));
3826 01740607 2020-11-04 stsp if (err)
3827 00bb5ea0 2020-07-23 stsp goto done;
3828 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3829 01740607 2020-11-04 stsp if (resolved == NULL) {
3830 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3831 01740607 2020-11-04 stsp goto done;
3832 00bb5ea0 2020-07-23 stsp }
3833 d0710d08 2019-07-22 stsp }
3834 d0710d08 2019-07-22 stsp }
3835 6c7ab921 2019-03-18 stsp
3836 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3837 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3838 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3839 6c7ab921 2019-03-18 stsp goto done;
3840 6c7ab921 2019-03-18 stsp }
3841 6c7ab921 2019-03-18 stsp
3842 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3843 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3844 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3845 f6d88e1a 2019-05-29 stsp if (err)
3846 f6d88e1a 2019-05-29 stsp goto done;
3847 f6d88e1a 2019-05-29 stsp } else {
3848 f6d88e1a 2019-05-29 stsp path = strdup("");
3849 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3850 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3851 f6d88e1a 2019-05-29 stsp goto done;
3852 f6d88e1a 2019-05-29 stsp }
3853 6c7ab921 2019-03-18 stsp }
3854 6c7ab921 2019-03-18 stsp
3855 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3856 6c7ab921 2019-03-18 stsp len = strlen(path);
3857 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3858 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3859 6c7ab921 2019-03-18 stsp len--;
3860 6c7ab921 2019-03-18 stsp }
3861 6c7ab921 2019-03-18 stsp done:
3862 01740607 2020-11-04 stsp free(abspath);
3863 6c7ab921 2019-03-18 stsp free(resolved);
3864 d0710d08 2019-07-22 stsp free(cwd);
3865 6c7ab921 2019-03-18 stsp if (err == NULL)
3866 6c7ab921 2019-03-18 stsp *wt_path = path;
3867 6c7ab921 2019-03-18 stsp else
3868 6c7ab921 2019-03-18 stsp free(path);
3869 d00136be 2019-03-26 stsp return err;
3870 d00136be 2019-03-26 stsp }
3871 d00136be 2019-03-26 stsp
3872 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3873 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3874 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3875 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3876 4e68cba3 2019-11-23 stsp void *progress_arg;
3877 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3878 4e68cba3 2019-11-23 stsp };
3879 4e68cba3 2019-11-23 stsp
3880 4e68cba3 2019-11-23 stsp static const struct got_error *
3881 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3882 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3883 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3884 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3885 07f5b47a 2019-06-02 stsp {
3886 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3887 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3888 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3889 6d022e97 2019-08-04 stsp struct stat sb;
3890 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3891 07f5b47a 2019-06-02 stsp
3892 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3893 dbb83fbd 2019-12-12 stsp relpath) == -1)
3894 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3895 dbb83fbd 2019-12-12 stsp
3896 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3897 6d022e97 2019-08-04 stsp if (ie) {
3898 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3899 12463d8b 2019-12-13 stsp de_name, a->repo);
3900 6d022e97 2019-08-04 stsp if (err)
3901 dbb83fbd 2019-12-12 stsp goto done;
3902 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3903 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3904 dbb83fbd 2019-12-12 stsp goto done;
3905 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3906 dbb83fbd 2019-12-12 stsp if (err)
3907 dbb83fbd 2019-12-12 stsp goto done;
3908 6d022e97 2019-08-04 stsp }
3909 07f5b47a 2019-06-02 stsp
3910 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3911 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3912 dbb83fbd 2019-12-12 stsp goto done;
3913 4e68cba3 2019-11-23 stsp }
3914 07f5b47a 2019-06-02 stsp
3915 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3916 4e68cba3 2019-11-23 stsp if (err)
3917 4e68cba3 2019-11-23 stsp goto done;
3918 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3919 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3920 3969253a 2020-03-07 stsp if (err) {
3921 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3922 3969253a 2020-03-07 stsp goto done;
3923 3969253a 2020-03-07 stsp }
3924 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3925 07f5b47a 2019-06-02 stsp if (err) {
3926 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3927 4e68cba3 2019-11-23 stsp goto done;
3928 07f5b47a 2019-06-02 stsp }
3929 4e68cba3 2019-11-23 stsp done:
3930 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3931 dbb83fbd 2019-12-12 stsp if (err)
3932 dbb83fbd 2019-12-12 stsp return err;
3933 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3934 dbb83fbd 2019-12-12 stsp return NULL;
3935 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3936 07f5b47a 2019-06-02 stsp }
3937 07f5b47a 2019-06-02 stsp
3938 d00136be 2019-03-26 stsp const struct got_error *
3939 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3940 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3941 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3942 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3943 d00136be 2019-03-26 stsp {
3944 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3945 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3946 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3947 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3948 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3949 d00136be 2019-03-26 stsp
3950 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3951 d00136be 2019-03-26 stsp if (err)
3952 d00136be 2019-03-26 stsp return err;
3953 d00136be 2019-03-26 stsp
3954 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3955 d00136be 2019-03-26 stsp if (err)
3956 d00136be 2019-03-26 stsp goto done;
3957 d00136be 2019-03-26 stsp
3958 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3959 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3960 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3961 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3962 4e68cba3 2019-11-23 stsp saa.repo = repo;
3963 4e68cba3 2019-11-23 stsp
3964 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3965 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3966 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3967 1dd54920 2019-05-11 stsp if (err)
3968 af12c6b9 2019-06-04 stsp break;
3969 1dd54920 2019-05-11 stsp }
3970 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3971 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3972 af12c6b9 2019-06-04 stsp err = sync_err;
3973 d00136be 2019-03-26 stsp done:
3974 fb399478 2019-07-12 stsp free(fileindex_path);
3975 d00136be 2019-03-26 stsp if (fileindex)
3976 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3977 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3978 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3979 d00136be 2019-03-26 stsp err = unlockerr;
3980 6c7ab921 2019-03-18 stsp return err;
3981 6c7ab921 2019-03-18 stsp }
3982 17ed4618 2019-06-02 stsp
3983 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3984 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3985 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3986 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3987 f2a9dc41 2019-12-13 tracey void *progress_arg;
3988 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3989 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3990 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3991 766841c2 2020-08-13 stsp const char *status_codes;
3992 f2a9dc41 2019-12-13 tracey };
3993 f2a9dc41 2019-12-13 tracey
3994 f2a9dc41 2019-12-13 tracey static const struct got_error *
3995 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3996 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3997 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3998 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3999 17ed4618 2019-06-02 stsp {
4000 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4001 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4002 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4003 17ed4618 2019-06-02 stsp struct stat sb;
4004 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4005 17ed4618 2019-06-02 stsp
4006 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4007 17ed4618 2019-06-02 stsp if (ie == NULL)
4008 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4009 2ec1f75b 2019-03-26 stsp
4010 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4011 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4012 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4013 9acbc4fa 2019-08-03 stsp return NULL;
4014 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4015 9acbc4fa 2019-08-03 stsp }
4016 9acbc4fa 2019-08-03 stsp
4017 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4018 f2a9dc41 2019-12-13 tracey relpath) == -1)
4019 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4020 f2a9dc41 2019-12-13 tracey
4021 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4022 12463d8b 2019-12-13 stsp a->repo);
4023 17ed4618 2019-06-02 stsp if (err)
4024 f2a9dc41 2019-12-13 tracey goto done;
4025 17ed4618 2019-06-02 stsp
4026 766841c2 2020-08-13 stsp if (a->status_codes) {
4027 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4028 766841c2 2020-08-13 stsp int i;
4029 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4030 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4031 766841c2 2020-08-13 stsp break;
4032 766841c2 2020-08-13 stsp }
4033 766841c2 2020-08-13 stsp if (i == ncodes) {
4034 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4035 766841c2 2020-08-13 stsp free(ondisk_path);
4036 766841c2 2020-08-13 stsp return NULL;
4037 766841c2 2020-08-13 stsp }
4038 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4039 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4040 766841c2 2020-08-13 stsp static char msg[64];
4041 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4042 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4043 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4044 766841c2 2020-08-13 stsp goto done;
4045 766841c2 2020-08-13 stsp }
4046 766841c2 2020-08-13 stsp }
4047 766841c2 2020-08-13 stsp
4048 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4049 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4050 f2a9dc41 2019-12-13 tracey goto done;
4051 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4052 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4053 f2a9dc41 2019-12-13 tracey goto done;
4054 f2a9dc41 2019-12-13 tracey }
4055 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4056 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4057 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4058 f2a9dc41 2019-12-13 tracey goto done;
4059 f2a9dc41 2019-12-13 tracey }
4060 17ed4618 2019-06-02 stsp }
4061 17ed4618 2019-06-02 stsp
4062 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4063 20a2ad1c 2020-10-20 stsp size_t root_len;
4064 20a2ad1c 2020-10-20 stsp
4065 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4066 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4067 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4068 12463d8b 2019-12-13 stsp ondisk_path);
4069 12463d8b 2019-12-13 stsp goto done;
4070 12463d8b 2019-12-13 stsp }
4071 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4072 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4073 12463d8b 2019-12-13 stsp goto done;
4074 15341bfd 2020-03-05 tracey }
4075 15341bfd 2020-03-05 tracey
4076 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4077 20a2ad1c 2020-10-20 stsp do {
4078 20a2ad1c 2020-10-20 stsp char *parent;
4079 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4080 20a2ad1c 2020-10-20 stsp if (err)
4081 2513f20a 2020-10-20 stsp goto done;
4082 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4083 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4084 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4085 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4086 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4087 20a2ad1c 2020-10-20 stsp ondisk_path);
4088 15341bfd 2020-03-05 tracey break;
4089 15341bfd 2020-03-05 tracey }
4090 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4091 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4092 f2a9dc41 2019-12-13 tracey }
4093 17ed4618 2019-06-02 stsp
4094 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4095 f2a9dc41 2019-12-13 tracey done:
4096 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4097 f2a9dc41 2019-12-13 tracey if (err)
4098 f2a9dc41 2019-12-13 tracey return err;
4099 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4100 f2a9dc41 2019-12-13 tracey return NULL;
4101 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4102 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4103 17ed4618 2019-06-02 stsp }
4104 17ed4618 2019-06-02 stsp
4105 2ec1f75b 2019-03-26 stsp const struct got_error *
4106 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4107 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4108 766841c2 2020-08-13 stsp const char *status_codes,
4109 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4110 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4111 2ec1f75b 2019-03-26 stsp {
4112 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4113 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4114 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4115 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4116 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4117 2ec1f75b 2019-03-26 stsp
4118 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4119 2ec1f75b 2019-03-26 stsp if (err)
4120 2ec1f75b 2019-03-26 stsp return err;
4121 2ec1f75b 2019-03-26 stsp
4122 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4123 2ec1f75b 2019-03-26 stsp if (err)
4124 2ec1f75b 2019-03-26 stsp goto done;
4125 f2a9dc41 2019-12-13 tracey
4126 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4127 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4128 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4129 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4130 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4131 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4132 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4133 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4134 2ec1f75b 2019-03-26 stsp
4135 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4136 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4137 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4138 17ed4618 2019-06-02 stsp if (err)
4139 af12c6b9 2019-06-04 stsp break;
4140 2ec1f75b 2019-03-26 stsp }
4141 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4142 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4143 af12c6b9 2019-06-04 stsp err = sync_err;
4144 2ec1f75b 2019-03-26 stsp done:
4145 fb399478 2019-07-12 stsp free(fileindex_path);
4146 2ec1f75b 2019-03-26 stsp if (fileindex)
4147 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4148 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4149 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4150 2ec1f75b 2019-03-26 stsp err = unlockerr;
4151 33aa809d 2019-08-08 stsp return err;
4152 33aa809d 2019-08-08 stsp }
4153 33aa809d 2019-08-08 stsp
4154 33aa809d 2019-08-08 stsp static const struct got_error *
4155 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4156 33aa809d 2019-08-08 stsp {
4157 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4158 33aa809d 2019-08-08 stsp char *line = NULL;
4159 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4160 33aa809d 2019-08-08 stsp ssize_t linelen;
4161 33aa809d 2019-08-08 stsp
4162 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4163 33aa809d 2019-08-08 stsp if (linelen == -1) {
4164 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4165 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4166 33aa809d 2019-08-08 stsp goto done;
4167 33aa809d 2019-08-08 stsp }
4168 33aa809d 2019-08-08 stsp return NULL;
4169 33aa809d 2019-08-08 stsp }
4170 33aa809d 2019-08-08 stsp if (outfile) {
4171 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4172 33aa809d 2019-08-08 stsp if (n != linelen) {
4173 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4174 33aa809d 2019-08-08 stsp goto done;
4175 33aa809d 2019-08-08 stsp }
4176 33aa809d 2019-08-08 stsp }
4177 33aa809d 2019-08-08 stsp if (rejectfile) {
4178 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4179 33aa809d 2019-08-08 stsp if (n != linelen)
4180 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4181 33aa809d 2019-08-08 stsp }
4182 33aa809d 2019-08-08 stsp done:
4183 33aa809d 2019-08-08 stsp free(line);
4184 2ec1f75b 2019-03-26 stsp return err;
4185 2ec1f75b 2019-03-26 stsp }
4186 1f1abb7e 2019-08-08 stsp
4187 33aa809d 2019-08-08 stsp static const struct got_error *
4188 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4189 33aa809d 2019-08-08 stsp {
4190 33aa809d 2019-08-08 stsp char *line = NULL;
4191 33aa809d 2019-08-08 stsp size_t linesize = 0;
4192 33aa809d 2019-08-08 stsp ssize_t linelen;
4193 33aa809d 2019-08-08 stsp
4194 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4195 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4196 500467ff 2019-09-25 hiltjo if (ferror(f))
4197 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4198 500467ff 2019-09-25 hiltjo return NULL;
4199 500467ff 2019-09-25 hiltjo }
4200 33aa809d 2019-08-08 stsp free(line);
4201 33aa809d 2019-08-08 stsp return NULL;
4202 33aa809d 2019-08-08 stsp }
4203 33aa809d 2019-08-08 stsp
4204 33aa809d 2019-08-08 stsp static const struct got_error *
4205 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4206 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4207 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4208 33aa809d 2019-08-08 stsp {
4209 33aa809d 2019-08-08 stsp const struct got_error *err;
4210 33aa809d 2019-08-08 stsp
4211 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4212 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4213 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4214 33aa809d 2019-08-08 stsp if (err)
4215 33aa809d 2019-08-08 stsp return err;
4216 33aa809d 2019-08-08 stsp (*line_cur1)++;
4217 33aa809d 2019-08-08 stsp }
4218 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4219 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4220 33aa809d 2019-08-08 stsp if (rejectfile)
4221 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4222 33aa809d 2019-08-08 stsp else
4223 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4224 33aa809d 2019-08-08 stsp if (err)
4225 33aa809d 2019-08-08 stsp return err;
4226 33aa809d 2019-08-08 stsp (*line_cur2)++;
4227 33aa809d 2019-08-08 stsp }
4228 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4229 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4230 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4231 33aa809d 2019-08-08 stsp if (err)
4232 33aa809d 2019-08-08 stsp return err;
4233 33aa809d 2019-08-08 stsp (*line_cur2)++;
4234 33aa809d 2019-08-08 stsp }
4235 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4236 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4237 33aa809d 2019-08-08 stsp if (rejectfile)
4238 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4239 33aa809d 2019-08-08 stsp else
4240 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4241 33aa809d 2019-08-08 stsp if (err)
4242 33aa809d 2019-08-08 stsp return err;
4243 33aa809d 2019-08-08 stsp (*line_cur1)++;
4244 33aa809d 2019-08-08 stsp }
4245 f1e81a05 2019-08-10 stsp
4246 f1e81a05 2019-08-10 stsp return NULL;
4247 f1e81a05 2019-08-10 stsp }
4248 f1e81a05 2019-08-10 stsp
4249 f1e81a05 2019-08-10 stsp static const struct got_error *
4250 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4251 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4252 f1e81a05 2019-08-10 stsp {
4253 f1e81a05 2019-08-10 stsp const struct got_error *err;
4254 f1e81a05 2019-08-10 stsp
4255 f1e81a05 2019-08-10 stsp if (outfile) {
4256 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4257 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4258 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4259 f1e81a05 2019-08-10 stsp if (err)
4260 f1e81a05 2019-08-10 stsp return err;
4261 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4262 f1e81a05 2019-08-10 stsp }
4263 f1e81a05 2019-08-10 stsp }
4264 f1e81a05 2019-08-10 stsp if (rejectfile) {
4265 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4266 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4267 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4268 f1e81a05 2019-08-10 stsp if (err)
4269 f1e81a05 2019-08-10 stsp return err;
4270 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4271 f1e81a05 2019-08-10 stsp }
4272 33aa809d 2019-08-08 stsp }
4273 33aa809d 2019-08-08 stsp
4274 33aa809d 2019-08-08 stsp return NULL;
4275 33aa809d 2019-08-08 stsp }
4276 33aa809d 2019-08-08 stsp
4277 33aa809d 2019-08-08 stsp static const struct got_error *
4278 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4279 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4280 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4281 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4282 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4283 33aa809d 2019-08-08 stsp {
4284 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4285 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4286 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4287 33aa809d 2019-08-08 stsp FILE *hunkfile;
4288 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4289 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4290 fe621944 2020-11-10 stsp int rc;
4291 33aa809d 2019-08-08 stsp
4292 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4293 33aa809d 2019-08-08 stsp
4294 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4295 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4296 fe621944 2020-11-10 stsp start_old = cc.left.start;
4297 fe621944 2020-11-10 stsp end_old = cc.left.end;
4298 fe621944 2020-11-10 stsp start_new = cc.right.start;
4299 fe621944 2020-11-10 stsp end_new = cc.right.end;
4300 33aa809d 2019-08-08 stsp
4301 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4302 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4303 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4304 33aa809d 2019-08-08 stsp
4305 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4306 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4307 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4308 33aa809d 2019-08-08 stsp
4309 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4310 fe621944 2020-11-10 stsp if (diff_state == NULL)
4311 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4312 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4313 fe621944 2020-11-10 stsp
4314 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4315 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4316 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4317 33aa809d 2019-08-08 stsp goto done;
4318 33aa809d 2019-08-08 stsp }
4319 fe621944 2020-11-10 stsp
4320 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4321 fe621944 2020-11-10 stsp diff_result, &cc);
4322 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4323 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4324 33aa809d 2019-08-08 stsp goto done;
4325 33aa809d 2019-08-08 stsp }
4326 fe621944 2020-11-10 stsp
4327 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4328 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4329 33aa809d 2019-08-08 stsp goto done;
4330 33aa809d 2019-08-08 stsp }
4331 33aa809d 2019-08-08 stsp
4332 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4333 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4334 33aa809d 2019-08-08 stsp if (err)
4335 33aa809d 2019-08-08 stsp goto done;
4336 33aa809d 2019-08-08 stsp
4337 33aa809d 2019-08-08 stsp switch (*choice) {
4338 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4339 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4340 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4341 33aa809d 2019-08-08 stsp break;
4342 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4343 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4344 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4345 33aa809d 2019-08-08 stsp break;
4346 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4347 33aa809d 2019-08-08 stsp break;
4348 33aa809d 2019-08-08 stsp default:
4349 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4350 33aa809d 2019-08-08 stsp break;
4351 33aa809d 2019-08-08 stsp }
4352 33aa809d 2019-08-08 stsp done:
4353 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4354 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4355 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4356 33aa809d 2019-08-08 stsp return err;
4357 33aa809d 2019-08-08 stsp }
4358 33aa809d 2019-08-08 stsp
4359 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4360 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4361 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4362 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4363 1f1abb7e 2019-08-08 stsp void *progress_arg;
4364 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4365 33aa809d 2019-08-08 stsp void *patch_arg;
4366 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4367 1f1abb7e 2019-08-08 stsp };
4368 a129376b 2019-03-28 stsp
4369 e20a8b6f 2019-06-04 stsp static const struct got_error *
4370 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4371 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4372 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4373 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4374 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4375 33aa809d 2019-08-08 stsp {
4376 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4377 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4378 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4379 1ebedb77 2019-10-19 stsp int fd2 = -1;
4380 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4381 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4382 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4383 fe621944 2020-11-10 stsp struct stat sb2;
4384 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4385 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4386 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4387 33aa809d 2019-08-08 stsp
4388 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4389 33aa809d 2019-08-08 stsp
4390 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4391 33aa809d 2019-08-08 stsp if (err)
4392 33aa809d 2019-08-08 stsp return err;
4393 33aa809d 2019-08-08 stsp
4394 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4395 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4396 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4397 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4398 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4399 fa3cef63 2020-07-23 stsp goto done;
4400 fa3cef63 2020-07-23 stsp }
4401 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4402 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4403 fa3cef63 2020-07-23 stsp if (link_len == -1)
4404 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4405 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4406 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4407 12463d8b 2019-12-13 stsp }
4408 12463d8b 2019-12-13 stsp } else {
4409 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4410 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4411 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4412 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4413 fa3cef63 2020-07-23 stsp goto done;
4414 fa3cef63 2020-07-23 stsp }
4415 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4416 fa3cef63 2020-07-23 stsp sizeof(link_target));
4417 fa3cef63 2020-07-23 stsp if (link_len == -1)
4418 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4419 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4420 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4421 12463d8b 2019-12-13 stsp }
4422 1ebedb77 2019-10-19 stsp }
4423 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4424 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4425 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4426 fa3cef63 2020-07-23 stsp goto done;
4427 fa3cef63 2020-07-23 stsp }
4428 1ebedb77 2019-10-19 stsp
4429 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4430 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4431 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4432 fa3cef63 2020-07-23 stsp goto done;
4433 fa3cef63 2020-07-23 stsp }
4434 fa3cef63 2020-07-23 stsp fd2 = -1;
4435 fa3cef63 2020-07-23 stsp } else {
4436 fa3cef63 2020-07-23 stsp size_t n;
4437 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4438 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4439 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4440 fa3cef63 2020-07-23 stsp goto done;
4441 fa3cef63 2020-07-23 stsp }
4442 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4443 fa3cef63 2020-07-23 stsp if (n != link_len) {
4444 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4445 fa3cef63 2020-07-23 stsp goto done;
4446 fa3cef63 2020-07-23 stsp }
4447 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4448 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4449 fa3cef63 2020-07-23 stsp goto done;
4450 fa3cef63 2020-07-23 stsp }
4451 fa3cef63 2020-07-23 stsp rewind(f2);
4452 33aa809d 2019-08-08 stsp }
4453 33aa809d 2019-08-08 stsp
4454 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4455 33aa809d 2019-08-08 stsp if (err)
4456 33aa809d 2019-08-08 stsp goto done;
4457 33aa809d 2019-08-08 stsp
4458 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4459 33aa809d 2019-08-08 stsp if (err)
4460 33aa809d 2019-08-08 stsp goto done;
4461 33aa809d 2019-08-08 stsp
4462 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4463 33aa809d 2019-08-08 stsp if (err)
4464 33aa809d 2019-08-08 stsp goto done;
4465 33aa809d 2019-08-08 stsp
4466 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4467 fe621944 2020-11-10 stsp NULL);
4468 33aa809d 2019-08-08 stsp if (err)
4469 33aa809d 2019-08-08 stsp goto done;
4470 33aa809d 2019-08-08 stsp
4471 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4472 33aa809d 2019-08-08 stsp if (err)
4473 33aa809d 2019-08-08 stsp goto done;
4474 33aa809d 2019-08-08 stsp
4475 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4476 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4477 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4478 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4479 fe621944 2020-11-10 stsp
4480 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4481 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4482 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4483 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4484 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4485 fe621944 2020-11-10 stsp nchanges++;
4486 fe621944 2020-11-10 stsp }
4487 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4488 33aa809d 2019-08-08 stsp int choice;
4489 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4490 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4491 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4492 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4493 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4494 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4495 33aa809d 2019-08-08 stsp if (err)
4496 33aa809d 2019-08-08 stsp goto done;
4497 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4498 33aa809d 2019-08-08 stsp have_content = 1;
4499 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4500 33aa809d 2019-08-08 stsp break;
4501 33aa809d 2019-08-08 stsp }
4502 1ebedb77 2019-10-19 stsp if (have_content) {
4503 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4504 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4505 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4506 1ebedb77 2019-10-19 stsp if (err)
4507 1ebedb77 2019-10-19 stsp goto done;
4508 1ebedb77 2019-10-19 stsp
4509 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4510 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4511 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4512 fa3cef63 2020-07-23 stsp goto done;
4513 fa3cef63 2020-07-23 stsp }
4514 1ebedb77 2019-10-19 stsp }
4515 1ebedb77 2019-10-19 stsp }
4516 33aa809d 2019-08-08 stsp done:
4517 33aa809d 2019-08-08 stsp free(id_str);
4518 33aa809d 2019-08-08 stsp if (blob)
4519 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4520 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4521 fe621944 2020-11-10 stsp if (err == NULL)
4522 fe621944 2020-11-10 stsp err = free_err;
4523 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4524 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4525 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4526 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4527 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4528 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4529 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4530 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4531 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4532 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4533 33aa809d 2019-08-08 stsp if (err || !have_content) {
4534 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4535 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4536 33aa809d 2019-08-08 stsp free(*path_outfile);
4537 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4538 33aa809d 2019-08-08 stsp }
4539 33aa809d 2019-08-08 stsp free(path1);
4540 33aa809d 2019-08-08 stsp return err;
4541 33aa809d 2019-08-08 stsp }
4542 33aa809d 2019-08-08 stsp
4543 33aa809d 2019-08-08 stsp static const struct got_error *
4544 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4545 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4546 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4547 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4548 a129376b 2019-03-28 stsp {
4549 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4550 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4551 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4552 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4553 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4554 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4555 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4556 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4557 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4558 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4559 a129376b 2019-03-28 stsp
4560 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4561 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4562 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4563 d3bcc3d1 2019-08-08 stsp return NULL;
4564 3d69ad8d 2019-08-17 semarie
4565 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4566 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4567 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4568 d3bcc3d1 2019-08-08 stsp
4569 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4570 65084dad 2019-08-08 stsp if (ie == NULL)
4571 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4572 a129376b 2019-03-28 stsp
4573 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4574 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4575 a129376b 2019-03-28 stsp if (err) {
4576 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4577 a129376b 2019-03-28 stsp goto done;
4578 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4579 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4580 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4581 e20a8b6f 2019-06-04 stsp goto done;
4582 e20a8b6f 2019-06-04 stsp }
4583 a129376b 2019-03-28 stsp }
4584 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4585 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4586 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4587 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4588 a129376b 2019-03-28 stsp goto done;
4589 a129376b 2019-03-28 stsp }
4590 a129376b 2019-03-28 stsp } else {
4591 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4592 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4593 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4594 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4595 a129376b 2019-03-28 stsp goto done;
4596 a129376b 2019-03-28 stsp }
4597 a129376b 2019-03-28 stsp } else {
4598 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4599 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4601 a129376b 2019-03-28 stsp goto done;
4602 a129376b 2019-03-28 stsp }
4603 a129376b 2019-03-28 stsp }
4604 a129376b 2019-03-28 stsp }
4605 a129376b 2019-03-28 stsp
4606 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4607 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4608 a9fa2909 2019-07-27 stsp if (err) {
4609 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4610 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4611 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4612 a9fa2909 2019-07-27 stsp goto done;
4613 a9fa2909 2019-07-27 stsp } else {
4614 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4615 a9fa2909 2019-07-27 stsp if (err)
4616 a9fa2909 2019-07-27 stsp goto done;
4617 a9fa2909 2019-07-27 stsp
4618 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4619 1233e6b6 2020-10-19 stsp if (err)
4620 a9fa2909 2019-07-27 stsp goto done;
4621 a9fa2909 2019-07-27 stsp
4622 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4623 1233e6b6 2020-10-19 stsp free(te_name);
4624 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4625 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4626 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4627 a9fa2909 2019-07-27 stsp goto done;
4628 a9fa2909 2019-07-27 stsp }
4629 a129376b 2019-03-28 stsp }
4630 a129376b 2019-03-28 stsp
4631 a129376b 2019-03-28 stsp switch (status) {
4632 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4633 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4634 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4635 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4636 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4637 33aa809d 2019-08-08 stsp if (err)
4638 33aa809d 2019-08-08 stsp goto done;
4639 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4640 33aa809d 2019-08-08 stsp break;
4641 33aa809d 2019-08-08 stsp }
4642 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4643 1f1abb7e 2019-08-08 stsp ie->path);
4644 1ee397ad 2019-07-12 stsp if (err)
4645 1ee397ad 2019-07-12 stsp goto done;
4646 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4647 a129376b 2019-03-28 stsp break;
4648 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4649 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4650 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4651 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4652 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4653 33aa809d 2019-08-08 stsp if (err)
4654 33aa809d 2019-08-08 stsp goto done;
4655 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4656 33aa809d 2019-08-08 stsp break;
4657 33aa809d 2019-08-08 stsp }
4658 33aa809d 2019-08-08 stsp /* fall through */
4659 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4660 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4661 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4662 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4663 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4664 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4665 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4666 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4667 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4668 24278f30 2019-08-03 stsp } else
4669 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4670 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4671 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4672 a129376b 2019-03-28 stsp if (err)
4673 65084dad 2019-08-08 stsp goto done;
4674 65084dad 2019-08-08 stsp
4675 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4676 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4677 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4678 a129376b 2019-03-28 stsp goto done;
4679 65084dad 2019-08-08 stsp }
4680 65084dad 2019-08-08 stsp
4681 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4682 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4683 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4684 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4685 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4686 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4687 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4688 33aa809d 2019-08-08 stsp break;
4689 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4690 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4691 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4692 369fd7e5 2020-07-23 stsp path_content);
4693 369fd7e5 2020-07-23 stsp break;
4694 369fd7e5 2020-07-23 stsp }
4695 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4696 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4697 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4698 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4699 369fd7e5 2020-07-23 stsp } else {
4700 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4701 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4702 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4703 369fd7e5 2020-07-23 stsp goto done;
4704 369fd7e5 2020-07-23 stsp }
4705 33aa809d 2019-08-08 stsp }
4706 33aa809d 2019-08-08 stsp } else {
4707 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4708 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4709 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4710 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4711 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4712 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4713 2e1fa222 2020-07-23 stsp } else {
4714 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4715 2e1fa222 2020-07-23 stsp ie->path,
4716 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4717 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4718 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4719 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4720 2e1fa222 2020-07-23 stsp }
4721 a129376b 2019-03-28 stsp if (err)
4722 a129376b 2019-03-28 stsp goto done;
4723 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4724 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4725 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4726 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4727 437adc9d 2020-12-10 yzhong blob->id.sha1,
4728 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4729 2e1fa222 2020-07-23 stsp if (err)
4730 2e1fa222 2020-07-23 stsp goto done;
4731 2e1fa222 2020-07-23 stsp }
4732 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4733 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4734 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4735 33aa809d 2019-08-08 stsp }
4736 a129376b 2019-03-28 stsp }
4737 a129376b 2019-03-28 stsp break;
4738 e20a8b6f 2019-06-04 stsp }
4739 a129376b 2019-03-28 stsp default:
4740 1f1abb7e 2019-08-08 stsp break;
4741 a129376b 2019-03-28 stsp }
4742 a129376b 2019-03-28 stsp done:
4743 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4744 33aa809d 2019-08-08 stsp free(path_content);
4745 e20a8b6f 2019-06-04 stsp free(parent_path);
4746 a129376b 2019-03-28 stsp free(tree_path);
4747 a129376b 2019-03-28 stsp if (blob)
4748 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4749 a129376b 2019-03-28 stsp if (tree)
4750 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4751 a129376b 2019-03-28 stsp free(tree_id);
4752 e20a8b6f 2019-06-04 stsp return err;
4753 e20a8b6f 2019-06-04 stsp }
4754 e20a8b6f 2019-06-04 stsp
4755 e20a8b6f 2019-06-04 stsp const struct got_error *
4756 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4757 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4758 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4759 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4760 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4761 e20a8b6f 2019-06-04 stsp {
4762 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4763 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4764 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4765 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4766 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4767 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4768 e20a8b6f 2019-06-04 stsp
4769 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4770 e20a8b6f 2019-06-04 stsp if (err)
4771 e20a8b6f 2019-06-04 stsp return err;
4772 e20a8b6f 2019-06-04 stsp
4773 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4774 e20a8b6f 2019-06-04 stsp if (err)
4775 e20a8b6f 2019-06-04 stsp goto done;
4776 e20a8b6f 2019-06-04 stsp
4777 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4778 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4779 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4780 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4781 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4782 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4783 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4784 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4785 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4786 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4787 e20a8b6f 2019-06-04 stsp if (err)
4788 af12c6b9 2019-06-04 stsp break;
4789 e20a8b6f 2019-06-04 stsp }
4790 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4791 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4792 e20a8b6f 2019-06-04 stsp err = sync_err;
4793 af12c6b9 2019-06-04 stsp done:
4794 fb399478 2019-07-12 stsp free(fileindex_path);
4795 a129376b 2019-03-28 stsp if (fileindex)
4796 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4797 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4798 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4799 a129376b 2019-03-28 stsp err = unlockerr;
4800 c4296144 2019-05-09 stsp return err;
4801 c4296144 2019-05-09 stsp }
4802 c4296144 2019-05-09 stsp
4803 cf066bf8 2019-05-09 stsp static void
4804 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4805 cf066bf8 2019-05-09 stsp {
4806 24519714 2019-05-09 stsp free(ct->path);
4807 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4808 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4809 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4810 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4811 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4812 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4813 cf066bf8 2019-05-09 stsp free(ct);
4814 cf066bf8 2019-05-09 stsp }
4815 24519714 2019-05-09 stsp
4816 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4817 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4818 24519714 2019-05-09 stsp struct got_repository *repo;
4819 24519714 2019-05-09 stsp struct got_worktree *worktree;
4820 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4821 5f8a88c6 2019-08-03 stsp int have_staged_files;
4822 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4823 24519714 2019-05-09 stsp };
4824 cf066bf8 2019-05-09 stsp
4825 c4296144 2019-05-09 stsp static const struct got_error *
4826 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4827 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4828 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4829 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4830 c4296144 2019-05-09 stsp {
4831 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4832 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4833 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4834 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4835 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4836 768aea60 2019-05-09 stsp struct stat sb;
4837 c4296144 2019-05-09 stsp
4838 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4839 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4840 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4841 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4842 5f8a88c6 2019-08-03 stsp return NULL;
4843 5f8a88c6 2019-08-03 stsp } else {
4844 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4845 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4846 c4296144 2019-05-09 stsp
4847 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4848 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4849 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4850 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4851 5f8a88c6 2019-08-03 stsp return NULL;
4852 5f8a88c6 2019-08-03 stsp }
4853 0b5cc0d6 2019-05-09 stsp
4854 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4855 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4856 036813ee 2019-05-09 stsp goto done;
4857 036813ee 2019-05-09 stsp }
4858 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4859 036813ee 2019-05-09 stsp parent_path = strdup("");
4860 69960a46 2019-05-09 stsp if (parent_path == NULL)
4861 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4862 69960a46 2019-05-09 stsp } else {
4863 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4864 69960a46 2019-05-09 stsp if (err)
4865 69960a46 2019-05-09 stsp return err;
4866 69960a46 2019-05-09 stsp }
4867 c4296144 2019-05-09 stsp
4868 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4869 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4870 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4871 c4296144 2019-05-09 stsp goto done;
4872 768aea60 2019-05-09 stsp }
4873 768aea60 2019-05-09 stsp
4874 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4875 768aea60 2019-05-09 stsp relpath) == -1) {
4876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4877 768aea60 2019-05-09 stsp goto done;
4878 768aea60 2019-05-09 stsp }
4879 0aeb8099 2020-07-23 stsp
4880 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4881 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4882 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4883 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4884 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4885 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4886 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4887 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4888 0aeb8099 2020-07-23 stsp break;
4889 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4890 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4891 0aeb8099 2020-07-23 stsp break;
4892 0aeb8099 2020-07-23 stsp default:
4893 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4894 0aeb8099 2020-07-23 stsp goto done;
4895 0aeb8099 2020-07-23 stsp }
4896 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4897 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4898 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4899 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4900 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4901 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4902 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4903 12463d8b 2019-12-13 stsp ct->ondisk_path);
4904 12463d8b 2019-12-13 stsp goto done;
4905 12463d8b 2019-12-13 stsp }
4906 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4907 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4908 768aea60 2019-05-09 stsp goto done;
4909 768aea60 2019-05-09 stsp }
4910 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4911 c4296144 2019-05-09 stsp }
4912 c4296144 2019-05-09 stsp
4913 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4914 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4915 44d03001 2019-05-09 stsp relpath) == -1) {
4916 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4917 44d03001 2019-05-09 stsp goto done;
4918 44d03001 2019-05-09 stsp }
4919 44d03001 2019-05-09 stsp
4920 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4921 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4922 35213c7c 2020-07-23 stsp int is_bad_symlink;
4923 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4924 35213c7c 2020-07-23 stsp ssize_t target_len;
4925 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4926 35213c7c 2020-07-23 stsp sizeof(target_path));
4927 35213c7c 2020-07-23 stsp if (target_len == -1) {
4928 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4929 35213c7c 2020-07-23 stsp ct->ondisk_path);
4930 35213c7c 2020-07-23 stsp goto done;
4931 35213c7c 2020-07-23 stsp }
4932 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4933 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4934 35213c7c 2020-07-23 stsp if (err)
4935 35213c7c 2020-07-23 stsp goto done;
4936 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4937 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4938 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4939 35213c7c 2020-07-23 stsp goto done;
4940 35213c7c 2020-07-23 stsp }
4941 35213c7c 2020-07-23 stsp }
4942 35213c7c 2020-07-23 stsp
4943 35213c7c 2020-07-23 stsp
4944 cf066bf8 2019-05-09 stsp ct->status = status;
4945 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4946 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4947 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4948 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4949 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4950 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4951 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4952 b416585c 2019-05-13 stsp goto done;
4953 b416585c 2019-05-13 stsp }
4954 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4955 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4956 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4957 f0b75401 2019-08-03 stsp goto done;
4958 f0b75401 2019-08-03 stsp }
4959 f0b75401 2019-08-03 stsp }
4960 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4961 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4962 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4963 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4964 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4965 036813ee 2019-05-09 stsp goto done;
4966 036813ee 2019-05-09 stsp }
4967 ca2503ea 2019-05-09 stsp }
4968 24519714 2019-05-09 stsp ct->path = strdup(path);
4969 24519714 2019-05-09 stsp if (ct->path == NULL) {
4970 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4971 24519714 2019-05-09 stsp goto done;
4972 24519714 2019-05-09 stsp }
4973 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4974 c4296144 2019-05-09 stsp done:
4975 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4976 ed175427 2019-05-09 stsp free_commitable(ct);
4977 24519714 2019-05-09 stsp free(parent_path);
4978 036813ee 2019-05-09 stsp free(path);
4979 a129376b 2019-03-28 stsp return err;
4980 a129376b 2019-03-28 stsp }
4981 c4296144 2019-05-09 stsp
4982 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4983 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4984 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4985 036813ee 2019-05-09 stsp struct got_repository *);
4986 ed175427 2019-05-09 stsp
4987 ed175427 2019-05-09 stsp static const struct got_error *
4988 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4989 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4990 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4991 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4992 afa376bf 2019-05-09 stsp struct got_repository *repo)
4993 ed175427 2019-05-09 stsp {
4994 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4995 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4996 036813ee 2019-05-09 stsp char *subpath;
4997 ed175427 2019-05-09 stsp
4998 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4999 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5000 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5001 ed175427 2019-05-09 stsp
5002 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5003 ed175427 2019-05-09 stsp if (err)
5004 ed175427 2019-05-09 stsp return err;
5005 ed175427 2019-05-09 stsp
5006 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5007 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5008 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5009 036813ee 2019-05-09 stsp free(subpath);
5010 036813ee 2019-05-09 stsp return err;
5011 036813ee 2019-05-09 stsp }
5012 ed175427 2019-05-09 stsp
5013 036813ee 2019-05-09 stsp static const struct got_error *
5014 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5015 036813ee 2019-05-09 stsp {
5016 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5017 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5018 ed175427 2019-05-09 stsp
5019 036813ee 2019-05-09 stsp *match = 0;
5020 ed175427 2019-05-09 stsp
5021 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5022 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5023 0f63689d 2019-05-10 stsp return NULL;
5024 036813ee 2019-05-09 stsp }
5025 0b5cc0d6 2019-05-09 stsp
5026 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5027 0f63689d 2019-05-10 stsp if (err)
5028 0f63689d 2019-05-10 stsp return err;
5029 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5030 036813ee 2019-05-09 stsp free(ct_parent_path);
5031 036813ee 2019-05-09 stsp return err;
5032 036813ee 2019-05-09 stsp }
5033 0b5cc0d6 2019-05-09 stsp
5034 768aea60 2019-05-09 stsp static mode_t
5035 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5036 768aea60 2019-05-09 stsp {
5037 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5038 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5039 3d9a4ec4 2020-07-23 stsp
5040 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5041 768aea60 2019-05-09 stsp }
5042 768aea60 2019-05-09 stsp
5043 036813ee 2019-05-09 stsp static const struct got_error *
5044 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5045 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5046 036813ee 2019-05-09 stsp {
5047 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5048 ca2503ea 2019-05-09 stsp
5049 036813ee 2019-05-09 stsp *new_te = NULL;
5050 0b5cc0d6 2019-05-09 stsp
5051 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5052 036813ee 2019-05-09 stsp if (err)
5053 036813ee 2019-05-09 stsp goto done;
5054 0b5cc0d6 2019-05-09 stsp
5055 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5056 036813ee 2019-05-09 stsp
5057 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5058 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5059 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5060 5f8a88c6 2019-08-03 stsp else
5061 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5062 036813ee 2019-05-09 stsp done:
5063 036813ee 2019-05-09 stsp if (err && *new_te) {
5064 56e0773d 2019-11-28 stsp free(*new_te);
5065 036813ee 2019-05-09 stsp *new_te = NULL;
5066 036813ee 2019-05-09 stsp }
5067 036813ee 2019-05-09 stsp return err;
5068 036813ee 2019-05-09 stsp }
5069 036813ee 2019-05-09 stsp
5070 036813ee 2019-05-09 stsp static const struct got_error *
5071 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5072 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5073 036813ee 2019-05-09 stsp {
5074 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5075 102b254e 2020-10-19 stsp char *ct_name = NULL;
5076 036813ee 2019-05-09 stsp
5077 036813ee 2019-05-09 stsp *new_te = NULL;
5078 036813ee 2019-05-09 stsp
5079 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5080 036813ee 2019-05-09 stsp if (*new_te == NULL)
5081 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5082 036813ee 2019-05-09 stsp
5083 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5084 102b254e 2020-10-19 stsp if (err)
5085 036813ee 2019-05-09 stsp goto done;
5086 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5087 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5088 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5089 036813ee 2019-05-09 stsp goto done;
5090 036813ee 2019-05-09 stsp }
5091 036813ee 2019-05-09 stsp
5092 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5093 036813ee 2019-05-09 stsp
5094 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5095 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5096 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5097 5f8a88c6 2019-08-03 stsp else
5098 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5099 036813ee 2019-05-09 stsp done:
5100 102b254e 2020-10-19 stsp free(ct_name);
5101 036813ee 2019-05-09 stsp if (err && *new_te) {
5102 56e0773d 2019-11-28 stsp free(*new_te);
5103 036813ee 2019-05-09 stsp *new_te = NULL;
5104 036813ee 2019-05-09 stsp }
5105 036813ee 2019-05-09 stsp return err;
5106 036813ee 2019-05-09 stsp }
5107 036813ee 2019-05-09 stsp
5108 036813ee 2019-05-09 stsp static const struct got_error *
5109 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5110 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5111 036813ee 2019-05-09 stsp {
5112 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5113 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5114 036813ee 2019-05-09 stsp
5115 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5116 036813ee 2019-05-09 stsp if (err)
5117 036813ee 2019-05-09 stsp return err;
5118 036813ee 2019-05-09 stsp if (new_pe == NULL)
5119 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5120 036813ee 2019-05-09 stsp return NULL;
5121 afa376bf 2019-05-09 stsp }
5122 afa376bf 2019-05-09 stsp
5123 afa376bf 2019-05-09 stsp static const struct got_error *
5124 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5125 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5126 afa376bf 2019-05-09 stsp {
5127 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5128 5f8a88c6 2019-08-03 stsp unsigned char status;
5129 5f8a88c6 2019-08-03 stsp
5130 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5131 afa376bf 2019-05-09 stsp ct_path++;
5132 5f8a88c6 2019-08-03 stsp
5133 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5134 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5135 5f8a88c6 2019-08-03 stsp else
5136 5f8a88c6 2019-08-03 stsp status = ct->status;
5137 5f8a88c6 2019-08-03 stsp
5138 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5139 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5140 036813ee 2019-05-09 stsp }
5141 036813ee 2019-05-09 stsp
5142 036813ee 2019-05-09 stsp static const struct got_error *
5143 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5144 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5145 44d03001 2019-05-09 stsp {
5146 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5147 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5148 44d03001 2019-05-09 stsp char *te_path;
5149 44d03001 2019-05-09 stsp
5150 44d03001 2019-05-09 stsp *modified = 0;
5151 44d03001 2019-05-09 stsp
5152 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5153 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5154 44d03001 2019-05-09 stsp te->name) == -1)
5155 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5156 44d03001 2019-05-09 stsp
5157 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5158 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5159 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5160 44d03001 2019-05-09 stsp strlen(te_path));
5161 62d463ca 2020-10-20 naddy if (*modified)
5162 44d03001 2019-05-09 stsp break;
5163 44d03001 2019-05-09 stsp }
5164 44d03001 2019-05-09 stsp
5165 44d03001 2019-05-09 stsp free(te_path);
5166 44d03001 2019-05-09 stsp return err;
5167 44d03001 2019-05-09 stsp }
5168 44d03001 2019-05-09 stsp
5169 44d03001 2019-05-09 stsp static const struct got_error *
5170 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5171 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5172 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5173 036813ee 2019-05-09 stsp {
5174 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5175 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5176 036813ee 2019-05-09 stsp
5177 036813ee 2019-05-09 stsp *ctp = NULL;
5178 036813ee 2019-05-09 stsp
5179 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5180 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5181 036813ee 2019-05-09 stsp char *ct_name = NULL;
5182 036813ee 2019-05-09 stsp int path_matches;
5183 036813ee 2019-05-09 stsp
5184 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5185 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5186 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5187 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5188 5f8a88c6 2019-08-03 stsp continue;
5189 5f8a88c6 2019-08-03 stsp } else {
5190 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5191 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5192 5f8a88c6 2019-08-03 stsp continue;
5193 5f8a88c6 2019-08-03 stsp }
5194 036813ee 2019-05-09 stsp
5195 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5196 036813ee 2019-05-09 stsp continue;
5197 036813ee 2019-05-09 stsp
5198 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5199 62d463ca 2020-10-20 naddy if (err)
5200 036813ee 2019-05-09 stsp return err;
5201 036813ee 2019-05-09 stsp if (!path_matches)
5202 036813ee 2019-05-09 stsp continue;
5203 036813ee 2019-05-09 stsp
5204 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5205 d34b633e 2020-10-19 stsp if (err)
5206 d34b633e 2020-10-19 stsp return err;
5207 d34b633e 2020-10-19 stsp
5208 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5209 d34b633e 2020-10-19 stsp free(ct_name);
5210 036813ee 2019-05-09 stsp continue;
5211 d34b633e 2020-10-19 stsp }
5212 d34b633e 2020-10-19 stsp free(ct_name);
5213 036813ee 2019-05-09 stsp
5214 036813ee 2019-05-09 stsp *ctp = ct;
5215 036813ee 2019-05-09 stsp break;
5216 036813ee 2019-05-09 stsp }
5217 2b496619 2019-07-10 stsp
5218 2b496619 2019-07-10 stsp return err;
5219 2b496619 2019-07-10 stsp }
5220 2b496619 2019-07-10 stsp
5221 2b496619 2019-07-10 stsp static const struct got_error *
5222 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5223 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5224 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5225 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5226 2b496619 2019-07-10 stsp struct got_repository *repo)
5227 2b496619 2019-07-10 stsp {
5228 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5229 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5230 2b496619 2019-07-10 stsp char *subtree_path;
5231 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5232 ba580f68 2020-03-22 stsp int nentries;
5233 2b496619 2019-07-10 stsp
5234 2b496619 2019-07-10 stsp *new_tep = NULL;
5235 2b496619 2019-07-10 stsp
5236 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5237 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5238 2b496619 2019-07-10 stsp child_path) == -1)
5239 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5240 036813ee 2019-05-09 stsp
5241 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5242 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5243 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5244 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5245 56e0773d 2019-11-28 stsp
5246 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5247 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5248 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5249 2b496619 2019-07-10 stsp goto done;
5250 2b496619 2019-07-10 stsp }
5251 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5252 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5253 2b496619 2019-07-10 stsp if (err) {
5254 56e0773d 2019-11-28 stsp free(new_te);
5255 2b496619 2019-07-10 stsp goto done;
5256 2b496619 2019-07-10 stsp }
5257 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5258 2b496619 2019-07-10 stsp done:
5259 56e0773d 2019-11-28 stsp free(id);
5260 2b496619 2019-07-10 stsp free(subtree_path);
5261 2b496619 2019-07-10 stsp if (err == NULL)
5262 2b496619 2019-07-10 stsp *new_tep = new_te;
5263 036813ee 2019-05-09 stsp return err;
5264 036813ee 2019-05-09 stsp }
5265 036813ee 2019-05-09 stsp
5266 036813ee 2019-05-09 stsp static const struct got_error *
5267 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5268 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5269 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5270 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5271 036813ee 2019-05-09 stsp struct got_repository *repo)
5272 036813ee 2019-05-09 stsp {
5273 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5274 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5275 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5276 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5277 036813ee 2019-05-09 stsp
5278 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5279 ba580f68 2020-03-22 stsp *nentries = 0;
5280 036813ee 2019-05-09 stsp
5281 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5282 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5283 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5284 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5285 036813ee 2019-05-09 stsp
5286 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5287 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5288 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5289 036813ee 2019-05-09 stsp continue;
5290 036813ee 2019-05-09 stsp
5291 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5292 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5293 036813ee 2019-05-09 stsp continue;
5294 036813ee 2019-05-09 stsp
5295 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5296 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5297 036813ee 2019-05-09 stsp if (err)
5298 036813ee 2019-05-09 stsp goto done;
5299 036813ee 2019-05-09 stsp
5300 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5301 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5302 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5303 036813ee 2019-05-09 stsp if (err)
5304 036813ee 2019-05-09 stsp goto done;
5305 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5306 afa376bf 2019-05-09 stsp if (err)
5307 afa376bf 2019-05-09 stsp goto done;
5308 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5309 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5310 2b496619 2019-07-10 stsp if (err)
5311 2f51b5b3 2019-05-09 stsp goto done;
5312 ba580f68 2020-03-22 stsp (*nentries)++;
5313 2b496619 2019-07-10 stsp } else {
5314 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5315 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5316 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5317 2b496619 2019-07-10 stsp == NULL) {
5318 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5319 2b496619 2019-07-10 stsp child_path, path_base_tree,
5320 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5321 2b496619 2019-07-10 stsp repo);
5322 2b496619 2019-07-10 stsp if (err)
5323 2b496619 2019-07-10 stsp goto done;
5324 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5325 2b496619 2019-07-10 stsp if (err)
5326 2b496619 2019-07-10 stsp goto done;
5327 ba580f68 2020-03-22 stsp (*nentries)++;
5328 9ba0479c 2019-05-10 stsp }
5329 ed175427 2019-05-09 stsp }
5330 2f51b5b3 2019-05-09 stsp }
5331 2f51b5b3 2019-05-09 stsp
5332 2f51b5b3 2019-05-09 stsp if (base_tree) {
5333 56e0773d 2019-11-28 stsp int i, nbase_entries;
5334 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5335 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5336 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5337 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5338 63c5ca5d 2019-08-24 stsp
5339 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5340 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5341 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5342 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5343 63c5ca5d 2019-08-24 stsp if (err)
5344 63c5ca5d 2019-08-24 stsp goto done;
5345 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5346 63c5ca5d 2019-08-24 stsp if (err)
5347 63c5ca5d 2019-08-24 stsp goto done;
5348 ba580f68 2020-03-22 stsp (*nentries)++;
5349 63c5ca5d 2019-08-24 stsp continue;
5350 63c5ca5d 2019-08-24 stsp }
5351 2f51b5b3 2019-05-09 stsp
5352 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5353 44d03001 2019-05-09 stsp int modified;
5354 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5355 036813ee 2019-05-09 stsp if (err)
5356 036813ee 2019-05-09 stsp goto done;
5357 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5358 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5359 2f51b5b3 2019-05-09 stsp if (err)
5360 2f51b5b3 2019-05-09 stsp goto done;
5361 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5362 44d03001 2019-05-09 stsp if (modified) {
5363 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5364 ba580f68 2020-03-22 stsp int nsubentries;
5365 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5366 ba580f68 2020-03-22 stsp &nsubentries, te,
5367 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5368 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5369 44d03001 2019-05-09 stsp if (err)
5370 44d03001 2019-05-09 stsp goto done;
5371 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5372 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5373 ba580f68 2020-03-22 stsp free(new_id);
5374 ba580f68 2020-03-22 stsp continue;
5375 ba580f68 2020-03-22 stsp }
5376 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5377 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5378 56e0773d 2019-11-28 stsp free(new_id);
5379 44d03001 2019-05-09 stsp }
5380 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5381 036813ee 2019-05-09 stsp if (err)
5382 036813ee 2019-05-09 stsp goto done;
5383 ba580f68 2020-03-22 stsp (*nentries)++;
5384 2f51b5b3 2019-05-09 stsp continue;
5385 036813ee 2019-05-09 stsp }
5386 2f51b5b3 2019-05-09 stsp
5387 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5388 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5389 f66c734c 2019-09-22 stsp if (err)
5390 f66c734c 2019-09-22 stsp goto done;
5391 2f51b5b3 2019-05-09 stsp if (ct) {
5392 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5393 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5394 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5395 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5396 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5397 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5398 2f51b5b3 2019-05-09 stsp if (err)
5399 2f51b5b3 2019-05-09 stsp goto done;
5400 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5401 2f51b5b3 2019-05-09 stsp if (err)
5402 2f51b5b3 2019-05-09 stsp goto done;
5403 ba580f68 2020-03-22 stsp (*nentries)++;
5404 2f51b5b3 2019-05-09 stsp }
5405 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5406 b416585c 2019-05-13 stsp status_arg);
5407 afa376bf 2019-05-09 stsp if (err)
5408 afa376bf 2019-05-09 stsp goto done;
5409 2f51b5b3 2019-05-09 stsp } else {
5410 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5411 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5412 2f51b5b3 2019-05-09 stsp if (err)
5413 2f51b5b3 2019-05-09 stsp goto done;
5414 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5415 2f51b5b3 2019-05-09 stsp if (err)
5416 2f51b5b3 2019-05-09 stsp goto done;
5417 ba580f68 2020-03-22 stsp (*nentries)++;
5418 2f51b5b3 2019-05-09 stsp }
5419 ca2503ea 2019-05-09 stsp }
5420 ed175427 2019-05-09 stsp }
5421 0b5cc0d6 2019-05-09 stsp
5422 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5423 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5424 ed175427 2019-05-09 stsp done:
5425 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5426 2e1fa222 2020-07-23 stsp return err;
5427 2e1fa222 2020-07-23 stsp }
5428 2e1fa222 2020-07-23 stsp
5429 2e1fa222 2020-07-23 stsp static const struct got_error *
5430 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5431 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5432 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5433 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5434 ebf99748 2019-05-09 stsp {
5435 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5436 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5437 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5438 ebf99748 2019-05-09 stsp
5439 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5440 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5441 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5442 ebf99748 2019-05-09 stsp
5443 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5444 437adc9d 2020-12-10 yzhong
5445 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5446 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5447 437adc9d 2020-12-10 yzhong if (err)
5448 437adc9d 2020-12-10 yzhong goto done;
5449 437adc9d 2020-12-10 yzhong
5450 ebf99748 2019-05-09 stsp if (ie) {
5451 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5452 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5453 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5454 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5455 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5456 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5457 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5458 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5459 437adc9d 2020-12-10 yzhong
5460 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5461 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5462 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5463 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5464 72fd46fa 2019-09-06 stsp !have_staged_files);
5465 ebf99748 2019-05-09 stsp } else
5466 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5467 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5468 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5469 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5470 72fd46fa 2019-09-06 stsp !have_staged_files);
5471 ebf99748 2019-05-09 stsp } else {
5472 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5473 ebf99748 2019-05-09 stsp if (err)
5474 437adc9d 2020-12-10 yzhong goto done;
5475 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5476 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5477 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5478 3969253a 2020-03-07 stsp if (err) {
5479 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5480 437adc9d 2020-12-10 yzhong goto done;
5481 3969253a 2020-03-07 stsp }
5482 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5483 3969253a 2020-03-07 stsp if (err) {
5484 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5485 437adc9d 2020-12-10 yzhong goto done;
5486 3969253a 2020-03-07 stsp }
5487 ebf99748 2019-05-09 stsp }
5488 437adc9d 2020-12-10 yzhong free(relpath);
5489 437adc9d 2020-12-10 yzhong relpath = NULL;
5490 ebf99748 2019-05-09 stsp }
5491 437adc9d 2020-12-10 yzhong done:
5492 437adc9d 2020-12-10 yzhong free(relpath);
5493 d56d26ce 2019-05-10 stsp return err;
5494 d56d26ce 2019-05-10 stsp }
5495 735ef5ac 2019-08-03 stsp
5496 d56d26ce 2019-05-10 stsp
5497 d56d26ce 2019-05-10 stsp static const struct got_error *
5498 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5499 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5500 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5501 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5502 735ef5ac 2019-08-03 stsp int ood_errcode)
5503 d56d26ce 2019-05-10 stsp {
5504 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5505 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5506 1a36436d 2019-06-10 stsp
5507 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5508 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5509 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5510 1a36436d 2019-06-10 stsp return NULL;
5511 9bead371 2019-07-28 stsp /*
5512 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5513 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5514 9bead371 2019-07-28 stsp */
5515 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5516 735ef5ac 2019-08-03 stsp in_repo_path);
5517 9bead371 2019-07-28 stsp if (err) {
5518 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5519 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5520 1a36436d 2019-06-10 stsp goto done;
5521 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5522 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5523 1a36436d 2019-06-10 stsp } else {
5524 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5525 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5526 735ef5ac 2019-08-03 stsp in_repo_path);
5527 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5528 1a36436d 2019-06-10 stsp goto done;
5529 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5530 1a36436d 2019-06-10 stsp }
5531 1a36436d 2019-06-10 stsp done:
5532 1a36436d 2019-06-10 stsp free(id);
5533 1a36436d 2019-06-10 stsp return err;
5534 ebf99748 2019-05-09 stsp }
5535 ebf99748 2019-05-09 stsp
5536 c4296144 2019-05-09 stsp const struct got_error *
5537 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5538 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5539 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5540 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5541 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5542 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5543 afa376bf 2019-05-09 stsp struct got_repository *repo)
5544 c4296144 2019-05-09 stsp {
5545 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5546 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5547 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5548 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5549 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5550 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5551 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5552 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5553 ba580f68 2020-03-22 stsp int nentries;
5554 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5555 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5556 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5557 c4296144 2019-05-09 stsp
5558 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5559 c4296144 2019-05-09 stsp
5560 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5561 675c7539 2019-05-09 stsp
5562 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5563 588edf97 2019-05-10 stsp if (err)
5564 588edf97 2019-05-10 stsp goto done;
5565 de18fc63 2019-05-09 stsp
5566 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5567 588edf97 2019-05-10 stsp if (err)
5568 588edf97 2019-05-10 stsp goto done;
5569 588edf97 2019-05-10 stsp
5570 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5571 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5572 33ad4cbe 2019-05-12 jcs if (err)
5573 33ad4cbe 2019-05-12 jcs goto done;
5574 33ad4cbe 2019-05-12 jcs }
5575 c4296144 2019-05-09 stsp
5576 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5577 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5578 33ad4cbe 2019-05-12 jcs goto done;
5579 33ad4cbe 2019-05-12 jcs }
5580 33ad4cbe 2019-05-12 jcs
5581 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5582 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5583 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5584 cf066bf8 2019-05-09 stsp char *ondisk_path;
5585 cf066bf8 2019-05-09 stsp
5586 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5587 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5588 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5589 5f8a88c6 2019-08-03 stsp continue;
5590 5f8a88c6 2019-08-03 stsp
5591 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5592 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5593 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5594 cf066bf8 2019-05-09 stsp continue;
5595 cf066bf8 2019-05-09 stsp
5596 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5597 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5598 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5599 cf066bf8 2019-05-09 stsp goto done;
5600 cf066bf8 2019-05-09 stsp }
5601 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5602 2e1fa222 2020-07-23 stsp free(ondisk_path);
5603 2e1fa222 2020-07-23 stsp if (err)
5604 cf066bf8 2019-05-09 stsp goto done;
5605 cf066bf8 2019-05-09 stsp }
5606 036813ee 2019-05-09 stsp
5607 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5608 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5609 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5610 036813ee 2019-05-09 stsp if (err)
5611 036813ee 2019-05-09 stsp goto done;
5612 036813ee 2019-05-09 stsp
5613 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5614 de18fc63 2019-05-09 stsp if (err)
5615 de18fc63 2019-05-09 stsp goto done;
5616 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5617 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5618 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5619 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5620 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5621 33ad4cbe 2019-05-12 jcs free(logmsg);
5622 9d40349a 2019-05-09 stsp if (err)
5623 9d40349a 2019-05-09 stsp goto done;
5624 9d40349a 2019-05-09 stsp
5625 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5626 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5627 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5628 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5629 09f5bd90 2019-05-09 stsp goto done;
5630 09f5bd90 2019-05-09 stsp }
5631 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5632 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5633 09f5bd90 2019-05-09 stsp if (err)
5634 09f5bd90 2019-05-09 stsp goto done;
5635 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5636 ebf99748 2019-05-09 stsp if (err)
5637 ebf99748 2019-05-09 stsp goto done;
5638 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5639 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5640 09f5bd90 2019-05-09 stsp goto done;
5641 09f5bd90 2019-05-09 stsp }
5642 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5643 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5644 f2c16586 2019-05-09 stsp if (err)
5645 f2c16586 2019-05-09 stsp goto done;
5646 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5647 f2c16586 2019-05-09 stsp if (err)
5648 f2c16586 2019-05-09 stsp goto done;
5649 f2c16586 2019-05-09 stsp
5650 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5651 09f5bd90 2019-05-09 stsp if (err)
5652 09f5bd90 2019-05-09 stsp goto done;
5653 09f5bd90 2019-05-09 stsp
5654 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5655 ebf99748 2019-05-09 stsp if (err)
5656 ebf99748 2019-05-09 stsp goto done;
5657 c4296144 2019-05-09 stsp done:
5658 588edf97 2019-05-10 stsp if (head_tree)
5659 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5660 588edf97 2019-05-10 stsp if (head_commit)
5661 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5662 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5663 2f17228e 2019-05-12 stsp if (head_ref2) {
5664 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5665 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5666 2f17228e 2019-05-12 stsp err = unlockerr;
5667 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5668 39cd0ff6 2019-07-12 stsp }
5669 39cd0ff6 2019-07-12 stsp return err;
5670 39cd0ff6 2019-07-12 stsp }
5671 5c1e53bc 2019-07-28 stsp
5672 5c1e53bc 2019-07-28 stsp static const struct got_error *
5673 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5674 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5675 5c1e53bc 2019-07-28 stsp {
5676 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5677 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5678 39cd0ff6 2019-07-12 stsp
5679 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5680 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5681 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5682 5c1e53bc 2019-07-28 stsp
5683 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5684 5c1e53bc 2019-07-28 stsp ct_path++;
5685 5c1e53bc 2019-07-28 stsp
5686 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5687 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5688 5c1e53bc 2019-07-28 stsp break;
5689 5c1e53bc 2019-07-28 stsp }
5690 5c1e53bc 2019-07-28 stsp
5691 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5692 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5693 5c1e53bc 2019-07-28 stsp
5694 5c1e53bc 2019-07-28 stsp return NULL;
5695 5c1e53bc 2019-07-28 stsp }
5696 5c1e53bc 2019-07-28 stsp
5697 f0b75401 2019-08-03 stsp static const struct got_error *
5698 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5699 f0b75401 2019-08-03 stsp {
5700 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5701 f0b75401 2019-08-03 stsp
5702 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5703 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5704 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5705 f0b75401 2019-08-03 stsp }
5706 f0b75401 2019-08-03 stsp
5707 f0b75401 2019-08-03 stsp return NULL;
5708 f0b75401 2019-08-03 stsp }
5709 f0b75401 2019-08-03 stsp
5710 5f8a88c6 2019-08-03 stsp static const struct got_error *
5711 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5712 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5713 5f8a88c6 2019-08-03 stsp {
5714 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5715 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5716 5f8a88c6 2019-08-03 stsp
5717 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5718 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5719 5f8a88c6 2019-08-03 stsp continue;
5720 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5721 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5722 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5723 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5724 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5725 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5726 5f8a88c6 2019-08-03 stsp }
5727 5f8a88c6 2019-08-03 stsp
5728 5f8a88c6 2019-08-03 stsp return NULL;
5729 5f8a88c6 2019-08-03 stsp }
5730 5f8a88c6 2019-08-03 stsp
5731 39cd0ff6 2019-07-12 stsp const struct got_error *
5732 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5733 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5734 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5735 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5736 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5737 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5738 39cd0ff6 2019-07-12 stsp {
5739 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5740 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5741 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5742 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5743 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5744 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5745 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5746 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5747 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5748 39cd0ff6 2019-07-12 stsp
5749 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5750 39cd0ff6 2019-07-12 stsp
5751 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5752 39cd0ff6 2019-07-12 stsp
5753 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5754 39cd0ff6 2019-07-12 stsp if (err)
5755 39cd0ff6 2019-07-12 stsp goto done;
5756 39cd0ff6 2019-07-12 stsp
5757 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5758 39cd0ff6 2019-07-12 stsp if (err)
5759 39cd0ff6 2019-07-12 stsp goto done;
5760 39cd0ff6 2019-07-12 stsp
5761 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5762 39cd0ff6 2019-07-12 stsp if (err)
5763 39cd0ff6 2019-07-12 stsp goto done;
5764 39cd0ff6 2019-07-12 stsp
5765 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5766 39cd0ff6 2019-07-12 stsp if (err)
5767 39cd0ff6 2019-07-12 stsp goto done;
5768 39cd0ff6 2019-07-12 stsp
5769 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5770 5f8a88c6 2019-08-03 stsp &have_staged_files);
5771 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5772 5f8a88c6 2019-08-03 stsp goto done;
5773 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5774 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5775 5f8a88c6 2019-08-03 stsp if (err)
5776 5f8a88c6 2019-08-03 stsp goto done;
5777 5f8a88c6 2019-08-03 stsp }
5778 5f8a88c6 2019-08-03 stsp
5779 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5780 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5781 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5782 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5783 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5784 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5785 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5786 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5787 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5788 5c1e53bc 2019-07-28 stsp if (err)
5789 5c1e53bc 2019-07-28 stsp goto done;
5790 5c1e53bc 2019-07-28 stsp }
5791 39cd0ff6 2019-07-12 stsp
5792 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5793 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5794 39cd0ff6 2019-07-12 stsp goto done;
5795 5c1e53bc 2019-07-28 stsp }
5796 5c1e53bc 2019-07-28 stsp
5797 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5798 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5799 5c1e53bc 2019-07-28 stsp if (err)
5800 5c1e53bc 2019-07-28 stsp goto done;
5801 39cd0ff6 2019-07-12 stsp }
5802 f0b75401 2019-08-03 stsp
5803 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5804 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5805 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5806 f0b75401 2019-08-03 stsp
5807 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5808 f0b75401 2019-08-03 stsp ct_path++;
5809 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5810 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5811 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5812 b50cabdf 2019-07-12 stsp if (err)
5813 b50cabdf 2019-07-12 stsp goto done;
5814 f0b75401 2019-08-03 stsp
5815 b50cabdf 2019-07-12 stsp }
5816 b50cabdf 2019-07-12 stsp
5817 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5818 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5819 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5820 39cd0ff6 2019-07-12 stsp if (err)
5821 39cd0ff6 2019-07-12 stsp goto done;
5822 39cd0ff6 2019-07-12 stsp
5823 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5824 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5825 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5826 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5827 39cd0ff6 2019-07-12 stsp err = sync_err;
5828 39cd0ff6 2019-07-12 stsp done:
5829 39cd0ff6 2019-07-12 stsp if (fileindex)
5830 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5831 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5832 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5833 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5834 39cd0ff6 2019-07-12 stsp err = unlockerr;
5835 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5836 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5837 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5838 39cd0ff6 2019-07-12 stsp }
5839 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5840 c4296144 2019-05-09 stsp return err;
5841 8656d6c4 2019-05-20 stsp }
5842 8656d6c4 2019-05-20 stsp
5843 8656d6c4 2019-05-20 stsp const char *
5844 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5845 8656d6c4 2019-05-20 stsp {
5846 8656d6c4 2019-05-20 stsp return ct->path;
5847 8656d6c4 2019-05-20 stsp }
5848 8656d6c4 2019-05-20 stsp
5849 8656d6c4 2019-05-20 stsp unsigned int
5850 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5851 8656d6c4 2019-05-20 stsp {
5852 8656d6c4 2019-05-20 stsp return ct->status;
5853 818c7501 2019-07-11 stsp }
5854 818c7501 2019-07-11 stsp
5855 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5856 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5857 818c7501 2019-07-11 stsp struct got_repository *repo;
5858 818c7501 2019-07-11 stsp };
5859 818c7501 2019-07-11 stsp
5860 818c7501 2019-07-11 stsp static const struct got_error *
5861 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5862 818c7501 2019-07-11 stsp {
5863 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5864 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5865 818c7501 2019-07-11 stsp unsigned char status;
5866 818c7501 2019-07-11 stsp struct stat sb;
5867 818c7501 2019-07-11 stsp char *ondisk_path;
5868 818c7501 2019-07-11 stsp
5869 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5870 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5871 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5872 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5873 818c7501 2019-07-11 stsp
5874 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5875 818c7501 2019-07-11 stsp == -1)
5876 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5877 818c7501 2019-07-11 stsp
5878 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5879 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5880 818c7501 2019-07-11 stsp free(ondisk_path);
5881 818c7501 2019-07-11 stsp if (err)
5882 818c7501 2019-07-11 stsp return err;
5883 818c7501 2019-07-11 stsp
5884 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5885 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5886 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5887 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5888 818c7501 2019-07-11 stsp
5889 818c7501 2019-07-11 stsp return NULL;
5890 818c7501 2019-07-11 stsp }
5891 818c7501 2019-07-11 stsp
5892 818c7501 2019-07-11 stsp const struct got_error *
5893 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5894 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5895 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5896 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5897 818c7501 2019-07-11 stsp {
5898 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5899 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5900 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5901 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5902 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5903 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5904 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5905 818c7501 2019-07-11 stsp
5906 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5907 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5908 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5909 818c7501 2019-07-11 stsp
5910 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5911 818c7501 2019-07-11 stsp if (err)
5912 818c7501 2019-07-11 stsp return err;
5913 818c7501 2019-07-11 stsp
5914 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5915 818c7501 2019-07-11 stsp if (err)
5916 818c7501 2019-07-11 stsp goto done;
5917 818c7501 2019-07-11 stsp
5918 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5919 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5920 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5921 818c7501 2019-07-11 stsp &ok_arg);
5922 818c7501 2019-07-11 stsp if (err)
5923 818c7501 2019-07-11 stsp goto done;
5924 818c7501 2019-07-11 stsp
5925 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5926 818c7501 2019-07-11 stsp if (err)
5927 818c7501 2019-07-11 stsp goto done;
5928 818c7501 2019-07-11 stsp
5929 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5930 818c7501 2019-07-11 stsp if (err)
5931 818c7501 2019-07-11 stsp goto done;
5932 818c7501 2019-07-11 stsp
5933 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5934 818c7501 2019-07-11 stsp if (err)
5935 818c7501 2019-07-11 stsp goto done;
5936 818c7501 2019-07-11 stsp
5937 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5938 818c7501 2019-07-11 stsp 0);
5939 e51d7b55 2020-01-04 stsp if (err)
5940 e51d7b55 2020-01-04 stsp goto done;
5941 e51d7b55 2020-01-04 stsp
5942 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5943 818c7501 2019-07-11 stsp if (err)
5944 818c7501 2019-07-11 stsp goto done;
5945 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5946 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5947 e51d7b55 2020-01-04 stsp goto done;
5948 e51d7b55 2020-01-04 stsp }
5949 818c7501 2019-07-11 stsp
5950 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5951 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5952 818c7501 2019-07-11 stsp if (err)
5953 818c7501 2019-07-11 stsp goto done;
5954 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5955 818c7501 2019-07-11 stsp if (err)
5956 818c7501 2019-07-11 stsp goto done;
5957 818c7501 2019-07-11 stsp
5958 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5959 818c7501 2019-07-11 stsp
5960 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5961 818c7501 2019-07-11 stsp if (err)
5962 818c7501 2019-07-11 stsp goto done;
5963 818c7501 2019-07-11 stsp
5964 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5965 818c7501 2019-07-11 stsp if (err)
5966 818c7501 2019-07-11 stsp goto done;
5967 818c7501 2019-07-11 stsp
5968 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5969 818c7501 2019-07-11 stsp worktree->base_commit_id);
5970 818c7501 2019-07-11 stsp if (err)
5971 818c7501 2019-07-11 stsp goto done;
5972 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5973 818c7501 2019-07-11 stsp if (err)
5974 818c7501 2019-07-11 stsp goto done;
5975 818c7501 2019-07-11 stsp
5976 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5977 818c7501 2019-07-11 stsp if (err)
5978 818c7501 2019-07-11 stsp goto done;
5979 818c7501 2019-07-11 stsp done:
5980 818c7501 2019-07-11 stsp free(fileindex_path);
5981 818c7501 2019-07-11 stsp free(tmp_branch_name);
5982 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5983 818c7501 2019-07-11 stsp free(branch_ref_name);
5984 818c7501 2019-07-11 stsp if (branch_ref)
5985 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5986 818c7501 2019-07-11 stsp if (wt_branch)
5987 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5988 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5989 818c7501 2019-07-11 stsp if (err) {
5990 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5991 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5992 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5993 818c7501 2019-07-11 stsp }
5994 818c7501 2019-07-11 stsp if (*tmp_branch) {
5995 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5996 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5997 818c7501 2019-07-11 stsp }
5998 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5999 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6000 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6001 3e3a69f1 2019-07-25 stsp }
6002 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6003 818c7501 2019-07-11 stsp }
6004 818c7501 2019-07-11 stsp return err;
6005 818c7501 2019-07-11 stsp }
6006 818c7501 2019-07-11 stsp
6007 818c7501 2019-07-11 stsp const struct got_error *
6008 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6009 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6010 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6011 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6012 818c7501 2019-07-11 stsp {
6013 818c7501 2019-07-11 stsp const struct got_error *err;
6014 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6015 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6016 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6017 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6018 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6019 818c7501 2019-07-11 stsp
6020 818c7501 2019-07-11 stsp *commit_id = NULL;
6021 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6022 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6023 3e3a69f1 2019-07-25 stsp *branch = NULL;
6024 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6025 3e3a69f1 2019-07-25 stsp
6026 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6027 3e3a69f1 2019-07-25 stsp if (err)
6028 3e3a69f1 2019-07-25 stsp return err;
6029 818c7501 2019-07-11 stsp
6030 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6031 3e3a69f1 2019-07-25 stsp if (err)
6032 f032f1f7 2019-08-04 stsp goto done;
6033 f032f1f7 2019-08-04 stsp
6034 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6035 f032f1f7 2019-08-04 stsp &have_staged_files);
6036 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6037 f032f1f7 2019-08-04 stsp goto done;
6038 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6039 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6040 3e3a69f1 2019-07-25 stsp goto done;
6041 f032f1f7 2019-08-04 stsp }
6042 3e3a69f1 2019-07-25 stsp
6043 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6044 818c7501 2019-07-11 stsp if (err)
6045 f032f1f7 2019-08-04 stsp goto done;
6046 818c7501 2019-07-11 stsp
6047 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6048 818c7501 2019-07-11 stsp if (err)
6049 818c7501 2019-07-11 stsp goto done;
6050 818c7501 2019-07-11 stsp
6051 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6052 818c7501 2019-07-11 stsp if (err)
6053 818c7501 2019-07-11 stsp goto done;
6054 818c7501 2019-07-11 stsp
6055 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6056 818c7501 2019-07-11 stsp if (err)
6057 818c7501 2019-07-11 stsp goto done;
6058 818c7501 2019-07-11 stsp
6059 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6060 818c7501 2019-07-11 stsp if (err)
6061 818c7501 2019-07-11 stsp goto done;
6062 818c7501 2019-07-11 stsp
6063 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6064 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6065 818c7501 2019-07-11 stsp if (err)
6066 818c7501 2019-07-11 stsp goto done;
6067 818c7501 2019-07-11 stsp
6068 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6069 818c7501 2019-07-11 stsp if (err)
6070 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6073 818c7501 2019-07-11 stsp if (err)
6074 818c7501 2019-07-11 stsp goto done;
6075 818c7501 2019-07-11 stsp
6076 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6077 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6078 818c7501 2019-07-11 stsp if (err)
6079 818c7501 2019-07-11 stsp goto done;
6080 818c7501 2019-07-11 stsp
6081 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6082 818c7501 2019-07-11 stsp if (err)
6083 818c7501 2019-07-11 stsp goto done;
6084 818c7501 2019-07-11 stsp done:
6085 818c7501 2019-07-11 stsp free(commit_ref_name);
6086 818c7501 2019-07-11 stsp free(branch_ref_name);
6087 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6088 818c7501 2019-07-11 stsp if (commit_ref)
6089 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6090 818c7501 2019-07-11 stsp if (branch_ref)
6091 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6092 818c7501 2019-07-11 stsp if (err) {
6093 818c7501 2019-07-11 stsp free(*commit_id);
6094 818c7501 2019-07-11 stsp *commit_id = NULL;
6095 818c7501 2019-07-11 stsp if (*tmp_branch) {
6096 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6097 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6098 818c7501 2019-07-11 stsp }
6099 818c7501 2019-07-11 stsp if (*new_base_branch) {
6100 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6101 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6102 818c7501 2019-07-11 stsp }
6103 818c7501 2019-07-11 stsp if (*branch) {
6104 818c7501 2019-07-11 stsp got_ref_close(*branch);
6105 818c7501 2019-07-11 stsp *branch = NULL;
6106 818c7501 2019-07-11 stsp }
6107 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6108 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6109 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6110 3e3a69f1 2019-07-25 stsp }
6111 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6112 818c7501 2019-07-11 stsp }
6113 818c7501 2019-07-11 stsp return err;
6114 c4296144 2019-05-09 stsp }
6115 818c7501 2019-07-11 stsp
6116 818c7501 2019-07-11 stsp const struct got_error *
6117 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6118 818c7501 2019-07-11 stsp {
6119 818c7501 2019-07-11 stsp const struct got_error *err;
6120 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6121 818c7501 2019-07-11 stsp
6122 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6123 818c7501 2019-07-11 stsp if (err)
6124 818c7501 2019-07-11 stsp return err;
6125 818c7501 2019-07-11 stsp
6126 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6127 818c7501 2019-07-11 stsp free(tmp_branch_name);
6128 818c7501 2019-07-11 stsp return NULL;
6129 818c7501 2019-07-11 stsp }
6130 818c7501 2019-07-11 stsp
6131 818c7501 2019-07-11 stsp static const struct got_error *
6132 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6133 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6134 818c7501 2019-07-11 stsp {
6135 0ebf8283 2019-07-24 stsp *logmsg = arg;
6136 818c7501 2019-07-11 stsp return NULL;
6137 818c7501 2019-07-11 stsp }
6138 818c7501 2019-07-11 stsp
6139 818c7501 2019-07-11 stsp static const struct got_error *
6140 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6141 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6142 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6143 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6144 818c7501 2019-07-11 stsp {
6145 818c7501 2019-07-11 stsp return NULL;
6146 01757395 2019-07-12 stsp }
6147 01757395 2019-07-12 stsp
6148 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6149 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6150 01757395 2019-07-12 stsp void *progress_arg;
6151 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6152 01757395 2019-07-12 stsp };
6153 01757395 2019-07-12 stsp
6154 01757395 2019-07-12 stsp static const struct got_error *
6155 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6156 01757395 2019-07-12 stsp {
6157 01757395 2019-07-12 stsp const struct got_error *err;
6158 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6159 01757395 2019-07-12 stsp char *p;
6160 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6161 01757395 2019-07-12 stsp
6162 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6163 01757395 2019-07-12 stsp if (err)
6164 01757395 2019-07-12 stsp return err;
6165 01757395 2019-07-12 stsp
6166 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6167 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6168 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6169 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6170 01757395 2019-07-12 stsp return NULL;
6171 01757395 2019-07-12 stsp
6172 01757395 2019-07-12 stsp p = strdup(path);
6173 01757395 2019-07-12 stsp if (p == NULL)
6174 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6175 01757395 2019-07-12 stsp
6176 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6177 01757395 2019-07-12 stsp if (err || new == NULL)
6178 01757395 2019-07-12 stsp free(p);
6179 01757395 2019-07-12 stsp return err;
6180 01757395 2019-07-12 stsp }
6181 01757395 2019-07-12 stsp
6182 01757395 2019-07-12 stsp void
6183 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6184 01757395 2019-07-12 stsp {
6185 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6186 01757395 2019-07-12 stsp
6187 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6188 01757395 2019-07-12 stsp free((char *)pe->path);
6189 01757395 2019-07-12 stsp
6190 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6191 818c7501 2019-07-11 stsp }
6192 818c7501 2019-07-11 stsp
6193 0ebf8283 2019-07-24 stsp static const struct got_error *
6194 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6195 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6196 818c7501 2019-07-11 stsp {
6197 818c7501 2019-07-11 stsp const struct got_error *err;
6198 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6199 818c7501 2019-07-11 stsp
6200 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6201 818c7501 2019-07-11 stsp if (err) {
6202 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6203 818c7501 2019-07-11 stsp goto done;
6204 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6205 818c7501 2019-07-11 stsp if (err)
6206 818c7501 2019-07-11 stsp goto done;
6207 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6208 818c7501 2019-07-11 stsp if (err)
6209 818c7501 2019-07-11 stsp goto done;
6210 de05890f 2020-03-05 stsp } else if (is_rebase) {
6211 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6212 818c7501 2019-07-11 stsp int cmp;
6213 818c7501 2019-07-11 stsp
6214 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6215 818c7501 2019-07-11 stsp if (err)
6216 818c7501 2019-07-11 stsp goto done;
6217 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6218 818c7501 2019-07-11 stsp free(stored_id);
6219 818c7501 2019-07-11 stsp if (cmp != 0) {
6220 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6221 818c7501 2019-07-11 stsp goto done;
6222 818c7501 2019-07-11 stsp }
6223 818c7501 2019-07-11 stsp }
6224 0ebf8283 2019-07-24 stsp done:
6225 0ebf8283 2019-07-24 stsp if (commit_ref)
6226 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6227 0ebf8283 2019-07-24 stsp return err;
6228 0ebf8283 2019-07-24 stsp }
6229 0ebf8283 2019-07-24 stsp
6230 0ebf8283 2019-07-24 stsp static const struct got_error *
6231 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6232 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6233 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6234 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6235 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6236 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6237 0ebf8283 2019-07-24 stsp {
6238 0ebf8283 2019-07-24 stsp const struct got_error *err;
6239 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6240 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6241 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6242 818c7501 2019-07-11 stsp
6243 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6244 0ebf8283 2019-07-24 stsp
6245 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6246 0ebf8283 2019-07-24 stsp if (err)
6247 0ebf8283 2019-07-24 stsp return err;
6248 0ebf8283 2019-07-24 stsp
6249 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6250 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6251 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6252 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6253 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6254 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6255 818c7501 2019-07-11 stsp if (commit_ref)
6256 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6257 818c7501 2019-07-11 stsp return err;
6258 818c7501 2019-07-11 stsp }
6259 818c7501 2019-07-11 stsp
6260 818c7501 2019-07-11 stsp const struct got_error *
6261 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6262 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6263 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6264 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6265 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6266 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6267 818c7501 2019-07-11 stsp {
6268 0ebf8283 2019-07-24 stsp const struct got_error *err;
6269 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6270 0ebf8283 2019-07-24 stsp
6271 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6272 0ebf8283 2019-07-24 stsp if (err)
6273 0ebf8283 2019-07-24 stsp return err;
6274 0ebf8283 2019-07-24 stsp
6275 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6276 0ebf8283 2019-07-24 stsp if (err)
6277 0ebf8283 2019-07-24 stsp goto done;
6278 0ebf8283 2019-07-24 stsp
6279 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6280 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6281 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6282 0ebf8283 2019-07-24 stsp done:
6283 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6284 0ebf8283 2019-07-24 stsp return err;
6285 0ebf8283 2019-07-24 stsp }
6286 0ebf8283 2019-07-24 stsp
6287 0ebf8283 2019-07-24 stsp const struct got_error *
6288 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6289 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6290 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6291 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6292 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6293 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6294 0ebf8283 2019-07-24 stsp {
6295 0ebf8283 2019-07-24 stsp const struct got_error *err;
6296 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6297 0ebf8283 2019-07-24 stsp
6298 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6299 0ebf8283 2019-07-24 stsp if (err)
6300 0ebf8283 2019-07-24 stsp return err;
6301 0ebf8283 2019-07-24 stsp
6302 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6303 0ebf8283 2019-07-24 stsp if (err)
6304 0ebf8283 2019-07-24 stsp goto done;
6305 0ebf8283 2019-07-24 stsp
6306 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6307 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6308 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6309 0ebf8283 2019-07-24 stsp done:
6310 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6311 0ebf8283 2019-07-24 stsp return err;
6312 0ebf8283 2019-07-24 stsp }
6313 0ebf8283 2019-07-24 stsp
6314 0ebf8283 2019-07-24 stsp static const struct got_error *
6315 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6316 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6317 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6318 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6319 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6320 0ebf8283 2019-07-24 stsp {
6321 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6322 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6323 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6324 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6325 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6326 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6327 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6328 818c7501 2019-07-11 stsp
6329 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6330 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6331 a0e95631 2019-07-12 stsp
6332 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6333 818c7501 2019-07-11 stsp
6334 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6335 a0e95631 2019-07-12 stsp if (err)
6336 3e3a69f1 2019-07-25 stsp return err;
6337 a0e95631 2019-07-12 stsp
6338 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6339 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6340 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6341 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6342 01757395 2019-07-12 stsp /*
6343 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6344 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6345 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6346 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6347 01757395 2019-07-12 stsp */
6348 01757395 2019-07-12 stsp if (merged_paths) {
6349 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6350 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6351 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6352 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6353 f2a9dc41 2019-12-13 tracey 0);
6354 01757395 2019-07-12 stsp if (err)
6355 01757395 2019-07-12 stsp goto done;
6356 01757395 2019-07-12 stsp }
6357 01757395 2019-07-12 stsp } else {
6358 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6359 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6360 01757395 2019-07-12 stsp if (err)
6361 01757395 2019-07-12 stsp goto done;
6362 01757395 2019-07-12 stsp }
6363 a0e95631 2019-07-12 stsp
6364 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6365 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6366 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6367 a0e95631 2019-07-12 stsp if (err)
6368 a0e95631 2019-07-12 stsp goto done;
6369 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6370 a0e95631 2019-07-12 stsp goto done;
6371 ff0d2220 2019-07-11 stsp }
6372 818c7501 2019-07-11 stsp
6373 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6374 edd02c5e 2019-07-11 stsp if (err)
6375 edd02c5e 2019-07-11 stsp goto done;
6376 edd02c5e 2019-07-11 stsp
6377 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6378 818c7501 2019-07-11 stsp if (err)
6379 818c7501 2019-07-11 stsp goto done;
6380 818c7501 2019-07-11 stsp
6381 5943eee2 2019-08-13 stsp if (new_logmsg) {
6382 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6383 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6384 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6385 5943eee2 2019-08-13 stsp goto done;
6386 5943eee2 2019-08-13 stsp }
6387 5943eee2 2019-08-13 stsp } else {
6388 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6389 5943eee2 2019-08-13 stsp if (err)
6390 5943eee2 2019-08-13 stsp goto done;
6391 5943eee2 2019-08-13 stsp }
6392 0ebf8283 2019-07-24 stsp
6393 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6394 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6395 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6396 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6397 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6398 a0e95631 2019-07-12 stsp if (err)
6399 a0e95631 2019-07-12 stsp goto done;
6400 a0e95631 2019-07-12 stsp
6401 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6402 a0e95631 2019-07-12 stsp if (err)
6403 a0e95631 2019-07-12 stsp goto done;
6404 a0e95631 2019-07-12 stsp
6405 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6406 a0e95631 2019-07-12 stsp if (err)
6407 a0e95631 2019-07-12 stsp goto done;
6408 a0e95631 2019-07-12 stsp
6409 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6410 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6411 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6412 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6413 a0e95631 2019-07-12 stsp err = sync_err;
6414 818c7501 2019-07-11 stsp done:
6415 a0e95631 2019-07-12 stsp free(fileindex_path);
6416 a0e95631 2019-07-12 stsp free(head_commit_id);
6417 a0e95631 2019-07-12 stsp if (head_ref)
6418 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6419 818c7501 2019-07-11 stsp if (err) {
6420 818c7501 2019-07-11 stsp free(*new_commit_id);
6421 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6422 818c7501 2019-07-11 stsp }
6423 818c7501 2019-07-11 stsp return err;
6424 818c7501 2019-07-11 stsp }
6425 818c7501 2019-07-11 stsp
6426 818c7501 2019-07-11 stsp const struct got_error *
6427 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6428 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6429 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6430 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6431 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6432 0ebf8283 2019-07-24 stsp {
6433 0ebf8283 2019-07-24 stsp const struct got_error *err;
6434 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6435 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6436 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6437 0ebf8283 2019-07-24 stsp
6438 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6439 0ebf8283 2019-07-24 stsp if (err)
6440 0ebf8283 2019-07-24 stsp return err;
6441 0ebf8283 2019-07-24 stsp
6442 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6443 0ebf8283 2019-07-24 stsp if (err)
6444 0ebf8283 2019-07-24 stsp goto done;
6445 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6446 0ebf8283 2019-07-24 stsp if (err)
6447 0ebf8283 2019-07-24 stsp goto done;
6448 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6449 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6450 0ebf8283 2019-07-24 stsp goto done;
6451 0ebf8283 2019-07-24 stsp }
6452 0ebf8283 2019-07-24 stsp
6453 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6454 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6455 0ebf8283 2019-07-24 stsp done:
6456 0ebf8283 2019-07-24 stsp if (commit_ref)
6457 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6458 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6459 0ebf8283 2019-07-24 stsp free(commit_id);
6460 0ebf8283 2019-07-24 stsp return err;
6461 0ebf8283 2019-07-24 stsp }
6462 0ebf8283 2019-07-24 stsp
6463 0ebf8283 2019-07-24 stsp const struct got_error *
6464 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6465 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6466 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6467 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6468 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6469 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6470 0ebf8283 2019-07-24 stsp {
6471 0ebf8283 2019-07-24 stsp const struct got_error *err;
6472 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6473 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6474 0ebf8283 2019-07-24 stsp
6475 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6476 0ebf8283 2019-07-24 stsp if (err)
6477 0ebf8283 2019-07-24 stsp return err;
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6480 0ebf8283 2019-07-24 stsp if (err)
6481 0ebf8283 2019-07-24 stsp goto done;
6482 0ebf8283 2019-07-24 stsp
6483 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6484 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6485 0ebf8283 2019-07-24 stsp done:
6486 0ebf8283 2019-07-24 stsp if (commit_ref)
6487 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6488 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6489 0ebf8283 2019-07-24 stsp return err;
6490 0ebf8283 2019-07-24 stsp }
6491 0ebf8283 2019-07-24 stsp
6492 0ebf8283 2019-07-24 stsp const struct got_error *
6493 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6494 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6495 818c7501 2019-07-11 stsp {
6496 3e3a69f1 2019-07-25 stsp if (fileindex)
6497 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6498 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6499 69844fba 2019-07-11 stsp }
6500 69844fba 2019-07-11 stsp
6501 69844fba 2019-07-11 stsp static const struct got_error *
6502 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6503 69844fba 2019-07-11 stsp {
6504 69844fba 2019-07-11 stsp const struct got_error *err;
6505 69844fba 2019-07-11 stsp struct got_reference *ref;
6506 69844fba 2019-07-11 stsp
6507 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6508 69844fba 2019-07-11 stsp if (err) {
6509 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6510 69844fba 2019-07-11 stsp return NULL;
6511 69844fba 2019-07-11 stsp return err;
6512 69844fba 2019-07-11 stsp }
6513 69844fba 2019-07-11 stsp
6514 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6515 69844fba 2019-07-11 stsp got_ref_close(ref);
6516 69844fba 2019-07-11 stsp return err;
6517 818c7501 2019-07-11 stsp }
6518 818c7501 2019-07-11 stsp
6519 69844fba 2019-07-11 stsp static const struct got_error *
6520 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6521 69844fba 2019-07-11 stsp {
6522 69844fba 2019-07-11 stsp const struct got_error *err;
6523 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6524 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6525 69844fba 2019-07-11 stsp
6526 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6527 69844fba 2019-07-11 stsp if (err)
6528 69844fba 2019-07-11 stsp goto done;
6529 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6530 69844fba 2019-07-11 stsp if (err)
6531 69844fba 2019-07-11 stsp goto done;
6532 69844fba 2019-07-11 stsp
6533 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6534 69844fba 2019-07-11 stsp if (err)
6535 69844fba 2019-07-11 stsp goto done;
6536 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6537 69844fba 2019-07-11 stsp if (err)
6538 69844fba 2019-07-11 stsp goto done;
6539 69844fba 2019-07-11 stsp
6540 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6541 69844fba 2019-07-11 stsp if (err)
6542 69844fba 2019-07-11 stsp goto done;
6543 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6544 69844fba 2019-07-11 stsp if (err)
6545 69844fba 2019-07-11 stsp goto done;
6546 69844fba 2019-07-11 stsp
6547 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6548 69844fba 2019-07-11 stsp if (err)
6549 69844fba 2019-07-11 stsp goto done;
6550 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6551 69844fba 2019-07-11 stsp if (err)
6552 69844fba 2019-07-11 stsp goto done;
6553 69844fba 2019-07-11 stsp
6554 69844fba 2019-07-11 stsp done:
6555 69844fba 2019-07-11 stsp free(tmp_branch_name);
6556 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6557 69844fba 2019-07-11 stsp free(branch_ref_name);
6558 69844fba 2019-07-11 stsp free(commit_ref_name);
6559 e600f124 2021-03-21 stsp return err;
6560 e600f124 2021-03-21 stsp }
6561 e600f124 2021-03-21 stsp
6562 e600f124 2021-03-21 stsp const struct got_error *
6563 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6564 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6565 e600f124 2021-03-21 stsp {
6566 e600f124 2021-03-21 stsp const struct got_error *err;
6567 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6568 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6569 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6570 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6571 e600f124 2021-03-21 stsp char *refname = NULL;
6572 e600f124 2021-03-21 stsp
6573 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6574 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6575 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6576 e600f124 2021-03-21 stsp branch_name += 11;
6577 e600f124 2021-03-21 stsp
6578 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6579 e600f124 2021-03-21 stsp if (err)
6580 e600f124 2021-03-21 stsp return err;
6581 e600f124 2021-03-21 stsp
6582 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6583 e600f124 2021-03-21 stsp new_id_str) == -1) {
6584 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6585 e600f124 2021-03-21 stsp goto done;
6586 e600f124 2021-03-21 stsp }
6587 e600f124 2021-03-21 stsp
6588 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6589 e600f124 2021-03-21 stsp if (err)
6590 e600f124 2021-03-21 stsp goto done;
6591 e600f124 2021-03-21 stsp
6592 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6593 e600f124 2021-03-21 stsp if (err)
6594 e600f124 2021-03-21 stsp goto done;
6595 e600f124 2021-03-21 stsp
6596 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6597 e600f124 2021-03-21 stsp done:
6598 e600f124 2021-03-21 stsp free(new_id_str);
6599 e600f124 2021-03-21 stsp free(refname);
6600 e600f124 2021-03-21 stsp free(old_commit_id);
6601 e600f124 2021-03-21 stsp if (ref)
6602 e600f124 2021-03-21 stsp got_ref_close(ref);
6603 69844fba 2019-07-11 stsp return err;
6604 69844fba 2019-07-11 stsp }
6605 69844fba 2019-07-11 stsp
6606 818c7501 2019-07-11 stsp const struct got_error *
6607 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6608 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6609 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6610 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6611 818c7501 2019-07-11 stsp {
6612 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6613 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6614 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6615 818c7501 2019-07-11 stsp
6616 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6617 818c7501 2019-07-11 stsp if (err)
6618 818c7501 2019-07-11 stsp return err;
6619 e600f124 2021-03-21 stsp
6620 e600f124 2021-03-21 stsp if (create_backup) {
6621 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6622 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6623 e600f124 2021-03-21 stsp if (err)
6624 e600f124 2021-03-21 stsp goto done;
6625 e600f124 2021-03-21 stsp }
6626 818c7501 2019-07-11 stsp
6627 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6628 818c7501 2019-07-11 stsp if (err)
6629 818c7501 2019-07-11 stsp goto done;
6630 818c7501 2019-07-11 stsp
6631 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6632 818c7501 2019-07-11 stsp if (err)
6633 818c7501 2019-07-11 stsp goto done;
6634 818c7501 2019-07-11 stsp
6635 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6636 818c7501 2019-07-11 stsp if (err)
6637 818c7501 2019-07-11 stsp goto done;
6638 818c7501 2019-07-11 stsp
6639 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6640 a615e0e7 2020-12-16 stsp if (err)
6641 a615e0e7 2020-12-16 stsp goto done;
6642 a615e0e7 2020-12-16 stsp
6643 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6644 a615e0e7 2020-12-16 stsp if (err)
6645 a615e0e7 2020-12-16 stsp goto done;
6646 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6647 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6648 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6649 a615e0e7 2020-12-16 stsp err = sync_err;
6650 818c7501 2019-07-11 stsp done:
6651 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6652 a615e0e7 2020-12-16 stsp free(fileindex_path);
6653 818c7501 2019-07-11 stsp free(new_head_commit_id);
6654 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6655 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6656 818c7501 2019-07-11 stsp err = unlockerr;
6657 818c7501 2019-07-11 stsp return err;
6658 818c7501 2019-07-11 stsp }
6659 818c7501 2019-07-11 stsp
6660 818c7501 2019-07-11 stsp const struct got_error *
6661 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6662 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6663 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6664 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6665 818c7501 2019-07-11 stsp {
6666 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6667 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6668 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6669 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6670 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6671 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6672 818c7501 2019-07-11 stsp
6673 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6674 818c7501 2019-07-11 stsp if (err)
6675 818c7501 2019-07-11 stsp return err;
6676 818c7501 2019-07-11 stsp
6677 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6678 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6679 818c7501 2019-07-11 stsp if (err)
6680 818c7501 2019-07-11 stsp goto done;
6681 818c7501 2019-07-11 stsp
6682 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6683 818c7501 2019-07-11 stsp if (err)
6684 818c7501 2019-07-11 stsp goto done;
6685 818c7501 2019-07-11 stsp
6686 818c7501 2019-07-11 stsp /*
6687 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6688 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6689 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6690 818c7501 2019-07-11 stsp */
6691 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6692 818c7501 2019-07-11 stsp if (err)
6693 818c7501 2019-07-11 stsp goto done;
6694 818c7501 2019-07-11 stsp
6695 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6696 818c7501 2019-07-11 stsp if (err)
6697 818c7501 2019-07-11 stsp goto done;
6698 818c7501 2019-07-11 stsp
6699 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6700 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6701 ca355955 2019-07-12 stsp if (err)
6702 ca355955 2019-07-12 stsp goto done;
6703 ca355955 2019-07-12 stsp
6704 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6705 ca355955 2019-07-12 stsp if (err)
6706 ca355955 2019-07-12 stsp goto done;
6707 ca355955 2019-07-12 stsp
6708 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6709 818c7501 2019-07-11 stsp if (err)
6710 818c7501 2019-07-11 stsp goto done;
6711 818c7501 2019-07-11 stsp
6712 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6713 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6714 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6715 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6716 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6717 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6718 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6719 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6720 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6721 55bd499d 2019-07-12 stsp if (err)
6722 1f1abb7e 2019-08-08 stsp goto sync;
6723 55bd499d 2019-07-12 stsp
6724 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6725 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6726 ca355955 2019-07-12 stsp sync:
6727 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6728 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6729 ca355955 2019-07-12 stsp err = sync_err;
6730 818c7501 2019-07-11 stsp done:
6731 818c7501 2019-07-11 stsp got_ref_close(resolved);
6732 a3a2faf2 2019-07-12 stsp free(tree_id);
6733 818c7501 2019-07-11 stsp free(commit_id);
6734 0ebf8283 2019-07-24 stsp if (fileindex)
6735 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6736 0ebf8283 2019-07-24 stsp free(fileindex_path);
6737 0ebf8283 2019-07-24 stsp
6738 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6739 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6740 0ebf8283 2019-07-24 stsp err = unlockerr;
6741 0ebf8283 2019-07-24 stsp return err;
6742 0ebf8283 2019-07-24 stsp }
6743 0ebf8283 2019-07-24 stsp
6744 0ebf8283 2019-07-24 stsp const struct got_error *
6745 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6746 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6747 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6748 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6749 0ebf8283 2019-07-24 stsp {
6750 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6751 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6752 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6753 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6754 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6755 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6756 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6757 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6760 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6761 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6762 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6763 0ebf8283 2019-07-24 stsp
6764 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6765 0ebf8283 2019-07-24 stsp if (err)
6766 0ebf8283 2019-07-24 stsp return err;
6767 0ebf8283 2019-07-24 stsp
6768 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6769 0ebf8283 2019-07-24 stsp if (err)
6770 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp
6772 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6773 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6774 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6775 0ebf8283 2019-07-24 stsp &ok_arg);
6776 0ebf8283 2019-07-24 stsp if (err)
6777 0ebf8283 2019-07-24 stsp goto done;
6778 0ebf8283 2019-07-24 stsp
6779 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6780 0ebf8283 2019-07-24 stsp if (err)
6781 0ebf8283 2019-07-24 stsp goto done;
6782 0ebf8283 2019-07-24 stsp
6783 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6784 0ebf8283 2019-07-24 stsp if (err)
6785 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp
6787 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6788 0ebf8283 2019-07-24 stsp worktree);
6789 0ebf8283 2019-07-24 stsp if (err)
6790 0ebf8283 2019-07-24 stsp goto done;
6791 0ebf8283 2019-07-24 stsp
6792 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6793 0ebf8283 2019-07-24 stsp 0);
6794 0ebf8283 2019-07-24 stsp if (err)
6795 0ebf8283 2019-07-24 stsp goto done;
6796 0ebf8283 2019-07-24 stsp
6797 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6798 0ebf8283 2019-07-24 stsp if (err)
6799 0ebf8283 2019-07-24 stsp goto done;
6800 0ebf8283 2019-07-24 stsp
6801 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6802 0ebf8283 2019-07-24 stsp if (err)
6803 0ebf8283 2019-07-24 stsp goto done;
6804 0ebf8283 2019-07-24 stsp
6805 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6806 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6807 0ebf8283 2019-07-24 stsp if (err)
6808 0ebf8283 2019-07-24 stsp goto done;
6809 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6810 0ebf8283 2019-07-24 stsp if (err)
6811 0ebf8283 2019-07-24 stsp goto done;
6812 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6813 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6814 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6815 0ebf8283 2019-07-24 stsp goto done;
6816 0ebf8283 2019-07-24 stsp }
6817 0ebf8283 2019-07-24 stsp
6818 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6819 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6820 0ebf8283 2019-07-24 stsp if (err)
6821 0ebf8283 2019-07-24 stsp goto done;
6822 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6823 0ebf8283 2019-07-24 stsp if (err)
6824 0ebf8283 2019-07-24 stsp goto done;
6825 0ebf8283 2019-07-24 stsp
6826 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6827 0ebf8283 2019-07-24 stsp if (err)
6828 0ebf8283 2019-07-24 stsp goto done;
6829 0ebf8283 2019-07-24 stsp done:
6830 0ebf8283 2019-07-24 stsp free(fileindex_path);
6831 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6832 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6833 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6834 0ebf8283 2019-07-24 stsp if (wt_branch)
6835 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6836 0ebf8283 2019-07-24 stsp if (err) {
6837 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6838 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6839 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6840 0ebf8283 2019-07-24 stsp }
6841 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6842 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6843 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6844 0ebf8283 2019-07-24 stsp }
6845 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6846 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6847 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6848 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6849 3e3a69f1 2019-07-25 stsp }
6850 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6851 0ebf8283 2019-07-24 stsp }
6852 0ebf8283 2019-07-24 stsp return err;
6853 0ebf8283 2019-07-24 stsp }
6854 0ebf8283 2019-07-24 stsp
6855 0ebf8283 2019-07-24 stsp const struct got_error *
6856 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6857 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6858 0ebf8283 2019-07-24 stsp {
6859 3e3a69f1 2019-07-25 stsp if (fileindex)
6860 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6861 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6862 0ebf8283 2019-07-24 stsp }
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp const struct got_error *
6865 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6866 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6867 0ebf8283 2019-07-24 stsp {
6868 0ebf8283 2019-07-24 stsp const struct got_error *err;
6869 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6870 0ebf8283 2019-07-24 stsp
6871 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6872 0ebf8283 2019-07-24 stsp if (err)
6873 0ebf8283 2019-07-24 stsp return err;
6874 0ebf8283 2019-07-24 stsp
6875 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6876 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6877 0ebf8283 2019-07-24 stsp return NULL;
6878 0ebf8283 2019-07-24 stsp }
6879 0ebf8283 2019-07-24 stsp
6880 0ebf8283 2019-07-24 stsp const struct got_error *
6881 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6882 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6883 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6884 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6885 0ebf8283 2019-07-24 stsp {
6886 0ebf8283 2019-07-24 stsp const struct got_error *err;
6887 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6888 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6889 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6890 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6891 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6892 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6893 0ebf8283 2019-07-24 stsp
6894 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6895 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6896 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6897 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6898 0ebf8283 2019-07-24 stsp
6899 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6900 3e3a69f1 2019-07-25 stsp if (err)
6901 3e3a69f1 2019-07-25 stsp return err;
6902 3e3a69f1 2019-07-25 stsp
6903 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6904 3e3a69f1 2019-07-25 stsp if (err)
6905 f032f1f7 2019-08-04 stsp goto done;
6906 f032f1f7 2019-08-04 stsp
6907 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6908 f032f1f7 2019-08-04 stsp &have_staged_files);
6909 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6910 f032f1f7 2019-08-04 stsp goto done;
6911 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6912 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6913 3e3a69f1 2019-07-25 stsp goto done;
6914 f032f1f7 2019-08-04 stsp }
6915 3e3a69f1 2019-07-25 stsp
6916 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6917 0ebf8283 2019-07-24 stsp if (err)
6918 f032f1f7 2019-08-04 stsp goto done;
6919 0ebf8283 2019-07-24 stsp
6920 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6921 0ebf8283 2019-07-24 stsp if (err)
6922 0ebf8283 2019-07-24 stsp goto done;
6923 0ebf8283 2019-07-24 stsp
6924 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6925 0ebf8283 2019-07-24 stsp if (err)
6926 0ebf8283 2019-07-24 stsp goto done;
6927 0ebf8283 2019-07-24 stsp
6928 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6929 0ebf8283 2019-07-24 stsp worktree);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp
6933 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6934 0ebf8283 2019-07-24 stsp if (err)
6935 0ebf8283 2019-07-24 stsp goto done;
6936 0ebf8283 2019-07-24 stsp
6937 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6938 0ebf8283 2019-07-24 stsp if (err)
6939 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6941 0ebf8283 2019-07-24 stsp if (err)
6942 0ebf8283 2019-07-24 stsp goto done;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6945 0ebf8283 2019-07-24 stsp if (err)
6946 0ebf8283 2019-07-24 stsp goto done;
6947 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6948 0ebf8283 2019-07-24 stsp if (err)
6949 0ebf8283 2019-07-24 stsp goto done;
6950 0ebf8283 2019-07-24 stsp
6951 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6952 0ebf8283 2019-07-24 stsp if (err)
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp done:
6955 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6956 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6957 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6958 0ebf8283 2019-07-24 stsp if (commit_ref)
6959 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6960 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6961 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6962 0ebf8283 2019-07-24 stsp if (err) {
6963 0ebf8283 2019-07-24 stsp free(*commit_id);
6964 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6965 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6966 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6967 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6968 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6969 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6970 0ebf8283 2019-07-24 stsp }
6971 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6972 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6973 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6974 3e3a69f1 2019-07-25 stsp }
6975 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6976 0ebf8283 2019-07-24 stsp }
6977 0ebf8283 2019-07-24 stsp return err;
6978 0ebf8283 2019-07-24 stsp }
6979 0ebf8283 2019-07-24 stsp
6980 0ebf8283 2019-07-24 stsp static const struct got_error *
6981 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6982 0ebf8283 2019-07-24 stsp {
6983 0ebf8283 2019-07-24 stsp const struct got_error *err;
6984 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6985 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6986 0ebf8283 2019-07-24 stsp
6987 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6988 0ebf8283 2019-07-24 stsp if (err)
6989 0ebf8283 2019-07-24 stsp goto done;
6990 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6991 0ebf8283 2019-07-24 stsp if (err)
6992 0ebf8283 2019-07-24 stsp goto done;
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6995 0ebf8283 2019-07-24 stsp worktree);
6996 0ebf8283 2019-07-24 stsp if (err)
6997 0ebf8283 2019-07-24 stsp goto done;
6998 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6999 0ebf8283 2019-07-24 stsp if (err)
7000 0ebf8283 2019-07-24 stsp goto done;
7001 0ebf8283 2019-07-24 stsp
7002 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7003 0ebf8283 2019-07-24 stsp if (err)
7004 0ebf8283 2019-07-24 stsp goto done;
7005 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7006 0ebf8283 2019-07-24 stsp if (err)
7007 0ebf8283 2019-07-24 stsp goto done;
7008 0ebf8283 2019-07-24 stsp
7009 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7010 0ebf8283 2019-07-24 stsp if (err)
7011 0ebf8283 2019-07-24 stsp goto done;
7012 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7013 0ebf8283 2019-07-24 stsp if (err)
7014 0ebf8283 2019-07-24 stsp goto done;
7015 0ebf8283 2019-07-24 stsp done:
7016 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7017 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7018 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7019 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7020 0ebf8283 2019-07-24 stsp return err;
7021 0ebf8283 2019-07-24 stsp }
7022 0ebf8283 2019-07-24 stsp
7023 0ebf8283 2019-07-24 stsp const struct got_error *
7024 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7025 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7026 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7027 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7028 0ebf8283 2019-07-24 stsp {
7029 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7030 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7031 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7032 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7033 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7036 0ebf8283 2019-07-24 stsp if (err)
7037 0ebf8283 2019-07-24 stsp return err;
7038 0ebf8283 2019-07-24 stsp
7039 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7040 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7041 0ebf8283 2019-07-24 stsp if (err)
7042 0ebf8283 2019-07-24 stsp goto done;
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7045 0ebf8283 2019-07-24 stsp if (err)
7046 0ebf8283 2019-07-24 stsp goto done;
7047 0ebf8283 2019-07-24 stsp
7048 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7049 0ebf8283 2019-07-24 stsp if (err)
7050 0ebf8283 2019-07-24 stsp goto done;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7053 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7054 0ebf8283 2019-07-24 stsp if (err)
7055 0ebf8283 2019-07-24 stsp goto done;
7056 0ebf8283 2019-07-24 stsp
7057 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7058 0ebf8283 2019-07-24 stsp if (err)
7059 0ebf8283 2019-07-24 stsp goto done;
7060 0ebf8283 2019-07-24 stsp
7061 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7062 0ebf8283 2019-07-24 stsp if (err)
7063 0ebf8283 2019-07-24 stsp goto done;
7064 0ebf8283 2019-07-24 stsp
7065 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7066 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7067 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7068 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7069 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7070 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7071 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7072 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7073 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7074 0ebf8283 2019-07-24 stsp if (err)
7075 1f1abb7e 2019-08-08 stsp goto sync;
7076 0ebf8283 2019-07-24 stsp
7077 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7078 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7079 0ebf8283 2019-07-24 stsp sync:
7080 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7081 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7082 0ebf8283 2019-07-24 stsp err = sync_err;
7083 0ebf8283 2019-07-24 stsp done:
7084 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7085 0ebf8283 2019-07-24 stsp free(tree_id);
7086 818c7501 2019-07-11 stsp free(fileindex_path);
7087 818c7501 2019-07-11 stsp
7088 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7089 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7090 818c7501 2019-07-11 stsp err = unlockerr;
7091 818c7501 2019-07-11 stsp return err;
7092 818c7501 2019-07-11 stsp }
7093 0ebf8283 2019-07-24 stsp
7094 0ebf8283 2019-07-24 stsp const struct got_error *
7095 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7096 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7097 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7098 0ebf8283 2019-07-24 stsp {
7099 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7100 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7101 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7102 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7103 0ebf8283 2019-07-24 stsp
7104 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7105 0ebf8283 2019-07-24 stsp if (err)
7106 0ebf8283 2019-07-24 stsp return err;
7107 0ebf8283 2019-07-24 stsp
7108 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7109 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7110 e600f124 2021-03-21 stsp if (err)
7111 e600f124 2021-03-21 stsp goto done;
7112 e600f124 2021-03-21 stsp
7113 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7114 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7115 0ebf8283 2019-07-24 stsp if (err)
7116 0ebf8283 2019-07-24 stsp goto done;
7117 0ebf8283 2019-07-24 stsp
7118 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7119 0ebf8283 2019-07-24 stsp if (err)
7120 0ebf8283 2019-07-24 stsp goto done;
7121 0ebf8283 2019-07-24 stsp
7122 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7123 0ebf8283 2019-07-24 stsp if (err)
7124 0ebf8283 2019-07-24 stsp goto done;
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7127 0ebf8283 2019-07-24 stsp if (err)
7128 0ebf8283 2019-07-24 stsp goto done;
7129 0ebf8283 2019-07-24 stsp
7130 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7131 a615e0e7 2020-12-16 stsp if (err)
7132 a615e0e7 2020-12-16 stsp goto done;
7133 a615e0e7 2020-12-16 stsp
7134 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7135 a615e0e7 2020-12-16 stsp if (err)
7136 a615e0e7 2020-12-16 stsp goto done;
7137 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7138 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7139 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7140 a615e0e7 2020-12-16 stsp err = sync_err;
7141 0ebf8283 2019-07-24 stsp done:
7142 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7143 a615e0e7 2020-12-16 stsp free(fileindex_path);
7144 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7145 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7146 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7147 0ebf8283 2019-07-24 stsp err = unlockerr;
7148 0ebf8283 2019-07-24 stsp return err;
7149 0ebf8283 2019-07-24 stsp }
7150 0ebf8283 2019-07-24 stsp
7151 0ebf8283 2019-07-24 stsp const struct got_error *
7152 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7153 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7154 0ebf8283 2019-07-24 stsp {
7155 0ebf8283 2019-07-24 stsp const struct got_error *err;
7156 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7157 0ebf8283 2019-07-24 stsp
7158 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7159 0ebf8283 2019-07-24 stsp if (err)
7160 0ebf8283 2019-07-24 stsp return err;
7161 0ebf8283 2019-07-24 stsp
7162 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7163 0ebf8283 2019-07-24 stsp if (err)
7164 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7167 0ebf8283 2019-07-24 stsp done:
7168 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7169 2822a352 2019-10-15 stsp return err;
7170 2822a352 2019-10-15 stsp }
7171 2822a352 2019-10-15 stsp
7172 2822a352 2019-10-15 stsp const struct got_error *
7173 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7174 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7175 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7176 2822a352 2019-10-15 stsp struct got_repository *repo)
7177 2822a352 2019-10-15 stsp {
7178 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7179 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7180 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7181 2822a352 2019-10-15 stsp
7182 2822a352 2019-10-15 stsp *fileindex = NULL;
7183 2822a352 2019-10-15 stsp *branch_ref = NULL;
7184 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7185 2822a352 2019-10-15 stsp
7186 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7187 2822a352 2019-10-15 stsp if (err)
7188 2822a352 2019-10-15 stsp return err;
7189 2822a352 2019-10-15 stsp
7190 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7191 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7192 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7193 2822a352 2019-10-15 stsp "update -b or different branch name required");
7194 2822a352 2019-10-15 stsp goto done;
7195 2822a352 2019-10-15 stsp }
7196 2822a352 2019-10-15 stsp
7197 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7198 2822a352 2019-10-15 stsp if (err)
7199 2822a352 2019-10-15 stsp goto done;
7200 2822a352 2019-10-15 stsp
7201 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7202 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7203 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7204 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7205 2822a352 2019-10-15 stsp &ok_arg);
7206 2822a352 2019-10-15 stsp if (err)
7207 2822a352 2019-10-15 stsp goto done;
7208 2822a352 2019-10-15 stsp
7209 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7210 2822a352 2019-10-15 stsp if (err)
7211 2822a352 2019-10-15 stsp goto done;
7212 2822a352 2019-10-15 stsp
7213 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7214 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7215 2822a352 2019-10-15 stsp done:
7216 2822a352 2019-10-15 stsp if (err) {
7217 2822a352 2019-10-15 stsp if (*branch_ref) {
7218 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7219 2822a352 2019-10-15 stsp *branch_ref = NULL;
7220 2822a352 2019-10-15 stsp }
7221 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7222 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7223 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7224 2822a352 2019-10-15 stsp }
7225 2822a352 2019-10-15 stsp if (*fileindex) {
7226 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7227 2822a352 2019-10-15 stsp *fileindex = NULL;
7228 2822a352 2019-10-15 stsp }
7229 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7230 2822a352 2019-10-15 stsp }
7231 0cb83759 2019-08-03 stsp return err;
7232 0cb83759 2019-08-03 stsp }
7233 0cb83759 2019-08-03 stsp
7234 2822a352 2019-10-15 stsp const struct got_error *
7235 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7236 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7237 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7238 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7239 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7240 2822a352 2019-10-15 stsp {
7241 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7242 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7243 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7244 2822a352 2019-10-15 stsp
7245 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7246 2822a352 2019-10-15 stsp if (err)
7247 2822a352 2019-10-15 stsp goto done;
7248 2822a352 2019-10-15 stsp
7249 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7250 2822a352 2019-10-15 stsp if (err)
7251 2822a352 2019-10-15 stsp goto done;
7252 2822a352 2019-10-15 stsp
7253 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7254 2822a352 2019-10-15 stsp worktree->path_prefix);
7255 2822a352 2019-10-15 stsp if (err)
7256 2822a352 2019-10-15 stsp goto done;
7257 2822a352 2019-10-15 stsp
7258 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7259 2822a352 2019-10-15 stsp if (err)
7260 2822a352 2019-10-15 stsp goto done;
7261 2822a352 2019-10-15 stsp
7262 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7263 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7264 2822a352 2019-10-15 stsp if (err)
7265 2822a352 2019-10-15 stsp goto sync;
7266 2822a352 2019-10-15 stsp
7267 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7268 2822a352 2019-10-15 stsp if (err)
7269 2822a352 2019-10-15 stsp goto sync;
7270 2822a352 2019-10-15 stsp
7271 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7272 6e210706 2021-01-22 stsp if (err)
7273 6e210706 2021-01-22 stsp goto sync;
7274 6e210706 2021-01-22 stsp
7275 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7276 2822a352 2019-10-15 stsp sync:
7277 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7278 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7279 2822a352 2019-10-15 stsp err = sync_err;
7280 2822a352 2019-10-15 stsp
7281 2822a352 2019-10-15 stsp done:
7282 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7283 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7284 2822a352 2019-10-15 stsp err = unlockerr;
7285 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7286 2822a352 2019-10-15 stsp
7287 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7288 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7289 2822a352 2019-10-15 stsp err = unlockerr;
7290 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7291 2822a352 2019-10-15 stsp
7292 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7293 2822a352 2019-10-15 stsp free(fileindex_path);
7294 2822a352 2019-10-15 stsp free(tree_id);
7295 2822a352 2019-10-15 stsp
7296 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7297 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7298 2822a352 2019-10-15 stsp err = unlockerr;
7299 2822a352 2019-10-15 stsp return err;
7300 2822a352 2019-10-15 stsp }
7301 2822a352 2019-10-15 stsp
7302 2822a352 2019-10-15 stsp const struct got_error *
7303 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7304 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7305 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7306 2822a352 2019-10-15 stsp {
7307 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7308 8b692cd0 2019-10-21 stsp
7309 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7310 8b692cd0 2019-10-21 stsp
7311 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7312 8b692cd0 2019-10-21 stsp
7313 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7314 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7315 8b692cd0 2019-10-21 stsp err = unlockerr;
7316 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7317 8b692cd0 2019-10-21 stsp
7318 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7319 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7320 8b692cd0 2019-10-21 stsp err = unlockerr;
7321 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7322 8b692cd0 2019-10-21 stsp
7323 8b692cd0 2019-10-21 stsp return err;
7324 2822a352 2019-10-15 stsp }
7325 2822a352 2019-10-15 stsp
7326 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7327 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7328 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7329 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7330 2db2652d 2019-08-07 stsp struct got_repository *repo;
7331 2db2652d 2019-08-07 stsp int have_changes;
7332 2db2652d 2019-08-07 stsp };
7333 2db2652d 2019-08-07 stsp
7334 2db2652d 2019-08-07 stsp const struct got_error *
7335 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7336 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7337 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7338 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7339 735ef5ac 2019-08-03 stsp {
7340 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7341 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7342 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7343 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7344 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7345 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7346 8b13ce36 2019-08-08 stsp
7347 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7348 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7349 8b13ce36 2019-08-08 stsp return NULL;
7350 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7351 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7352 735ef5ac 2019-08-03 stsp
7353 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7354 735ef5ac 2019-08-03 stsp if (ie == NULL)
7355 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7356 735ef5ac 2019-08-03 stsp
7357 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7358 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7359 735ef5ac 2019-08-03 stsp relpath) == -1)
7360 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7361 735ef5ac 2019-08-03 stsp
7362 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7363 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7364 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7365 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7366 735ef5ac 2019-08-03 stsp }
7367 735ef5ac 2019-08-03 stsp
7368 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7369 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7370 3aa5969e 2019-08-06 stsp goto done;
7371 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7372 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7373 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7374 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7375 735ef5ac 2019-08-03 stsp goto done;
7376 3aa5969e 2019-08-06 stsp }
7377 735ef5ac 2019-08-03 stsp
7378 2db2652d 2019-08-07 stsp a->have_changes = 1;
7379 2db2652d 2019-08-07 stsp
7380 735ef5ac 2019-08-03 stsp p = in_repo_path;
7381 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7382 735ef5ac 2019-08-03 stsp p++;
7383 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7384 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7385 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7386 735ef5ac 2019-08-03 stsp done:
7387 735ef5ac 2019-08-03 stsp free(in_repo_path);
7388 dc424a06 2019-08-07 stsp return err;
7389 dc424a06 2019-08-07 stsp }
7390 dc424a06 2019-08-07 stsp
7391 2db2652d 2019-08-07 stsp struct stage_path_arg {
7392 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7393 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7394 2db2652d 2019-08-07 stsp struct got_repository *repo;
7395 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7396 2db2652d 2019-08-07 stsp void *status_arg;
7397 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7398 2db2652d 2019-08-07 stsp void *patch_arg;
7399 7b5dc508 2019-10-28 stsp int staged_something;
7400 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7401 2db2652d 2019-08-07 stsp };
7402 2db2652d 2019-08-07 stsp
7403 2db2652d 2019-08-07 stsp static const struct got_error *
7404 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7405 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7406 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7407 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7408 0cb83759 2019-08-03 stsp {
7409 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7410 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7411 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7412 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7413 0cb83759 2019-08-03 stsp uint32_t stage;
7414 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7415 0aeb8099 2020-07-23 stsp struct stat sb;
7416 8b13ce36 2019-08-08 stsp
7417 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7418 8b13ce36 2019-08-08 stsp return NULL;
7419 0cb83759 2019-08-03 stsp
7420 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7421 d3e7c587 2019-08-03 stsp if (ie == NULL)
7422 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7423 0cb83759 2019-08-03 stsp
7424 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7425 2db2652d 2019-08-07 stsp relpath)== -1)
7426 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7427 0cb83759 2019-08-03 stsp
7428 0cb83759 2019-08-03 stsp switch (status) {
7429 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7430 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7431 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7432 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7433 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7434 0aeb8099 2020-07-23 stsp break;
7435 0aeb8099 2020-07-23 stsp }
7436 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7437 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7438 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7439 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7440 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7441 dc424a06 2019-08-07 stsp if (err)
7442 dc424a06 2019-08-07 stsp break;
7443 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7444 dc424a06 2019-08-07 stsp break;
7445 dc424a06 2019-08-07 stsp } else {
7446 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7447 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7448 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7449 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7450 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7451 dc424a06 2019-08-07 stsp break;
7452 dc424a06 2019-08-07 stsp }
7453 dc424a06 2019-08-07 stsp }
7454 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7455 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7456 0cb83759 2019-08-03 stsp if (err)
7457 dc424a06 2019-08-07 stsp break;
7458 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7459 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7460 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7461 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7462 0cb83759 2019-08-03 stsp else
7463 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7464 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7465 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7466 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7467 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7468 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7469 35213c7c 2020-07-23 stsp ssize_t target_len;
7470 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7471 35213c7c 2020-07-23 stsp sizeof(target_path));
7472 35213c7c 2020-07-23 stsp if (target_len == -1) {
7473 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7474 35213c7c 2020-07-23 stsp ondisk_path);
7475 35213c7c 2020-07-23 stsp break;
7476 35213c7c 2020-07-23 stsp }
7477 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7478 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7479 35213c7c 2020-07-23 stsp a->worktree->root_path);
7480 35213c7c 2020-07-23 stsp if (err)
7481 35213c7c 2020-07-23 stsp break;
7482 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7483 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7484 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7485 35213c7c 2020-07-23 stsp break;
7486 35213c7c 2020-07-23 stsp }
7487 35213c7c 2020-07-23 stsp }
7488 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7489 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7490 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7491 35213c7c 2020-07-23 stsp else
7492 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7493 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7494 0aeb8099 2020-07-23 stsp } else {
7495 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7496 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7497 0aeb8099 2020-07-23 stsp }
7498 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7499 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7500 dc424a06 2019-08-07 stsp break;
7501 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7502 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7503 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7504 0cb83759 2019-08-03 stsp break;
7505 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7506 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7507 d3e7c587 2019-08-03 stsp break;
7508 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7509 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7510 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7511 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7512 dc424a06 2019-08-07 stsp if (err)
7513 dc424a06 2019-08-07 stsp break;
7514 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7515 88f33a19 2019-08-08 stsp break;
7516 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7517 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7518 dc424a06 2019-08-07 stsp break;
7519 88f33a19 2019-08-08 stsp }
7520 dc424a06 2019-08-07 stsp }
7521 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7522 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7523 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7524 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7525 dc424a06 2019-08-07 stsp break;
7526 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7527 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7528 12463d8b 2019-12-13 stsp de_name);
7529 0cb83759 2019-08-03 stsp break;
7530 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7531 d3e7c587 2019-08-03 stsp break;
7532 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7533 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7534 ebf48fd5 2019-08-03 stsp break;
7535 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7536 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7537 2a06fe5f 2019-08-24 stsp break;
7538 0cb83759 2019-08-03 stsp default:
7539 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7540 537ac44b 2019-08-03 stsp break;
7541 0cb83759 2019-08-03 stsp }
7542 dc424a06 2019-08-07 stsp
7543 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7544 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7545 dc424a06 2019-08-07 stsp free(path_content);
7546 2db2652d 2019-08-07 stsp free(ondisk_path);
7547 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7548 0ebf8283 2019-07-24 stsp return err;
7549 0ebf8283 2019-07-24 stsp }
7550 0cb83759 2019-08-03 stsp
7551 0cb83759 2019-08-03 stsp const struct got_error *
7552 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7553 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7554 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7555 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7556 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7557 0cb83759 2019-08-03 stsp {
7558 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7559 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7560 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7561 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7562 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7563 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7564 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7565 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7566 0cb83759 2019-08-03 stsp
7567 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7568 0cb83759 2019-08-03 stsp if (err)
7569 0cb83759 2019-08-03 stsp return err;
7570 0cb83759 2019-08-03 stsp
7571 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7572 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7573 735ef5ac 2019-08-03 stsp if (err)
7574 735ef5ac 2019-08-03 stsp goto done;
7575 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7576 735ef5ac 2019-08-03 stsp if (err)
7577 735ef5ac 2019-08-03 stsp goto done;
7578 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7579 0cb83759 2019-08-03 stsp if (err)
7580 0cb83759 2019-08-03 stsp goto done;
7581 0cb83759 2019-08-03 stsp
7582 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7583 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7584 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7585 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7586 2db2652d 2019-08-07 stsp oka.repo = repo;
7587 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7588 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7589 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7590 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7591 735ef5ac 2019-08-03 stsp if (err)
7592 735ef5ac 2019-08-03 stsp goto done;
7593 735ef5ac 2019-08-03 stsp }
7594 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7595 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7596 2db2652d 2019-08-07 stsp goto done;
7597 2db2652d 2019-08-07 stsp }
7598 735ef5ac 2019-08-03 stsp
7599 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7600 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7601 2db2652d 2019-08-07 stsp spa.repo = repo;
7602 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7603 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7604 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7605 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7606 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7607 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7608 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7609 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7610 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7611 42005733 2019-08-03 stsp if (err)
7612 2db2652d 2019-08-07 stsp goto done;
7613 7b5dc508 2019-10-28 stsp }
7614 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7615 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7616 7b5dc508 2019-10-28 stsp goto done;
7617 0cb83759 2019-08-03 stsp }
7618 0cb83759 2019-08-03 stsp
7619 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7620 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7621 0cb83759 2019-08-03 stsp err = sync_err;
7622 0cb83759 2019-08-03 stsp done:
7623 735ef5ac 2019-08-03 stsp if (head_ref)
7624 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7625 735ef5ac 2019-08-03 stsp free(head_commit_id);
7626 0cb83759 2019-08-03 stsp free(fileindex_path);
7627 0cb83759 2019-08-03 stsp if (fileindex)
7628 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7629 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7630 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7631 0cb83759 2019-08-03 stsp err = unlockerr;
7632 0cb83759 2019-08-03 stsp return err;
7633 0cb83759 2019-08-03 stsp }
7634 ad493afc 2019-08-03 stsp
7635 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7636 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7637 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7638 ad493afc 2019-08-03 stsp struct got_repository *repo;
7639 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7640 ad493afc 2019-08-03 stsp void *progress_arg;
7641 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7642 2e1f37b0 2019-08-08 stsp void *patch_arg;
7643 ad493afc 2019-08-03 stsp };
7644 2e1f37b0 2019-08-08 stsp
7645 2e1f37b0 2019-08-08 stsp static const struct got_error *
7646 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7647 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7648 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7649 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7650 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7651 2e1f37b0 2019-08-08 stsp {
7652 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7653 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7654 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7655 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7656 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7657 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7658 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7659 2e1f37b0 2019-08-08 stsp
7660 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7661 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7662 2e1f37b0 2019-08-08 stsp
7663 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7664 2e1f37b0 2019-08-08 stsp if (err)
7665 2e1f37b0 2019-08-08 stsp return err;
7666 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7667 2e1f37b0 2019-08-08 stsp if (err)
7668 2e1f37b0 2019-08-08 stsp goto done;
7669 2e1f37b0 2019-08-08 stsp
7670 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7671 2e1f37b0 2019-08-08 stsp if (err)
7672 2e1f37b0 2019-08-08 stsp goto done;
7673 2e1f37b0 2019-08-08 stsp
7674 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7675 2e1f37b0 2019-08-08 stsp if (err)
7676 2e1f37b0 2019-08-08 stsp goto done;
7677 2e1f37b0 2019-08-08 stsp
7678 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7679 2e1f37b0 2019-08-08 stsp if (err)
7680 2e1f37b0 2019-08-08 stsp goto done;
7681 2e1f37b0 2019-08-08 stsp
7682 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7683 2e1f37b0 2019-08-08 stsp if (err)
7684 2e1f37b0 2019-08-08 stsp goto done;
7685 ad493afc 2019-08-03 stsp
7686 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7687 2e1f37b0 2019-08-08 stsp if (err)
7688 2e1f37b0 2019-08-08 stsp goto done;
7689 2e1f37b0 2019-08-08 stsp
7690 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7691 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7692 2e1f37b0 2019-08-08 stsp if (err)
7693 2e1f37b0 2019-08-08 stsp goto done;
7694 2e1f37b0 2019-08-08 stsp
7695 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7696 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7697 2e1f37b0 2019-08-08 stsp if (err)
7698 2e1f37b0 2019-08-08 stsp goto done;
7699 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7700 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7701 2e1f37b0 2019-08-08 stsp if (err)
7702 2e1f37b0 2019-08-08 stsp goto done;
7703 2e1f37b0 2019-08-08 stsp
7704 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7705 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7706 2e1f37b0 2019-08-08 stsp goto done;
7707 2e1f37b0 2019-08-08 stsp }
7708 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7709 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7710 2e1f37b0 2019-08-08 stsp goto done;
7711 2e1f37b0 2019-08-08 stsp }
7712 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7713 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7714 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7715 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7716 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7717 fe621944 2020-11-10 stsp nchanges++;
7718 fe621944 2020-11-10 stsp }
7719 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7720 2e1f37b0 2019-08-08 stsp int choice;
7721 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7722 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7723 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7724 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7725 2e1f37b0 2019-08-08 stsp if (err)
7726 2e1f37b0 2019-08-08 stsp goto done;
7727 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7728 2e1f37b0 2019-08-08 stsp have_content = 1;
7729 19e4b907 2019-08-08 stsp else
7730 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7731 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7732 2e1f37b0 2019-08-08 stsp break;
7733 2e1f37b0 2019-08-08 stsp }
7734 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7735 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7736 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7737 2e1f37b0 2019-08-08 stsp done:
7738 2e1f37b0 2019-08-08 stsp free(label1);
7739 2e1f37b0 2019-08-08 stsp if (blob)
7740 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7741 2e1f37b0 2019-08-08 stsp if (staged_blob)
7742 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7743 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7744 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7745 fe621944 2020-11-10 stsp err = free_err;
7746 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7747 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7748 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7749 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7750 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7751 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7752 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7753 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7754 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7755 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7756 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7757 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7758 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7759 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7760 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7761 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7762 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7763 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7764 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7765 2e1f37b0 2019-08-08 stsp }
7766 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7767 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7768 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7769 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7770 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7771 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7772 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7773 2e1f37b0 2019-08-08 stsp }
7774 2e1f37b0 2019-08-08 stsp free(path1);
7775 2e1f37b0 2019-08-08 stsp free(path2);
7776 fda8017d 2020-07-23 stsp return err;
7777 fda8017d 2020-07-23 stsp }
7778 fda8017d 2020-07-23 stsp
7779 fda8017d 2020-07-23 stsp static const struct got_error *
7780 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7781 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7782 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7783 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7784 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7785 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7786 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7787 fda8017d 2020-07-23 stsp {
7788 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7789 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7790 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7791 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
7792 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
7793 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7794 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7795 fda8017d 2020-07-23 stsp struct stat sb;
7796 fda8017d 2020-07-23 stsp
7797 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7798 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7799 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7800 fda8017d 2020-07-23 stsp if (err)
7801 fda8017d 2020-07-23 stsp return err;
7802 fda8017d 2020-07-23 stsp
7803 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7804 fda8017d 2020-07-23 stsp return NULL;
7805 fda8017d 2020-07-23 stsp
7806 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7807 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7808 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7809 fda8017d 2020-07-23 stsp if (err)
7810 fda8017d 2020-07-23 stsp goto done;
7811 fda8017d 2020-07-23 stsp }
7812 fda8017d 2020-07-23 stsp
7813 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7814 fda8017d 2020-07-23 stsp if (f == NULL) {
7815 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7816 fda8017d 2020-07-23 stsp path_unstaged_content);
7817 fda8017d 2020-07-23 stsp goto done;
7818 fda8017d 2020-07-23 stsp }
7819 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7820 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7821 fda8017d 2020-07-23 stsp goto done;
7822 fda8017d 2020-07-23 stsp }
7823 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7824 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7825 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7826 fda8017d 2020-07-23 stsp size_t r;
7827 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7828 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7829 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7830 fda8017d 2020-07-23 stsp goto done;
7831 fda8017d 2020-07-23 stsp }
7832 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7833 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7834 fda8017d 2020-07-23 stsp goto done;
7835 fda8017d 2020-07-23 stsp }
7836 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7837 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7838 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7839 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7840 fda8017d 2020-07-23 stsp progress_arg);
7841 fda8017d 2020-07-23 stsp } else {
7842 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7843 67a66647 2021-05-31 stsp
7844 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
7845 67a66647 2021-05-31 stsp if (err)
7846 67a66647 2021-05-31 stsp return err;
7847 67a66647 2021-05-31 stsp
7848 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7849 67a66647 2021-05-31 stsp parent) == -1) {
7850 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
7851 67a66647 2021-05-31 stsp base_path = NULL;
7852 67a66647 2021-05-31 stsp goto done;
7853 67a66647 2021-05-31 stsp }
7854 67a66647 2021-05-31 stsp
7855 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7856 67a66647 2021-05-31 stsp if (err)
7857 67a66647 2021-05-31 stsp goto done;
7858 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7859 67a66647 2021-05-31 stsp blob_base);
7860 67a66647 2021-05-31 stsp if (err)
7861 67a66647 2021-05-31 stsp goto done;
7862 67a66647 2021-05-31 stsp
7863 eec2f5a9 2021-06-03 stsp /*
7864 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
7865 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
7866 eec2f5a9 2021-06-03 stsp */
7867 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7868 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
7869 eec2f5a9 2021-06-03 stsp ondisk_path);
7870 eec2f5a9 2021-06-03 stsp if (err)
7871 eec2f5a9 2021-06-03 stsp goto done;
7872 eec2f5a9 2021-06-03 stsp } else {
7873 eec2f5a9 2021-06-03 stsp int fd;
7874 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7875 eec2f5a9 2021-06-03 stsp if (fd == -1) {
7876 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
7877 eec2f5a9 2021-06-03 stsp goto done;
7878 eec2f5a9 2021-06-03 stsp }
7879 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
7880 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
7881 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
7882 eec2f5a9 2021-06-03 stsp close(fd);
7883 eec2f5a9 2021-06-03 stsp goto done;
7884 eec2f5a9 2021-06-03 stsp }
7885 eec2f5a9 2021-06-03 stsp }
7886 eec2f5a9 2021-06-03 stsp
7887 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7888 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
7889 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7890 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7891 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7892 fda8017d 2020-07-23 stsp }
7893 fda8017d 2020-07-23 stsp if (err)
7894 fda8017d 2020-07-23 stsp goto done;
7895 fda8017d 2020-07-23 stsp
7896 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7897 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7898 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7899 2ac8aa02 2020-11-09 stsp } else {
7900 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7901 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7902 2ac8aa02 2020-11-09 stsp }
7903 fda8017d 2020-07-23 stsp done:
7904 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7905 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7906 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7907 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7908 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7909 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7910 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7911 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7912 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
7913 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
7914 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7915 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7916 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7917 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7918 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
7919 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7920 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7921 67a66647 2021-05-31 stsp free(blob_base_path);
7922 67a66647 2021-05-31 stsp free(parent);
7923 67a66647 2021-05-31 stsp free(base_path);
7924 2e1f37b0 2019-08-08 stsp return err;
7925 2e1f37b0 2019-08-08 stsp }
7926 2e1f37b0 2019-08-08 stsp
7927 ad493afc 2019-08-03 stsp static const struct got_error *
7928 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7929 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7930 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7931 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7932 ad493afc 2019-08-03 stsp {
7933 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7934 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7935 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7936 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7937 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7938 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7939 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7940 9bc94a15 2019-08-03 stsp struct stat sb;
7941 ad493afc 2019-08-03 stsp
7942 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7943 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7944 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7945 2e1f37b0 2019-08-08 stsp return NULL;
7946 2e1f37b0 2019-08-08 stsp
7947 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7948 ad493afc 2019-08-03 stsp if (ie == NULL)
7949 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7950 9bc94a15 2019-08-03 stsp
7951 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7952 9bc94a15 2019-08-03 stsp == -1)
7953 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7954 ad493afc 2019-08-03 stsp
7955 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7956 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7957 f69721c3 2019-10-21 stsp if (err)
7958 f69721c3 2019-10-21 stsp goto done;
7959 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7960 f69721c3 2019-10-21 stsp id_str) == -1) {
7961 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7962 f69721c3 2019-10-21 stsp goto done;
7963 f69721c3 2019-10-21 stsp }
7964 f69721c3 2019-10-21 stsp
7965 ad493afc 2019-08-03 stsp switch (staged_status) {
7966 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7967 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7968 ad493afc 2019-08-03 stsp blob_id, 8192);
7969 ad493afc 2019-08-03 stsp if (err)
7970 ad493afc 2019-08-03 stsp break;
7971 ad493afc 2019-08-03 stsp /* fall through */
7972 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7973 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7974 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7975 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7976 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7977 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7978 2e1f37b0 2019-08-08 stsp if (err)
7979 2e1f37b0 2019-08-08 stsp break;
7980 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7981 2e1f37b0 2019-08-08 stsp break;
7982 2e1f37b0 2019-08-08 stsp } else {
7983 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7984 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7985 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7986 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7987 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7988 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7989 2e1f37b0 2019-08-08 stsp }
7990 2e1f37b0 2019-08-08 stsp }
7991 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7992 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7993 ad493afc 2019-08-03 stsp if (err)
7994 ad493afc 2019-08-03 stsp break;
7995 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7996 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7997 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7998 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7999 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8000 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8001 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8002 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8003 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8004 ea7786be 2020-07-23 stsp break;
8005 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8006 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8007 36bf999c 2020-07-23 stsp char *staged_target;
8008 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8009 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8010 36bf999c 2020-07-23 stsp if (err)
8011 36bf999c 2020-07-23 stsp goto done;
8012 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8013 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8014 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8015 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8016 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8017 36bf999c 2020-07-23 stsp free(staged_target);
8018 dfe9fba0 2020-07-23 stsp } else {
8019 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8020 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8021 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8022 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8023 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8024 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8025 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8026 dfe9fba0 2020-07-23 stsp }
8027 ea7786be 2020-07-23 stsp break;
8028 ea7786be 2020-07-23 stsp default:
8029 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8030 ea7786be 2020-07-23 stsp break;
8031 ea7786be 2020-07-23 stsp }
8032 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8033 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8034 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8035 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8036 2ac8aa02 2020-11-09 stsp }
8037 ad493afc 2019-08-03 stsp break;
8038 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8039 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8040 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8041 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8042 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8043 2e1f37b0 2019-08-08 stsp if (err)
8044 2e1f37b0 2019-08-08 stsp break;
8045 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8046 2e1f37b0 2019-08-08 stsp break;
8047 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8048 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8049 2e1f37b0 2019-08-08 stsp break;
8050 2e1f37b0 2019-08-08 stsp }
8051 2e1f37b0 2019-08-08 stsp }
8052 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8053 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8054 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8055 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8056 9bc94a15 2019-08-03 stsp if (err)
8057 9bc94a15 2019-08-03 stsp break;
8058 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8059 ad493afc 2019-08-03 stsp break;
8060 ad493afc 2019-08-03 stsp }
8061 f69721c3 2019-10-21 stsp done:
8062 ad493afc 2019-08-03 stsp free(ondisk_path);
8063 ad493afc 2019-08-03 stsp if (blob_base)
8064 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8065 ad493afc 2019-08-03 stsp if (blob_staged)
8066 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8067 f69721c3 2019-10-21 stsp free(id_str);
8068 f69721c3 2019-10-21 stsp free(label_orig);
8069 ad493afc 2019-08-03 stsp return err;
8070 ad493afc 2019-08-03 stsp }
8071 ad493afc 2019-08-03 stsp
8072 ad493afc 2019-08-03 stsp const struct got_error *
8073 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8074 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8075 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8076 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8077 ad493afc 2019-08-03 stsp struct got_repository *repo)
8078 ad493afc 2019-08-03 stsp {
8079 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8080 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8081 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8082 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8083 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8084 ad493afc 2019-08-03 stsp
8085 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8086 ad493afc 2019-08-03 stsp if (err)
8087 ad493afc 2019-08-03 stsp return err;
8088 ad493afc 2019-08-03 stsp
8089 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8090 ad493afc 2019-08-03 stsp if (err)
8091 ad493afc 2019-08-03 stsp goto done;
8092 ad493afc 2019-08-03 stsp
8093 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8094 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8095 ad493afc 2019-08-03 stsp upa.repo = repo;
8096 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8097 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8098 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8099 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8100 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8101 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8102 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8103 ad493afc 2019-08-03 stsp if (err)
8104 ad493afc 2019-08-03 stsp goto done;
8105 ad493afc 2019-08-03 stsp }
8106 ad493afc 2019-08-03 stsp
8107 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8108 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8109 ad493afc 2019-08-03 stsp err = sync_err;
8110 ad493afc 2019-08-03 stsp done:
8111 ad493afc 2019-08-03 stsp free(fileindex_path);
8112 ad493afc 2019-08-03 stsp if (fileindex)
8113 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8114 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8115 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8116 ad493afc 2019-08-03 stsp err = unlockerr;
8117 ad493afc 2019-08-03 stsp return err;
8118 ad493afc 2019-08-03 stsp }
8119 b2118c49 2020-07-28 stsp
8120 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8121 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8122 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8123 b2118c49 2020-07-28 stsp void *info_arg;
8124 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8125 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8126 b2118c49 2020-07-28 stsp void *cancel_arg;
8127 b2118c49 2020-07-28 stsp };
8128 b2118c49 2020-07-28 stsp
8129 b2118c49 2020-07-28 stsp static const struct got_error *
8130 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8131 b2118c49 2020-07-28 stsp {
8132 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8133 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8134 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8135 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8136 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8137 b2118c49 2020-07-28 stsp int stage;
8138 b2118c49 2020-07-28 stsp
8139 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8140 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8141 b2118c49 2020-07-28 stsp
8142 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8143 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8144 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8145 b2118c49 2020-07-28 stsp break;
8146 b2118c49 2020-07-28 stsp }
8147 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8148 b2118c49 2020-07-28 stsp return NULL;
8149 b2118c49 2020-07-28 stsp
8150 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8151 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8152 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8153 b2118c49 2020-07-28 stsp }
8154 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8155 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8156 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8157 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8158 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8159 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8160 b2118c49 2020-07-28 stsp }
8161 b2118c49 2020-07-28 stsp
8162 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8163 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8164 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8165 b2118c49 2020-07-28 stsp }
8166 b2118c49 2020-07-28 stsp
8167 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8168 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8169 b2118c49 2020-07-28 stsp }
8170 b2118c49 2020-07-28 stsp
8171 b2118c49 2020-07-28 stsp const struct got_error *
8172 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8173 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8174 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8175 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8176 b2118c49 2020-07-28 stsp
8177 b2118c49 2020-07-28 stsp {
8178 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8179 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8180 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8181 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8182 b2118c49 2020-07-28 stsp
8183 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8184 b2118c49 2020-07-28 stsp if (err)
8185 b2118c49 2020-07-28 stsp return err;
8186 b2118c49 2020-07-28 stsp
8187 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8188 b2118c49 2020-07-28 stsp if (err)
8189 b2118c49 2020-07-28 stsp goto done;
8190 b2118c49 2020-07-28 stsp
8191 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8192 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8193 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8194 b2118c49 2020-07-28 stsp arg.paths = paths;
8195 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8196 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8197 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8198 b2118c49 2020-07-28 stsp &arg);
8199 b2118c49 2020-07-28 stsp done:
8200 b2118c49 2020-07-28 stsp free(fileindex_path);
8201 b2118c49 2020-07-28 stsp if (fileindex)
8202 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8203 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8204 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8205 b2118c49 2020-07-28 stsp err = unlockerr;
8206 b2118c49 2020-07-28 stsp return err;
8207 b2118c49 2020-07-28 stsp }