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 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && 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 6d9d28c3 2018-03-11 stsp done:
433 eaccb85f 2018-12-25 stsp if (repo)
434 eaccb85f 2018-12-25 stsp got_repo_close(repo);
435 7ac97322 2018-03-11 stsp free(path_got);
436 056e7441 2018-03-11 stsp free(path_lock);
437 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
438 c442a90d 2019-03-10 stsp free(uuidstr);
439 bd165944 2019-03-10 stsp free(formatstr);
440 6d9d28c3 2018-03-11 stsp if (err) {
441 6d9d28c3 2018-03-11 stsp if (fd != -1)
442 6d9d28c3 2018-03-11 stsp close(fd);
443 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
444 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
445 6d9d28c3 2018-03-11 stsp *worktree = NULL;
446 6d9d28c3 2018-03-11 stsp } else
447 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
448 6d9d28c3 2018-03-11 stsp
449 6d9d28c3 2018-03-11 stsp return err;
450 86c3caaf 2018-03-09 stsp }
451 247140b2 2019-02-05 stsp
452 247140b2 2019-02-05 stsp const struct got_error *
453 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
454 247140b2 2019-02-05 stsp {
455 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
456 86c3caaf 2018-03-09 stsp
457 247140b2 2019-02-05 stsp do {
458 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
459 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
460 247140b2 2019-02-05 stsp return err;
461 247140b2 2019-02-05 stsp if (*worktree)
462 247140b2 2019-02-05 stsp return NULL;
463 247140b2 2019-02-05 stsp path = dirname(path);
464 d1542a27 2019-02-05 stsp if (path == NULL)
465 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
466 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
467 247140b2 2019-02-05 stsp
468 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
469 247140b2 2019-02-05 stsp }
470 247140b2 2019-02-05 stsp
471 3a6ce05a 2019-02-11 stsp const struct got_error *
472 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
473 86c3caaf 2018-03-09 stsp {
474 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
475 cde76477 2018-03-11 stsp free(worktree->repo_path);
476 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
477 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
478 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
479 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
480 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
481 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
482 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
483 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
484 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
485 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
486 6d9d28c3 2018-03-11 stsp free(worktree);
487 3a6ce05a 2019-02-11 stsp return err;
488 86c3caaf 2018-03-09 stsp }
489 86c3caaf 2018-03-09 stsp
490 2fbdb5ae 2018-12-29 stsp const char *
491 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
492 c7f4312f 2019-02-05 stsp {
493 c7f4312f 2019-02-05 stsp return worktree->root_path;
494 c7f4312f 2019-02-05 stsp }
495 c7f4312f 2019-02-05 stsp
496 c7f4312f 2019-02-05 stsp const char *
497 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
498 86c3caaf 2018-03-09 stsp {
499 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
500 49520a32 2018-12-29 stsp }
501 49520a32 2018-12-29 stsp const char *
502 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
503 49520a32 2018-12-29 stsp {
504 f609be2e 2018-12-29 stsp return worktree->path_prefix;
505 e5dc7198 2018-12-29 stsp }
506 e5dc7198 2018-12-29 stsp
507 e5dc7198 2018-12-29 stsp const struct got_error *
508 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 e5dc7198 2018-12-29 stsp const char *path_prefix)
510 e5dc7198 2018-12-29 stsp {
511 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
512 e5dc7198 2018-12-29 stsp
513 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
514 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
516 e5dc7198 2018-12-29 stsp }
517 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
518 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
519 e5dc7198 2018-12-29 stsp free(absprefix);
520 e5dc7198 2018-12-29 stsp return NULL;
521 86c3caaf 2018-03-09 stsp }
522 86c3caaf 2018-03-09 stsp
523 bc70eb79 2019-05-09 stsp const char *
524 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
525 86c3caaf 2018-03-09 stsp {
526 36a38700 2019-05-10 stsp return worktree->head_ref_name;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp const struct got_error *
530 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
531 024e9686 2019-05-14 stsp struct got_reference *head_ref)
532 024e9686 2019-05-14 stsp {
533 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
534 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
535 024e9686 2019-05-14 stsp
536 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
538 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
539 024e9686 2019-05-14 stsp path_got = NULL;
540 024e9686 2019-05-14 stsp goto done;
541 024e9686 2019-05-14 stsp }
542 024e9686 2019-05-14 stsp
543 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
544 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
545 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
546 024e9686 2019-05-14 stsp goto done;
547 024e9686 2019-05-14 stsp }
548 024e9686 2019-05-14 stsp
549 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
550 024e9686 2019-05-14 stsp if (err)
551 024e9686 2019-05-14 stsp goto done;
552 024e9686 2019-05-14 stsp
553 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
554 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
555 024e9686 2019-05-14 stsp done:
556 024e9686 2019-05-14 stsp free(path_got);
557 024e9686 2019-05-14 stsp if (err)
558 024e9686 2019-05-14 stsp free(head_ref_name);
559 024e9686 2019-05-14 stsp return err;
560 507dc3bb 2018-12-29 stsp }
561 507dc3bb 2018-12-29 stsp
562 b72f483a 2019-02-05 stsp struct got_object_id *
563 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
564 507dc3bb 2018-12-29 stsp {
565 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
566 86c3caaf 2018-03-09 stsp }
567 86c3caaf 2018-03-09 stsp
568 507dc3bb 2018-12-29 stsp const struct got_error *
569 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
571 507dc3bb 2018-12-29 stsp {
572 507dc3bb 2018-12-29 stsp const struct got_error *err;
573 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
574 507dc3bb 2018-12-29 stsp char *id_str = NULL;
575 507dc3bb 2018-12-29 stsp char *path_got = NULL;
576 507dc3bb 2018-12-29 stsp
577 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
580 507dc3bb 2018-12-29 stsp path_got = NULL;
581 507dc3bb 2018-12-29 stsp goto done;
582 507dc3bb 2018-12-29 stsp }
583 507dc3bb 2018-12-29 stsp
584 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
585 507dc3bb 2018-12-29 stsp if (err)
586 507dc3bb 2018-12-29 stsp return err;
587 507dc3bb 2018-12-29 stsp
588 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
590 507dc3bb 2018-12-29 stsp goto done;
591 507dc3bb 2018-12-29 stsp }
592 507dc3bb 2018-12-29 stsp
593 507dc3bb 2018-12-29 stsp /* Record our base commit. */
594 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
595 507dc3bb 2018-12-29 stsp if (err)
596 507dc3bb 2018-12-29 stsp goto done;
597 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
598 507dc3bb 2018-12-29 stsp if (err)
599 507dc3bb 2018-12-29 stsp goto done;
600 507dc3bb 2018-12-29 stsp
601 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
602 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
603 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
605 507dc3bb 2018-12-29 stsp goto done;
606 507dc3bb 2018-12-29 stsp }
607 507dc3bb 2018-12-29 stsp done:
608 507dc3bb 2018-12-29 stsp if (obj)
609 507dc3bb 2018-12-29 stsp got_object_close(obj);
610 507dc3bb 2018-12-29 stsp free(id_str);
611 507dc3bb 2018-12-29 stsp free(path_got);
612 507dc3bb 2018-12-29 stsp return err;
613 50b0790e 2020-09-11 stsp }
614 50b0790e 2020-09-11 stsp
615 50b0790e 2020-09-11 stsp const struct got_gotconfig *
616 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
617 50b0790e 2020-09-11 stsp {
618 50b0790e 2020-09-11 stsp return worktree->gotconfig;
619 507dc3bb 2018-12-29 stsp }
620 507dc3bb 2018-12-29 stsp
621 9d31a1d8 2018-03-11 stsp static const struct got_error *
622 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
623 86c3caaf 2018-03-09 stsp {
624 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
625 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
626 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
627 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
628 86c3caaf 2018-03-09 stsp return NULL;
629 21908da4 2019-01-13 stsp }
630 21908da4 2019-01-13 stsp
631 21908da4 2019-01-13 stsp static const struct got_error *
632 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
633 4a1ddfc2 2019-01-12 stsp {
634 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
635 4a1ddfc2 2019-01-12 stsp char *abspath;
636 4a1ddfc2 2019-01-12 stsp
637 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
638 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
639 4a1ddfc2 2019-01-12 stsp
640 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
641 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
642 ddcd8544 2019-03-11 stsp struct stat sb;
643 ddcd8544 2019-03-11 stsp err = NULL;
644 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
645 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
646 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
647 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
648 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
649 ddcd8544 2019-03-11 stsp }
650 ddcd8544 2019-03-11 stsp }
651 4a1ddfc2 2019-01-12 stsp free(abspath);
652 68c76935 2019-02-19 stsp return err;
653 68c76935 2019-02-19 stsp }
654 68c76935 2019-02-19 stsp
655 68c76935 2019-02-19 stsp static const struct got_error *
656 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
657 68c76935 2019-02-19 stsp {
658 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
659 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
660 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
661 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
662 68c76935 2019-02-19 stsp
663 68c76935 2019-02-19 stsp *same = 1;
664 68c76935 2019-02-19 stsp
665 230a42bd 2019-05-11 jcs for (;;) {
666 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
667 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
668 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
669 68c76935 2019-02-19 stsp break;
670 68c76935 2019-02-19 stsp }
671 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
672 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
673 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
674 68c76935 2019-02-19 stsp break;
675 68c76935 2019-02-19 stsp }
676 68c76935 2019-02-19 stsp if (flen1 == 0) {
677 68c76935 2019-02-19 stsp if (flen2 != 0)
678 68c76935 2019-02-19 stsp *same = 0;
679 68c76935 2019-02-19 stsp break;
680 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
681 68c76935 2019-02-19 stsp if (flen1 != 0)
682 68c76935 2019-02-19 stsp *same = 0;
683 68c76935 2019-02-19 stsp break;
684 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
685 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
686 68c76935 2019-02-19 stsp *same = 0;
687 68c76935 2019-02-19 stsp break;
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp } else {
690 68c76935 2019-02-19 stsp *same = 0;
691 68c76935 2019-02-19 stsp break;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp }
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp return err;
696 68c76935 2019-02-19 stsp }
697 68c76935 2019-02-19 stsp
698 68c76935 2019-02-19 stsp static const struct got_error *
699 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
700 68c76935 2019-02-19 stsp {
701 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
702 68c76935 2019-02-19 stsp struct stat sb;
703 68c76935 2019-02-19 stsp size_t size1, size2;
704 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
705 68c76935 2019-02-19 stsp
706 68c76935 2019-02-19 stsp *same = 1;
707 68c76935 2019-02-19 stsp
708 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
710 68c76935 2019-02-19 stsp goto done;
711 68c76935 2019-02-19 stsp }
712 68c76935 2019-02-19 stsp size1 = sb.st_size;
713 68c76935 2019-02-19 stsp
714 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
715 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
716 68c76935 2019-02-19 stsp goto done;
717 68c76935 2019-02-19 stsp }
718 68c76935 2019-02-19 stsp size2 = sb.st_size;
719 68c76935 2019-02-19 stsp
720 68c76935 2019-02-19 stsp if (size1 != size2) {
721 68c76935 2019-02-19 stsp *same = 0;
722 68c76935 2019-02-19 stsp return NULL;
723 68c76935 2019-02-19 stsp }
724 68c76935 2019-02-19 stsp
725 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
726 68c76935 2019-02-19 stsp if (f1 == NULL)
727 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
728 68c76935 2019-02-19 stsp
729 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
730 68c76935 2019-02-19 stsp if (f2 == NULL) {
731 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
732 68c76935 2019-02-19 stsp goto done;
733 68c76935 2019-02-19 stsp }
734 68c76935 2019-02-19 stsp
735 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
736 68c76935 2019-02-19 stsp done:
737 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
738 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
739 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
740 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
741 68c76935 2019-02-19 stsp
742 6353ad76 2019-02-08 stsp return err;
743 6353ad76 2019-02-08 stsp }
744 6353ad76 2019-02-08 stsp
745 6353ad76 2019-02-08 stsp /*
746 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
747 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
748 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
749 6353ad76 2019-02-08 stsp */
750 6353ad76 2019-02-08 stsp static const struct got_error *
751 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
752 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
753 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
754 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
755 f69721c3 2019-10-21 stsp struct got_repository *repo,
756 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
757 6353ad76 2019-02-08 stsp {
758 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
759 6353ad76 2019-02-08 stsp int merged_fd = -1;
760 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
761 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
762 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
763 234035bc 2019-06-01 stsp int overlapcnt = 0;
764 af54ae4a 2019-02-19 stsp char *parent;
765 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
766 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
767 6353ad76 2019-02-08 stsp
768 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
769 234035bc 2019-06-01 stsp
770 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
771 af54ae4a 2019-02-19 stsp if (parent == NULL)
772 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
773 af54ae4a 2019-02-19 stsp
774 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
775 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
776 af54ae4a 2019-02-19 stsp
777 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
778 6353ad76 2019-02-08 stsp if (err)
779 6353ad76 2019-02-08 stsp goto done;
780 6353ad76 2019-02-08 stsp
781 af54ae4a 2019-02-19 stsp free(base_path);
782 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
783 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
784 af54ae4a 2019-02-19 stsp base_path = NULL;
785 af54ae4a 2019-02-19 stsp goto done;
786 af54ae4a 2019-02-19 stsp }
787 af54ae4a 2019-02-19 stsp
788 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
789 6353ad76 2019-02-08 stsp if (err)
790 6353ad76 2019-02-08 stsp goto done;
791 46b6ee73 2019-06-04 stsp if (blob_orig) {
792 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
793 46b6ee73 2019-06-04 stsp blob_orig);
794 1430b4e0 2019-03-27 stsp if (err)
795 1430b4e0 2019-03-27 stsp goto done;
796 1430b4e0 2019-03-27 stsp } else {
797 1430b4e0 2019-03-27 stsp /*
798 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
799 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
800 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
801 1430b4e0 2019-03-27 stsp */
802 1430b4e0 2019-03-27 stsp }
803 6353ad76 2019-02-08 stsp
804 3b9f0f87 2020-07-23 stsp /*
805 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
806 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
807 3b9f0f87 2020-07-23 stsp */
808 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
809 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
810 3b9f0f87 2020-07-23 stsp ssize_t target_len;
811 3b9f0f87 2020-07-23 stsp size_t n;
812 3b9f0f87 2020-07-23 stsp
813 3b9f0f87 2020-07-23 stsp free(base_path);
814 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
815 3b9f0f87 2020-07-23 stsp parent) == -1) {
816 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
817 3b9f0f87 2020-07-23 stsp base_path = NULL;
818 3b9f0f87 2020-07-23 stsp goto done;
819 3b9f0f87 2020-07-23 stsp }
820 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
821 3b9f0f87 2020-07-23 stsp if (err)
822 3b9f0f87 2020-07-23 stsp goto done;
823 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
824 3b9f0f87 2020-07-23 stsp sizeof(target_path));
825 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
826 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
827 3b9f0f87 2020-07-23 stsp goto done;
828 3b9f0f87 2020-07-23 stsp }
829 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
830 3b9f0f87 2020-07-23 stsp if (n != target_len) {
831 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
832 3b9f0f87 2020-07-23 stsp goto done;
833 3b9f0f87 2020-07-23 stsp }
834 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
835 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
836 3b9f0f87 2020-07-23 stsp goto done;
837 3b9f0f87 2020-07-23 stsp }
838 3b9f0f87 2020-07-23 stsp }
839 3b9f0f87 2020-07-23 stsp
840 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
841 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
842 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
843 6353ad76 2019-02-08 stsp if (err)
844 6353ad76 2019-02-08 stsp goto done;
845 6353ad76 2019-02-08 stsp
846 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
847 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
848 1ee397ad 2019-07-12 stsp if (err)
849 1ee397ad 2019-07-12 stsp goto done;
850 6353ad76 2019-02-08 stsp
851 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
852 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
853 816dc654 2019-02-16 stsp goto done;
854 68c76935 2019-02-19 stsp }
855 68c76935 2019-02-19 stsp
856 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
857 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
858 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
859 68c76935 2019-02-19 stsp merged_path);
860 68c76935 2019-02-19 stsp if (err)
861 68c76935 2019-02-19 stsp goto done;
862 816dc654 2019-02-16 stsp }
863 6353ad76 2019-02-08 stsp
864 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
865 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
866 70a0c8ec 2019-02-20 stsp goto done;
867 70a0c8ec 2019-02-20 stsp }
868 70a0c8ec 2019-02-20 stsp
869 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
870 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
871 230a42bd 2019-05-11 jcs ondisk_path);
872 6353ad76 2019-02-08 stsp goto done;
873 6353ad76 2019-02-08 stsp }
874 6353ad76 2019-02-08 stsp done:
875 fdcb7daf 2019-12-15 stsp if (err) {
876 fdcb7daf 2019-12-15 stsp if (merged_path)
877 fdcb7daf 2019-12-15 stsp unlink(merged_path);
878 3b9f0f87 2020-07-23 stsp }
879 3b9f0f87 2020-07-23 stsp if (symlink_path) {
880 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
881 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
882 fdcb7daf 2019-12-15 stsp }
883 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
884 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
885 3b9f0f87 2020-07-23 stsp free(symlink_path);
886 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
887 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
888 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
889 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
890 6353ad76 2019-02-08 stsp free(merged_path);
891 af54ae4a 2019-02-19 stsp free(base_path);
892 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
893 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
894 46b6ee73 2019-06-04 stsp free(blob_orig_path);
895 af57b12a 2020-07-23 stsp }
896 af57b12a 2020-07-23 stsp return err;
897 af57b12a 2020-07-23 stsp }
898 af57b12a 2020-07-23 stsp
899 af57b12a 2020-07-23 stsp static const struct got_error *
900 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
901 af57b12a 2020-07-23 stsp size_t target_len)
902 af57b12a 2020-07-23 stsp {
903 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
904 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
905 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
906 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
907 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
908 af57b12a 2020-07-23 stsp ondisk_path);
909 af57b12a 2020-07-23 stsp return NULL;
910 af57b12a 2020-07-23 stsp }
911 af57b12a 2020-07-23 stsp
912 af57b12a 2020-07-23 stsp /*
913 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
914 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
915 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
916 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
917 993e2a1b 2020-07-23 stsp *
918 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
919 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
920 993e2a1b 2020-07-23 stsp *
921 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
922 993e2a1b 2020-07-23 stsp * has deleted this symlink.
923 11cc08c1 2020-07-23 stsp */
924 11cc08c1 2020-07-23 stsp static const struct got_error *
925 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
926 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
927 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
928 11cc08c1 2020-07-23 stsp {
929 11cc08c1 2020-07-23 stsp const struct got_error *err;
930 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
931 11cc08c1 2020-07-23 stsp FILE *f = NULL;
932 11cc08c1 2020-07-23 stsp
933 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
934 11cc08c1 2020-07-23 stsp if (err)
935 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
936 11cc08c1 2020-07-23 stsp
937 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
938 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
939 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
940 11cc08c1 2020-07-23 stsp goto done;
941 11cc08c1 2020-07-23 stsp }
942 11cc08c1 2020-07-23 stsp
943 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
944 11cc08c1 2020-07-23 stsp if (err)
945 11cc08c1 2020-07-23 stsp goto done;
946 11cc08c1 2020-07-23 stsp
947 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
948 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
949 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
950 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
951 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
952 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
953 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
954 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
955 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
956 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
957 11cc08c1 2020-07-23 stsp goto done;
958 11cc08c1 2020-07-23 stsp }
959 11cc08c1 2020-07-23 stsp
960 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
961 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
962 11cc08c1 2020-07-23 stsp goto done;
963 11cc08c1 2020-07-23 stsp }
964 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
965 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
966 11cc08c1 2020-07-23 stsp goto done;
967 11cc08c1 2020-07-23 stsp }
968 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
969 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
970 11cc08c1 2020-07-23 stsp goto done;
971 11cc08c1 2020-07-23 stsp }
972 11cc08c1 2020-07-23 stsp done:
973 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
974 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
975 11cc08c1 2020-07-23 stsp free(path);
976 11cc08c1 2020-07-23 stsp free(id_str);
977 11cc08c1 2020-07-23 stsp free(label_deriv);
978 11cc08c1 2020-07-23 stsp return err;
979 11cc08c1 2020-07-23 stsp }
980 d219f183 2020-07-23 stsp
981 d219f183 2020-07-23 stsp /* forward declaration */
982 d219f183 2020-07-23 stsp static const struct got_error *
983 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
984 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
985 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
986 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
987 11cc08c1 2020-07-23 stsp
988 11cc08c1 2020-07-23 stsp /*
989 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
990 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
991 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
992 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
993 af57b12a 2020-07-23 stsp */
994 af57b12a 2020-07-23 stsp static const struct got_error *
995 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
996 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
997 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
998 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
999 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1000 af57b12a 2020-07-23 stsp {
1001 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1002 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1003 af57b12a 2020-07-23 stsp struct stat sb;
1004 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1005 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1006 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1007 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1008 af57b12a 2020-07-23 stsp
1009 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1010 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1011 af57b12a 2020-07-23 stsp
1012 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1013 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1014 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1015 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1016 af57b12a 2020-07-23 stsp ondisk_path);
1017 af57b12a 2020-07-23 stsp goto done;
1018 af57b12a 2020-07-23 stsp }
1019 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1020 af57b12a 2020-07-23 stsp
1021 526a746f 2020-07-23 stsp if (blob_orig) {
1022 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1023 526a746f 2020-07-23 stsp if (err)
1024 526a746f 2020-07-23 stsp goto done;
1025 526a746f 2020-07-23 stsp }
1026 af57b12a 2020-07-23 stsp
1027 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1028 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1029 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1030 11cc08c1 2020-07-23 stsp have_local_change = 1;
1031 11cc08c1 2020-07-23 stsp
1032 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1033 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1034 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1035 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1036 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1037 11cc08c1 2020-07-23 stsp
1038 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1039 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1040 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1041 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1042 11cc08c1 2020-07-23 stsp path);
1043 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1044 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1045 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1046 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1047 11cc08c1 2020-07-23 stsp path);
1048 11cc08c1 2020-07-23 stsp } else {
1049 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1050 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1051 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1052 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1053 11cc08c1 2020-07-23 stsp if (err)
1054 11cc08c1 2020-07-23 stsp goto done;
1055 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1056 11cc08c1 2020-07-23 stsp path);
1057 11cc08c1 2020-07-23 stsp }
1058 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1059 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1060 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1061 af57b12a 2020-07-23 stsp strlen(deriv_target));
1062 af57b12a 2020-07-23 stsp if (err)
1063 af57b12a 2020-07-23 stsp goto done;
1064 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1065 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1066 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1067 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1068 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1069 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1070 ea7786be 2020-07-23 stsp path);
1071 ea7786be 2020-07-23 stsp } else {
1072 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1073 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1074 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1075 ea7786be 2020-07-23 stsp if (err)
1076 ea7786be 2020-07-23 stsp goto done;
1077 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1078 ea7786be 2020-07-23 stsp path);
1079 ea7786be 2020-07-23 stsp }
1080 6353ad76 2019-02-08 stsp }
1081 11cc08c1 2020-07-23 stsp
1082 af57b12a 2020-07-23 stsp done:
1083 af57b12a 2020-07-23 stsp free(ancestor_target);
1084 14c901f1 2019-08-08 stsp return err;
1085 14c901f1 2019-08-08 stsp }
1086 14c901f1 2019-08-08 stsp
1087 14c901f1 2019-08-08 stsp /*
1088 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1089 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1090 14c901f1 2019-08-08 stsp * acts as the second derived version.
1091 14c901f1 2019-08-08 stsp */
1092 14c901f1 2019-08-08 stsp static const struct got_error *
1093 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1094 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1095 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1096 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1097 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1098 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1099 14c901f1 2019-08-08 stsp {
1100 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1101 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1102 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1103 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1104 14c901f1 2019-08-08 stsp
1105 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1106 14c901f1 2019-08-08 stsp
1107 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1108 14c901f1 2019-08-08 stsp if (parent == NULL)
1109 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1110 14c901f1 2019-08-08 stsp
1111 14c901f1 2019-08-08 stsp free(base_path);
1112 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1113 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1114 14c901f1 2019-08-08 stsp base_path = NULL;
1115 14c901f1 2019-08-08 stsp goto done;
1116 14c901f1 2019-08-08 stsp }
1117 14c901f1 2019-08-08 stsp
1118 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1119 14c901f1 2019-08-08 stsp if (err)
1120 14c901f1 2019-08-08 stsp goto done;
1121 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1122 14c901f1 2019-08-08 stsp blob_deriv);
1123 14c901f1 2019-08-08 stsp if (err)
1124 14c901f1 2019-08-08 stsp goto done;
1125 14c901f1 2019-08-08 stsp
1126 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1127 14c901f1 2019-08-08 stsp if (err)
1128 14c901f1 2019-08-08 stsp goto done;
1129 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1130 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1131 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1132 14c901f1 2019-08-08 stsp goto done;
1133 14c901f1 2019-08-08 stsp }
1134 14c901f1 2019-08-08 stsp
1135 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1136 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1137 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1138 14c901f1 2019-08-08 stsp done:
1139 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1140 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1141 14c901f1 2019-08-08 stsp free(base_path);
1142 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1143 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1144 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1145 14c901f1 2019-08-08 stsp }
1146 6353ad76 2019-02-08 stsp free(id_str);
1147 818c7501 2019-07-11 stsp free(label_deriv);
1148 4a1ddfc2 2019-01-12 stsp return err;
1149 4a1ddfc2 2019-01-12 stsp }
1150 4a1ddfc2 2019-01-12 stsp
1151 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1152 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1153 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1154 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1155 13d9040b 2019-03-27 stsp {
1156 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1157 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1158 13d9040b 2019-03-27 stsp
1159 65b05cec 2020-07-23 stsp *new_iep = NULL;
1160 65b05cec 2020-07-23 stsp
1161 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1162 054041d0 2020-06-24 stsp if (err)
1163 054041d0 2020-06-24 stsp return err;
1164 054041d0 2020-06-24 stsp
1165 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1166 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1167 054041d0 2020-06-24 stsp if (err)
1168 054041d0 2020-06-24 stsp goto done;
1169 054041d0 2020-06-24 stsp
1170 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1171 054041d0 2020-06-24 stsp done:
1172 054041d0 2020-06-24 stsp if (err)
1173 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1174 65b05cec 2020-07-23 stsp else
1175 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1176 13d9040b 2019-03-27 stsp return err;
1177 1ebedb77 2019-10-19 stsp }
1178 1ebedb77 2019-10-19 stsp
1179 1ebedb77 2019-10-19 stsp static mode_t
1180 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1181 1ebedb77 2019-10-19 stsp {
1182 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1183 1ebedb77 2019-10-19 stsp
1184 1ebedb77 2019-10-19 stsp if (executable) {
1185 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1186 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1187 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1188 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1189 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1190 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1191 1ebedb77 2019-10-19 stsp }
1192 1ebedb77 2019-10-19 stsp
1193 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1194 13d9040b 2019-03-27 stsp }
1195 13d9040b 2019-03-27 stsp
1196 8ba819a3 2020-07-23 stsp /* forward declaration */
1197 13d9040b 2019-03-27 stsp static const struct got_error *
1198 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1199 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1200 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1201 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1202 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1203 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1204 8ba819a3 2020-07-23 stsp
1205 5a1fbc73 2020-07-23 stsp /*
1206 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1207 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1208 5a1fbc73 2020-07-23 stsp */
1209 8ba819a3 2020-07-23 stsp static const struct got_error *
1210 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1211 5a1fbc73 2020-07-23 stsp size_t target_len)
1212 5a1fbc73 2020-07-23 stsp {
1213 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1214 5a1fbc73 2020-07-23 stsp ssize_t elen;
1215 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1216 5a1fbc73 2020-07-23 stsp int fd;
1217 5a1fbc73 2020-07-23 stsp
1218 5a1fbc73 2020-07-23 stsp /*
1219 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1220 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1221 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1222 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1223 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1224 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1225 5a1fbc73 2020-07-23 stsp */
1226 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1227 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1228 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1229 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1230 5a1fbc73 2020-07-23 stsp
1231 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1232 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1233 5a1fbc73 2020-07-23 stsp if (elen == -1)
1234 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1235 5a1fbc73 2020-07-23 stsp
1236 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1237 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1238 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1239 5a1fbc73 2020-07-23 stsp }
1240 5a1fbc73 2020-07-23 stsp
1241 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1242 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1243 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1244 5a1fbc73 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp }
1246 3c1ec782 2020-07-23 stsp
1247 3c1ec782 2020-07-23 stsp static const struct got_error *
1248 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1249 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1250 3c1ec782 2020-07-23 stsp {
1251 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1252 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1253 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1256 3c1ec782 2020-07-23 stsp
1257 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1259 3c1ec782 2020-07-23 stsp return NULL;
1260 3c1ec782 2020-07-23 stsp }
1261 3c1ec782 2020-07-23 stsp
1262 3c1ec782 2020-07-23 stsp /*
1263 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1264 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1265 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1266 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1267 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1268 3c1ec782 2020-07-23 stsp */
1269 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1270 3c1ec782 2020-07-23 stsp char *abspath;
1271 3c1ec782 2020-07-23 stsp char *parent = dirname(ondisk_path);
1272 3c1ec782 2020-07-23 stsp if (parent == NULL)
1273 3c1ec782 2020-07-23 stsp return got_error_from_errno2("dirname", ondisk_path);
1274 3c1ec782 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1275 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1276 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1277 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1278 3c1ec782 2020-07-23 stsp free(abspath);
1279 3c1ec782 2020-07-23 stsp return err;
1280 3c1ec782 2020-07-23 stsp }
1281 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1282 3c1ec782 2020-07-23 stsp free(abspath);
1283 3c1ec782 2020-07-23 stsp if (err)
1284 3c1ec782 2020-07-23 stsp return err;
1285 3c1ec782 2020-07-23 stsp } else {
1286 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1287 3c1ec782 2020-07-23 stsp if (err)
1288 3c1ec782 2020-07-23 stsp return err;
1289 3c1ec782 2020-07-23 stsp }
1290 3c1ec782 2020-07-23 stsp
1291 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1292 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1293 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1294 3c1ec782 2020-07-23 stsp return NULL;
1295 3c1ec782 2020-07-23 stsp }
1296 3c1ec782 2020-07-23 stsp
1297 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1298 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1299 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1300 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1301 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1302 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1303 3c1ec782 2020-07-23 stsp
1304 3c1ec782 2020-07-23 stsp free(path_got);
1305 3c1ec782 2020-07-23 stsp return NULL;
1306 5a1fbc73 2020-07-23 stsp }
1307 5a1fbc73 2020-07-23 stsp
1308 5a1fbc73 2020-07-23 stsp static const struct got_error *
1309 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1310 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1311 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1312 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1313 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1314 9d31a1d8 2018-03-11 stsp {
1315 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1316 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1317 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1318 906c123b 2020-07-23 stsp char *path_got = NULL;
1319 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1320 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1321 8ba819a3 2020-07-23 stsp
1322 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1323 2e1fa222 2020-07-23 stsp
1324 8ba819a3 2020-07-23 stsp /*
1325 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1326 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1327 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1328 8ba819a3 2020-07-23 stsp * in the blob object.
1329 8ba819a3 2020-07-23 stsp */
1330 8ba819a3 2020-07-23 stsp do {
1331 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1332 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1333 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1334 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1335 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1336 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1337 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1338 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1339 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1340 3b9f0f87 2020-07-23 stsp progress_arg);
1341 8ba819a3 2020-07-23 stsp }
1342 8ba819a3 2020-07-23 stsp if (len > 0) {
1343 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1344 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1345 8ba819a3 2020-07-23 stsp len - hdrlen);
1346 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1347 8ba819a3 2020-07-23 stsp hdrlen = 0;
1348 8ba819a3 2020-07-23 stsp }
1349 8ba819a3 2020-07-23 stsp } while (len != 0);
1350 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1351 8ba819a3 2020-07-23 stsp
1352 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1353 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1354 3c1ec782 2020-07-23 stsp if (err)
1355 3c1ec782 2020-07-23 stsp return err;
1356 8ba819a3 2020-07-23 stsp
1357 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1358 906c123b 2020-07-23 stsp /* install as a regular file */
1359 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1360 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1361 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1362 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1364 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1365 906c123b 2020-07-23 stsp goto done;
1366 906c123b 2020-07-23 stsp }
1367 906c123b 2020-07-23 stsp
1368 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1369 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1370 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1371 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1372 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1373 c90c8ce3 2020-07-23 stsp goto done;
1374 c90c8ce3 2020-07-23 stsp }
1375 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1376 5a1fbc73 2020-07-23 stsp target_path, target_len);
1377 5a1fbc73 2020-07-23 stsp if (err)
1378 f35fa46a 2020-07-23 stsp goto done;
1379 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1380 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1381 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1382 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1383 6e1eade5 2020-07-23 stsp path);
1384 f35fa46a 2020-07-23 stsp }
1385 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1386 f35fa46a 2020-07-23 stsp }
1387 f35fa46a 2020-07-23 stsp
1388 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1389 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1390 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1391 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1392 f35fa46a 2020-07-23 stsp ondisk_path);
1393 f35fa46a 2020-07-23 stsp goto done;
1394 f35fa46a 2020-07-23 stsp }
1395 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1396 f35fa46a 2020-07-23 stsp if (err)
1397 f35fa46a 2020-07-23 stsp goto done;
1398 f35fa46a 2020-07-23 stsp /*
1399 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1400 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1401 f35fa46a 2020-07-23 stsp */
1402 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1403 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1404 f35fa46a 2020-07-23 stsp goto done;
1405 f35fa46a 2020-07-23 stsp }
1406 f35fa46a 2020-07-23 stsp }
1407 f35fa46a 2020-07-23 stsp
1408 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1409 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1410 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1411 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1412 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1413 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1414 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1416 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1417 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1418 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1419 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1420 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1421 8ba819a3 2020-07-23 stsp } else {
1422 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1423 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1424 8ba819a3 2020-07-23 stsp }
1425 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1426 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1427 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1428 8ba819a3 2020-07-23 stsp done:
1429 906c123b 2020-07-23 stsp free(path_got);
1430 8ba819a3 2020-07-23 stsp return err;
1431 8ba819a3 2020-07-23 stsp }
1432 8ba819a3 2020-07-23 stsp
1433 8ba819a3 2020-07-23 stsp static const struct got_error *
1434 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1435 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1436 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1437 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1438 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1439 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1440 8ba819a3 2020-07-23 stsp {
1441 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1442 507dc3bb 2018-12-29 stsp int fd = -1;
1443 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1444 507dc3bb 2018-12-29 stsp int update = 0;
1445 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1446 8ba819a3 2020-07-23 stsp
1447 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1449 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1450 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1451 21908da4 2019-01-13 stsp char *parent = dirname(path);
1452 21908da4 2019-01-13 stsp if (parent == NULL)
1453 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1454 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1455 21908da4 2019-01-13 stsp if (err)
1456 21908da4 2019-01-13 stsp return err;
1457 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1458 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1459 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1460 21908da4 2019-01-13 stsp if (fd == -1)
1461 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1462 230a42bd 2019-05-11 jcs ondisk_path);
1463 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1464 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1465 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1466 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1467 3b9f0f87 2020-07-23 stsp goto done;
1468 3b9f0f87 2020-07-23 stsp }
1469 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1470 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1471 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1472 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1473 507dc3bb 2018-12-29 stsp goto done;
1474 d70b8e30 2018-12-27 stsp } else {
1475 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1476 507dc3bb 2018-12-29 stsp ondisk_path);
1477 507dc3bb 2018-12-29 stsp if (err)
1478 507dc3bb 2018-12-29 stsp goto done;
1479 507dc3bb 2018-12-29 stsp update = 1;
1480 9d31a1d8 2018-03-11 stsp }
1481 507dc3bb 2018-12-29 stsp } else
1482 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp
1485 bd6aa359 2020-07-23 stsp if (progress_cb) {
1486 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1487 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1488 bd6aa359 2020-07-23 stsp path);
1489 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1490 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1491 bd6aa359 2020-07-23 stsp path);
1492 bd6aa359 2020-07-23 stsp else
1493 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1494 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1495 bd6aa359 2020-07-23 stsp if (err)
1496 bd6aa359 2020-07-23 stsp goto done;
1497 bd6aa359 2020-07-23 stsp }
1498 d7b62c98 2018-12-27 stsp
1499 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1500 9d31a1d8 2018-03-11 stsp do {
1501 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1502 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1503 9d31a1d8 2018-03-11 stsp if (err)
1504 9d31a1d8 2018-03-11 stsp break;
1505 9d31a1d8 2018-03-11 stsp if (len > 0) {
1506 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1507 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1508 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1510 b87c6f83 2018-12-24 stsp goto done;
1511 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1512 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1513 b87c6f83 2018-12-24 stsp goto done;
1514 9d31a1d8 2018-03-11 stsp }
1515 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1516 9d31a1d8 2018-03-11 stsp }
1517 9d31a1d8 2018-03-11 stsp } while (len != 0);
1518 9d31a1d8 2018-03-11 stsp
1519 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1521 816dc654 2019-02-16 stsp goto done;
1522 816dc654 2019-02-16 stsp }
1523 9d31a1d8 2018-03-11 stsp
1524 507dc3bb 2018-12-29 stsp if (update) {
1525 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1526 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1527 230a42bd 2019-05-11 jcs ondisk_path);
1528 2a57020b 2019-02-20 stsp unlink(tmppath);
1529 68ed9ba5 2019-02-10 stsp goto done;
1530 68ed9ba5 2019-02-10 stsp }
1531 68ed9ba5 2019-02-10 stsp }
1532 ba8a0d4d 2019-02-10 stsp
1533 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1534 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1535 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1536 1ebedb77 2019-10-19 stsp goto done;
1537 507dc3bb 2018-12-29 stsp }
1538 507dc3bb 2018-12-29 stsp
1539 9d31a1d8 2018-03-11 stsp done:
1540 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1541 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1542 507dc3bb 2018-12-29 stsp free(tmppath);
1543 6353ad76 2019-02-08 stsp return err;
1544 6353ad76 2019-02-08 stsp }
1545 6353ad76 2019-02-08 stsp
1546 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1547 6353ad76 2019-02-08 stsp static const struct got_error *
1548 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1549 7154f6ce 2019-03-27 stsp {
1550 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1551 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1552 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1553 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1554 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1555 7154f6ce 2019-03-27 stsp };
1556 7154f6ce 2019-03-27 stsp int i = 0;
1557 7154f6ce 2019-03-27 stsp char *line;
1558 7154f6ce 2019-03-27 stsp size_t len;
1559 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1560 7154f6ce 2019-03-27 stsp
1561 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1562 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1563 7154f6ce 2019-03-27 stsp if (line == NULL) {
1564 7154f6ce 2019-03-27 stsp if (feof(f))
1565 7154f6ce 2019-03-27 stsp break;
1566 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1567 7154f6ce 2019-03-27 stsp break;
1568 7154f6ce 2019-03-27 stsp }
1569 7154f6ce 2019-03-27 stsp
1570 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1571 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1572 19332e6d 2019-05-13 stsp == 0)
1573 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1574 7154f6ce 2019-03-27 stsp else
1575 7154f6ce 2019-03-27 stsp i++;
1576 7154f6ce 2019-03-27 stsp }
1577 7154f6ce 2019-03-27 stsp }
1578 7154f6ce 2019-03-27 stsp
1579 7154f6ce 2019-03-27 stsp return err;
1580 e2b1e152 2019-07-13 stsp }
1581 e2b1e152 2019-07-13 stsp
1582 e2b1e152 2019-07-13 stsp static int
1583 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1584 1ebedb77 2019-10-19 stsp {
1585 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1586 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1587 1ebedb77 2019-10-19 stsp }
1588 1ebedb77 2019-10-19 stsp
1589 1ebedb77 2019-10-19 stsp static int
1590 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1591 e2b1e152 2019-07-13 stsp {
1592 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1593 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1594 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1595 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1596 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1597 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1598 c363b2c1 2019-08-03 stsp }
1599 c363b2c1 2019-08-03 stsp
1600 c363b2c1 2019-08-03 stsp static unsigned char
1601 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1602 c363b2c1 2019-08-03 stsp {
1603 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1604 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1605 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1606 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1607 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1608 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1609 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1610 c363b2c1 2019-08-03 stsp default:
1611 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1612 a919d5c4 2020-07-23 stsp }
1613 a919d5c4 2020-07-23 stsp }
1614 a919d5c4 2020-07-23 stsp
1615 a919d5c4 2020-07-23 stsp static const struct got_error *
1616 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1617 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1618 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1619 a919d5c4 2020-07-23 stsp {
1620 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1621 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1622 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1623 a919d5c4 2020-07-23 stsp ssize_t elen;
1624 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1625 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1626 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1627 a919d5c4 2020-07-23 stsp
1628 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1629 a919d5c4 2020-07-23 stsp
1630 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1631 a919d5c4 2020-07-23 stsp do {
1632 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1633 a919d5c4 2020-07-23 stsp if (err)
1634 a919d5c4 2020-07-23 stsp return err;
1635 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1636 a919d5c4 2020-07-23 stsp /*
1637 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1638 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1639 a919d5c4 2020-07-23 stsp */
1640 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1641 a919d5c4 2020-07-23 stsp }
1642 a919d5c4 2020-07-23 stsp if (len > 0) {
1643 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1644 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1645 a919d5c4 2020-07-23 stsp len - hdrlen);
1646 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1647 a919d5c4 2020-07-23 stsp hdrlen = 0;
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp } while (len != 0);
1650 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1651 a919d5c4 2020-07-23 stsp
1652 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1653 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1654 a919d5c4 2020-07-23 stsp if (elen == -1)
1655 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1656 a919d5c4 2020-07-23 stsp } else {
1657 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1658 a919d5c4 2020-07-23 stsp if (elen == -1)
1659 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1660 c363b2c1 2019-08-03 stsp }
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1663 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp return NULL;
1666 7154f6ce 2019-03-27 stsp }
1667 7154f6ce 2019-03-27 stsp
1668 7154f6ce 2019-03-27 stsp static const struct got_error *
1669 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1670 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1671 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1672 6353ad76 2019-02-08 stsp {
1673 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1674 6353ad76 2019-02-08 stsp struct got_object_id id;
1675 6353ad76 2019-02-08 stsp size_t hdrlen;
1676 1338848f 2019-12-13 stsp int fd = -1;
1677 6353ad76 2019-02-08 stsp FILE *f = NULL;
1678 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1679 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1680 6353ad76 2019-02-08 stsp size_t flen, blen;
1681 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1682 6353ad76 2019-02-08 stsp
1683 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1684 6353ad76 2019-02-08 stsp
1685 7f91a133 2019-12-13 stsp /*
1686 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1687 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1688 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1689 7f91a133 2019-12-13 stsp */
1690 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1691 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1692 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1693 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1694 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1695 882ef1b9 2019-12-13 stsp else
1696 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1697 882ef1b9 2019-12-13 stsp goto done;
1698 882ef1b9 2019-12-13 stsp }
1699 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1700 3d35a492 2019-12-13 stsp goto done;
1701 3d35a492 2019-12-13 stsp }
1702 7f91a133 2019-12-13 stsp } else {
1703 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1704 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1705 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1706 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1707 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1708 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1709 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1710 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1711 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1712 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1713 3d35a492 2019-12-13 stsp else
1714 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1715 3d35a492 2019-12-13 stsp goto done;
1716 3d35a492 2019-12-13 stsp }
1717 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1718 1338848f 2019-12-13 stsp goto done;
1719 a378724f 2019-02-10 stsp }
1720 a378724f 2019-02-10 stsp }
1721 6353ad76 2019-02-08 stsp
1722 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1723 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1724 1338848f 2019-12-13 stsp goto done;
1725 3f148bc6 2019-07-27 stsp }
1726 efdd40df 2019-07-27 stsp
1727 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1728 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1729 1338848f 2019-12-13 stsp goto done;
1730 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1731 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1732 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1733 1338848f 2019-12-13 stsp goto done;
1734 d00136be 2019-03-26 stsp }
1735 b8f41171 2019-02-10 stsp
1736 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1737 f179e66d 2020-07-23 stsp goto done;
1738 f179e66d 2020-07-23 stsp
1739 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1740 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1741 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1742 1338848f 2019-12-13 stsp goto done;
1743 f179e66d 2020-07-23 stsp }
1744 6353ad76 2019-02-08 stsp
1745 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1746 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1747 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1748 c363b2c1 2019-08-03 stsp else
1749 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1750 c363b2c1 2019-08-03 stsp
1751 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1752 6353ad76 2019-02-08 stsp if (err)
1753 1338848f 2019-12-13 stsp goto done;
1754 6353ad76 2019-02-08 stsp
1755 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1756 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1757 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1758 a919d5c4 2020-07-23 stsp goto done;
1759 a919d5c4 2020-07-23 stsp }
1760 a919d5c4 2020-07-23 stsp
1761 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1762 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1763 ab0d4361 2019-12-13 stsp if (fd == -1) {
1764 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1765 ab0d4361 2019-12-13 stsp goto done;
1766 ab0d4361 2019-12-13 stsp }
1767 3d35a492 2019-12-13 stsp }
1768 3d35a492 2019-12-13 stsp
1769 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1770 6353ad76 2019-02-08 stsp if (f == NULL) {
1771 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1772 6353ad76 2019-02-08 stsp goto done;
1773 6353ad76 2019-02-08 stsp }
1774 1338848f 2019-12-13 stsp fd = -1;
1775 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1776 656b1f76 2019-05-11 jcs for (;;) {
1777 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1778 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1779 6353ad76 2019-02-08 stsp if (err)
1780 7154f6ce 2019-03-27 stsp goto done;
1781 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1782 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1783 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1784 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1785 7154f6ce 2019-03-27 stsp goto done;
1786 80c5c120 2019-02-19 stsp }
1787 6353ad76 2019-02-08 stsp if (blen == 0) {
1788 6353ad76 2019-02-08 stsp if (flen != 0)
1789 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1790 6353ad76 2019-02-08 stsp break;
1791 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1792 6353ad76 2019-02-08 stsp if (blen != 0)
1793 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1794 6353ad76 2019-02-08 stsp break;
1795 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1796 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1797 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1798 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1799 6353ad76 2019-02-08 stsp break;
1800 6353ad76 2019-02-08 stsp }
1801 6353ad76 2019-02-08 stsp } else {
1802 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1803 6353ad76 2019-02-08 stsp break;
1804 6353ad76 2019-02-08 stsp }
1805 6353ad76 2019-02-08 stsp hdrlen = 0;
1806 6353ad76 2019-02-08 stsp }
1807 7154f6ce 2019-03-27 stsp
1808 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1809 7154f6ce 2019-03-27 stsp rewind(f);
1810 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1811 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1812 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1813 6353ad76 2019-02-08 stsp done:
1814 6353ad76 2019-02-08 stsp if (blob)
1815 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1816 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1817 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1818 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1819 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1820 9d31a1d8 2018-03-11 stsp return err;
1821 e2b1e152 2019-07-13 stsp }
1822 e2b1e152 2019-07-13 stsp
1823 e2b1e152 2019-07-13 stsp /*
1824 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1825 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1826 e2b1e152 2019-07-13 stsp */
1827 e2b1e152 2019-07-13 stsp static const struct got_error *
1828 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1829 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1830 e2b1e152 2019-07-13 stsp {
1831 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1832 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1833 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1834 e2b1e152 2019-07-13 stsp
1835 e2b1e152 2019-07-13 stsp return NULL;
1836 9d31a1d8 2018-03-11 stsp }
1837 9d31a1d8 2018-03-11 stsp
1838 9d31a1d8 2018-03-11 stsp static const struct got_error *
1839 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1840 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1841 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1842 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1843 0584f854 2019-04-06 stsp void *progress_arg)
1844 9d31a1d8 2018-03-11 stsp {
1845 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1846 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1847 6353ad76 2019-02-08 stsp char *ondisk_path;
1848 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1849 b8f41171 2019-02-10 stsp struct stat sb;
1850 9d31a1d8 2018-03-11 stsp
1851 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1852 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1853 6353ad76 2019-02-08 stsp
1854 abb4604f 2019-07-27 stsp if (ie) {
1855 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1856 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1857 a76c42e6 2019-08-03 stsp goto done;
1858 a76c42e6 2019-08-03 stsp }
1859 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1860 7f91a133 2019-12-13 stsp repo);
1861 abb4604f 2019-07-27 stsp if (err)
1862 abb4604f 2019-07-27 stsp goto done;
1863 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1864 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1865 c90c8ce3 2020-07-23 stsp } else {
1866 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1867 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1868 c90c8ce3 2020-07-23 stsp }
1869 6353ad76 2019-02-08 stsp
1870 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1871 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1872 b8f41171 2019-02-10 stsp goto done;
1873 b8f41171 2019-02-10 stsp }
1874 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1875 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1876 5036ab18 2020-04-18 stsp path);
1877 5036ab18 2020-04-18 stsp goto done;
1878 5036ab18 2020-04-18 stsp }
1879 b8f41171 2019-02-10 stsp
1880 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1881 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1882 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1883 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1884 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1885 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1886 e2b1e152 2019-07-13 stsp if (err)
1887 e2b1e152 2019-07-13 stsp goto done;
1888 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 b8f41171 2019-02-10 stsp path);
1890 6353ad76 2019-02-08 stsp goto done;
1891 a378724f 2019-02-10 stsp }
1892 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1893 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1894 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1895 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1896 b8f41171 2019-02-10 stsp goto done;
1897 e2b1e152 2019-07-13 stsp }
1898 9d31a1d8 2018-03-11 stsp }
1899 9d31a1d8 2018-03-11 stsp
1900 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1901 8da9e5f4 2019-01-12 stsp if (err)
1902 6353ad76 2019-02-08 stsp goto done;
1903 512f0d0e 2019-01-02 stsp
1904 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 234035bc 2019-06-01 stsp int update_timestamps;
1906 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1907 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1908 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1909 234035bc 2019-06-01 stsp struct got_object_id id2;
1910 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1911 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1912 234035bc 2019-06-01 stsp if (err)
1913 f69721c3 2019-10-21 stsp goto done;
1914 f69721c3 2019-10-21 stsp }
1915 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1916 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1917 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1918 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1919 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1920 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1921 f69721c3 2019-10-21 stsp goto done;
1922 f69721c3 2019-10-21 stsp }
1923 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1924 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1925 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1926 234035bc 2019-06-01 stsp goto done;
1927 f69721c3 2019-10-21 stsp }
1928 234035bc 2019-06-01 stsp }
1929 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1930 36bf999c 2020-07-23 stsp char *link_target;
1931 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1932 36bf999c 2020-07-23 stsp if (err)
1933 36bf999c 2020-07-23 stsp goto done;
1934 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1935 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1936 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1937 36bf999c 2020-07-23 stsp free(link_target);
1938 993e2a1b 2020-07-23 stsp } else {
1939 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1940 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1941 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1942 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1943 993e2a1b 2020-07-23 stsp }
1944 f69721c3 2019-10-21 stsp free(label_orig);
1945 234035bc 2019-06-01 stsp if (blob2)
1946 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1947 909d120e 2019-09-22 stsp if (err)
1948 909d120e 2019-09-22 stsp goto done;
1949 234035bc 2019-06-01 stsp /*
1950 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1951 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1952 234035bc 2019-06-01 stsp * unmodified files again.
1953 234035bc 2019-06-01 stsp */
1954 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1955 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1956 234035bc 2019-06-01 stsp update_timestamps);
1957 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1958 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1959 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1960 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1961 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1962 1ee397ad 2019-07-12 stsp if (err)
1963 1ee397ad 2019-07-12 stsp goto done;
1964 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1965 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1966 13d9040b 2019-03-27 stsp if (err)
1967 13d9040b 2019-03-27 stsp goto done;
1968 13d9040b 2019-03-27 stsp } else {
1969 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1970 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1971 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1972 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1973 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1974 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1975 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1976 2e1fa222 2020-07-23 stsp } else {
1977 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1978 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1979 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1980 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1981 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1982 2e1fa222 2020-07-23 stsp }
1983 13d9040b 2019-03-27 stsp if (err)
1984 13d9040b 2019-03-27 stsp goto done;
1985 65b05cec 2020-07-23 stsp
1986 054041d0 2020-06-24 stsp if (ie) {
1987 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1988 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1989 054041d0 2020-06-24 stsp } else {
1990 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1991 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1992 054041d0 2020-06-24 stsp &blob->id);
1993 054041d0 2020-06-24 stsp }
1994 13d9040b 2019-03-27 stsp if (err)
1995 13d9040b 2019-03-27 stsp goto done;
1996 65b05cec 2020-07-23 stsp
1997 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1998 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1999 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2000 2e1fa222 2020-07-23 stsp }
2001 13d9040b 2019-03-27 stsp }
2002 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2003 6353ad76 2019-02-08 stsp done:
2004 6353ad76 2019-02-08 stsp free(ondisk_path);
2005 8da9e5f4 2019-01-12 stsp return err;
2006 90285c3b 2019-01-08 stsp }
2007 90285c3b 2019-01-08 stsp
2008 90285c3b 2019-01-08 stsp static const struct got_error *
2009 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2010 90285c3b 2019-01-08 stsp {
2011 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2012 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2013 90285c3b 2019-01-08 stsp
2014 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2015 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2016 90285c3b 2019-01-08 stsp
2017 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2018 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2019 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2020 c97665ae 2019-01-13 stsp } else {
2021 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2022 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2023 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2024 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2025 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2026 230a42bd 2019-05-11 jcs parent);
2027 90285c3b 2019-01-08 stsp break;
2028 90285c3b 2019-01-08 stsp }
2029 90285c3b 2019-01-08 stsp parent = dirname(parent);
2030 90285c3b 2019-01-08 stsp }
2031 90285c3b 2019-01-08 stsp }
2032 90285c3b 2019-01-08 stsp free(ondisk_path);
2033 90285c3b 2019-01-08 stsp return err;
2034 512f0d0e 2019-01-02 stsp }
2035 512f0d0e 2019-01-02 stsp
2036 708d8e67 2019-03-27 stsp static const struct got_error *
2037 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2038 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2039 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2040 708d8e67 2019-03-27 stsp {
2041 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2042 708d8e67 2019-03-27 stsp unsigned char status;
2043 708d8e67 2019-03-27 stsp struct stat sb;
2044 708d8e67 2019-03-27 stsp char *ondisk_path;
2045 708d8e67 2019-03-27 stsp
2046 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2047 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2048 a76c42e6 2019-08-03 stsp
2049 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2050 708d8e67 2019-03-27 stsp == -1)
2051 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2052 708d8e67 2019-03-27 stsp
2053 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2054 708d8e67 2019-03-27 stsp if (err)
2055 5a58a424 2020-06-23 stsp goto done;
2056 708d8e67 2019-03-27 stsp
2057 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2058 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2059 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2060 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2061 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2062 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2063 993e2a1b 2020-07-23 stsp goto done;
2064 993e2a1b 2020-07-23 stsp }
2065 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2066 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2067 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2068 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2069 993e2a1b 2020-07-23 stsp if (err)
2070 993e2a1b 2020-07-23 stsp goto done;
2071 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2072 993e2a1b 2020-07-23 stsp ie->path);
2073 993e2a1b 2020-07-23 stsp goto done;
2074 993e2a1b 2020-07-23 stsp }
2075 993e2a1b 2020-07-23 stsp
2076 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2077 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2078 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2079 1ee397ad 2019-07-12 stsp if (err)
2080 5a58a424 2020-06-23 stsp goto done;
2081 fc6346c4 2019-03-27 stsp /*
2082 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2083 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2084 fc6346c4 2019-03-27 stsp */
2085 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2086 fc6346c4 2019-03-27 stsp 0);
2087 fc6346c4 2019-03-27 stsp } else {
2088 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2089 1ee397ad 2019-07-12 stsp if (err)
2090 5a58a424 2020-06-23 stsp goto done;
2091 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2092 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2093 fc6346c4 2019-03-27 stsp if (err)
2094 5a58a424 2020-06-23 stsp goto done;
2095 fc6346c4 2019-03-27 stsp }
2096 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2097 708d8e67 2019-03-27 stsp }
2098 5a58a424 2020-06-23 stsp done:
2099 5a58a424 2020-06-23 stsp free(ondisk_path);
2100 fc6346c4 2019-03-27 stsp return err;
2101 708d8e67 2019-03-27 stsp }
2102 708d8e67 2019-03-27 stsp
2103 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2104 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2105 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2106 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2107 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2108 8da9e5f4 2019-01-12 stsp void *progress_arg;
2109 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2110 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2111 8da9e5f4 2019-01-12 stsp };
2112 8da9e5f4 2019-01-12 stsp
2113 512f0d0e 2019-01-02 stsp static const struct got_error *
2114 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2115 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2116 512f0d0e 2019-01-02 stsp {
2117 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2118 0584f854 2019-04-06 stsp
2119 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2121 512f0d0e 2019-01-02 stsp
2122 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2123 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 8da9e5f4 2019-01-12 stsp }
2125 512f0d0e 2019-01-02 stsp
2126 8da9e5f4 2019-01-12 stsp static const struct got_error *
2127 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2128 8da9e5f4 2019-01-12 stsp {
2129 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2130 7a9df742 2019-01-08 stsp
2131 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2132 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2133 0584f854 2019-04-06 stsp
2134 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2135 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2136 9d31a1d8 2018-03-11 stsp }
2137 9d31a1d8 2018-03-11 stsp
2138 9d31a1d8 2018-03-11 stsp static const struct got_error *
2139 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2140 9d31a1d8 2018-03-11 stsp {
2141 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2142 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2143 8da9e5f4 2019-01-12 stsp char *path;
2144 9d31a1d8 2018-03-11 stsp
2145 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2147 0584f854 2019-04-06 stsp
2148 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2149 63c5ca5d 2019-08-24 stsp return NULL;
2150 63c5ca5d 2019-08-24 stsp
2151 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2152 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2153 8da9e5f4 2019-01-12 stsp == -1)
2154 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2155 9d31a1d8 2018-03-11 stsp
2156 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2157 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2158 8da9e5f4 2019-01-12 stsp else
2159 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2160 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2161 9d31a1d8 2018-03-11 stsp
2162 8da9e5f4 2019-01-12 stsp free(path);
2163 0cd1c46a 2019-03-11 stsp return err;
2164 0cd1c46a 2019-03-11 stsp }
2165 0cd1c46a 2019-03-11 stsp
2166 b2118c49 2020-07-28 stsp const struct got_error *
2167 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2168 b2118c49 2020-07-28 stsp {
2169 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2170 b2118c49 2020-07-28 stsp
2171 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2172 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2173 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2174 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2175 b2118c49 2020-07-28 stsp }
2176 b2118c49 2020-07-28 stsp
2177 b2118c49 2020-07-28 stsp return NULL;
2178 b2118c49 2020-07-28 stsp }
2179 b2118c49 2020-07-28 stsp
2180 818c7501 2019-07-11 stsp static const struct got_error *
2181 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2182 0cd1c46a 2019-03-11 stsp {
2183 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2184 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2185 0cd1c46a 2019-03-11 stsp
2186 517bab73 2019-03-11 stsp *refname = NULL;
2187 517bab73 2019-03-11 stsp
2188 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2189 b2118c49 2020-07-28 stsp if (err)
2190 b2118c49 2020-07-28 stsp return err;
2191 0cd1c46a 2019-03-11 stsp
2192 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2194 0647c563 2019-03-11 stsp *refname = NULL;
2195 0cd1c46a 2019-03-11 stsp }
2196 517bab73 2019-03-11 stsp free(uuidstr);
2197 517bab73 2019-03-11 stsp return err;
2198 517bab73 2019-03-11 stsp }
2199 517bab73 2019-03-11 stsp
2200 818c7501 2019-07-11 stsp const struct got_error *
2201 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2202 818c7501 2019-07-11 stsp {
2203 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 818c7501 2019-07-11 stsp }
2205 818c7501 2019-07-11 stsp
2206 818c7501 2019-07-11 stsp static const struct got_error *
2207 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 818c7501 2019-07-11 stsp {
2209 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2210 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 818c7501 2019-07-11 stsp }
2212 818c7501 2019-07-11 stsp
2213 818c7501 2019-07-11 stsp static const struct got_error *
2214 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2215 818c7501 2019-07-11 stsp {
2216 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 818c7501 2019-07-11 stsp }
2218 818c7501 2019-07-11 stsp
2219 818c7501 2019-07-11 stsp static const struct got_error *
2220 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2221 818c7501 2019-07-11 stsp {
2222 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2223 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 818c7501 2019-07-11 stsp }
2225 818c7501 2019-07-11 stsp
2226 818c7501 2019-07-11 stsp static const struct got_error *
2227 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2228 818c7501 2019-07-11 stsp {
2229 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2230 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 0ebf8283 2019-07-24 stsp }
2232 0ebf8283 2019-07-24 stsp
2233 0ebf8283 2019-07-24 stsp static const struct got_error *
2234 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 0ebf8283 2019-07-24 stsp {
2236 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2237 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 0ebf8283 2019-07-24 stsp }
2239 0ebf8283 2019-07-24 stsp
2240 0ebf8283 2019-07-24 stsp static const struct got_error *
2241 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2242 0ebf8283 2019-07-24 stsp {
2243 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2244 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 0ebf8283 2019-07-24 stsp }
2246 0ebf8283 2019-07-24 stsp
2247 0ebf8283 2019-07-24 stsp static const struct got_error *
2248 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2249 0ebf8283 2019-07-24 stsp {
2250 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2251 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 818c7501 2019-07-11 stsp }
2253 818c7501 2019-07-11 stsp
2254 0ebf8283 2019-07-24 stsp static const struct got_error *
2255 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 0ebf8283 2019-07-24 stsp {
2257 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2258 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 0ebf8283 2019-07-24 stsp }
2260 818c7501 2019-07-11 stsp
2261 0ebf8283 2019-07-24 stsp const struct got_error *
2262 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2263 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2264 0ebf8283 2019-07-24 stsp {
2265 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2266 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 0ebf8283 2019-07-24 stsp *path = NULL;
2268 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2269 0ebf8283 2019-07-24 stsp }
2270 0ebf8283 2019-07-24 stsp return NULL;
2271 0ebf8283 2019-07-24 stsp }
2272 0ebf8283 2019-07-24 stsp
2273 517bab73 2019-03-11 stsp /*
2274 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2275 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2276 517bab73 2019-03-11 stsp */
2277 517bab73 2019-03-11 stsp static const struct got_error *
2278 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2279 517bab73 2019-03-11 stsp {
2280 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2281 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2282 517bab73 2019-03-11 stsp char *refname;
2283 517bab73 2019-03-11 stsp
2284 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2285 517bab73 2019-03-11 stsp if (err)
2286 517bab73 2019-03-11 stsp return err;
2287 0cd1c46a 2019-03-11 stsp
2288 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2289 0cd1c46a 2019-03-11 stsp if (err)
2290 0cd1c46a 2019-03-11 stsp goto done;
2291 0cd1c46a 2019-03-11 stsp
2292 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2293 0cd1c46a 2019-03-11 stsp done:
2294 0cd1c46a 2019-03-11 stsp free(refname);
2295 0cd1c46a 2019-03-11 stsp if (ref)
2296 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2297 3e3a69f1 2019-07-25 stsp return err;
2298 3e3a69f1 2019-07-25 stsp }
2299 3e3a69f1 2019-07-25 stsp
2300 3e3a69f1 2019-07-25 stsp static const struct got_error *
2301 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2302 3e3a69f1 2019-07-25 stsp {
2303 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2304 3e3a69f1 2019-07-25 stsp
2305 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2306 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2307 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2308 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2309 3e3a69f1 2019-07-25 stsp }
2310 9d31a1d8 2018-03-11 stsp return err;
2311 9d31a1d8 2018-03-11 stsp }
2312 9d31a1d8 2018-03-11 stsp
2313 3e3a69f1 2019-07-25 stsp
2314 ebf99748 2019-05-09 stsp static const struct got_error *
2315 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2316 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2317 ebf99748 2019-05-09 stsp {
2318 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2319 ebf99748 2019-05-09 stsp FILE *index = NULL;
2320 0cd1c46a 2019-03-11 stsp
2321 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2322 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2323 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2324 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2325 ebf99748 2019-05-09 stsp
2326 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2327 3e3a69f1 2019-07-25 stsp if (err)
2328 ebf99748 2019-05-09 stsp goto done;
2329 ebf99748 2019-05-09 stsp
2330 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2331 ebf99748 2019-05-09 stsp if (index == NULL) {
2332 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2333 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2334 ebf99748 2019-05-09 stsp } else {
2335 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2336 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2337 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2338 ebf99748 2019-05-09 stsp }
2339 ebf99748 2019-05-09 stsp done:
2340 ebf99748 2019-05-09 stsp if (err) {
2341 ebf99748 2019-05-09 stsp free(*fileindex_path);
2342 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2343 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2344 ebf99748 2019-05-09 stsp *fileindex = NULL;
2345 ebf99748 2019-05-09 stsp }
2346 ebf99748 2019-05-09 stsp return err;
2347 ebf99748 2019-05-09 stsp }
2348 c932eeeb 2019-05-22 stsp
2349 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2350 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2351 c932eeeb 2019-05-22 stsp const char *path;
2352 c932eeeb 2019-05-22 stsp size_t path_len;
2353 c932eeeb 2019-05-22 stsp const char *entry_name;
2354 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2355 a484d721 2019-06-10 stsp void *progress_arg;
2356 c932eeeb 2019-05-22 stsp };
2357 ebf99748 2019-05-09 stsp
2358 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2359 c932eeeb 2019-05-22 stsp static const struct got_error *
2360 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2361 c932eeeb 2019-05-22 stsp {
2362 1ee397ad 2019-07-12 stsp const struct got_error *err;
2363 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2364 c932eeeb 2019-05-22 stsp
2365 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2366 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2367 c932eeeb 2019-05-22 stsp return NULL;
2368 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2369 102ff934 2019-06-11 stsp return NULL;
2370 102ff934 2019-06-11 stsp
2371 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2372 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2373 c932eeeb 2019-05-22 stsp return NULL;
2374 c932eeeb 2019-05-22 stsp
2375 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2376 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2377 edd02c5e 2019-07-11 stsp ie->path);
2378 1ee397ad 2019-07-12 stsp if (err)
2379 1ee397ad 2019-07-12 stsp return err;
2380 1ee397ad 2019-07-12 stsp }
2381 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2382 c932eeeb 2019-05-22 stsp return NULL;
2383 9c6338c4 2019-06-02 stsp }
2384 9c6338c4 2019-06-02 stsp
2385 9c6338c4 2019-06-02 stsp static const struct got_error *
2386 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2387 9c6338c4 2019-06-02 stsp {
2388 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2389 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2390 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2391 867630bb 2020-01-17 stsp struct timespec timeout;
2392 9c6338c4 2019-06-02 stsp
2393 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2394 9c6338c4 2019-06-02 stsp fileindex_path);
2395 9c6338c4 2019-06-02 stsp if (err)
2396 9c6338c4 2019-06-02 stsp goto done;
2397 9c6338c4 2019-06-02 stsp
2398 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2399 9c6338c4 2019-06-02 stsp if (err)
2400 9c6338c4 2019-06-02 stsp goto done;
2401 9c6338c4 2019-06-02 stsp
2402 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2403 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2404 9c6338c4 2019-06-02 stsp fileindex_path);
2405 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2406 9c6338c4 2019-06-02 stsp }
2407 867630bb 2020-01-17 stsp
2408 867630bb 2020-01-17 stsp /*
2409 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2410 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2411 867630bb 2020-01-17 stsp * was recorded in the file index.
2412 867630bb 2020-01-17 stsp */
2413 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2414 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2415 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2416 9c6338c4 2019-06-02 stsp done:
2417 9c6338c4 2019-06-02 stsp if (new_index)
2418 9c6338c4 2019-06-02 stsp fclose(new_index);
2419 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2420 9c6338c4 2019-06-02 stsp return err;
2421 c932eeeb 2019-05-22 stsp }
2422 6ced4176 2019-07-12 stsp
2423 6ced4176 2019-07-12 stsp static const struct got_error *
2424 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2425 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2426 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2427 6ced4176 2019-07-12 stsp {
2428 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2429 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2430 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2431 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2432 6ced4176 2019-07-12 stsp
2433 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2434 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2435 6ced4176 2019-07-12 stsp *tree_id = NULL;
2436 6ced4176 2019-07-12 stsp
2437 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2438 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2439 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2440 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2441 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2442 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2443 6ced4176 2019-07-12 stsp goto done;
2444 6ced4176 2019-07-12 stsp }
2445 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2446 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2447 6ced4176 2019-07-12 stsp if (err)
2448 6ced4176 2019-07-12 stsp goto done;
2449 6ced4176 2019-07-12 stsp return NULL;
2450 6ced4176 2019-07-12 stsp }
2451 6ced4176 2019-07-12 stsp
2452 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2453 6ced4176 2019-07-12 stsp
2454 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2455 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2456 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2457 6ced4176 2019-07-12 stsp goto done;
2458 6ced4176 2019-07-12 stsp }
2459 6ced4176 2019-07-12 stsp
2460 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2461 6ced4176 2019-07-12 stsp in_repo_path);
2462 6ced4176 2019-07-12 stsp if (err)
2463 6ced4176 2019-07-12 stsp goto done;
2464 6ced4176 2019-07-12 stsp
2465 6ced4176 2019-07-12 stsp free(in_repo_path);
2466 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2467 6ced4176 2019-07-12 stsp
2468 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2469 6ced4176 2019-07-12 stsp if (err)
2470 6ced4176 2019-07-12 stsp goto done;
2471 c932eeeb 2019-05-22 stsp
2472 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2473 6ced4176 2019-07-12 stsp /* Check out a single file. */
2474 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2475 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2476 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2477 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2478 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2479 6ced4176 2019-07-12 stsp goto done;
2480 6ced4176 2019-07-12 stsp }
2481 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2482 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2483 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2484 6ced4176 2019-07-12 stsp goto done;
2485 6ced4176 2019-07-12 stsp }
2486 6ced4176 2019-07-12 stsp } else {
2487 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2488 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2489 6ced4176 2019-07-12 stsp if (err)
2490 6ced4176 2019-07-12 stsp return err;
2491 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2492 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2493 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2494 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2495 6ced4176 2019-07-12 stsp goto done;
2496 6ced4176 2019-07-12 stsp }
2497 6ced4176 2019-07-12 stsp }
2498 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2499 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2500 6ced4176 2019-07-12 stsp } else {
2501 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2502 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2503 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2504 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2505 6ced4176 2019-07-12 stsp goto done;
2506 6ced4176 2019-07-12 stsp }
2507 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2508 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2509 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2510 6ced4176 2019-07-12 stsp goto done;
2511 6ced4176 2019-07-12 stsp }
2512 6ced4176 2019-07-12 stsp }
2513 6ced4176 2019-07-12 stsp done:
2514 6ced4176 2019-07-12 stsp free(id);
2515 6ced4176 2019-07-12 stsp free(in_repo_path);
2516 6ced4176 2019-07-12 stsp if (err) {
2517 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2518 6ced4176 2019-07-12 stsp free(*tree_relpath);
2519 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2520 6ced4176 2019-07-12 stsp free(*tree_id);
2521 6ced4176 2019-07-12 stsp *tree_id = NULL;
2522 6ced4176 2019-07-12 stsp }
2523 07ed472d 2019-07-12 stsp return err;
2524 07ed472d 2019-07-12 stsp }
2525 07ed472d 2019-07-12 stsp
2526 07ed472d 2019-07-12 stsp static const struct got_error *
2527 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2528 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2529 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2530 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2531 07ed472d 2019-07-12 stsp {
2532 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2533 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2534 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2535 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2536 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2537 07ed472d 2019-07-12 stsp
2538 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2539 7f47418f 2019-12-20 stsp if (err) {
2540 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2541 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2542 7f47418f 2019-12-20 stsp goto done;
2543 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2544 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2545 7f47418f 2019-12-20 stsp if (err)
2546 7f47418f 2019-12-20 stsp return err;
2547 7f47418f 2019-12-20 stsp }
2548 07ed472d 2019-07-12 stsp
2549 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2550 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2551 07ed472d 2019-07-12 stsp if (err)
2552 07ed472d 2019-07-12 stsp goto done;
2553 07ed472d 2019-07-12 stsp
2554 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2555 07ed472d 2019-07-12 stsp if (err)
2556 07ed472d 2019-07-12 stsp goto done;
2557 07ed472d 2019-07-12 stsp
2558 07ed472d 2019-07-12 stsp if (entry_name &&
2559 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2560 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2561 07ed472d 2019-07-12 stsp goto done;
2562 07ed472d 2019-07-12 stsp }
2563 07ed472d 2019-07-12 stsp
2564 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2565 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2566 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2567 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2568 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2569 07ed472d 2019-07-12 stsp arg.repo = repo;
2570 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2571 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2572 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2573 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2574 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2575 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2576 07ed472d 2019-07-12 stsp done:
2577 07ed472d 2019-07-12 stsp if (tree)
2578 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2579 07ed472d 2019-07-12 stsp if (commit)
2580 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2581 6ced4176 2019-07-12 stsp return err;
2582 6ced4176 2019-07-12 stsp }
2583 6ced4176 2019-07-12 stsp
2584 9d31a1d8 2018-03-11 stsp const struct got_error *
2585 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2586 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2587 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2588 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2589 9d31a1d8 2018-03-11 stsp {
2590 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2591 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2592 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2593 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2594 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2595 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2596 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2597 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2598 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2599 f2ea84fa 2019-07-27 stsp int entry_type;
2600 f2ea84fa 2019-07-27 stsp char *relpath;
2601 f2ea84fa 2019-07-27 stsp char *entry_name;
2602 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2603 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2604 9d31a1d8 2018-03-11 stsp
2605 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2606 f2ea84fa 2019-07-27 stsp
2607 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2608 9d31a1d8 2018-03-11 stsp if (err)
2609 9d31a1d8 2018-03-11 stsp return err;
2610 93a30277 2018-12-24 stsp
2611 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2612 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2613 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2614 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2615 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2616 f2ea84fa 2019-07-27 stsp goto done;
2617 f2ea84fa 2019-07-27 stsp }
2618 6ced4176 2019-07-12 stsp
2619 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2620 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2621 f2ea84fa 2019-07-27 stsp if (err) {
2622 f2ea84fa 2019-07-27 stsp free(tpd);
2623 6ced4176 2019-07-12 stsp goto done;
2624 6ced4176 2019-07-12 stsp }
2625 f2ea84fa 2019-07-27 stsp
2626 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2627 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2628 f2ea84fa 2019-07-27 stsp if (err) {
2629 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2630 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2631 f2ea84fa 2019-07-27 stsp free(tpd);
2632 f2ea84fa 2019-07-27 stsp goto done;
2633 f2ea84fa 2019-07-27 stsp }
2634 f2ea84fa 2019-07-27 stsp } else
2635 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2636 f2ea84fa 2019-07-27 stsp
2637 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2638 6ced4176 2019-07-12 stsp }
2639 6ced4176 2019-07-12 stsp
2640 51514078 2018-12-25 stsp /*
2641 51514078 2018-12-25 stsp * Read the file index.
2642 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2643 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2644 51514078 2018-12-25 stsp */
2645 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2646 8da9e5f4 2019-01-12 stsp if (err)
2647 c4cdcb68 2019-04-03 stsp goto done;
2648 c4cdcb68 2019-04-03 stsp
2649 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2650 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2651 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2652 f2ea84fa 2019-07-27 stsp
2653 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2654 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2655 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2656 f2ea84fa 2019-07-27 stsp if (err)
2657 f2ea84fa 2019-07-27 stsp break;
2658 f2ea84fa 2019-07-27 stsp
2659 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2660 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2661 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2662 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2663 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2664 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2665 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2666 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2667 f2ea84fa 2019-07-27 stsp if (err)
2668 f2ea84fa 2019-07-27 stsp break;
2669 f2ea84fa 2019-07-27 stsp
2670 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2671 711d9cd9 2019-07-12 stsp }
2672 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2673 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2674 af12c6b9 2019-06-04 stsp err = sync_err;
2675 9d31a1d8 2018-03-11 stsp done:
2676 9c6338c4 2019-06-02 stsp free(fileindex_path);
2677 edfa7d7f 2018-09-11 stsp if (tree)
2678 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2679 9d31a1d8 2018-03-11 stsp if (commit)
2680 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2681 5ade8233 2019-07-12 stsp if (fileindex)
2682 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2683 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2684 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2685 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2686 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2687 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2688 f2ea84fa 2019-07-27 stsp free(tpd);
2689 f2ea84fa 2019-07-27 stsp }
2690 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2691 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2692 9d31a1d8 2018-03-11 stsp err = unlockerr;
2693 f8d1f275 2019-02-04 stsp return err;
2694 f8d1f275 2019-02-04 stsp }
2695 f8d1f275 2019-02-04 stsp
2696 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2697 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2698 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2699 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2700 234035bc 2019-06-01 stsp void *progress_arg;
2701 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2702 234035bc 2019-06-01 stsp void *cancel_arg;
2703 f69721c3 2019-10-21 stsp const char *label_orig;
2704 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2705 234035bc 2019-06-01 stsp };
2706 234035bc 2019-06-01 stsp
2707 234035bc 2019-06-01 stsp static const struct got_error *
2708 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2709 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2710 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2711 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2712 234035bc 2019-06-01 stsp {
2713 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2714 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2715 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2716 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2717 234035bc 2019-06-01 stsp struct stat sb;
2718 234035bc 2019-06-01 stsp unsigned char status;
2719 234035bc 2019-06-01 stsp int local_changes_subsumed;
2720 234035bc 2019-06-01 stsp
2721 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2722 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2723 d6c87207 2019-08-02 stsp strlen(path2));
2724 1ee397ad 2019-07-12 stsp if (ie == NULL)
2725 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2726 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2727 234035bc 2019-06-01 stsp
2728 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2729 234035bc 2019-06-01 stsp path2) == -1)
2730 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2731 234035bc 2019-06-01 stsp
2732 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2733 7f91a133 2019-12-13 stsp repo);
2734 234035bc 2019-06-01 stsp if (err)
2735 234035bc 2019-06-01 stsp goto done;
2736 234035bc 2019-06-01 stsp
2737 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2738 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2739 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2740 234035bc 2019-06-01 stsp goto done;
2741 234035bc 2019-06-01 stsp }
2742 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2743 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2744 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2745 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2746 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2747 234035bc 2019-06-01 stsp goto done;
2748 234035bc 2019-06-01 stsp }
2749 234035bc 2019-06-01 stsp
2750 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2751 36bf999c 2020-07-23 stsp char *link_target2;
2752 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2753 36bf999c 2020-07-23 stsp if (err)
2754 36bf999c 2020-07-23 stsp goto done;
2755 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2756 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2757 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2758 36bf999c 2020-07-23 stsp free(link_target2);
2759 af57b12a 2020-07-23 stsp } else {
2760 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2761 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2762 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2763 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2764 af57b12a 2020-07-23 stsp }
2765 234035bc 2019-06-01 stsp } else if (blob1) {
2766 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2767 d6c87207 2019-08-02 stsp strlen(path1));
2768 1ee397ad 2019-07-12 stsp if (ie == NULL)
2769 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2770 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2771 2b92fad7 2019-06-02 stsp
2772 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 2b92fad7 2019-06-02 stsp path1) == -1)
2774 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2775 2b92fad7 2019-06-02 stsp
2776 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2777 7f91a133 2019-12-13 stsp repo);
2778 2b92fad7 2019-06-02 stsp if (err)
2779 2b92fad7 2019-06-02 stsp goto done;
2780 2b92fad7 2019-06-02 stsp
2781 2b92fad7 2019-06-02 stsp switch (status) {
2782 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2783 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2784 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2785 1ee397ad 2019-07-12 stsp if (err)
2786 1ee397ad 2019-07-12 stsp goto done;
2787 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2788 2b92fad7 2019-06-02 stsp if (err)
2789 2b92fad7 2019-06-02 stsp goto done;
2790 2b92fad7 2019-06-02 stsp if (ie)
2791 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2792 2b92fad7 2019-06-02 stsp break;
2793 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2794 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2795 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2796 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2797 1ee397ad 2019-07-12 stsp if (err)
2798 1ee397ad 2019-07-12 stsp goto done;
2799 2b92fad7 2019-06-02 stsp if (ie)
2800 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2801 2b92fad7 2019-06-02 stsp break;
2802 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2803 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2804 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2805 0a22ca1a 2020-09-23 stsp /*
2806 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2807 0a22ca1a 2020-09-23 stsp * exists in the repository.
2808 0a22ca1a 2020-09-23 stsp */
2809 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2810 0a22ca1a 2020-09-23 stsp if (err)
2811 0a22ca1a 2020-09-23 stsp goto done;
2812 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2813 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2814 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2815 0a22ca1a 2020-09-23 stsp if (err)
2816 0a22ca1a 2020-09-23 stsp goto done;
2817 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2818 0a22ca1a 2020-09-23 stsp path1);
2819 0a22ca1a 2020-09-23 stsp if (err)
2820 0a22ca1a 2020-09-23 stsp goto done;
2821 0a22ca1a 2020-09-23 stsp if (ie)
2822 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2823 0a22ca1a 2020-09-23 stsp ie);
2824 0a22ca1a 2020-09-23 stsp } else {
2825 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2826 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2827 0a22ca1a 2020-09-23 stsp }
2828 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2829 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2830 0a22ca1a 2020-09-23 stsp free(id);
2831 0a22ca1a 2020-09-23 stsp if (err)
2832 0a22ca1a 2020-09-23 stsp goto done;
2833 0a22ca1a 2020-09-23 stsp break;
2834 0a22ca1a 2020-09-23 stsp }
2835 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2836 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2837 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2838 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2839 1ee397ad 2019-07-12 stsp if (err)
2840 1ee397ad 2019-07-12 stsp goto done;
2841 2b92fad7 2019-06-02 stsp break;
2842 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2843 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2844 1ee397ad 2019-07-12 stsp if (err)
2845 1ee397ad 2019-07-12 stsp goto done;
2846 2b92fad7 2019-06-02 stsp break;
2847 2b92fad7 2019-06-02 stsp default:
2848 2b92fad7 2019-06-02 stsp break;
2849 2b92fad7 2019-06-02 stsp }
2850 234035bc 2019-06-01 stsp } else if (blob2) {
2851 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 234035bc 2019-06-01 stsp path2) == -1)
2853 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2854 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2855 d6c87207 2019-08-02 stsp strlen(path2));
2856 234035bc 2019-06-01 stsp if (ie) {
2857 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2858 7f91a133 2019-12-13 stsp -1, NULL, repo);
2859 234035bc 2019-06-01 stsp if (err)
2860 234035bc 2019-06-01 stsp goto done;
2861 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2862 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2863 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2864 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2865 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2866 1ee397ad 2019-07-12 stsp status, path2);
2867 234035bc 2019-06-01 stsp goto done;
2868 234035bc 2019-06-01 stsp }
2869 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2870 36bf999c 2020-07-23 stsp char *link_target2;
2871 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2872 36bf999c 2020-07-23 stsp blob2);
2873 36bf999c 2020-07-23 stsp if (err)
2874 36bf999c 2020-07-23 stsp goto done;
2875 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2876 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2877 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2878 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2879 36bf999c 2020-07-23 stsp free(link_target2);
2880 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2881 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2882 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2883 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2884 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2885 526a746f 2020-07-23 stsp a->progress_arg);
2886 dfe9fba0 2020-07-23 stsp } else {
2887 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2888 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2889 526a746f 2020-07-23 stsp }
2890 dfe9fba0 2020-07-23 stsp if (err)
2891 dfe9fba0 2020-07-23 stsp goto done;
2892 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2893 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2894 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2895 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2896 234035bc 2019-06-01 stsp if (err)
2897 234035bc 2019-06-01 stsp goto done;
2898 234035bc 2019-06-01 stsp }
2899 234035bc 2019-06-01 stsp } else {
2900 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2901 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2902 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2903 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2904 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2905 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2906 2e1fa222 2020-07-23 stsp } else {
2907 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2908 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2909 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2910 2e1fa222 2020-07-23 stsp }
2911 234035bc 2019-06-01 stsp if (err)
2912 234035bc 2019-06-01 stsp goto done;
2913 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2914 234035bc 2019-06-01 stsp if (err)
2915 234035bc 2019-06-01 stsp goto done;
2916 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2917 3969253a 2020-03-07 stsp NULL, NULL, 1);
2918 3969253a 2020-03-07 stsp if (err) {
2919 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2920 3969253a 2020-03-07 stsp goto done;
2921 3969253a 2020-03-07 stsp }
2922 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2923 2b92fad7 2019-06-02 stsp if (err) {
2924 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2925 2b92fad7 2019-06-02 stsp goto done;
2926 2b92fad7 2019-06-02 stsp }
2927 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2928 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2929 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2930 2e1fa222 2020-07-23 stsp }
2931 234035bc 2019-06-01 stsp }
2932 234035bc 2019-06-01 stsp }
2933 234035bc 2019-06-01 stsp done:
2934 234035bc 2019-06-01 stsp free(ondisk_path);
2935 234035bc 2019-06-01 stsp return err;
2936 234035bc 2019-06-01 stsp }
2937 234035bc 2019-06-01 stsp
2938 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2939 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2940 234035bc 2019-06-01 stsp struct got_repository *repo;
2941 234035bc 2019-06-01 stsp };
2942 234035bc 2019-06-01 stsp
2943 234035bc 2019-06-01 stsp static const struct got_error *
2944 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2945 234035bc 2019-06-01 stsp {
2946 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2947 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2948 234035bc 2019-06-01 stsp unsigned char status;
2949 234035bc 2019-06-01 stsp struct stat sb;
2950 234035bc 2019-06-01 stsp char *ondisk_path;
2951 234035bc 2019-06-01 stsp
2952 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2953 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2954 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2955 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2956 234035bc 2019-06-01 stsp
2957 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2958 234035bc 2019-06-01 stsp == -1)
2959 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2960 234035bc 2019-06-01 stsp
2961 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2962 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2963 234035bc 2019-06-01 stsp if (err)
2964 234035bc 2019-06-01 stsp return err;
2965 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2966 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2967 234035bc 2019-06-01 stsp
2968 234035bc 2019-06-01 stsp return NULL;
2969 234035bc 2019-06-01 stsp }
2970 234035bc 2019-06-01 stsp
2971 818c7501 2019-07-11 stsp static const struct got_error *
2972 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2973 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2974 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2975 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2976 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2977 234035bc 2019-06-01 stsp {
2978 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2979 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2980 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2981 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2982 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2983 234035bc 2019-06-01 stsp
2984 03415a1a 2019-06-02 stsp if (commit_id1) {
2985 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2986 03415a1a 2019-06-02 stsp worktree->path_prefix);
2987 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2988 03415a1a 2019-06-02 stsp goto done;
2989 69d57f3d 2020-07-31 stsp }
2990 69d57f3d 2020-07-31 stsp if (tree_id1) {
2991 69d57f3d 2020-07-31 stsp char *id_str;
2992 03415a1a 2019-06-02 stsp
2993 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2994 03415a1a 2019-06-02 stsp if (err)
2995 03415a1a 2019-06-02 stsp goto done;
2996 f69721c3 2019-10-21 stsp
2997 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2998 f69721c3 2019-10-21 stsp if (err)
2999 f69721c3 2019-10-21 stsp goto done;
3000 f69721c3 2019-10-21 stsp
3001 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3002 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3003 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3004 f69721c3 2019-10-21 stsp free(id_str);
3005 f69721c3 2019-10-21 stsp goto done;
3006 f69721c3 2019-10-21 stsp }
3007 f69721c3 2019-10-21 stsp free(id_str);
3008 03415a1a 2019-06-02 stsp }
3009 234035bc 2019-06-01 stsp
3010 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3011 234035bc 2019-06-01 stsp worktree->path_prefix);
3012 234035bc 2019-06-01 stsp if (err)
3013 234035bc 2019-06-01 stsp goto done;
3014 234035bc 2019-06-01 stsp
3015 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3016 234035bc 2019-06-01 stsp if (err)
3017 234035bc 2019-06-01 stsp goto done;
3018 234035bc 2019-06-01 stsp
3019 234035bc 2019-06-01 stsp arg.worktree = worktree;
3020 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3021 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3022 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3023 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3024 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3025 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3026 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3027 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3028 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3029 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3030 af12c6b9 2019-06-04 stsp err = sync_err;
3031 234035bc 2019-06-01 stsp done:
3032 234035bc 2019-06-01 stsp if (tree1)
3033 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3034 234035bc 2019-06-01 stsp if (tree2)
3035 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3036 f69721c3 2019-10-21 stsp free(label_orig);
3037 818c7501 2019-07-11 stsp return err;
3038 818c7501 2019-07-11 stsp }
3039 234035bc 2019-06-01 stsp
3040 818c7501 2019-07-11 stsp const struct got_error *
3041 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3042 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3043 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3044 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3045 818c7501 2019-07-11 stsp {
3046 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3047 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3048 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3049 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3050 818c7501 2019-07-11 stsp
3051 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3052 818c7501 2019-07-11 stsp if (err)
3053 818c7501 2019-07-11 stsp return err;
3054 818c7501 2019-07-11 stsp
3055 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3056 818c7501 2019-07-11 stsp if (err)
3057 818c7501 2019-07-11 stsp goto done;
3058 818c7501 2019-07-11 stsp
3059 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3060 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3061 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3062 818c7501 2019-07-11 stsp &mok_arg);
3063 818c7501 2019-07-11 stsp if (err)
3064 818c7501 2019-07-11 stsp goto done;
3065 818c7501 2019-07-11 stsp
3066 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3067 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3068 818c7501 2019-07-11 stsp done:
3069 818c7501 2019-07-11 stsp if (fileindex)
3070 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3071 818c7501 2019-07-11 stsp free(fileindex_path);
3072 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3073 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3074 234035bc 2019-06-01 stsp err = unlockerr;
3075 234035bc 2019-06-01 stsp return err;
3076 234035bc 2019-06-01 stsp }
3077 234035bc 2019-06-01 stsp
3078 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3079 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3080 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3081 927df6b7 2019-02-10 stsp const char *status_path;
3082 927df6b7 2019-02-10 stsp size_t status_path_len;
3083 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3084 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3085 f8d1f275 2019-02-04 stsp void *status_arg;
3086 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3087 f8d1f275 2019-02-04 stsp void *cancel_arg;
3088 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3089 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3090 f2a9dc41 2019-12-13 tracey int report_unchanged;
3091 3143d852 2020-06-25 stsp int no_ignores;
3092 f8d1f275 2019-02-04 stsp };
3093 88d0e355 2019-08-03 stsp
3094 f8d1f275 2019-02-04 stsp static const struct got_error *
3095 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3096 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3097 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3098 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3099 927df6b7 2019-02-10 stsp {
3100 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3101 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3102 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3103 927df6b7 2019-02-10 stsp struct stat sb;
3104 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3105 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3106 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3107 927df6b7 2019-02-10 stsp
3108 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3109 98eaaa12 2019-08-03 stsp if (err)
3110 98eaaa12 2019-08-03 stsp return err;
3111 98eaaa12 2019-08-03 stsp
3112 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3113 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3114 98eaaa12 2019-08-03 stsp return NULL;
3115 98eaaa12 2019-08-03 stsp
3116 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3117 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3118 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3119 98eaaa12 2019-08-03 stsp }
3120 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3121 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3122 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3123 927df6b7 2019-02-10 stsp }
3124 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3125 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3126 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3127 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3128 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3129 98eaaa12 2019-08-03 stsp }
3130 98eaaa12 2019-08-03 stsp
3131 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3132 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3133 927df6b7 2019-02-10 stsp }
3134 927df6b7 2019-02-10 stsp
3135 927df6b7 2019-02-10 stsp static const struct got_error *
3136 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3137 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3138 f8d1f275 2019-02-04 stsp {
3139 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3140 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3141 f8d1f275 2019-02-04 stsp char *abspath;
3142 f8d1f275 2019-02-04 stsp
3143 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3144 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3145 0584f854 2019-04-06 stsp
3146 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3147 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3148 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3149 927df6b7 2019-02-10 stsp return NULL;
3150 927df6b7 2019-02-10 stsp
3151 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3152 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3153 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3154 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3155 f8d1f275 2019-02-04 stsp } else {
3156 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3157 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3158 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3159 f8d1f275 2019-02-04 stsp }
3160 f8d1f275 2019-02-04 stsp
3161 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3162 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3163 f8d1f275 2019-02-04 stsp free(abspath);
3164 9d31a1d8 2018-03-11 stsp return err;
3165 9d31a1d8 2018-03-11 stsp }
3166 f8d1f275 2019-02-04 stsp
3167 f8d1f275 2019-02-04 stsp static const struct got_error *
3168 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3169 f8d1f275 2019-02-04 stsp {
3170 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3171 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3172 2ec1f75b 2019-03-26 stsp unsigned char status;
3173 927df6b7 2019-02-10 stsp
3174 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3175 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3176 0584f854 2019-04-06 stsp
3177 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3178 927df6b7 2019-02-10 stsp return NULL;
3179 927df6b7 2019-02-10 stsp
3180 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3181 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3182 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3183 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3184 2ec1f75b 2019-03-26 stsp else
3185 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3186 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3187 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3188 6841da00 2019-08-08 stsp }
3189 6841da00 2019-08-08 stsp
3190 6841da00 2019-08-08 stsp void
3191 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3192 6841da00 2019-08-08 stsp {
3193 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3194 6841da00 2019-08-08 stsp
3195 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3196 6841da00 2019-08-08 stsp free((char *)pe->path);
3197 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3198 6841da00 2019-08-08 stsp }
3199 6841da00 2019-08-08 stsp
3200 6841da00 2019-08-08 stsp void
3201 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3202 6841da00 2019-08-08 stsp {
3203 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3204 6841da00 2019-08-08 stsp
3205 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3206 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3207 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3208 6841da00 2019-08-08 stsp free((char *)pe->path);
3209 6841da00 2019-08-08 stsp }
3210 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3211 6841da00 2019-08-08 stsp }
3212 6841da00 2019-08-08 stsp
3213 6841da00 2019-08-08 stsp static const struct got_error *
3214 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3215 6841da00 2019-08-08 stsp {
3216 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3217 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3218 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3219 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3220 6841da00 2019-08-08 stsp size_t linesize = 0;
3221 6841da00 2019-08-08 stsp ssize_t linelen;
3222 6841da00 2019-08-08 stsp
3223 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3224 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3225 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3226 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3227 6841da00 2019-08-08 stsp
3228 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3229 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3230 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3231 bd8de430 2019-10-04 stsp
3232 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3233 bd8de430 2019-10-04 stsp if (line[0] == '#')
3234 bd8de430 2019-10-04 stsp continue;
3235 bd8de430 2019-10-04 stsp
3236 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3237 bd8de430 2019-10-04 stsp if (line[0] == '!')
3238 bd8de430 2019-10-04 stsp continue;
3239 bd8de430 2019-10-04 stsp
3240 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3241 6841da00 2019-08-08 stsp line) == -1) {
3242 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3243 6841da00 2019-08-08 stsp goto done;
3244 6841da00 2019-08-08 stsp }
3245 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3246 6841da00 2019-08-08 stsp if (err)
3247 6841da00 2019-08-08 stsp goto done;
3248 6841da00 2019-08-08 stsp }
3249 6841da00 2019-08-08 stsp if (ferror(f)) {
3250 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3251 6841da00 2019-08-08 stsp goto done;
3252 6841da00 2019-08-08 stsp }
3253 6841da00 2019-08-08 stsp
3254 6841da00 2019-08-08 stsp dirpath = strdup(path);
3255 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3256 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3257 6841da00 2019-08-08 stsp goto done;
3258 6841da00 2019-08-08 stsp }
3259 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3260 6841da00 2019-08-08 stsp done:
3261 6841da00 2019-08-08 stsp free(line);
3262 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3263 6841da00 2019-08-08 stsp free(dirpath);
3264 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3265 6841da00 2019-08-08 stsp }
3266 6841da00 2019-08-08 stsp return err;
3267 6841da00 2019-08-08 stsp }
3268 6841da00 2019-08-08 stsp
3269 6841da00 2019-08-08 stsp int
3270 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3271 6841da00 2019-08-08 stsp {
3272 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3273 bd8de430 2019-10-04 stsp
3274 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3275 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3276 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3277 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3278 bd8de430 2019-10-04 stsp
3279 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3280 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3281 6841da00 2019-08-08 stsp
3282 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3283 bd8de430 2019-10-04 stsp continue;
3284 bd8de430 2019-10-04 stsp pattern += 3;
3285 bd8de430 2019-10-04 stsp p = path;
3286 bd8de430 2019-10-04 stsp while (*p) {
3287 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3288 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3289 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3290 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3291 bd8de430 2019-10-04 stsp p++;
3292 bd8de430 2019-10-04 stsp while (*p == '/')
3293 bd8de430 2019-10-04 stsp p++;
3294 bd8de430 2019-10-04 stsp continue;
3295 bd8de430 2019-10-04 stsp }
3296 bd8de430 2019-10-04 stsp return 1;
3297 bd8de430 2019-10-04 stsp }
3298 bd8de430 2019-10-04 stsp }
3299 bd8de430 2019-10-04 stsp }
3300 bd8de430 2019-10-04 stsp
3301 6841da00 2019-08-08 stsp /*
3302 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3303 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3304 6841da00 2019-08-08 stsp * ignores backwards.
3305 6841da00 2019-08-08 stsp */
3306 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3307 6841da00 2019-08-08 stsp while (pe) {
3308 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3309 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3310 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3311 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3312 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3313 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3314 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3315 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3316 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3317 6841da00 2019-08-08 stsp continue;
3318 6841da00 2019-08-08 stsp return 1;
3319 6841da00 2019-08-08 stsp }
3320 6841da00 2019-08-08 stsp }
3321 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3322 6841da00 2019-08-08 stsp }
3323 6841da00 2019-08-08 stsp
3324 6841da00 2019-08-08 stsp return 0;
3325 6841da00 2019-08-08 stsp }
3326 6841da00 2019-08-08 stsp
3327 6841da00 2019-08-08 stsp static const struct got_error *
3328 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3329 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3330 6841da00 2019-08-08 stsp {
3331 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3332 6841da00 2019-08-08 stsp char *ignorespath;
3333 886cec17 2019-12-15 stsp int fd = -1;
3334 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3335 6841da00 2019-08-08 stsp
3336 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3337 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3338 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3339 6841da00 2019-08-08 stsp
3340 886cec17 2019-12-15 stsp if (dirfd != -1) {
3341 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3342 886cec17 2019-12-15 stsp if (fd == -1) {
3343 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3344 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3345 886cec17 2019-12-15 stsp ignorespath);
3346 886cec17 2019-12-15 stsp } else {
3347 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3348 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3349 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3350 886cec17 2019-12-15 stsp ignorespath);
3351 886cec17 2019-12-15 stsp else {
3352 886cec17 2019-12-15 stsp fd = -1;
3353 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3354 886cec17 2019-12-15 stsp }
3355 886cec17 2019-12-15 stsp }
3356 886cec17 2019-12-15 stsp } else {
3357 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3358 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3359 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3360 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3361 886cec17 2019-12-15 stsp ignorespath);
3362 886cec17 2019-12-15 stsp } else
3363 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3364 886cec17 2019-12-15 stsp }
3365 6841da00 2019-08-08 stsp
3366 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3367 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3368 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3369 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3370 6841da00 2019-08-08 stsp free(ignorespath);
3371 6841da00 2019-08-08 stsp return err;
3372 f8d1f275 2019-02-04 stsp }
3373 f8d1f275 2019-02-04 stsp
3374 f8d1f275 2019-02-04 stsp static const struct got_error *
3375 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3376 f8d1f275 2019-02-04 stsp {
3377 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3378 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3379 f8d1f275 2019-02-04 stsp char *path = NULL;
3380 f8d1f275 2019-02-04 stsp
3381 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3383 0584f854 2019-04-06 stsp
3384 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3385 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3386 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3387 f8d1f275 2019-02-04 stsp } else {
3388 f8d1f275 2019-02-04 stsp path = de->d_name;
3389 f8d1f275 2019-02-04 stsp }
3390 f8d1f275 2019-02-04 stsp
3391 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3392 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3393 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3394 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3395 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3396 f8d1f275 2019-02-04 stsp if (parent_path[0])
3397 f8d1f275 2019-02-04 stsp free(path);
3398 b72f483a 2019-02-05 stsp return err;
3399 f8d1f275 2019-02-04 stsp }
3400 f8d1f275 2019-02-04 stsp
3401 347d1d3e 2019-07-12 stsp static const struct got_error *
3402 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3403 3143d852 2020-06-25 stsp {
3404 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3405 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3406 3143d852 2020-06-25 stsp
3407 3143d852 2020-06-25 stsp if (a->no_ignores)
3408 3143d852 2020-06-25 stsp return NULL;
3409 3143d852 2020-06-25 stsp
3410 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3411 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3412 3143d852 2020-06-25 stsp if (err)
3413 3143d852 2020-06-25 stsp return err;
3414 3143d852 2020-06-25 stsp
3415 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3416 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3417 3143d852 2020-06-25 stsp
3418 3143d852 2020-06-25 stsp return err;
3419 3143d852 2020-06-25 stsp }
3420 3143d852 2020-06-25 stsp
3421 3143d852 2020-06-25 stsp static const struct got_error *
3422 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3423 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3424 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3425 abb4604f 2019-07-27 stsp {
3426 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3427 abb4604f 2019-07-27 stsp struct stat sb;
3428 abb4604f 2019-07-27 stsp
3429 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3430 abb4604f 2019-07-27 stsp if (ie)
3431 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3432 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3433 abb4604f 2019-07-27 stsp
3434 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3435 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3436 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3437 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3438 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3439 abb4604f 2019-07-27 stsp return NULL;
3440 abb4604f 2019-07-27 stsp }
3441 abb4604f 2019-07-27 stsp
3442 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3443 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3444 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3445 abb4604f 2019-07-27 stsp
3446 abb4604f 2019-07-27 stsp return NULL;
3447 3143d852 2020-06-25 stsp }
3448 3143d852 2020-06-25 stsp
3449 3143d852 2020-06-25 stsp static const struct got_error *
3450 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3451 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3452 3143d852 2020-06-25 stsp {
3453 3143d852 2020-06-25 stsp const struct got_error *err;
3454 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3455 3143d852 2020-06-25 stsp
3456 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3457 3143d852 2020-06-25 stsp ".cvsignore");
3458 3143d852 2020-06-25 stsp if (err)
3459 3143d852 2020-06-25 stsp return err;
3460 3143d852 2020-06-25 stsp
3461 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3462 3143d852 2020-06-25 stsp ".gitignore");
3463 3143d852 2020-06-25 stsp if (err)
3464 3143d852 2020-06-25 stsp return err;
3465 3143d852 2020-06-25 stsp
3466 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3467 3143d852 2020-06-25 stsp if (err) {
3468 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3469 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3470 3143d852 2020-06-25 stsp return err;
3471 3143d852 2020-06-25 stsp }
3472 3143d852 2020-06-25 stsp for (;;) {
3473 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3474 3143d852 2020-06-25 stsp ".cvsignore");
3475 3143d852 2020-06-25 stsp if (err)
3476 3143d852 2020-06-25 stsp break;
3477 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3478 3143d852 2020-06-25 stsp ".gitignore");
3479 3143d852 2020-06-25 stsp if (err)
3480 3143d852 2020-06-25 stsp break;
3481 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3482 3143d852 2020-06-25 stsp if (err) {
3483 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3484 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3485 3143d852 2020-06-25 stsp break;
3486 3143d852 2020-06-25 stsp }
3487 b737c85a 2020-06-26 stsp free(parent_path);
3488 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3489 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3490 3143d852 2020-06-25 stsp }
3491 3143d852 2020-06-25 stsp
3492 b737c85a 2020-06-26 stsp free(parent_path);
3493 b737c85a 2020-06-26 stsp free(next_parent_path);
3494 3143d852 2020-06-25 stsp return err;
3495 abb4604f 2019-07-27 stsp }
3496 abb4604f 2019-07-27 stsp
3497 abb4604f 2019-07-27 stsp static const struct got_error *
3498 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3499 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3500 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3501 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3502 f2a9dc41 2019-12-13 tracey int report_unchanged)
3503 f8d1f275 2019-02-04 stsp {
3504 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3505 6fc93f37 2019-12-13 stsp int fd = -1;
3506 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3507 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3508 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3509 3143d852 2020-06-25 stsp
3510 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3511 f8d1f275 2019-02-04 stsp
3512 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3513 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3514 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3515 8dc303cc 2019-07-27 stsp
3516 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3517 6fc93f37 2019-12-13 stsp if (fd == -1) {
3518 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3519 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3520 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3521 abb4604f 2019-07-27 stsp else
3522 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3523 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3524 f2a9dc41 2019-12-13 tracey report_unchanged);
3525 abb4604f 2019-07-27 stsp } else {
3526 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3527 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3528 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3529 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3530 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3531 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3532 abb4604f 2019-07-27 stsp arg.status_path = path;
3533 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3534 abb4604f 2019-07-27 stsp arg.repo = repo;
3535 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3536 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3537 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3538 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3539 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3540 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3541 022fae89 2019-12-06 tracey if (!no_ignores) {
3542 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3543 3143d852 2020-06-25 stsp worktree->root_path, path);
3544 3143d852 2020-06-25 stsp if (err)
3545 3143d852 2020-06-25 stsp goto done;
3546 022fae89 2019-12-06 tracey }
3547 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3548 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3549 927df6b7 2019-02-10 stsp }
3550 3143d852 2020-06-25 stsp done:
3551 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3552 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3553 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3554 927df6b7 2019-02-10 stsp free(ondisk_path);
3555 347d1d3e 2019-07-12 stsp return err;
3556 347d1d3e 2019-07-12 stsp }
3557 347d1d3e 2019-07-12 stsp
3558 347d1d3e 2019-07-12 stsp const struct got_error *
3559 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3560 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3561 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3562 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3563 347d1d3e 2019-07-12 stsp {
3564 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3565 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3566 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3567 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3568 347d1d3e 2019-07-12 stsp
3569 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3570 347d1d3e 2019-07-12 stsp if (err)
3571 347d1d3e 2019-07-12 stsp return err;
3572 347d1d3e 2019-07-12 stsp
3573 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3574 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3575 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3576 72ea6654 2019-07-27 stsp if (err)
3577 72ea6654 2019-07-27 stsp break;
3578 72ea6654 2019-07-27 stsp }
3579 f8d1f275 2019-02-04 stsp free(fileindex_path);
3580 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3581 f8d1f275 2019-02-04 stsp return err;
3582 f8d1f275 2019-02-04 stsp }
3583 6c7ab921 2019-03-18 stsp
3584 6c7ab921 2019-03-18 stsp const struct got_error *
3585 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3586 6c7ab921 2019-03-18 stsp const char *arg)
3587 6c7ab921 2019-03-18 stsp {
3588 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3589 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3590 6c7ab921 2019-03-18 stsp size_t len;
3591 00bb5ea0 2020-07-23 stsp struct stat sb;
3592 6c7ab921 2019-03-18 stsp
3593 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3594 6c7ab921 2019-03-18 stsp
3595 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3596 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3597 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3598 00bb5ea0 2020-07-23 stsp
3599 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3600 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3601 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3602 00bb5ea0 2020-07-23 stsp goto done;
3603 00bb5ea0 2020-07-23 stsp }
3604 00bb5ea0 2020-07-23 stsp }
3605 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3606 00bb5ea0 2020-07-23 stsp /*
3607 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3608 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3609 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3610 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3611 00bb5ea0 2020-07-23 stsp */
3612 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3613 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3614 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3615 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3616 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3617 00bb5ea0 2020-07-23 stsp goto done;
3618 00bb5ea0 2020-07-23 stsp }
3619 00bb5ea0 2020-07-23 stsp
3620 00bb5ea0 2020-07-23 stsp }
3621 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3622 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3623 00bb5ea0 2020-07-23 stsp if (err)
3624 d0710d08 2019-07-22 stsp goto done;
3625 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3626 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3627 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3628 00bb5ea0 2020-07-23 stsp goto done;
3629 00bb5ea0 2020-07-23 stsp }
3630 00bb5ea0 2020-07-23 stsp } else {
3631 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3632 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3633 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3634 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3635 00bb5ea0 2020-07-23 stsp goto done;
3636 00bb5ea0 2020-07-23 stsp }
3637 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3638 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3639 00bb5ea0 2020-07-23 stsp goto done;
3640 00bb5ea0 2020-07-23 stsp }
3641 d0710d08 2019-07-22 stsp }
3642 d0710d08 2019-07-22 stsp }
3643 6c7ab921 2019-03-18 stsp
3644 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3645 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3646 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3647 6c7ab921 2019-03-18 stsp goto done;
3648 6c7ab921 2019-03-18 stsp }
3649 6c7ab921 2019-03-18 stsp
3650 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3651 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3652 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3653 f6d88e1a 2019-05-29 stsp if (err)
3654 f6d88e1a 2019-05-29 stsp goto done;
3655 f6d88e1a 2019-05-29 stsp } else {
3656 f6d88e1a 2019-05-29 stsp path = strdup("");
3657 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3658 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3659 f6d88e1a 2019-05-29 stsp goto done;
3660 f6d88e1a 2019-05-29 stsp }
3661 6c7ab921 2019-03-18 stsp }
3662 6c7ab921 2019-03-18 stsp
3663 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3664 6c7ab921 2019-03-18 stsp len = strlen(path);
3665 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3666 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3667 6c7ab921 2019-03-18 stsp len--;
3668 6c7ab921 2019-03-18 stsp }
3669 6c7ab921 2019-03-18 stsp done:
3670 6c7ab921 2019-03-18 stsp free(resolved);
3671 d0710d08 2019-07-22 stsp free(cwd);
3672 6c7ab921 2019-03-18 stsp if (err == NULL)
3673 6c7ab921 2019-03-18 stsp *wt_path = path;
3674 6c7ab921 2019-03-18 stsp else
3675 6c7ab921 2019-03-18 stsp free(path);
3676 d00136be 2019-03-26 stsp return err;
3677 d00136be 2019-03-26 stsp }
3678 d00136be 2019-03-26 stsp
3679 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3680 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3681 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3682 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3683 4e68cba3 2019-11-23 stsp void *progress_arg;
3684 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3685 4e68cba3 2019-11-23 stsp };
3686 4e68cba3 2019-11-23 stsp
3687 4e68cba3 2019-11-23 stsp static const struct got_error *
3688 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3689 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3690 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3691 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3692 07f5b47a 2019-06-02 stsp {
3693 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3694 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3695 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3696 6d022e97 2019-08-04 stsp struct stat sb;
3697 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3698 07f5b47a 2019-06-02 stsp
3699 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3700 dbb83fbd 2019-12-12 stsp relpath) == -1)
3701 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3702 dbb83fbd 2019-12-12 stsp
3703 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3704 6d022e97 2019-08-04 stsp if (ie) {
3705 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3706 12463d8b 2019-12-13 stsp de_name, a->repo);
3707 6d022e97 2019-08-04 stsp if (err)
3708 dbb83fbd 2019-12-12 stsp goto done;
3709 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3710 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3711 dbb83fbd 2019-12-12 stsp goto done;
3712 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3713 dbb83fbd 2019-12-12 stsp if (err)
3714 dbb83fbd 2019-12-12 stsp goto done;
3715 6d022e97 2019-08-04 stsp }
3716 07f5b47a 2019-06-02 stsp
3717 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3718 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3719 dbb83fbd 2019-12-12 stsp goto done;
3720 4e68cba3 2019-11-23 stsp }
3721 07f5b47a 2019-06-02 stsp
3722 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3723 4e68cba3 2019-11-23 stsp if (err)
3724 4e68cba3 2019-11-23 stsp goto done;
3725 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3726 3969253a 2020-03-07 stsp if (err) {
3727 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3728 3969253a 2020-03-07 stsp goto done;
3729 3969253a 2020-03-07 stsp }
3730 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3731 07f5b47a 2019-06-02 stsp if (err) {
3732 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3733 4e68cba3 2019-11-23 stsp goto done;
3734 07f5b47a 2019-06-02 stsp }
3735 4e68cba3 2019-11-23 stsp done:
3736 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3737 dbb83fbd 2019-12-12 stsp if (err)
3738 dbb83fbd 2019-12-12 stsp return err;
3739 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3740 dbb83fbd 2019-12-12 stsp return NULL;
3741 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3742 07f5b47a 2019-06-02 stsp }
3743 07f5b47a 2019-06-02 stsp
3744 d00136be 2019-03-26 stsp const struct got_error *
3745 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3746 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3747 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3748 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3749 d00136be 2019-03-26 stsp {
3750 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3751 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3752 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3753 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3754 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3755 d00136be 2019-03-26 stsp
3756 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3757 d00136be 2019-03-26 stsp if (err)
3758 d00136be 2019-03-26 stsp return err;
3759 d00136be 2019-03-26 stsp
3760 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3761 d00136be 2019-03-26 stsp if (err)
3762 d00136be 2019-03-26 stsp goto done;
3763 d00136be 2019-03-26 stsp
3764 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3765 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3766 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3767 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3768 4e68cba3 2019-11-23 stsp saa.repo = repo;
3769 4e68cba3 2019-11-23 stsp
3770 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3771 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3772 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3773 1dd54920 2019-05-11 stsp if (err)
3774 af12c6b9 2019-06-04 stsp break;
3775 1dd54920 2019-05-11 stsp }
3776 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3777 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3778 af12c6b9 2019-06-04 stsp err = sync_err;
3779 d00136be 2019-03-26 stsp done:
3780 fb399478 2019-07-12 stsp free(fileindex_path);
3781 d00136be 2019-03-26 stsp if (fileindex)
3782 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3783 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3784 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3785 d00136be 2019-03-26 stsp err = unlockerr;
3786 6c7ab921 2019-03-18 stsp return err;
3787 6c7ab921 2019-03-18 stsp }
3788 17ed4618 2019-06-02 stsp
3789 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3790 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3791 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3792 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3793 f2a9dc41 2019-12-13 tracey void *progress_arg;
3794 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3795 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3796 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3797 766841c2 2020-08-13 stsp const char *status_codes;
3798 f2a9dc41 2019-12-13 tracey };
3799 f2a9dc41 2019-12-13 tracey
3800 f2a9dc41 2019-12-13 tracey static const struct got_error *
3801 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3802 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3803 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3804 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3805 17ed4618 2019-06-02 stsp {
3806 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3807 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3808 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3809 17ed4618 2019-06-02 stsp struct stat sb;
3810 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3811 17ed4618 2019-06-02 stsp
3812 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3813 17ed4618 2019-06-02 stsp if (ie == NULL)
3814 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3815 2ec1f75b 2019-03-26 stsp
3816 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3817 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3818 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3819 9acbc4fa 2019-08-03 stsp return NULL;
3820 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3821 9acbc4fa 2019-08-03 stsp }
3822 9acbc4fa 2019-08-03 stsp
3823 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3824 f2a9dc41 2019-12-13 tracey relpath) == -1)
3825 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3826 f2a9dc41 2019-12-13 tracey
3827 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3828 12463d8b 2019-12-13 stsp a->repo);
3829 17ed4618 2019-06-02 stsp if (err)
3830 f2a9dc41 2019-12-13 tracey goto done;
3831 17ed4618 2019-06-02 stsp
3832 766841c2 2020-08-13 stsp if (a->status_codes) {
3833 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3834 766841c2 2020-08-13 stsp int i;
3835 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3836 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3837 766841c2 2020-08-13 stsp break;
3838 766841c2 2020-08-13 stsp }
3839 766841c2 2020-08-13 stsp if (i == ncodes) {
3840 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3841 766841c2 2020-08-13 stsp free(ondisk_path);
3842 766841c2 2020-08-13 stsp return NULL;
3843 766841c2 2020-08-13 stsp }
3844 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3845 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3846 766841c2 2020-08-13 stsp static char msg[64];
3847 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3848 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3849 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3850 766841c2 2020-08-13 stsp goto done;
3851 766841c2 2020-08-13 stsp }
3852 766841c2 2020-08-13 stsp }
3853 766841c2 2020-08-13 stsp
3854 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3855 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3856 f2a9dc41 2019-12-13 tracey goto done;
3857 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3858 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3859 f2a9dc41 2019-12-13 tracey goto done;
3860 f2a9dc41 2019-12-13 tracey }
3861 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3862 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3863 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3864 f2a9dc41 2019-12-13 tracey goto done;
3865 f2a9dc41 2019-12-13 tracey }
3866 17ed4618 2019-06-02 stsp }
3867 17ed4618 2019-06-02 stsp
3868 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3869 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3870 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3871 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3872 12463d8b 2019-12-13 stsp ondisk_path);
3873 12463d8b 2019-12-13 stsp goto done;
3874 12463d8b 2019-12-13 stsp }
3875 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3876 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3877 12463d8b 2019-12-13 stsp goto done;
3878 15341bfd 2020-03-05 tracey }
3879 15341bfd 2020-03-05 tracey
3880 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3881 15341bfd 2020-03-05 tracey
3882 15341bfd 2020-03-05 tracey if (parent == NULL) {
3883 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3884 15341bfd 2020-03-05 tracey goto done;
3885 15341bfd 2020-03-05 tracey }
3886 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3887 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3888 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3889 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3890 15341bfd 2020-03-05 tracey parent);
3891 15341bfd 2020-03-05 tracey break;
3892 15341bfd 2020-03-05 tracey }
3893 15341bfd 2020-03-05 tracey parent = dirname(parent);
3894 15341bfd 2020-03-05 tracey if (parent == NULL) {
3895 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3896 15341bfd 2020-03-05 tracey goto done;
3897 15341bfd 2020-03-05 tracey }
3898 12463d8b 2019-12-13 stsp }
3899 f2a9dc41 2019-12-13 tracey }
3900 17ed4618 2019-06-02 stsp
3901 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3902 f2a9dc41 2019-12-13 tracey done:
3903 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3904 f2a9dc41 2019-12-13 tracey if (err)
3905 f2a9dc41 2019-12-13 tracey return err;
3906 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3907 f2a9dc41 2019-12-13 tracey return NULL;
3908 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3909 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3910 17ed4618 2019-06-02 stsp }
3911 17ed4618 2019-06-02 stsp
3912 2ec1f75b 2019-03-26 stsp const struct got_error *
3913 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3914 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3915 766841c2 2020-08-13 stsp const char *status_codes,
3916 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3917 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3918 2ec1f75b 2019-03-26 stsp {
3919 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3920 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3921 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3922 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3923 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3924 2ec1f75b 2019-03-26 stsp
3925 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3926 2ec1f75b 2019-03-26 stsp if (err)
3927 2ec1f75b 2019-03-26 stsp return err;
3928 2ec1f75b 2019-03-26 stsp
3929 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3930 2ec1f75b 2019-03-26 stsp if (err)
3931 2ec1f75b 2019-03-26 stsp goto done;
3932 f2a9dc41 2019-12-13 tracey
3933 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3934 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3935 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3936 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3937 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3938 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3939 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3940 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3941 2ec1f75b 2019-03-26 stsp
3942 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3943 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3944 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3945 17ed4618 2019-06-02 stsp if (err)
3946 af12c6b9 2019-06-04 stsp break;
3947 2ec1f75b 2019-03-26 stsp }
3948 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3949 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3950 af12c6b9 2019-06-04 stsp err = sync_err;
3951 2ec1f75b 2019-03-26 stsp done:
3952 fb399478 2019-07-12 stsp free(fileindex_path);
3953 2ec1f75b 2019-03-26 stsp if (fileindex)
3954 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3955 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3956 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3957 2ec1f75b 2019-03-26 stsp err = unlockerr;
3958 33aa809d 2019-08-08 stsp return err;
3959 33aa809d 2019-08-08 stsp }
3960 33aa809d 2019-08-08 stsp
3961 33aa809d 2019-08-08 stsp static const struct got_error *
3962 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3963 33aa809d 2019-08-08 stsp {
3964 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3965 33aa809d 2019-08-08 stsp char *line = NULL;
3966 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3967 33aa809d 2019-08-08 stsp ssize_t linelen;
3968 33aa809d 2019-08-08 stsp
3969 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3970 33aa809d 2019-08-08 stsp if (linelen == -1) {
3971 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3972 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3973 33aa809d 2019-08-08 stsp goto done;
3974 33aa809d 2019-08-08 stsp }
3975 33aa809d 2019-08-08 stsp return NULL;
3976 33aa809d 2019-08-08 stsp }
3977 33aa809d 2019-08-08 stsp if (outfile) {
3978 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3979 33aa809d 2019-08-08 stsp if (n != linelen) {
3980 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3981 33aa809d 2019-08-08 stsp goto done;
3982 33aa809d 2019-08-08 stsp }
3983 33aa809d 2019-08-08 stsp }
3984 33aa809d 2019-08-08 stsp if (rejectfile) {
3985 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3986 33aa809d 2019-08-08 stsp if (n != linelen)
3987 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3988 33aa809d 2019-08-08 stsp }
3989 33aa809d 2019-08-08 stsp done:
3990 33aa809d 2019-08-08 stsp free(line);
3991 2ec1f75b 2019-03-26 stsp return err;
3992 2ec1f75b 2019-03-26 stsp }
3993 1f1abb7e 2019-08-08 stsp
3994 33aa809d 2019-08-08 stsp static const struct got_error *
3995 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3996 33aa809d 2019-08-08 stsp {
3997 33aa809d 2019-08-08 stsp char *line = NULL;
3998 33aa809d 2019-08-08 stsp size_t linesize = 0;
3999 33aa809d 2019-08-08 stsp ssize_t linelen;
4000 33aa809d 2019-08-08 stsp
4001 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4002 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4003 500467ff 2019-09-25 hiltjo if (ferror(f))
4004 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4005 500467ff 2019-09-25 hiltjo return NULL;
4006 500467ff 2019-09-25 hiltjo }
4007 33aa809d 2019-08-08 stsp free(line);
4008 33aa809d 2019-08-08 stsp return NULL;
4009 33aa809d 2019-08-08 stsp }
4010 33aa809d 2019-08-08 stsp
4011 33aa809d 2019-08-08 stsp static const struct got_error *
4012 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4013 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4014 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4015 33aa809d 2019-08-08 stsp {
4016 33aa809d 2019-08-08 stsp const struct got_error *err;
4017 33aa809d 2019-08-08 stsp
4018 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4019 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4020 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4021 33aa809d 2019-08-08 stsp if (err)
4022 33aa809d 2019-08-08 stsp return err;
4023 33aa809d 2019-08-08 stsp (*line_cur1)++;
4024 33aa809d 2019-08-08 stsp }
4025 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4026 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4027 33aa809d 2019-08-08 stsp if (rejectfile)
4028 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4029 33aa809d 2019-08-08 stsp else
4030 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4031 33aa809d 2019-08-08 stsp if (err)
4032 33aa809d 2019-08-08 stsp return err;
4033 33aa809d 2019-08-08 stsp (*line_cur2)++;
4034 33aa809d 2019-08-08 stsp }
4035 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4036 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4037 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4038 33aa809d 2019-08-08 stsp if (err)
4039 33aa809d 2019-08-08 stsp return err;
4040 33aa809d 2019-08-08 stsp (*line_cur2)++;
4041 33aa809d 2019-08-08 stsp }
4042 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4043 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4044 33aa809d 2019-08-08 stsp if (rejectfile)
4045 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4046 33aa809d 2019-08-08 stsp else
4047 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4048 33aa809d 2019-08-08 stsp if (err)
4049 33aa809d 2019-08-08 stsp return err;
4050 33aa809d 2019-08-08 stsp (*line_cur1)++;
4051 33aa809d 2019-08-08 stsp }
4052 f1e81a05 2019-08-10 stsp
4053 f1e81a05 2019-08-10 stsp return NULL;
4054 f1e81a05 2019-08-10 stsp }
4055 f1e81a05 2019-08-10 stsp
4056 f1e81a05 2019-08-10 stsp static const struct got_error *
4057 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4058 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4059 f1e81a05 2019-08-10 stsp {
4060 f1e81a05 2019-08-10 stsp const struct got_error *err;
4061 f1e81a05 2019-08-10 stsp
4062 f1e81a05 2019-08-10 stsp if (outfile) {
4063 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4064 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4065 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4066 f1e81a05 2019-08-10 stsp if (err)
4067 f1e81a05 2019-08-10 stsp return err;
4068 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4069 f1e81a05 2019-08-10 stsp }
4070 f1e81a05 2019-08-10 stsp }
4071 f1e81a05 2019-08-10 stsp if (rejectfile) {
4072 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4073 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4074 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4075 f1e81a05 2019-08-10 stsp if (err)
4076 f1e81a05 2019-08-10 stsp return err;
4077 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4078 f1e81a05 2019-08-10 stsp }
4079 33aa809d 2019-08-08 stsp }
4080 33aa809d 2019-08-08 stsp
4081 33aa809d 2019-08-08 stsp return NULL;
4082 33aa809d 2019-08-08 stsp }
4083 33aa809d 2019-08-08 stsp
4084 33aa809d 2019-08-08 stsp static const struct got_error *
4085 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4086 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4087 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4088 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4089 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4090 33aa809d 2019-08-08 stsp {
4091 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4092 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4093 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4094 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4095 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4096 33aa809d 2019-08-08 stsp long pos1, pos2;
4097 33aa809d 2019-08-08 stsp FILE *hunkfile;
4098 33aa809d 2019-08-08 stsp
4099 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4100 33aa809d 2019-08-08 stsp
4101 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4102 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4103 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4104 33aa809d 2019-08-08 stsp
4105 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4106 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4107 33aa809d 2019-08-08 stsp
4108 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4109 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4110 33aa809d 2019-08-08 stsp
4111 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4112 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4113 33aa809d 2019-08-08 stsp goto done;
4114 33aa809d 2019-08-08 stsp }
4115 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4116 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4117 33aa809d 2019-08-08 stsp goto done;
4118 33aa809d 2019-08-08 stsp }
4119 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4120 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4121 33aa809d 2019-08-08 stsp goto done;
4122 33aa809d 2019-08-08 stsp }
4123 33aa809d 2019-08-08 stsp
4124 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4125 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4126 33aa809d 2019-08-08 stsp if (err)
4127 33aa809d 2019-08-08 stsp goto done;
4128 33aa809d 2019-08-08 stsp
4129 33aa809d 2019-08-08 stsp switch (*choice) {
4130 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4131 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4132 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4133 33aa809d 2019-08-08 stsp break;
4134 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4135 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4136 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4137 33aa809d 2019-08-08 stsp break;
4138 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4139 33aa809d 2019-08-08 stsp break;
4140 33aa809d 2019-08-08 stsp default:
4141 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4142 33aa809d 2019-08-08 stsp break;
4143 33aa809d 2019-08-08 stsp }
4144 33aa809d 2019-08-08 stsp done:
4145 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4146 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4147 33aa809d 2019-08-08 stsp return err;
4148 33aa809d 2019-08-08 stsp }
4149 33aa809d 2019-08-08 stsp
4150 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4151 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4152 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4153 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4154 1f1abb7e 2019-08-08 stsp void *progress_arg;
4155 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4156 33aa809d 2019-08-08 stsp void *patch_arg;
4157 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4158 1f1abb7e 2019-08-08 stsp };
4159 a129376b 2019-03-28 stsp
4160 e20a8b6f 2019-06-04 stsp static const struct got_error *
4161 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4162 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4163 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4164 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4165 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4166 33aa809d 2019-08-08 stsp {
4167 33aa809d 2019-08-08 stsp const struct got_error *err;
4168 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4169 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4170 1ebedb77 2019-10-19 stsp int fd2 = -1;
4171 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4172 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4173 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4174 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4175 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4176 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4177 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4178 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4179 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4180 33aa809d 2019-08-08 stsp int n = 0;
4181 33aa809d 2019-08-08 stsp
4182 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4183 33aa809d 2019-08-08 stsp
4184 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4185 33aa809d 2019-08-08 stsp if (err)
4186 33aa809d 2019-08-08 stsp return err;
4187 33aa809d 2019-08-08 stsp
4188 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4189 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4190 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4191 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4192 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4193 fa3cef63 2020-07-23 stsp goto done;
4194 fa3cef63 2020-07-23 stsp }
4195 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4196 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4197 fa3cef63 2020-07-23 stsp if (link_len == -1)
4198 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4199 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4200 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4201 12463d8b 2019-12-13 stsp }
4202 12463d8b 2019-12-13 stsp } else {
4203 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4204 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4205 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4206 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4207 fa3cef63 2020-07-23 stsp goto done;
4208 fa3cef63 2020-07-23 stsp }
4209 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4210 fa3cef63 2020-07-23 stsp sizeof(link_target));
4211 fa3cef63 2020-07-23 stsp if (link_len == -1)
4212 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4213 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4214 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4215 12463d8b 2019-12-13 stsp }
4216 1ebedb77 2019-10-19 stsp }
4217 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4218 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4219 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4220 fa3cef63 2020-07-23 stsp goto done;
4221 fa3cef63 2020-07-23 stsp }
4222 1ebedb77 2019-10-19 stsp
4223 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4224 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4225 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4226 fa3cef63 2020-07-23 stsp goto done;
4227 fa3cef63 2020-07-23 stsp }
4228 fa3cef63 2020-07-23 stsp fd2 = -1;
4229 fa3cef63 2020-07-23 stsp } else {
4230 fa3cef63 2020-07-23 stsp size_t n;
4231 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4232 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4233 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4234 fa3cef63 2020-07-23 stsp goto done;
4235 fa3cef63 2020-07-23 stsp }
4236 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4237 fa3cef63 2020-07-23 stsp if (n != link_len) {
4238 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4239 fa3cef63 2020-07-23 stsp goto done;
4240 fa3cef63 2020-07-23 stsp }
4241 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4242 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4243 fa3cef63 2020-07-23 stsp goto done;
4244 fa3cef63 2020-07-23 stsp }
4245 fa3cef63 2020-07-23 stsp rewind(f2);
4246 33aa809d 2019-08-08 stsp }
4247 33aa809d 2019-08-08 stsp
4248 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4249 33aa809d 2019-08-08 stsp if (err)
4250 33aa809d 2019-08-08 stsp goto done;
4251 33aa809d 2019-08-08 stsp
4252 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4253 33aa809d 2019-08-08 stsp if (err)
4254 33aa809d 2019-08-08 stsp goto done;
4255 33aa809d 2019-08-08 stsp
4256 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4257 33aa809d 2019-08-08 stsp if (err)
4258 33aa809d 2019-08-08 stsp goto done;
4259 33aa809d 2019-08-08 stsp
4260 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4261 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4262 33aa809d 2019-08-08 stsp goto done;
4263 33aa809d 2019-08-08 stsp }
4264 33aa809d 2019-08-08 stsp
4265 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4266 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4267 33aa809d 2019-08-08 stsp if (err)
4268 33aa809d 2019-08-08 stsp goto done;
4269 33aa809d 2019-08-08 stsp
4270 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4271 33aa809d 2019-08-08 stsp if (err)
4272 33aa809d 2019-08-08 stsp goto done;
4273 33aa809d 2019-08-08 stsp
4274 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4275 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4276 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4277 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4278 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4279 33aa809d 2019-08-08 stsp int choice;
4280 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4281 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4282 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4283 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4284 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4285 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4286 33aa809d 2019-08-08 stsp if (err)
4287 33aa809d 2019-08-08 stsp goto done;
4288 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4289 33aa809d 2019-08-08 stsp have_content = 1;
4290 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4291 33aa809d 2019-08-08 stsp break;
4292 33aa809d 2019-08-08 stsp }
4293 1ebedb77 2019-10-19 stsp if (have_content) {
4294 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4295 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4296 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4297 1ebedb77 2019-10-19 stsp if (err)
4298 1ebedb77 2019-10-19 stsp goto done;
4299 1ebedb77 2019-10-19 stsp
4300 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4301 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4302 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4303 fa3cef63 2020-07-23 stsp goto done;
4304 fa3cef63 2020-07-23 stsp }
4305 1ebedb77 2019-10-19 stsp }
4306 1ebedb77 2019-10-19 stsp }
4307 33aa809d 2019-08-08 stsp done:
4308 33aa809d 2019-08-08 stsp free(id_str);
4309 33aa809d 2019-08-08 stsp if (blob)
4310 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4311 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4312 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4313 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4314 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4315 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4316 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4317 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4318 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4319 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4320 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4321 33aa809d 2019-08-08 stsp if (err || !have_content) {
4322 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4323 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4324 33aa809d 2019-08-08 stsp free(*path_outfile);
4325 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4326 33aa809d 2019-08-08 stsp }
4327 33aa809d 2019-08-08 stsp free(args);
4328 33aa809d 2019-08-08 stsp if (ds) {
4329 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4330 33aa809d 2019-08-08 stsp free(ds);
4331 33aa809d 2019-08-08 stsp }
4332 33aa809d 2019-08-08 stsp if (changes)
4333 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4334 33aa809d 2019-08-08 stsp free(path1);
4335 33aa809d 2019-08-08 stsp return err;
4336 33aa809d 2019-08-08 stsp }
4337 33aa809d 2019-08-08 stsp
4338 33aa809d 2019-08-08 stsp static const struct got_error *
4339 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4340 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4341 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4342 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4343 a129376b 2019-03-28 stsp {
4344 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4345 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4346 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4347 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4348 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4349 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4350 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4351 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4352 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4353 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4354 a129376b 2019-03-28 stsp
4355 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4356 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4357 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4358 d3bcc3d1 2019-08-08 stsp return NULL;
4359 3d69ad8d 2019-08-17 semarie
4360 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4361 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4362 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4363 d3bcc3d1 2019-08-08 stsp
4364 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4365 65084dad 2019-08-08 stsp if (ie == NULL)
4366 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4367 a129376b 2019-03-28 stsp
4368 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4369 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4370 a129376b 2019-03-28 stsp if (err) {
4371 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4372 a129376b 2019-03-28 stsp goto done;
4373 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4374 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4375 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4376 e20a8b6f 2019-06-04 stsp goto done;
4377 e20a8b6f 2019-06-04 stsp }
4378 a129376b 2019-03-28 stsp }
4379 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4380 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4381 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4382 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4383 a129376b 2019-03-28 stsp goto done;
4384 a129376b 2019-03-28 stsp }
4385 a129376b 2019-03-28 stsp } else {
4386 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4387 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4388 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4389 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4390 a129376b 2019-03-28 stsp goto done;
4391 a129376b 2019-03-28 stsp }
4392 a129376b 2019-03-28 stsp } else {
4393 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4394 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4395 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4396 a129376b 2019-03-28 stsp goto done;
4397 a129376b 2019-03-28 stsp }
4398 a129376b 2019-03-28 stsp }
4399 a129376b 2019-03-28 stsp }
4400 a129376b 2019-03-28 stsp
4401 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4402 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4403 a9fa2909 2019-07-27 stsp if (err) {
4404 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4405 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4406 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4407 a9fa2909 2019-07-27 stsp goto done;
4408 a9fa2909 2019-07-27 stsp } else {
4409 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4410 a9fa2909 2019-07-27 stsp if (err)
4411 a9fa2909 2019-07-27 stsp goto done;
4412 a9fa2909 2019-07-27 stsp
4413 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4414 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4415 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4416 a9fa2909 2019-07-27 stsp goto done;
4417 a9fa2909 2019-07-27 stsp }
4418 a9fa2909 2019-07-27 stsp
4419 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4420 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4421 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4422 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4423 a9fa2909 2019-07-27 stsp goto done;
4424 a9fa2909 2019-07-27 stsp }
4425 a129376b 2019-03-28 stsp }
4426 a129376b 2019-03-28 stsp
4427 a129376b 2019-03-28 stsp switch (status) {
4428 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4429 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4430 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4431 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4432 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4433 33aa809d 2019-08-08 stsp if (err)
4434 33aa809d 2019-08-08 stsp goto done;
4435 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4436 33aa809d 2019-08-08 stsp break;
4437 33aa809d 2019-08-08 stsp }
4438 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4439 1f1abb7e 2019-08-08 stsp ie->path);
4440 1ee397ad 2019-07-12 stsp if (err)
4441 1ee397ad 2019-07-12 stsp goto done;
4442 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4443 a129376b 2019-03-28 stsp break;
4444 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4445 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4446 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4447 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4448 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4449 33aa809d 2019-08-08 stsp if (err)
4450 33aa809d 2019-08-08 stsp goto done;
4451 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4452 33aa809d 2019-08-08 stsp break;
4453 33aa809d 2019-08-08 stsp }
4454 33aa809d 2019-08-08 stsp /* fall through */
4455 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4456 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4457 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4458 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4459 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4460 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4461 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4462 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4463 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4464 24278f30 2019-08-03 stsp } else
4465 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4466 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4467 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4468 a129376b 2019-03-28 stsp if (err)
4469 65084dad 2019-08-08 stsp goto done;
4470 65084dad 2019-08-08 stsp
4471 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4472 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4473 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4474 a129376b 2019-03-28 stsp goto done;
4475 65084dad 2019-08-08 stsp }
4476 65084dad 2019-08-08 stsp
4477 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4478 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4479 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4480 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4481 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4482 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4483 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4484 33aa809d 2019-08-08 stsp break;
4485 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4486 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4487 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4488 369fd7e5 2020-07-23 stsp path_content);
4489 369fd7e5 2020-07-23 stsp break;
4490 369fd7e5 2020-07-23 stsp }
4491 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4492 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4493 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4494 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4495 369fd7e5 2020-07-23 stsp } else {
4496 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4497 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4498 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4499 369fd7e5 2020-07-23 stsp goto done;
4500 369fd7e5 2020-07-23 stsp }
4501 33aa809d 2019-08-08 stsp }
4502 33aa809d 2019-08-08 stsp } else {
4503 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4504 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4505 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4506 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4507 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4508 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4509 2e1fa222 2020-07-23 stsp } else {
4510 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4511 2e1fa222 2020-07-23 stsp ie->path,
4512 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4513 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4514 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4515 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4516 2e1fa222 2020-07-23 stsp }
4517 a129376b 2019-03-28 stsp if (err)
4518 a129376b 2019-03-28 stsp goto done;
4519 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4520 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4521 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4522 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4523 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4524 2e1fa222 2020-07-23 stsp if (err)
4525 2e1fa222 2020-07-23 stsp goto done;
4526 2e1fa222 2020-07-23 stsp }
4527 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4528 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4529 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4530 33aa809d 2019-08-08 stsp }
4531 a129376b 2019-03-28 stsp }
4532 a129376b 2019-03-28 stsp break;
4533 e20a8b6f 2019-06-04 stsp }
4534 a129376b 2019-03-28 stsp default:
4535 1f1abb7e 2019-08-08 stsp break;
4536 a129376b 2019-03-28 stsp }
4537 a129376b 2019-03-28 stsp done:
4538 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4539 33aa809d 2019-08-08 stsp free(path_content);
4540 e20a8b6f 2019-06-04 stsp free(parent_path);
4541 a129376b 2019-03-28 stsp free(tree_path);
4542 a129376b 2019-03-28 stsp if (blob)
4543 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4544 a129376b 2019-03-28 stsp if (tree)
4545 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4546 a129376b 2019-03-28 stsp free(tree_id);
4547 e20a8b6f 2019-06-04 stsp return err;
4548 e20a8b6f 2019-06-04 stsp }
4549 e20a8b6f 2019-06-04 stsp
4550 e20a8b6f 2019-06-04 stsp const struct got_error *
4551 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4552 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4553 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4554 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4555 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4556 e20a8b6f 2019-06-04 stsp {
4557 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4558 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4559 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4560 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4561 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4562 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4563 e20a8b6f 2019-06-04 stsp
4564 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4565 e20a8b6f 2019-06-04 stsp if (err)
4566 e20a8b6f 2019-06-04 stsp return err;
4567 e20a8b6f 2019-06-04 stsp
4568 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4569 e20a8b6f 2019-06-04 stsp if (err)
4570 e20a8b6f 2019-06-04 stsp goto done;
4571 e20a8b6f 2019-06-04 stsp
4572 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4573 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4574 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4575 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4576 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4577 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4578 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4579 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4580 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4581 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4582 e20a8b6f 2019-06-04 stsp if (err)
4583 af12c6b9 2019-06-04 stsp break;
4584 e20a8b6f 2019-06-04 stsp }
4585 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4586 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4587 e20a8b6f 2019-06-04 stsp err = sync_err;
4588 af12c6b9 2019-06-04 stsp done:
4589 fb399478 2019-07-12 stsp free(fileindex_path);
4590 a129376b 2019-03-28 stsp if (fileindex)
4591 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4592 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4593 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4594 a129376b 2019-03-28 stsp err = unlockerr;
4595 c4296144 2019-05-09 stsp return err;
4596 c4296144 2019-05-09 stsp }
4597 c4296144 2019-05-09 stsp
4598 cf066bf8 2019-05-09 stsp static void
4599 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4600 cf066bf8 2019-05-09 stsp {
4601 24519714 2019-05-09 stsp free(ct->path);
4602 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4603 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4604 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4605 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4606 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4607 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4608 cf066bf8 2019-05-09 stsp free(ct);
4609 cf066bf8 2019-05-09 stsp }
4610 24519714 2019-05-09 stsp
4611 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4612 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4613 24519714 2019-05-09 stsp struct got_repository *repo;
4614 24519714 2019-05-09 stsp struct got_worktree *worktree;
4615 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4616 5f8a88c6 2019-08-03 stsp int have_staged_files;
4617 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4618 24519714 2019-05-09 stsp };
4619 cf066bf8 2019-05-09 stsp
4620 c4296144 2019-05-09 stsp static const struct got_error *
4621 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4622 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4623 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4624 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4625 c4296144 2019-05-09 stsp {
4626 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4627 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4628 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4629 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4630 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4631 768aea60 2019-05-09 stsp struct stat sb;
4632 c4296144 2019-05-09 stsp
4633 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4634 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4635 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4636 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4637 5f8a88c6 2019-08-03 stsp return NULL;
4638 5f8a88c6 2019-08-03 stsp } else {
4639 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4640 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4641 c4296144 2019-05-09 stsp
4642 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4643 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4644 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4645 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4646 5f8a88c6 2019-08-03 stsp return NULL;
4647 5f8a88c6 2019-08-03 stsp }
4648 0b5cc0d6 2019-05-09 stsp
4649 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4650 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4651 036813ee 2019-05-09 stsp goto done;
4652 036813ee 2019-05-09 stsp }
4653 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4654 036813ee 2019-05-09 stsp parent_path = strdup("");
4655 69960a46 2019-05-09 stsp if (parent_path == NULL)
4656 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4657 69960a46 2019-05-09 stsp } else {
4658 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4659 69960a46 2019-05-09 stsp if (err)
4660 69960a46 2019-05-09 stsp return err;
4661 69960a46 2019-05-09 stsp }
4662 c4296144 2019-05-09 stsp
4663 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4664 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4665 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4666 c4296144 2019-05-09 stsp goto done;
4667 768aea60 2019-05-09 stsp }
4668 768aea60 2019-05-09 stsp
4669 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4670 768aea60 2019-05-09 stsp relpath) == -1) {
4671 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4672 768aea60 2019-05-09 stsp goto done;
4673 768aea60 2019-05-09 stsp }
4674 0aeb8099 2020-07-23 stsp
4675 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4676 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4677 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4678 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4679 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4680 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4681 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4682 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4683 0aeb8099 2020-07-23 stsp break;
4684 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4685 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4686 0aeb8099 2020-07-23 stsp break;
4687 0aeb8099 2020-07-23 stsp default:
4688 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4689 0aeb8099 2020-07-23 stsp goto done;
4690 0aeb8099 2020-07-23 stsp }
4691 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4692 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4693 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4694 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4695 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4696 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4697 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4698 12463d8b 2019-12-13 stsp ct->ondisk_path);
4699 12463d8b 2019-12-13 stsp goto done;
4700 12463d8b 2019-12-13 stsp }
4701 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4702 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4703 768aea60 2019-05-09 stsp goto done;
4704 768aea60 2019-05-09 stsp }
4705 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4706 c4296144 2019-05-09 stsp }
4707 c4296144 2019-05-09 stsp
4708 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4709 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4710 44d03001 2019-05-09 stsp relpath) == -1) {
4711 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4712 44d03001 2019-05-09 stsp goto done;
4713 44d03001 2019-05-09 stsp }
4714 44d03001 2019-05-09 stsp
4715 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4716 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4717 35213c7c 2020-07-23 stsp int is_bad_symlink;
4718 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4719 35213c7c 2020-07-23 stsp ssize_t target_len;
4720 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4721 35213c7c 2020-07-23 stsp sizeof(target_path));
4722 35213c7c 2020-07-23 stsp if (target_len == -1) {
4723 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4724 35213c7c 2020-07-23 stsp ct->ondisk_path);
4725 35213c7c 2020-07-23 stsp goto done;
4726 35213c7c 2020-07-23 stsp }
4727 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4728 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4729 35213c7c 2020-07-23 stsp if (err)
4730 35213c7c 2020-07-23 stsp goto done;
4731 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4732 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4733 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4734 35213c7c 2020-07-23 stsp goto done;
4735 35213c7c 2020-07-23 stsp }
4736 35213c7c 2020-07-23 stsp }
4737 35213c7c 2020-07-23 stsp
4738 35213c7c 2020-07-23 stsp
4739 cf066bf8 2019-05-09 stsp ct->status = status;
4740 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4741 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4742 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4743 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4744 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4745 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4746 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4747 b416585c 2019-05-13 stsp goto done;
4748 b416585c 2019-05-13 stsp }
4749 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4750 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4751 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4752 f0b75401 2019-08-03 stsp goto done;
4753 f0b75401 2019-08-03 stsp }
4754 f0b75401 2019-08-03 stsp }
4755 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4756 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4757 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4758 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4759 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4760 036813ee 2019-05-09 stsp goto done;
4761 036813ee 2019-05-09 stsp }
4762 ca2503ea 2019-05-09 stsp }
4763 24519714 2019-05-09 stsp ct->path = strdup(path);
4764 24519714 2019-05-09 stsp if (ct->path == NULL) {
4765 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4766 24519714 2019-05-09 stsp goto done;
4767 24519714 2019-05-09 stsp }
4768 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4769 c4296144 2019-05-09 stsp done:
4770 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4771 ed175427 2019-05-09 stsp free_commitable(ct);
4772 24519714 2019-05-09 stsp free(parent_path);
4773 036813ee 2019-05-09 stsp free(path);
4774 a129376b 2019-03-28 stsp return err;
4775 a129376b 2019-03-28 stsp }
4776 c4296144 2019-05-09 stsp
4777 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4778 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4779 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4780 036813ee 2019-05-09 stsp struct got_repository *);
4781 ed175427 2019-05-09 stsp
4782 ed175427 2019-05-09 stsp static const struct got_error *
4783 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4784 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4785 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4786 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4787 afa376bf 2019-05-09 stsp struct got_repository *repo)
4788 ed175427 2019-05-09 stsp {
4789 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4790 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4791 036813ee 2019-05-09 stsp char *subpath;
4792 ed175427 2019-05-09 stsp
4793 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4794 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4795 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4796 ed175427 2019-05-09 stsp
4797 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4798 ed175427 2019-05-09 stsp if (err)
4799 ed175427 2019-05-09 stsp return err;
4800 ed175427 2019-05-09 stsp
4801 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4802 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4803 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4804 036813ee 2019-05-09 stsp free(subpath);
4805 036813ee 2019-05-09 stsp return err;
4806 036813ee 2019-05-09 stsp }
4807 ed175427 2019-05-09 stsp
4808 036813ee 2019-05-09 stsp static const struct got_error *
4809 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4810 036813ee 2019-05-09 stsp {
4811 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4812 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4813 ed175427 2019-05-09 stsp
4814 036813ee 2019-05-09 stsp *match = 0;
4815 ed175427 2019-05-09 stsp
4816 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4817 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4818 0f63689d 2019-05-10 stsp return NULL;
4819 036813ee 2019-05-09 stsp }
4820 0b5cc0d6 2019-05-09 stsp
4821 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4822 0f63689d 2019-05-10 stsp if (err)
4823 0f63689d 2019-05-10 stsp return err;
4824 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4825 036813ee 2019-05-09 stsp free(ct_parent_path);
4826 036813ee 2019-05-09 stsp return err;
4827 036813ee 2019-05-09 stsp }
4828 0b5cc0d6 2019-05-09 stsp
4829 768aea60 2019-05-09 stsp static mode_t
4830 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4831 768aea60 2019-05-09 stsp {
4832 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4833 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4834 3d9a4ec4 2020-07-23 stsp
4835 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4836 768aea60 2019-05-09 stsp }
4837 768aea60 2019-05-09 stsp
4838 036813ee 2019-05-09 stsp static const struct got_error *
4839 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4840 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4841 036813ee 2019-05-09 stsp {
4842 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4843 ca2503ea 2019-05-09 stsp
4844 036813ee 2019-05-09 stsp *new_te = NULL;
4845 0b5cc0d6 2019-05-09 stsp
4846 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4847 036813ee 2019-05-09 stsp if (err)
4848 036813ee 2019-05-09 stsp goto done;
4849 0b5cc0d6 2019-05-09 stsp
4850 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4851 036813ee 2019-05-09 stsp
4852 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4853 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4854 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4855 5f8a88c6 2019-08-03 stsp else
4856 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4857 036813ee 2019-05-09 stsp done:
4858 036813ee 2019-05-09 stsp if (err && *new_te) {
4859 56e0773d 2019-11-28 stsp free(*new_te);
4860 036813ee 2019-05-09 stsp *new_te = NULL;
4861 036813ee 2019-05-09 stsp }
4862 036813ee 2019-05-09 stsp return err;
4863 036813ee 2019-05-09 stsp }
4864 036813ee 2019-05-09 stsp
4865 036813ee 2019-05-09 stsp static const struct got_error *
4866 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4867 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4868 036813ee 2019-05-09 stsp {
4869 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4870 036813ee 2019-05-09 stsp char *ct_name;
4871 036813ee 2019-05-09 stsp
4872 036813ee 2019-05-09 stsp *new_te = NULL;
4873 036813ee 2019-05-09 stsp
4874 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4875 036813ee 2019-05-09 stsp if (*new_te == NULL)
4876 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4877 036813ee 2019-05-09 stsp
4878 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4879 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4880 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4881 036813ee 2019-05-09 stsp goto done;
4882 036813ee 2019-05-09 stsp }
4883 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4884 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4885 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4886 036813ee 2019-05-09 stsp goto done;
4887 036813ee 2019-05-09 stsp }
4888 036813ee 2019-05-09 stsp
4889 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4890 036813ee 2019-05-09 stsp
4891 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4892 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4893 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4894 5f8a88c6 2019-08-03 stsp else
4895 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4896 036813ee 2019-05-09 stsp done:
4897 036813ee 2019-05-09 stsp if (err && *new_te) {
4898 56e0773d 2019-11-28 stsp free(*new_te);
4899 036813ee 2019-05-09 stsp *new_te = NULL;
4900 036813ee 2019-05-09 stsp }
4901 036813ee 2019-05-09 stsp return err;
4902 036813ee 2019-05-09 stsp }
4903 036813ee 2019-05-09 stsp
4904 036813ee 2019-05-09 stsp static const struct got_error *
4905 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4906 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4907 036813ee 2019-05-09 stsp {
4908 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4909 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4910 036813ee 2019-05-09 stsp
4911 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4912 036813ee 2019-05-09 stsp if (err)
4913 036813ee 2019-05-09 stsp return err;
4914 036813ee 2019-05-09 stsp if (new_pe == NULL)
4915 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4916 036813ee 2019-05-09 stsp return NULL;
4917 afa376bf 2019-05-09 stsp }
4918 afa376bf 2019-05-09 stsp
4919 afa376bf 2019-05-09 stsp static const struct got_error *
4920 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4921 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4922 afa376bf 2019-05-09 stsp {
4923 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4924 5f8a88c6 2019-08-03 stsp unsigned char status;
4925 5f8a88c6 2019-08-03 stsp
4926 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4927 afa376bf 2019-05-09 stsp ct_path++;
4928 5f8a88c6 2019-08-03 stsp
4929 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4930 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4931 5f8a88c6 2019-08-03 stsp else
4932 5f8a88c6 2019-08-03 stsp status = ct->status;
4933 5f8a88c6 2019-08-03 stsp
4934 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4935 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4936 036813ee 2019-05-09 stsp }
4937 036813ee 2019-05-09 stsp
4938 036813ee 2019-05-09 stsp static const struct got_error *
4939 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4940 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4941 44d03001 2019-05-09 stsp {
4942 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4943 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4944 44d03001 2019-05-09 stsp char *te_path;
4945 44d03001 2019-05-09 stsp
4946 44d03001 2019-05-09 stsp *modified = 0;
4947 44d03001 2019-05-09 stsp
4948 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4949 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4950 44d03001 2019-05-09 stsp te->name) == -1)
4951 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4952 44d03001 2019-05-09 stsp
4953 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4954 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4955 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4956 44d03001 2019-05-09 stsp strlen(te_path));
4957 44d03001 2019-05-09 stsp if (*modified)
4958 44d03001 2019-05-09 stsp break;
4959 44d03001 2019-05-09 stsp }
4960 44d03001 2019-05-09 stsp
4961 44d03001 2019-05-09 stsp free(te_path);
4962 44d03001 2019-05-09 stsp return err;
4963 44d03001 2019-05-09 stsp }
4964 44d03001 2019-05-09 stsp
4965 44d03001 2019-05-09 stsp static const struct got_error *
4966 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4967 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4968 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4969 036813ee 2019-05-09 stsp {
4970 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4971 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4972 036813ee 2019-05-09 stsp
4973 036813ee 2019-05-09 stsp *ctp = NULL;
4974 036813ee 2019-05-09 stsp
4975 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4976 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4977 036813ee 2019-05-09 stsp char *ct_name = NULL;
4978 036813ee 2019-05-09 stsp int path_matches;
4979 036813ee 2019-05-09 stsp
4980 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4981 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4982 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4983 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4984 5f8a88c6 2019-08-03 stsp continue;
4985 5f8a88c6 2019-08-03 stsp } else {
4986 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4987 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4988 5f8a88c6 2019-08-03 stsp continue;
4989 5f8a88c6 2019-08-03 stsp }
4990 036813ee 2019-05-09 stsp
4991 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4992 036813ee 2019-05-09 stsp continue;
4993 036813ee 2019-05-09 stsp
4994 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4995 036813ee 2019-05-09 stsp if (err)
4996 036813ee 2019-05-09 stsp return err;
4997 036813ee 2019-05-09 stsp if (!path_matches)
4998 036813ee 2019-05-09 stsp continue;
4999 036813ee 2019-05-09 stsp
5000 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
5001 036813ee 2019-05-09 stsp if (ct_name == NULL)
5002 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
5003 036813ee 2019-05-09 stsp
5004 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
5005 036813ee 2019-05-09 stsp continue;
5006 036813ee 2019-05-09 stsp
5007 036813ee 2019-05-09 stsp *ctp = ct;
5008 036813ee 2019-05-09 stsp break;
5009 036813ee 2019-05-09 stsp }
5010 2b496619 2019-07-10 stsp
5011 2b496619 2019-07-10 stsp return err;
5012 2b496619 2019-07-10 stsp }
5013 2b496619 2019-07-10 stsp
5014 2b496619 2019-07-10 stsp static const struct got_error *
5015 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5016 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5017 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5018 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5019 2b496619 2019-07-10 stsp struct got_repository *repo)
5020 2b496619 2019-07-10 stsp {
5021 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5022 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5023 2b496619 2019-07-10 stsp char *subtree_path;
5024 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5025 ba580f68 2020-03-22 stsp int nentries;
5026 2b496619 2019-07-10 stsp
5027 2b496619 2019-07-10 stsp *new_tep = NULL;
5028 2b496619 2019-07-10 stsp
5029 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5030 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5031 2b496619 2019-07-10 stsp child_path) == -1)
5032 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5033 036813ee 2019-05-09 stsp
5034 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5035 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5036 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5037 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5038 56e0773d 2019-11-28 stsp
5039 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5040 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5041 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5042 2b496619 2019-07-10 stsp goto done;
5043 2b496619 2019-07-10 stsp }
5044 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5045 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5046 2b496619 2019-07-10 stsp if (err) {
5047 56e0773d 2019-11-28 stsp free(new_te);
5048 2b496619 2019-07-10 stsp goto done;
5049 2b496619 2019-07-10 stsp }
5050 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5051 2b496619 2019-07-10 stsp done:
5052 56e0773d 2019-11-28 stsp free(id);
5053 2b496619 2019-07-10 stsp free(subtree_path);
5054 2b496619 2019-07-10 stsp if (err == NULL)
5055 2b496619 2019-07-10 stsp *new_tep = new_te;
5056 036813ee 2019-05-09 stsp return err;
5057 036813ee 2019-05-09 stsp }
5058 036813ee 2019-05-09 stsp
5059 036813ee 2019-05-09 stsp static const struct got_error *
5060 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5061 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5062 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5063 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5064 036813ee 2019-05-09 stsp struct got_repository *repo)
5065 036813ee 2019-05-09 stsp {
5066 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5067 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5068 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5069 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5070 036813ee 2019-05-09 stsp
5071 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5072 ba580f68 2020-03-22 stsp *nentries = 0;
5073 036813ee 2019-05-09 stsp
5074 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5075 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5076 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5077 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5078 036813ee 2019-05-09 stsp
5079 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5080 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5081 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5082 036813ee 2019-05-09 stsp continue;
5083 036813ee 2019-05-09 stsp
5084 f2b0a8b0 2020-07-31 stsp if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5085 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
5086 036813ee 2019-05-09 stsp continue;
5087 036813ee 2019-05-09 stsp
5088 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5089 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5090 036813ee 2019-05-09 stsp if (err)
5091 036813ee 2019-05-09 stsp goto done;
5092 036813ee 2019-05-09 stsp
5093 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5094 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5095 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5096 036813ee 2019-05-09 stsp if (err)
5097 036813ee 2019-05-09 stsp goto done;
5098 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5099 afa376bf 2019-05-09 stsp if (err)
5100 afa376bf 2019-05-09 stsp goto done;
5101 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5102 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5103 2b496619 2019-07-10 stsp if (err)
5104 2f51b5b3 2019-05-09 stsp goto done;
5105 ba580f68 2020-03-22 stsp (*nentries)++;
5106 2b496619 2019-07-10 stsp } else {
5107 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5108 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5109 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5110 2b496619 2019-07-10 stsp == NULL) {
5111 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5112 2b496619 2019-07-10 stsp child_path, path_base_tree,
5113 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5114 2b496619 2019-07-10 stsp repo);
5115 2b496619 2019-07-10 stsp if (err)
5116 2b496619 2019-07-10 stsp goto done;
5117 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5118 2b496619 2019-07-10 stsp if (err)
5119 2b496619 2019-07-10 stsp goto done;
5120 ba580f68 2020-03-22 stsp (*nentries)++;
5121 9ba0479c 2019-05-10 stsp }
5122 ed175427 2019-05-09 stsp }
5123 2f51b5b3 2019-05-09 stsp }
5124 2f51b5b3 2019-05-09 stsp
5125 2f51b5b3 2019-05-09 stsp if (base_tree) {
5126 56e0773d 2019-11-28 stsp int i, nbase_entries;
5127 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5128 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5129 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5130 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5131 63c5ca5d 2019-08-24 stsp
5132 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5133 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5134 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5135 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5136 63c5ca5d 2019-08-24 stsp if (err)
5137 63c5ca5d 2019-08-24 stsp goto done;
5138 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5139 63c5ca5d 2019-08-24 stsp if (err)
5140 63c5ca5d 2019-08-24 stsp goto done;
5141 ba580f68 2020-03-22 stsp (*nentries)++;
5142 63c5ca5d 2019-08-24 stsp continue;
5143 63c5ca5d 2019-08-24 stsp }
5144 2f51b5b3 2019-05-09 stsp
5145 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5146 44d03001 2019-05-09 stsp int modified;
5147 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5148 036813ee 2019-05-09 stsp if (err)
5149 036813ee 2019-05-09 stsp goto done;
5150 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5151 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5152 2f51b5b3 2019-05-09 stsp if (err)
5153 2f51b5b3 2019-05-09 stsp goto done;
5154 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5155 44d03001 2019-05-09 stsp if (modified) {
5156 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5157 ba580f68 2020-03-22 stsp int nsubentries;
5158 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5159 ba580f68 2020-03-22 stsp &nsubentries, te,
5160 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5161 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5162 44d03001 2019-05-09 stsp if (err)
5163 44d03001 2019-05-09 stsp goto done;
5164 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5165 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5166 ba580f68 2020-03-22 stsp free(new_id);
5167 ba580f68 2020-03-22 stsp continue;
5168 ba580f68 2020-03-22 stsp }
5169 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5170 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5171 56e0773d 2019-11-28 stsp free(new_id);
5172 44d03001 2019-05-09 stsp }
5173 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5174 036813ee 2019-05-09 stsp if (err)
5175 036813ee 2019-05-09 stsp goto done;
5176 ba580f68 2020-03-22 stsp (*nentries)++;
5177 2f51b5b3 2019-05-09 stsp continue;
5178 036813ee 2019-05-09 stsp }
5179 2f51b5b3 2019-05-09 stsp
5180 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5181 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5182 f66c734c 2019-09-22 stsp if (err)
5183 f66c734c 2019-09-22 stsp goto done;
5184 2f51b5b3 2019-05-09 stsp if (ct) {
5185 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5186 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5187 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5188 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5189 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5190 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5191 2f51b5b3 2019-05-09 stsp if (err)
5192 2f51b5b3 2019-05-09 stsp goto done;
5193 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5194 2f51b5b3 2019-05-09 stsp if (err)
5195 2f51b5b3 2019-05-09 stsp goto done;
5196 ba580f68 2020-03-22 stsp (*nentries)++;
5197 2f51b5b3 2019-05-09 stsp }
5198 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5199 b416585c 2019-05-13 stsp status_arg);
5200 afa376bf 2019-05-09 stsp if (err)
5201 afa376bf 2019-05-09 stsp goto done;
5202 2f51b5b3 2019-05-09 stsp } else {
5203 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5204 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5205 2f51b5b3 2019-05-09 stsp if (err)
5206 2f51b5b3 2019-05-09 stsp goto done;
5207 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5208 2f51b5b3 2019-05-09 stsp if (err)
5209 2f51b5b3 2019-05-09 stsp goto done;
5210 ba580f68 2020-03-22 stsp (*nentries)++;
5211 2f51b5b3 2019-05-09 stsp }
5212 ca2503ea 2019-05-09 stsp }
5213 ed175427 2019-05-09 stsp }
5214 0b5cc0d6 2019-05-09 stsp
5215 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5216 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5217 ed175427 2019-05-09 stsp done:
5218 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5219 2e1fa222 2020-07-23 stsp return err;
5220 2e1fa222 2020-07-23 stsp }
5221 2e1fa222 2020-07-23 stsp
5222 2e1fa222 2020-07-23 stsp static const struct got_error *
5223 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5224 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5225 72fd46fa 2019-09-06 stsp int have_staged_files)
5226 ebf99748 2019-05-09 stsp {
5227 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5228 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5229 ebf99748 2019-05-09 stsp
5230 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5231 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5232 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5233 ebf99748 2019-05-09 stsp
5234 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5235 ebf99748 2019-05-09 stsp if (ie) {
5236 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5237 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5238 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5239 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5240 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5241 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5242 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5243 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5244 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5245 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5246 72fd46fa 2019-09-06 stsp !have_staged_files);
5247 ebf99748 2019-05-09 stsp } else
5248 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5249 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5250 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5251 72fd46fa 2019-09-06 stsp !have_staged_files);
5252 ebf99748 2019-05-09 stsp } else {
5253 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5254 ebf99748 2019-05-09 stsp if (err)
5255 af12c6b9 2019-06-04 stsp break;
5256 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5257 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5258 3969253a 2020-03-07 stsp if (err) {
5259 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5260 3969253a 2020-03-07 stsp break;
5261 3969253a 2020-03-07 stsp }
5262 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5263 3969253a 2020-03-07 stsp if (err) {
5264 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5265 af12c6b9 2019-06-04 stsp break;
5266 3969253a 2020-03-07 stsp }
5267 ebf99748 2019-05-09 stsp }
5268 ebf99748 2019-05-09 stsp }
5269 d56d26ce 2019-05-10 stsp return err;
5270 d56d26ce 2019-05-10 stsp }
5271 735ef5ac 2019-08-03 stsp
5272 d56d26ce 2019-05-10 stsp
5273 d56d26ce 2019-05-10 stsp static const struct got_error *
5274 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5275 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5276 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5277 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5278 735ef5ac 2019-08-03 stsp int ood_errcode)
5279 d56d26ce 2019-05-10 stsp {
5280 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5281 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5282 1a36436d 2019-06-10 stsp
5283 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5284 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5285 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5286 1a36436d 2019-06-10 stsp return NULL;
5287 9bead371 2019-07-28 stsp /*
5288 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5289 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5290 9bead371 2019-07-28 stsp */
5291 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5292 735ef5ac 2019-08-03 stsp in_repo_path);
5293 9bead371 2019-07-28 stsp if (err) {
5294 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5295 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5296 1a36436d 2019-06-10 stsp goto done;
5297 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5298 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5299 1a36436d 2019-06-10 stsp } else {
5300 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5301 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5302 735ef5ac 2019-08-03 stsp in_repo_path);
5303 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5304 1a36436d 2019-06-10 stsp goto done;
5305 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5306 1a36436d 2019-06-10 stsp }
5307 1a36436d 2019-06-10 stsp done:
5308 1a36436d 2019-06-10 stsp free(id);
5309 1a36436d 2019-06-10 stsp return err;
5310 ebf99748 2019-05-09 stsp }
5311 ebf99748 2019-05-09 stsp
5312 c4296144 2019-05-09 stsp const struct got_error *
5313 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5314 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5315 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5316 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5317 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5318 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5319 afa376bf 2019-05-09 stsp struct got_repository *repo)
5320 c4296144 2019-05-09 stsp {
5321 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5322 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5323 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5324 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5325 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5326 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5327 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5328 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5329 ba580f68 2020-03-22 stsp int nentries;
5330 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5331 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5332 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5333 c4296144 2019-05-09 stsp
5334 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5335 c4296144 2019-05-09 stsp
5336 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5337 675c7539 2019-05-09 stsp
5338 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5339 588edf97 2019-05-10 stsp if (err)
5340 588edf97 2019-05-10 stsp goto done;
5341 de18fc63 2019-05-09 stsp
5342 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5343 588edf97 2019-05-10 stsp if (err)
5344 588edf97 2019-05-10 stsp goto done;
5345 588edf97 2019-05-10 stsp
5346 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5347 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5348 33ad4cbe 2019-05-12 jcs if (err)
5349 33ad4cbe 2019-05-12 jcs goto done;
5350 33ad4cbe 2019-05-12 jcs }
5351 c4296144 2019-05-09 stsp
5352 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5353 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5354 33ad4cbe 2019-05-12 jcs goto done;
5355 33ad4cbe 2019-05-12 jcs }
5356 33ad4cbe 2019-05-12 jcs
5357 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5358 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5359 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5360 cf066bf8 2019-05-09 stsp char *ondisk_path;
5361 cf066bf8 2019-05-09 stsp
5362 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5363 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5364 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5365 5f8a88c6 2019-08-03 stsp continue;
5366 5f8a88c6 2019-08-03 stsp
5367 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5368 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5369 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5370 cf066bf8 2019-05-09 stsp continue;
5371 cf066bf8 2019-05-09 stsp
5372 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5373 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5374 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5375 cf066bf8 2019-05-09 stsp goto done;
5376 cf066bf8 2019-05-09 stsp }
5377 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5378 2e1fa222 2020-07-23 stsp free(ondisk_path);
5379 2e1fa222 2020-07-23 stsp if (err)
5380 cf066bf8 2019-05-09 stsp goto done;
5381 cf066bf8 2019-05-09 stsp }
5382 036813ee 2019-05-09 stsp
5383 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5384 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5385 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5386 036813ee 2019-05-09 stsp if (err)
5387 036813ee 2019-05-09 stsp goto done;
5388 036813ee 2019-05-09 stsp
5389 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5390 de18fc63 2019-05-09 stsp if (err)
5391 de18fc63 2019-05-09 stsp goto done;
5392 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5393 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5394 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5395 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5396 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5397 33ad4cbe 2019-05-12 jcs free(logmsg);
5398 9d40349a 2019-05-09 stsp if (err)
5399 9d40349a 2019-05-09 stsp goto done;
5400 9d40349a 2019-05-09 stsp
5401 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5402 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5403 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5404 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5405 09f5bd90 2019-05-09 stsp goto done;
5406 09f5bd90 2019-05-09 stsp }
5407 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5408 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5409 09f5bd90 2019-05-09 stsp if (err)
5410 09f5bd90 2019-05-09 stsp goto done;
5411 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5412 ebf99748 2019-05-09 stsp if (err)
5413 ebf99748 2019-05-09 stsp goto done;
5414 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5415 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5416 09f5bd90 2019-05-09 stsp goto done;
5417 09f5bd90 2019-05-09 stsp }
5418 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5419 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5420 f2c16586 2019-05-09 stsp if (err)
5421 f2c16586 2019-05-09 stsp goto done;
5422 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5423 f2c16586 2019-05-09 stsp if (err)
5424 f2c16586 2019-05-09 stsp goto done;
5425 f2c16586 2019-05-09 stsp
5426 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5427 09f5bd90 2019-05-09 stsp if (err)
5428 09f5bd90 2019-05-09 stsp goto done;
5429 09f5bd90 2019-05-09 stsp
5430 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5431 ebf99748 2019-05-09 stsp if (err)
5432 ebf99748 2019-05-09 stsp goto done;
5433 c4296144 2019-05-09 stsp done:
5434 588edf97 2019-05-10 stsp if (head_tree)
5435 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5436 588edf97 2019-05-10 stsp if (head_commit)
5437 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5438 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5439 2f17228e 2019-05-12 stsp if (head_ref2) {
5440 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5441 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5442 2f17228e 2019-05-12 stsp err = unlockerr;
5443 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5444 39cd0ff6 2019-07-12 stsp }
5445 39cd0ff6 2019-07-12 stsp return err;
5446 39cd0ff6 2019-07-12 stsp }
5447 5c1e53bc 2019-07-28 stsp
5448 5c1e53bc 2019-07-28 stsp static const struct got_error *
5449 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5450 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5451 5c1e53bc 2019-07-28 stsp {
5452 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5453 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5454 39cd0ff6 2019-07-12 stsp
5455 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5456 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5457 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5458 5c1e53bc 2019-07-28 stsp
5459 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5460 5c1e53bc 2019-07-28 stsp ct_path++;
5461 5c1e53bc 2019-07-28 stsp
5462 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5463 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5464 5c1e53bc 2019-07-28 stsp break;
5465 5c1e53bc 2019-07-28 stsp }
5466 5c1e53bc 2019-07-28 stsp
5467 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5468 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5469 5c1e53bc 2019-07-28 stsp
5470 5c1e53bc 2019-07-28 stsp return NULL;
5471 5c1e53bc 2019-07-28 stsp }
5472 5c1e53bc 2019-07-28 stsp
5473 f0b75401 2019-08-03 stsp static const struct got_error *
5474 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5475 f0b75401 2019-08-03 stsp {
5476 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5477 f0b75401 2019-08-03 stsp
5478 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5479 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5480 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5481 f0b75401 2019-08-03 stsp }
5482 f0b75401 2019-08-03 stsp
5483 f0b75401 2019-08-03 stsp return NULL;
5484 f0b75401 2019-08-03 stsp }
5485 f0b75401 2019-08-03 stsp
5486 5f8a88c6 2019-08-03 stsp static const struct got_error *
5487 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5488 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5489 5f8a88c6 2019-08-03 stsp {
5490 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5491 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5492 5f8a88c6 2019-08-03 stsp
5493 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5494 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5495 5f8a88c6 2019-08-03 stsp continue;
5496 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5497 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5498 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5499 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5500 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5501 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5502 5f8a88c6 2019-08-03 stsp }
5503 5f8a88c6 2019-08-03 stsp
5504 5f8a88c6 2019-08-03 stsp return NULL;
5505 5f8a88c6 2019-08-03 stsp }
5506 5f8a88c6 2019-08-03 stsp
5507 39cd0ff6 2019-07-12 stsp const struct got_error *
5508 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5509 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5510 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5511 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5512 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5513 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5514 39cd0ff6 2019-07-12 stsp {
5515 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5516 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5517 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5518 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5519 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5520 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5521 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5522 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5523 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5524 39cd0ff6 2019-07-12 stsp
5525 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5526 39cd0ff6 2019-07-12 stsp
5527 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5528 39cd0ff6 2019-07-12 stsp
5529 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5530 39cd0ff6 2019-07-12 stsp if (err)
5531 39cd0ff6 2019-07-12 stsp goto done;
5532 39cd0ff6 2019-07-12 stsp
5533 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5534 39cd0ff6 2019-07-12 stsp if (err)
5535 39cd0ff6 2019-07-12 stsp goto done;
5536 39cd0ff6 2019-07-12 stsp
5537 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5538 39cd0ff6 2019-07-12 stsp if (err)
5539 39cd0ff6 2019-07-12 stsp goto done;
5540 39cd0ff6 2019-07-12 stsp
5541 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5542 39cd0ff6 2019-07-12 stsp if (err)
5543 39cd0ff6 2019-07-12 stsp goto done;
5544 39cd0ff6 2019-07-12 stsp
5545 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5546 5f8a88c6 2019-08-03 stsp &have_staged_files);
5547 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5548 5f8a88c6 2019-08-03 stsp goto done;
5549 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5550 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5551 5f8a88c6 2019-08-03 stsp if (err)
5552 5f8a88c6 2019-08-03 stsp goto done;
5553 5f8a88c6 2019-08-03 stsp }
5554 5f8a88c6 2019-08-03 stsp
5555 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5556 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5557 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5558 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5559 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5560 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5561 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5562 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5563 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5564 5c1e53bc 2019-07-28 stsp if (err)
5565 5c1e53bc 2019-07-28 stsp goto done;
5566 5c1e53bc 2019-07-28 stsp }
5567 39cd0ff6 2019-07-12 stsp
5568 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5569 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5570 39cd0ff6 2019-07-12 stsp goto done;
5571 5c1e53bc 2019-07-28 stsp }
5572 5c1e53bc 2019-07-28 stsp
5573 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5574 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5575 5c1e53bc 2019-07-28 stsp if (err)
5576 5c1e53bc 2019-07-28 stsp goto done;
5577 39cd0ff6 2019-07-12 stsp }
5578 f0b75401 2019-08-03 stsp
5579 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5580 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5581 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5582 f0b75401 2019-08-03 stsp
5583 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5584 f0b75401 2019-08-03 stsp ct_path++;
5585 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5586 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5587 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5588 b50cabdf 2019-07-12 stsp if (err)
5589 b50cabdf 2019-07-12 stsp goto done;
5590 f0b75401 2019-08-03 stsp
5591 b50cabdf 2019-07-12 stsp }
5592 b50cabdf 2019-07-12 stsp
5593 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5594 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5595 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5596 39cd0ff6 2019-07-12 stsp if (err)
5597 39cd0ff6 2019-07-12 stsp goto done;
5598 39cd0ff6 2019-07-12 stsp
5599 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5600 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5601 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5602 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5603 39cd0ff6 2019-07-12 stsp err = sync_err;
5604 39cd0ff6 2019-07-12 stsp done:
5605 39cd0ff6 2019-07-12 stsp if (fileindex)
5606 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5607 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5608 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5609 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5610 39cd0ff6 2019-07-12 stsp err = unlockerr;
5611 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5612 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5613 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5614 39cd0ff6 2019-07-12 stsp }
5615 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5616 c4296144 2019-05-09 stsp return err;
5617 8656d6c4 2019-05-20 stsp }
5618 8656d6c4 2019-05-20 stsp
5619 8656d6c4 2019-05-20 stsp const char *
5620 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5621 8656d6c4 2019-05-20 stsp {
5622 8656d6c4 2019-05-20 stsp return ct->path;
5623 8656d6c4 2019-05-20 stsp }
5624 8656d6c4 2019-05-20 stsp
5625 8656d6c4 2019-05-20 stsp unsigned int
5626 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5627 8656d6c4 2019-05-20 stsp {
5628 8656d6c4 2019-05-20 stsp return ct->status;
5629 818c7501 2019-07-11 stsp }
5630 818c7501 2019-07-11 stsp
5631 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5632 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5633 818c7501 2019-07-11 stsp struct got_repository *repo;
5634 818c7501 2019-07-11 stsp };
5635 818c7501 2019-07-11 stsp
5636 818c7501 2019-07-11 stsp static const struct got_error *
5637 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5638 818c7501 2019-07-11 stsp {
5639 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5640 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5641 818c7501 2019-07-11 stsp unsigned char status;
5642 818c7501 2019-07-11 stsp struct stat sb;
5643 818c7501 2019-07-11 stsp char *ondisk_path;
5644 818c7501 2019-07-11 stsp
5645 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5646 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5647 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5648 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5649 818c7501 2019-07-11 stsp
5650 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5651 818c7501 2019-07-11 stsp == -1)
5652 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5653 818c7501 2019-07-11 stsp
5654 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5655 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5656 818c7501 2019-07-11 stsp free(ondisk_path);
5657 818c7501 2019-07-11 stsp if (err)
5658 818c7501 2019-07-11 stsp return err;
5659 818c7501 2019-07-11 stsp
5660 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5661 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5662 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5663 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5664 818c7501 2019-07-11 stsp
5665 818c7501 2019-07-11 stsp return NULL;
5666 818c7501 2019-07-11 stsp }
5667 818c7501 2019-07-11 stsp
5668 818c7501 2019-07-11 stsp const struct got_error *
5669 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5670 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5671 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5672 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5673 818c7501 2019-07-11 stsp {
5674 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5675 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5676 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5677 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5678 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5679 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5680 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5681 818c7501 2019-07-11 stsp
5682 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5683 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5684 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5685 818c7501 2019-07-11 stsp
5686 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5687 818c7501 2019-07-11 stsp if (err)
5688 818c7501 2019-07-11 stsp return err;
5689 818c7501 2019-07-11 stsp
5690 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5691 818c7501 2019-07-11 stsp if (err)
5692 818c7501 2019-07-11 stsp goto done;
5693 818c7501 2019-07-11 stsp
5694 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5695 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5696 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5697 818c7501 2019-07-11 stsp &ok_arg);
5698 818c7501 2019-07-11 stsp if (err)
5699 818c7501 2019-07-11 stsp goto done;
5700 818c7501 2019-07-11 stsp
5701 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5702 818c7501 2019-07-11 stsp if (err)
5703 818c7501 2019-07-11 stsp goto done;
5704 818c7501 2019-07-11 stsp
5705 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5706 818c7501 2019-07-11 stsp if (err)
5707 818c7501 2019-07-11 stsp goto done;
5708 818c7501 2019-07-11 stsp
5709 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5710 818c7501 2019-07-11 stsp if (err)
5711 818c7501 2019-07-11 stsp goto done;
5712 818c7501 2019-07-11 stsp
5713 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5714 818c7501 2019-07-11 stsp 0);
5715 e51d7b55 2020-01-04 stsp if (err)
5716 e51d7b55 2020-01-04 stsp goto done;
5717 e51d7b55 2020-01-04 stsp
5718 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5719 818c7501 2019-07-11 stsp if (err)
5720 818c7501 2019-07-11 stsp goto done;
5721 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5722 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5723 e51d7b55 2020-01-04 stsp goto done;
5724 e51d7b55 2020-01-04 stsp }
5725 818c7501 2019-07-11 stsp
5726 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5727 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5728 818c7501 2019-07-11 stsp if (err)
5729 818c7501 2019-07-11 stsp goto done;
5730 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5731 818c7501 2019-07-11 stsp if (err)
5732 818c7501 2019-07-11 stsp goto done;
5733 818c7501 2019-07-11 stsp
5734 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5735 818c7501 2019-07-11 stsp
5736 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5737 818c7501 2019-07-11 stsp if (err)
5738 818c7501 2019-07-11 stsp goto done;
5739 818c7501 2019-07-11 stsp
5740 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5741 818c7501 2019-07-11 stsp if (err)
5742 818c7501 2019-07-11 stsp goto done;
5743 818c7501 2019-07-11 stsp
5744 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5745 818c7501 2019-07-11 stsp worktree->base_commit_id);
5746 818c7501 2019-07-11 stsp if (err)
5747 818c7501 2019-07-11 stsp goto done;
5748 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5749 818c7501 2019-07-11 stsp if (err)
5750 818c7501 2019-07-11 stsp goto done;
5751 818c7501 2019-07-11 stsp
5752 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5753 818c7501 2019-07-11 stsp if (err)
5754 818c7501 2019-07-11 stsp goto done;
5755 818c7501 2019-07-11 stsp done:
5756 818c7501 2019-07-11 stsp free(fileindex_path);
5757 818c7501 2019-07-11 stsp free(tmp_branch_name);
5758 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5759 818c7501 2019-07-11 stsp free(branch_ref_name);
5760 818c7501 2019-07-11 stsp if (branch_ref)
5761 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5762 818c7501 2019-07-11 stsp if (wt_branch)
5763 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5764 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5765 818c7501 2019-07-11 stsp if (err) {
5766 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5767 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5768 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5769 818c7501 2019-07-11 stsp }
5770 818c7501 2019-07-11 stsp if (*tmp_branch) {
5771 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5772 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5773 818c7501 2019-07-11 stsp }
5774 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5775 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5776 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5777 3e3a69f1 2019-07-25 stsp }
5778 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5779 818c7501 2019-07-11 stsp }
5780 818c7501 2019-07-11 stsp return err;
5781 818c7501 2019-07-11 stsp }
5782 818c7501 2019-07-11 stsp
5783 818c7501 2019-07-11 stsp const struct got_error *
5784 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5785 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5786 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5787 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5788 818c7501 2019-07-11 stsp {
5789 818c7501 2019-07-11 stsp const struct got_error *err;
5790 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5791 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5792 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5793 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5794 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5795 818c7501 2019-07-11 stsp
5796 818c7501 2019-07-11 stsp *commit_id = NULL;
5797 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5798 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5799 3e3a69f1 2019-07-25 stsp *branch = NULL;
5800 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5801 3e3a69f1 2019-07-25 stsp
5802 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5803 3e3a69f1 2019-07-25 stsp if (err)
5804 3e3a69f1 2019-07-25 stsp return err;
5805 818c7501 2019-07-11 stsp
5806 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5807 3e3a69f1 2019-07-25 stsp if (err)
5808 f032f1f7 2019-08-04 stsp goto done;
5809 f032f1f7 2019-08-04 stsp
5810 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5811 f032f1f7 2019-08-04 stsp &have_staged_files);
5812 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5813 f032f1f7 2019-08-04 stsp goto done;
5814 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5815 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5816 3e3a69f1 2019-07-25 stsp goto done;
5817 f032f1f7 2019-08-04 stsp }
5818 3e3a69f1 2019-07-25 stsp
5819 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5820 818c7501 2019-07-11 stsp if (err)
5821 f032f1f7 2019-08-04 stsp goto done;
5822 818c7501 2019-07-11 stsp
5823 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5824 818c7501 2019-07-11 stsp if (err)
5825 818c7501 2019-07-11 stsp goto done;
5826 818c7501 2019-07-11 stsp
5827 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5828 818c7501 2019-07-11 stsp if (err)
5829 818c7501 2019-07-11 stsp goto done;
5830 818c7501 2019-07-11 stsp
5831 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5832 818c7501 2019-07-11 stsp if (err)
5833 818c7501 2019-07-11 stsp goto done;
5834 818c7501 2019-07-11 stsp
5835 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5836 818c7501 2019-07-11 stsp if (err)
5837 818c7501 2019-07-11 stsp goto done;
5838 818c7501 2019-07-11 stsp
5839 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5840 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5841 818c7501 2019-07-11 stsp if (err)
5842 818c7501 2019-07-11 stsp goto done;
5843 818c7501 2019-07-11 stsp
5844 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5845 818c7501 2019-07-11 stsp if (err)
5846 818c7501 2019-07-11 stsp goto done;
5847 818c7501 2019-07-11 stsp
5848 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5849 818c7501 2019-07-11 stsp if (err)
5850 818c7501 2019-07-11 stsp goto done;
5851 818c7501 2019-07-11 stsp
5852 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5853 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5854 818c7501 2019-07-11 stsp if (err)
5855 818c7501 2019-07-11 stsp goto done;
5856 818c7501 2019-07-11 stsp
5857 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5858 818c7501 2019-07-11 stsp if (err)
5859 818c7501 2019-07-11 stsp goto done;
5860 818c7501 2019-07-11 stsp done:
5861 818c7501 2019-07-11 stsp free(commit_ref_name);
5862 818c7501 2019-07-11 stsp free(branch_ref_name);
5863 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5864 818c7501 2019-07-11 stsp if (commit_ref)
5865 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5866 818c7501 2019-07-11 stsp if (branch_ref)
5867 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5868 818c7501 2019-07-11 stsp if (err) {
5869 818c7501 2019-07-11 stsp free(*commit_id);
5870 818c7501 2019-07-11 stsp *commit_id = NULL;
5871 818c7501 2019-07-11 stsp if (*tmp_branch) {
5872 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5873 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5874 818c7501 2019-07-11 stsp }
5875 818c7501 2019-07-11 stsp if (*new_base_branch) {
5876 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5877 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5878 818c7501 2019-07-11 stsp }
5879 818c7501 2019-07-11 stsp if (*branch) {
5880 818c7501 2019-07-11 stsp got_ref_close(*branch);
5881 818c7501 2019-07-11 stsp *branch = NULL;
5882 818c7501 2019-07-11 stsp }
5883 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5884 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5885 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5886 3e3a69f1 2019-07-25 stsp }
5887 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5888 818c7501 2019-07-11 stsp }
5889 818c7501 2019-07-11 stsp return err;
5890 c4296144 2019-05-09 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_in_progress(int *in_progress, struct got_worktree *worktree)
5894 818c7501 2019-07-11 stsp {
5895 818c7501 2019-07-11 stsp const struct got_error *err;
5896 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5897 818c7501 2019-07-11 stsp
5898 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5899 818c7501 2019-07-11 stsp if (err)
5900 818c7501 2019-07-11 stsp return err;
5901 818c7501 2019-07-11 stsp
5902 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5903 818c7501 2019-07-11 stsp free(tmp_branch_name);
5904 818c7501 2019-07-11 stsp return NULL;
5905 818c7501 2019-07-11 stsp }
5906 818c7501 2019-07-11 stsp
5907 818c7501 2019-07-11 stsp static const struct got_error *
5908 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5909 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5910 818c7501 2019-07-11 stsp {
5911 0ebf8283 2019-07-24 stsp *logmsg = arg;
5912 818c7501 2019-07-11 stsp return NULL;
5913 818c7501 2019-07-11 stsp }
5914 818c7501 2019-07-11 stsp
5915 818c7501 2019-07-11 stsp static const struct got_error *
5916 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5917 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5918 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5919 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5920 818c7501 2019-07-11 stsp {
5921 818c7501 2019-07-11 stsp return NULL;
5922 01757395 2019-07-12 stsp }
5923 01757395 2019-07-12 stsp
5924 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5925 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5926 01757395 2019-07-12 stsp void *progress_arg;
5927 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5928 01757395 2019-07-12 stsp };
5929 01757395 2019-07-12 stsp
5930 01757395 2019-07-12 stsp static const struct got_error *
5931 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5932 01757395 2019-07-12 stsp {
5933 01757395 2019-07-12 stsp const struct got_error *err;
5934 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5935 01757395 2019-07-12 stsp char *p;
5936 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5937 01757395 2019-07-12 stsp
5938 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5939 01757395 2019-07-12 stsp if (err)
5940 01757395 2019-07-12 stsp return err;
5941 01757395 2019-07-12 stsp
5942 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5943 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5944 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5945 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5946 01757395 2019-07-12 stsp return NULL;
5947 01757395 2019-07-12 stsp
5948 01757395 2019-07-12 stsp p = strdup(path);
5949 01757395 2019-07-12 stsp if (p == NULL)
5950 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5951 01757395 2019-07-12 stsp
5952 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5953 01757395 2019-07-12 stsp if (err || new == NULL)
5954 01757395 2019-07-12 stsp free(p);
5955 01757395 2019-07-12 stsp return err;
5956 01757395 2019-07-12 stsp }
5957 01757395 2019-07-12 stsp
5958 01757395 2019-07-12 stsp void
5959 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5960 01757395 2019-07-12 stsp {
5961 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5962 01757395 2019-07-12 stsp
5963 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5964 01757395 2019-07-12 stsp free((char *)pe->path);
5965 01757395 2019-07-12 stsp
5966 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5967 818c7501 2019-07-11 stsp }
5968 818c7501 2019-07-11 stsp
5969 0ebf8283 2019-07-24 stsp static const struct got_error *
5970 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5971 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5972 818c7501 2019-07-11 stsp {
5973 818c7501 2019-07-11 stsp const struct got_error *err;
5974 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5975 818c7501 2019-07-11 stsp
5976 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5977 818c7501 2019-07-11 stsp if (err) {
5978 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5979 818c7501 2019-07-11 stsp goto done;
5980 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5981 818c7501 2019-07-11 stsp if (err)
5982 818c7501 2019-07-11 stsp goto done;
5983 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5984 818c7501 2019-07-11 stsp if (err)
5985 818c7501 2019-07-11 stsp goto done;
5986 de05890f 2020-03-05 stsp } else if (is_rebase) {
5987 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5988 818c7501 2019-07-11 stsp int cmp;
5989 818c7501 2019-07-11 stsp
5990 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5991 818c7501 2019-07-11 stsp if (err)
5992 818c7501 2019-07-11 stsp goto done;
5993 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5994 818c7501 2019-07-11 stsp free(stored_id);
5995 818c7501 2019-07-11 stsp if (cmp != 0) {
5996 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5997 818c7501 2019-07-11 stsp goto done;
5998 818c7501 2019-07-11 stsp }
5999 818c7501 2019-07-11 stsp }
6000 0ebf8283 2019-07-24 stsp done:
6001 0ebf8283 2019-07-24 stsp if (commit_ref)
6002 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6003 0ebf8283 2019-07-24 stsp return err;
6004 0ebf8283 2019-07-24 stsp }
6005 0ebf8283 2019-07-24 stsp
6006 0ebf8283 2019-07-24 stsp static const struct got_error *
6007 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6008 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6009 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6010 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6011 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6012 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6013 0ebf8283 2019-07-24 stsp {
6014 0ebf8283 2019-07-24 stsp const struct got_error *err;
6015 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6016 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6017 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6018 818c7501 2019-07-11 stsp
6019 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6020 0ebf8283 2019-07-24 stsp
6021 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6022 0ebf8283 2019-07-24 stsp if (err)
6023 0ebf8283 2019-07-24 stsp return err;
6024 0ebf8283 2019-07-24 stsp
6025 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6026 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6027 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6028 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6029 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6030 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6031 818c7501 2019-07-11 stsp if (commit_ref)
6032 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6033 818c7501 2019-07-11 stsp return err;
6034 818c7501 2019-07-11 stsp }
6035 818c7501 2019-07-11 stsp
6036 818c7501 2019-07-11 stsp const struct got_error *
6037 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6038 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6039 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6040 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6041 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6042 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6043 818c7501 2019-07-11 stsp {
6044 0ebf8283 2019-07-24 stsp const struct got_error *err;
6045 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6046 0ebf8283 2019-07-24 stsp
6047 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6048 0ebf8283 2019-07-24 stsp if (err)
6049 0ebf8283 2019-07-24 stsp return err;
6050 0ebf8283 2019-07-24 stsp
6051 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6052 0ebf8283 2019-07-24 stsp if (err)
6053 0ebf8283 2019-07-24 stsp goto done;
6054 0ebf8283 2019-07-24 stsp
6055 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6056 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6057 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6058 0ebf8283 2019-07-24 stsp done:
6059 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6060 0ebf8283 2019-07-24 stsp return err;
6061 0ebf8283 2019-07-24 stsp }
6062 0ebf8283 2019-07-24 stsp
6063 0ebf8283 2019-07-24 stsp const struct got_error *
6064 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6065 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6066 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6067 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6068 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6069 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6070 0ebf8283 2019-07-24 stsp {
6071 0ebf8283 2019-07-24 stsp const struct got_error *err;
6072 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6073 0ebf8283 2019-07-24 stsp
6074 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6075 0ebf8283 2019-07-24 stsp if (err)
6076 0ebf8283 2019-07-24 stsp return err;
6077 0ebf8283 2019-07-24 stsp
6078 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6079 0ebf8283 2019-07-24 stsp if (err)
6080 0ebf8283 2019-07-24 stsp goto done;
6081 0ebf8283 2019-07-24 stsp
6082 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6083 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6084 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6085 0ebf8283 2019-07-24 stsp done:
6086 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6087 0ebf8283 2019-07-24 stsp return err;
6088 0ebf8283 2019-07-24 stsp }
6089 0ebf8283 2019-07-24 stsp
6090 0ebf8283 2019-07-24 stsp static const struct got_error *
6091 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6092 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6093 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6094 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6095 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6096 0ebf8283 2019-07-24 stsp {
6097 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6098 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6099 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6100 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6101 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6102 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6103 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6104 818c7501 2019-07-11 stsp
6105 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6106 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6107 a0e95631 2019-07-12 stsp
6108 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6109 818c7501 2019-07-11 stsp
6110 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6111 a0e95631 2019-07-12 stsp if (err)
6112 3e3a69f1 2019-07-25 stsp return err;
6113 a0e95631 2019-07-12 stsp
6114 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6115 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6116 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6117 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6118 01757395 2019-07-12 stsp /*
6119 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6120 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6121 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6122 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6123 01757395 2019-07-12 stsp */
6124 01757395 2019-07-12 stsp if (merged_paths) {
6125 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6126 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6127 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6128 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6129 f2a9dc41 2019-12-13 tracey 0);
6130 01757395 2019-07-12 stsp if (err)
6131 01757395 2019-07-12 stsp goto done;
6132 01757395 2019-07-12 stsp }
6133 01757395 2019-07-12 stsp } else {
6134 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6135 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6136 01757395 2019-07-12 stsp if (err)
6137 01757395 2019-07-12 stsp goto done;
6138 01757395 2019-07-12 stsp }
6139 a0e95631 2019-07-12 stsp
6140 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6141 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6142 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6143 a0e95631 2019-07-12 stsp if (err)
6144 a0e95631 2019-07-12 stsp goto done;
6145 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6146 a0e95631 2019-07-12 stsp goto done;
6147 ff0d2220 2019-07-11 stsp }
6148 818c7501 2019-07-11 stsp
6149 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6150 edd02c5e 2019-07-11 stsp if (err)
6151 edd02c5e 2019-07-11 stsp goto done;
6152 edd02c5e 2019-07-11 stsp
6153 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6154 818c7501 2019-07-11 stsp if (err)
6155 818c7501 2019-07-11 stsp goto done;
6156 818c7501 2019-07-11 stsp
6157 5943eee2 2019-08-13 stsp if (new_logmsg) {
6158 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6159 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6160 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6161 5943eee2 2019-08-13 stsp goto done;
6162 5943eee2 2019-08-13 stsp }
6163 5943eee2 2019-08-13 stsp } else {
6164 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6165 5943eee2 2019-08-13 stsp if (err)
6166 5943eee2 2019-08-13 stsp goto done;
6167 5943eee2 2019-08-13 stsp }
6168 0ebf8283 2019-07-24 stsp
6169 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6170 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6171 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6172 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6173 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6174 a0e95631 2019-07-12 stsp if (err)
6175 a0e95631 2019-07-12 stsp goto done;
6176 a0e95631 2019-07-12 stsp
6177 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6178 a0e95631 2019-07-12 stsp if (err)
6179 a0e95631 2019-07-12 stsp goto done;
6180 a0e95631 2019-07-12 stsp
6181 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6182 a0e95631 2019-07-12 stsp if (err)
6183 a0e95631 2019-07-12 stsp goto done;
6184 a0e95631 2019-07-12 stsp
6185 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6186 72fd46fa 2019-09-06 stsp fileindex, 0);
6187 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6188 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6189 a0e95631 2019-07-12 stsp err = sync_err;
6190 818c7501 2019-07-11 stsp done:
6191 a0e95631 2019-07-12 stsp free(fileindex_path);
6192 a0e95631 2019-07-12 stsp free(head_commit_id);
6193 a0e95631 2019-07-12 stsp if (head_ref)
6194 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6195 818c7501 2019-07-11 stsp if (err) {
6196 818c7501 2019-07-11 stsp free(*new_commit_id);
6197 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6198 818c7501 2019-07-11 stsp }
6199 818c7501 2019-07-11 stsp return err;
6200 818c7501 2019-07-11 stsp }
6201 818c7501 2019-07-11 stsp
6202 818c7501 2019-07-11 stsp const struct got_error *
6203 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6204 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6205 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6206 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6207 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6208 0ebf8283 2019-07-24 stsp {
6209 0ebf8283 2019-07-24 stsp const struct got_error *err;
6210 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6211 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6212 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6213 0ebf8283 2019-07-24 stsp
6214 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6215 0ebf8283 2019-07-24 stsp if (err)
6216 0ebf8283 2019-07-24 stsp return err;
6217 0ebf8283 2019-07-24 stsp
6218 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6219 0ebf8283 2019-07-24 stsp if (err)
6220 0ebf8283 2019-07-24 stsp goto done;
6221 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6222 0ebf8283 2019-07-24 stsp if (err)
6223 0ebf8283 2019-07-24 stsp goto done;
6224 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6225 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6226 0ebf8283 2019-07-24 stsp goto done;
6227 0ebf8283 2019-07-24 stsp }
6228 0ebf8283 2019-07-24 stsp
6229 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6230 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6231 0ebf8283 2019-07-24 stsp done:
6232 0ebf8283 2019-07-24 stsp if (commit_ref)
6233 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6234 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6235 0ebf8283 2019-07-24 stsp free(commit_id);
6236 0ebf8283 2019-07-24 stsp return err;
6237 0ebf8283 2019-07-24 stsp }
6238 0ebf8283 2019-07-24 stsp
6239 0ebf8283 2019-07-24 stsp const struct got_error *
6240 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6241 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6242 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6243 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6244 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6245 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6246 0ebf8283 2019-07-24 stsp {
6247 0ebf8283 2019-07-24 stsp const struct got_error *err;
6248 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6249 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6250 0ebf8283 2019-07-24 stsp
6251 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6252 0ebf8283 2019-07-24 stsp if (err)
6253 0ebf8283 2019-07-24 stsp return err;
6254 0ebf8283 2019-07-24 stsp
6255 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6256 0ebf8283 2019-07-24 stsp if (err)
6257 0ebf8283 2019-07-24 stsp goto done;
6258 0ebf8283 2019-07-24 stsp
6259 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6260 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6261 0ebf8283 2019-07-24 stsp done:
6262 0ebf8283 2019-07-24 stsp if (commit_ref)
6263 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6264 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6265 0ebf8283 2019-07-24 stsp return err;
6266 0ebf8283 2019-07-24 stsp }
6267 0ebf8283 2019-07-24 stsp
6268 0ebf8283 2019-07-24 stsp const struct got_error *
6269 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6270 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6271 818c7501 2019-07-11 stsp {
6272 3e3a69f1 2019-07-25 stsp if (fileindex)
6273 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6274 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6275 69844fba 2019-07-11 stsp }
6276 69844fba 2019-07-11 stsp
6277 69844fba 2019-07-11 stsp static const struct got_error *
6278 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6279 69844fba 2019-07-11 stsp {
6280 69844fba 2019-07-11 stsp const struct got_error *err;
6281 69844fba 2019-07-11 stsp struct got_reference *ref;
6282 69844fba 2019-07-11 stsp
6283 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6284 69844fba 2019-07-11 stsp if (err) {
6285 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6286 69844fba 2019-07-11 stsp return NULL;
6287 69844fba 2019-07-11 stsp return err;
6288 69844fba 2019-07-11 stsp }
6289 69844fba 2019-07-11 stsp
6290 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6291 69844fba 2019-07-11 stsp got_ref_close(ref);
6292 69844fba 2019-07-11 stsp return err;
6293 818c7501 2019-07-11 stsp }
6294 818c7501 2019-07-11 stsp
6295 69844fba 2019-07-11 stsp static const struct got_error *
6296 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6297 69844fba 2019-07-11 stsp {
6298 69844fba 2019-07-11 stsp const struct got_error *err;
6299 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6300 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6301 69844fba 2019-07-11 stsp
6302 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6303 69844fba 2019-07-11 stsp if (err)
6304 69844fba 2019-07-11 stsp goto done;
6305 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6306 69844fba 2019-07-11 stsp if (err)
6307 69844fba 2019-07-11 stsp goto done;
6308 69844fba 2019-07-11 stsp
6309 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6310 69844fba 2019-07-11 stsp if (err)
6311 69844fba 2019-07-11 stsp goto done;
6312 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6313 69844fba 2019-07-11 stsp if (err)
6314 69844fba 2019-07-11 stsp goto done;
6315 69844fba 2019-07-11 stsp
6316 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6317 69844fba 2019-07-11 stsp if (err)
6318 69844fba 2019-07-11 stsp goto done;
6319 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6320 69844fba 2019-07-11 stsp if (err)
6321 69844fba 2019-07-11 stsp goto done;
6322 69844fba 2019-07-11 stsp
6323 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6324 69844fba 2019-07-11 stsp if (err)
6325 69844fba 2019-07-11 stsp goto done;
6326 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6327 69844fba 2019-07-11 stsp if (err)
6328 69844fba 2019-07-11 stsp goto done;
6329 69844fba 2019-07-11 stsp
6330 69844fba 2019-07-11 stsp done:
6331 69844fba 2019-07-11 stsp free(tmp_branch_name);
6332 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6333 69844fba 2019-07-11 stsp free(branch_ref_name);
6334 69844fba 2019-07-11 stsp free(commit_ref_name);
6335 69844fba 2019-07-11 stsp return err;
6336 69844fba 2019-07-11 stsp }
6337 69844fba 2019-07-11 stsp
6338 818c7501 2019-07-11 stsp const struct got_error *
6339 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6340 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6341 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6342 818c7501 2019-07-11 stsp struct got_repository *repo)
6343 818c7501 2019-07-11 stsp {
6344 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6345 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6346 818c7501 2019-07-11 stsp
6347 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6348 818c7501 2019-07-11 stsp if (err)
6349 818c7501 2019-07-11 stsp return err;
6350 818c7501 2019-07-11 stsp
6351 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6352 818c7501 2019-07-11 stsp if (err)
6353 818c7501 2019-07-11 stsp goto done;
6354 818c7501 2019-07-11 stsp
6355 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6356 818c7501 2019-07-11 stsp if (err)
6357 818c7501 2019-07-11 stsp goto done;
6358 818c7501 2019-07-11 stsp
6359 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6360 818c7501 2019-07-11 stsp if (err)
6361 818c7501 2019-07-11 stsp goto done;
6362 818c7501 2019-07-11 stsp
6363 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6364 818c7501 2019-07-11 stsp done:
6365 3e3a69f1 2019-07-25 stsp if (fileindex)
6366 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6367 818c7501 2019-07-11 stsp free(new_head_commit_id);
6368 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6369 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6370 818c7501 2019-07-11 stsp err = unlockerr;
6371 818c7501 2019-07-11 stsp return err;
6372 818c7501 2019-07-11 stsp }
6373 818c7501 2019-07-11 stsp
6374 818c7501 2019-07-11 stsp const struct got_error *
6375 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6376 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6377 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6378 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6379 818c7501 2019-07-11 stsp {
6380 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6381 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6382 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6383 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6384 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6385 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6386 818c7501 2019-07-11 stsp
6387 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6388 818c7501 2019-07-11 stsp if (err)
6389 818c7501 2019-07-11 stsp return err;
6390 818c7501 2019-07-11 stsp
6391 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6392 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6393 818c7501 2019-07-11 stsp if (err)
6394 818c7501 2019-07-11 stsp goto done;
6395 818c7501 2019-07-11 stsp
6396 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6397 818c7501 2019-07-11 stsp if (err)
6398 818c7501 2019-07-11 stsp goto done;
6399 818c7501 2019-07-11 stsp
6400 818c7501 2019-07-11 stsp /*
6401 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6402 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6403 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6404 818c7501 2019-07-11 stsp */
6405 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6406 818c7501 2019-07-11 stsp if (err)
6407 818c7501 2019-07-11 stsp goto done;
6408 818c7501 2019-07-11 stsp
6409 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6410 818c7501 2019-07-11 stsp if (err)
6411 818c7501 2019-07-11 stsp goto done;
6412 818c7501 2019-07-11 stsp
6413 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6414 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6415 ca355955 2019-07-12 stsp if (err)
6416 ca355955 2019-07-12 stsp goto done;
6417 ca355955 2019-07-12 stsp
6418 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6419 ca355955 2019-07-12 stsp if (err)
6420 ca355955 2019-07-12 stsp goto done;
6421 ca355955 2019-07-12 stsp
6422 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6423 818c7501 2019-07-11 stsp if (err)
6424 818c7501 2019-07-11 stsp goto done;
6425 818c7501 2019-07-11 stsp
6426 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6427 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6428 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6429 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6430 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6431 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6432 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6433 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6434 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6435 55bd499d 2019-07-12 stsp if (err)
6436 1f1abb7e 2019-08-08 stsp goto sync;
6437 55bd499d 2019-07-12 stsp
6438 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6439 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6440 ca355955 2019-07-12 stsp sync:
6441 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6442 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6443 ca355955 2019-07-12 stsp err = sync_err;
6444 818c7501 2019-07-11 stsp done:
6445 818c7501 2019-07-11 stsp got_ref_close(resolved);
6446 a3a2faf2 2019-07-12 stsp free(tree_id);
6447 818c7501 2019-07-11 stsp free(commit_id);
6448 0ebf8283 2019-07-24 stsp if (fileindex)
6449 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6450 0ebf8283 2019-07-24 stsp free(fileindex_path);
6451 0ebf8283 2019-07-24 stsp
6452 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6453 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6454 0ebf8283 2019-07-24 stsp err = unlockerr;
6455 0ebf8283 2019-07-24 stsp return err;
6456 0ebf8283 2019-07-24 stsp }
6457 0ebf8283 2019-07-24 stsp
6458 0ebf8283 2019-07-24 stsp const struct got_error *
6459 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6460 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6461 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6462 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6463 0ebf8283 2019-07-24 stsp {
6464 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6465 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6466 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6467 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6468 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6469 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6470 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6471 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6472 0ebf8283 2019-07-24 stsp
6473 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6474 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6475 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6476 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6477 0ebf8283 2019-07-24 stsp
6478 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6479 0ebf8283 2019-07-24 stsp if (err)
6480 0ebf8283 2019-07-24 stsp return err;
6481 0ebf8283 2019-07-24 stsp
6482 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6483 0ebf8283 2019-07-24 stsp if (err)
6484 0ebf8283 2019-07-24 stsp goto done;
6485 0ebf8283 2019-07-24 stsp
6486 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6487 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6488 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6489 0ebf8283 2019-07-24 stsp &ok_arg);
6490 0ebf8283 2019-07-24 stsp if (err)
6491 0ebf8283 2019-07-24 stsp goto done;
6492 0ebf8283 2019-07-24 stsp
6493 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6494 0ebf8283 2019-07-24 stsp if (err)
6495 0ebf8283 2019-07-24 stsp goto done;
6496 0ebf8283 2019-07-24 stsp
6497 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6498 0ebf8283 2019-07-24 stsp if (err)
6499 0ebf8283 2019-07-24 stsp goto done;
6500 0ebf8283 2019-07-24 stsp
6501 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6502 0ebf8283 2019-07-24 stsp worktree);
6503 0ebf8283 2019-07-24 stsp if (err)
6504 0ebf8283 2019-07-24 stsp goto done;
6505 0ebf8283 2019-07-24 stsp
6506 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6507 0ebf8283 2019-07-24 stsp 0);
6508 0ebf8283 2019-07-24 stsp if (err)
6509 0ebf8283 2019-07-24 stsp goto done;
6510 0ebf8283 2019-07-24 stsp
6511 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6512 0ebf8283 2019-07-24 stsp if (err)
6513 0ebf8283 2019-07-24 stsp goto done;
6514 0ebf8283 2019-07-24 stsp
6515 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6516 0ebf8283 2019-07-24 stsp if (err)
6517 0ebf8283 2019-07-24 stsp goto done;
6518 0ebf8283 2019-07-24 stsp
6519 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6520 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6521 0ebf8283 2019-07-24 stsp if (err)
6522 0ebf8283 2019-07-24 stsp goto done;
6523 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6524 0ebf8283 2019-07-24 stsp if (err)
6525 0ebf8283 2019-07-24 stsp goto done;
6526 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6527 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6528 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6529 0ebf8283 2019-07-24 stsp goto done;
6530 0ebf8283 2019-07-24 stsp }
6531 0ebf8283 2019-07-24 stsp
6532 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6533 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6534 0ebf8283 2019-07-24 stsp if (err)
6535 0ebf8283 2019-07-24 stsp goto done;
6536 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6537 0ebf8283 2019-07-24 stsp if (err)
6538 0ebf8283 2019-07-24 stsp goto done;
6539 0ebf8283 2019-07-24 stsp
6540 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6541 0ebf8283 2019-07-24 stsp if (err)
6542 0ebf8283 2019-07-24 stsp goto done;
6543 0ebf8283 2019-07-24 stsp done:
6544 0ebf8283 2019-07-24 stsp free(fileindex_path);
6545 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6546 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6547 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6548 0ebf8283 2019-07-24 stsp if (wt_branch)
6549 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6550 0ebf8283 2019-07-24 stsp if (err) {
6551 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6552 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6553 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6554 0ebf8283 2019-07-24 stsp }
6555 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6556 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6557 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6558 0ebf8283 2019-07-24 stsp }
6559 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6560 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6561 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6562 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6563 3e3a69f1 2019-07-25 stsp }
6564 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6565 0ebf8283 2019-07-24 stsp }
6566 0ebf8283 2019-07-24 stsp return err;
6567 0ebf8283 2019-07-24 stsp }
6568 0ebf8283 2019-07-24 stsp
6569 0ebf8283 2019-07-24 stsp const struct got_error *
6570 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6571 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6572 0ebf8283 2019-07-24 stsp {
6573 3e3a69f1 2019-07-25 stsp if (fileindex)
6574 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6575 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6576 0ebf8283 2019-07-24 stsp }
6577 0ebf8283 2019-07-24 stsp
6578 0ebf8283 2019-07-24 stsp const struct got_error *
6579 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6580 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6581 0ebf8283 2019-07-24 stsp {
6582 0ebf8283 2019-07-24 stsp const struct got_error *err;
6583 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6584 0ebf8283 2019-07-24 stsp
6585 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6586 0ebf8283 2019-07-24 stsp if (err)
6587 0ebf8283 2019-07-24 stsp return err;
6588 0ebf8283 2019-07-24 stsp
6589 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6590 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6591 0ebf8283 2019-07-24 stsp return NULL;
6592 0ebf8283 2019-07-24 stsp }
6593 0ebf8283 2019-07-24 stsp
6594 0ebf8283 2019-07-24 stsp const struct got_error *
6595 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6596 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6597 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6598 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6599 0ebf8283 2019-07-24 stsp {
6600 0ebf8283 2019-07-24 stsp const struct got_error *err;
6601 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6602 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6603 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6604 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6605 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6606 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6607 0ebf8283 2019-07-24 stsp
6608 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6609 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6610 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6611 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6612 0ebf8283 2019-07-24 stsp
6613 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6614 3e3a69f1 2019-07-25 stsp if (err)
6615 3e3a69f1 2019-07-25 stsp return err;
6616 3e3a69f1 2019-07-25 stsp
6617 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6618 3e3a69f1 2019-07-25 stsp if (err)
6619 f032f1f7 2019-08-04 stsp goto done;
6620 f032f1f7 2019-08-04 stsp
6621 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6622 f032f1f7 2019-08-04 stsp &have_staged_files);
6623 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6624 f032f1f7 2019-08-04 stsp goto done;
6625 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6626 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6627 3e3a69f1 2019-07-25 stsp goto done;
6628 f032f1f7 2019-08-04 stsp }
6629 3e3a69f1 2019-07-25 stsp
6630 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6631 0ebf8283 2019-07-24 stsp if (err)
6632 f032f1f7 2019-08-04 stsp goto done;
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6635 0ebf8283 2019-07-24 stsp if (err)
6636 0ebf8283 2019-07-24 stsp goto done;
6637 0ebf8283 2019-07-24 stsp
6638 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6639 0ebf8283 2019-07-24 stsp if (err)
6640 0ebf8283 2019-07-24 stsp goto done;
6641 0ebf8283 2019-07-24 stsp
6642 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6643 0ebf8283 2019-07-24 stsp worktree);
6644 0ebf8283 2019-07-24 stsp if (err)
6645 0ebf8283 2019-07-24 stsp goto done;
6646 0ebf8283 2019-07-24 stsp
6647 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6648 0ebf8283 2019-07-24 stsp if (err)
6649 0ebf8283 2019-07-24 stsp goto done;
6650 0ebf8283 2019-07-24 stsp
6651 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6652 0ebf8283 2019-07-24 stsp if (err)
6653 0ebf8283 2019-07-24 stsp goto done;
6654 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6655 0ebf8283 2019-07-24 stsp if (err)
6656 0ebf8283 2019-07-24 stsp goto done;
6657 0ebf8283 2019-07-24 stsp
6658 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6659 0ebf8283 2019-07-24 stsp if (err)
6660 0ebf8283 2019-07-24 stsp goto done;
6661 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6662 0ebf8283 2019-07-24 stsp if (err)
6663 0ebf8283 2019-07-24 stsp goto done;
6664 0ebf8283 2019-07-24 stsp
6665 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6666 0ebf8283 2019-07-24 stsp if (err)
6667 0ebf8283 2019-07-24 stsp goto done;
6668 0ebf8283 2019-07-24 stsp done:
6669 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6670 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6671 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6672 0ebf8283 2019-07-24 stsp if (commit_ref)
6673 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6674 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6675 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6676 0ebf8283 2019-07-24 stsp if (err) {
6677 0ebf8283 2019-07-24 stsp free(*commit_id);
6678 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6679 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6680 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6681 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6682 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6683 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6684 0ebf8283 2019-07-24 stsp }
6685 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6686 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6687 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6688 3e3a69f1 2019-07-25 stsp }
6689 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6690 0ebf8283 2019-07-24 stsp }
6691 0ebf8283 2019-07-24 stsp return err;
6692 0ebf8283 2019-07-24 stsp }
6693 0ebf8283 2019-07-24 stsp
6694 0ebf8283 2019-07-24 stsp static const struct got_error *
6695 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6696 0ebf8283 2019-07-24 stsp {
6697 0ebf8283 2019-07-24 stsp const struct got_error *err;
6698 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6699 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6700 0ebf8283 2019-07-24 stsp
6701 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6702 0ebf8283 2019-07-24 stsp if (err)
6703 0ebf8283 2019-07-24 stsp goto done;
6704 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6705 0ebf8283 2019-07-24 stsp if (err)
6706 0ebf8283 2019-07-24 stsp goto done;
6707 0ebf8283 2019-07-24 stsp
6708 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6709 0ebf8283 2019-07-24 stsp worktree);
6710 0ebf8283 2019-07-24 stsp if (err)
6711 0ebf8283 2019-07-24 stsp goto done;
6712 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6713 0ebf8283 2019-07-24 stsp if (err)
6714 0ebf8283 2019-07-24 stsp goto done;
6715 0ebf8283 2019-07-24 stsp
6716 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6717 0ebf8283 2019-07-24 stsp if (err)
6718 0ebf8283 2019-07-24 stsp goto done;
6719 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6720 0ebf8283 2019-07-24 stsp if (err)
6721 0ebf8283 2019-07-24 stsp goto done;
6722 0ebf8283 2019-07-24 stsp
6723 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6724 0ebf8283 2019-07-24 stsp if (err)
6725 0ebf8283 2019-07-24 stsp goto done;
6726 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6727 0ebf8283 2019-07-24 stsp if (err)
6728 0ebf8283 2019-07-24 stsp goto done;
6729 0ebf8283 2019-07-24 stsp done:
6730 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6731 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6732 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6733 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6734 0ebf8283 2019-07-24 stsp return err;
6735 0ebf8283 2019-07-24 stsp }
6736 0ebf8283 2019-07-24 stsp
6737 0ebf8283 2019-07-24 stsp const struct got_error *
6738 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6739 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6740 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6741 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6742 0ebf8283 2019-07-24 stsp {
6743 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6744 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6745 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6746 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6747 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6748 0ebf8283 2019-07-24 stsp
6749 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6750 0ebf8283 2019-07-24 stsp if (err)
6751 0ebf8283 2019-07-24 stsp return err;
6752 0ebf8283 2019-07-24 stsp
6753 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6754 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6755 0ebf8283 2019-07-24 stsp if (err)
6756 0ebf8283 2019-07-24 stsp goto done;
6757 0ebf8283 2019-07-24 stsp
6758 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6759 0ebf8283 2019-07-24 stsp if (err)
6760 0ebf8283 2019-07-24 stsp goto done;
6761 0ebf8283 2019-07-24 stsp
6762 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6763 0ebf8283 2019-07-24 stsp if (err)
6764 0ebf8283 2019-07-24 stsp goto done;
6765 0ebf8283 2019-07-24 stsp
6766 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6767 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6768 0ebf8283 2019-07-24 stsp if (err)
6769 0ebf8283 2019-07-24 stsp goto done;
6770 0ebf8283 2019-07-24 stsp
6771 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6772 0ebf8283 2019-07-24 stsp if (err)
6773 0ebf8283 2019-07-24 stsp goto done;
6774 0ebf8283 2019-07-24 stsp
6775 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6776 0ebf8283 2019-07-24 stsp if (err)
6777 0ebf8283 2019-07-24 stsp goto done;
6778 0ebf8283 2019-07-24 stsp
6779 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6780 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6781 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6782 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6783 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6784 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6785 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6786 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6787 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6788 0ebf8283 2019-07-24 stsp if (err)
6789 1f1abb7e 2019-08-08 stsp goto sync;
6790 0ebf8283 2019-07-24 stsp
6791 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6792 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6793 0ebf8283 2019-07-24 stsp sync:
6794 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6795 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6796 0ebf8283 2019-07-24 stsp err = sync_err;
6797 0ebf8283 2019-07-24 stsp done:
6798 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6799 0ebf8283 2019-07-24 stsp free(tree_id);
6800 818c7501 2019-07-11 stsp free(fileindex_path);
6801 818c7501 2019-07-11 stsp
6802 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6803 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6804 818c7501 2019-07-11 stsp err = unlockerr;
6805 818c7501 2019-07-11 stsp return err;
6806 818c7501 2019-07-11 stsp }
6807 0ebf8283 2019-07-24 stsp
6808 0ebf8283 2019-07-24 stsp const struct got_error *
6809 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6810 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6811 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6812 0ebf8283 2019-07-24 stsp {
6813 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6814 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6815 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6816 0ebf8283 2019-07-24 stsp
6817 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6818 0ebf8283 2019-07-24 stsp if (err)
6819 0ebf8283 2019-07-24 stsp return err;
6820 0ebf8283 2019-07-24 stsp
6821 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6822 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
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_ref_change_ref(resolved, new_head_commit_id);
6827 0ebf8283 2019-07-24 stsp if (err)
6828 0ebf8283 2019-07-24 stsp goto done;
6829 0ebf8283 2019-07-24 stsp
6830 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6831 0ebf8283 2019-07-24 stsp if (err)
6832 0ebf8283 2019-07-24 stsp goto done;
6833 0ebf8283 2019-07-24 stsp
6834 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6835 0ebf8283 2019-07-24 stsp if (err)
6836 0ebf8283 2019-07-24 stsp goto done;
6837 0ebf8283 2019-07-24 stsp
6838 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6839 0ebf8283 2019-07-24 stsp done:
6840 3e3a69f1 2019-07-25 stsp if (fileindex)
6841 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6842 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6843 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6844 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6845 0ebf8283 2019-07-24 stsp err = unlockerr;
6846 0ebf8283 2019-07-24 stsp return err;
6847 0ebf8283 2019-07-24 stsp }
6848 0ebf8283 2019-07-24 stsp
6849 0ebf8283 2019-07-24 stsp const struct got_error *
6850 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6851 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6852 0ebf8283 2019-07-24 stsp {
6853 0ebf8283 2019-07-24 stsp const struct got_error *err;
6854 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6855 0ebf8283 2019-07-24 stsp
6856 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6857 0ebf8283 2019-07-24 stsp if (err)
6858 0ebf8283 2019-07-24 stsp return err;
6859 0ebf8283 2019-07-24 stsp
6860 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6861 0ebf8283 2019-07-24 stsp if (err)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6865 0ebf8283 2019-07-24 stsp done:
6866 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6867 2822a352 2019-10-15 stsp return err;
6868 2822a352 2019-10-15 stsp }
6869 2822a352 2019-10-15 stsp
6870 2822a352 2019-10-15 stsp const struct got_error *
6871 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6872 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6873 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6874 2822a352 2019-10-15 stsp struct got_repository *repo)
6875 2822a352 2019-10-15 stsp {
6876 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6877 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6878 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6879 2822a352 2019-10-15 stsp
6880 2822a352 2019-10-15 stsp *fileindex = NULL;
6881 2822a352 2019-10-15 stsp *branch_ref = NULL;
6882 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6883 2822a352 2019-10-15 stsp
6884 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6885 2822a352 2019-10-15 stsp if (err)
6886 2822a352 2019-10-15 stsp return err;
6887 2822a352 2019-10-15 stsp
6888 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6889 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6890 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6891 2822a352 2019-10-15 stsp "update -b or different branch name required");
6892 2822a352 2019-10-15 stsp goto done;
6893 2822a352 2019-10-15 stsp }
6894 2822a352 2019-10-15 stsp
6895 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6896 2822a352 2019-10-15 stsp if (err)
6897 2822a352 2019-10-15 stsp goto done;
6898 2822a352 2019-10-15 stsp
6899 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6900 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6901 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6902 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6903 2822a352 2019-10-15 stsp &ok_arg);
6904 2822a352 2019-10-15 stsp if (err)
6905 2822a352 2019-10-15 stsp goto done;
6906 2822a352 2019-10-15 stsp
6907 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6908 2822a352 2019-10-15 stsp if (err)
6909 2822a352 2019-10-15 stsp goto done;
6910 2822a352 2019-10-15 stsp
6911 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6912 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6913 2822a352 2019-10-15 stsp done:
6914 2822a352 2019-10-15 stsp if (err) {
6915 2822a352 2019-10-15 stsp if (*branch_ref) {
6916 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6917 2822a352 2019-10-15 stsp *branch_ref = NULL;
6918 2822a352 2019-10-15 stsp }
6919 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6920 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6921 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6922 2822a352 2019-10-15 stsp }
6923 2822a352 2019-10-15 stsp if (*fileindex) {
6924 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6925 2822a352 2019-10-15 stsp *fileindex = NULL;
6926 2822a352 2019-10-15 stsp }
6927 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6928 2822a352 2019-10-15 stsp }
6929 0cb83759 2019-08-03 stsp return err;
6930 0cb83759 2019-08-03 stsp }
6931 0cb83759 2019-08-03 stsp
6932 2822a352 2019-10-15 stsp const struct got_error *
6933 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6934 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6935 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6936 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6937 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6938 2822a352 2019-10-15 stsp {
6939 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6940 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6941 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6942 2822a352 2019-10-15 stsp
6943 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6944 2822a352 2019-10-15 stsp if (err)
6945 2822a352 2019-10-15 stsp goto done;
6946 2822a352 2019-10-15 stsp
6947 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6948 2822a352 2019-10-15 stsp if (err)
6949 2822a352 2019-10-15 stsp goto done;
6950 2822a352 2019-10-15 stsp
6951 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6952 2822a352 2019-10-15 stsp worktree->path_prefix);
6953 2822a352 2019-10-15 stsp if (err)
6954 2822a352 2019-10-15 stsp goto done;
6955 2822a352 2019-10-15 stsp
6956 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6957 2822a352 2019-10-15 stsp if (err)
6958 2822a352 2019-10-15 stsp goto done;
6959 2822a352 2019-10-15 stsp
6960 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6961 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6962 2822a352 2019-10-15 stsp if (err)
6963 2822a352 2019-10-15 stsp goto sync;
6964 2822a352 2019-10-15 stsp
6965 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6966 2822a352 2019-10-15 stsp if (err)
6967 2822a352 2019-10-15 stsp goto sync;
6968 2822a352 2019-10-15 stsp
6969 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6970 2822a352 2019-10-15 stsp sync:
6971 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6972 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6973 2822a352 2019-10-15 stsp err = sync_err;
6974 2822a352 2019-10-15 stsp
6975 2822a352 2019-10-15 stsp done:
6976 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6977 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6978 2822a352 2019-10-15 stsp err = unlockerr;
6979 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6980 2822a352 2019-10-15 stsp
6981 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6982 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6983 2822a352 2019-10-15 stsp err = unlockerr;
6984 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6985 2822a352 2019-10-15 stsp
6986 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6987 2822a352 2019-10-15 stsp free(fileindex_path);
6988 2822a352 2019-10-15 stsp free(tree_id);
6989 2822a352 2019-10-15 stsp
6990 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6991 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6992 2822a352 2019-10-15 stsp err = unlockerr;
6993 2822a352 2019-10-15 stsp return err;
6994 2822a352 2019-10-15 stsp }
6995 2822a352 2019-10-15 stsp
6996 2822a352 2019-10-15 stsp const struct got_error *
6997 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6998 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6999 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7000 2822a352 2019-10-15 stsp {
7001 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7002 8b692cd0 2019-10-21 stsp
7003 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7004 8b692cd0 2019-10-21 stsp
7005 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7006 8b692cd0 2019-10-21 stsp
7007 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7008 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7009 8b692cd0 2019-10-21 stsp err = unlockerr;
7010 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7011 8b692cd0 2019-10-21 stsp
7012 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7013 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7014 8b692cd0 2019-10-21 stsp err = unlockerr;
7015 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7016 8b692cd0 2019-10-21 stsp
7017 8b692cd0 2019-10-21 stsp return err;
7018 2822a352 2019-10-15 stsp }
7019 2822a352 2019-10-15 stsp
7020 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7021 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7022 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7023 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7024 2db2652d 2019-08-07 stsp struct got_repository *repo;
7025 2db2652d 2019-08-07 stsp int have_changes;
7026 2db2652d 2019-08-07 stsp };
7027 2db2652d 2019-08-07 stsp
7028 2db2652d 2019-08-07 stsp const struct got_error *
7029 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7030 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7031 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7032 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7033 735ef5ac 2019-08-03 stsp {
7034 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7035 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7036 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7037 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7038 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7039 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7040 8b13ce36 2019-08-08 stsp
7041 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7042 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7043 8b13ce36 2019-08-08 stsp return NULL;
7044 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7045 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7046 735ef5ac 2019-08-03 stsp
7047 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7048 735ef5ac 2019-08-03 stsp if (ie == NULL)
7049 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7050 735ef5ac 2019-08-03 stsp
7051 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7052 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7053 735ef5ac 2019-08-03 stsp relpath) == -1)
7054 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7055 735ef5ac 2019-08-03 stsp
7056 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7057 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7058 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7059 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7060 735ef5ac 2019-08-03 stsp }
7061 735ef5ac 2019-08-03 stsp
7062 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7063 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7064 3aa5969e 2019-08-06 stsp goto done;
7065 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7066 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7067 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7068 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7069 735ef5ac 2019-08-03 stsp goto done;
7070 3aa5969e 2019-08-06 stsp }
7071 735ef5ac 2019-08-03 stsp
7072 2db2652d 2019-08-07 stsp a->have_changes = 1;
7073 2db2652d 2019-08-07 stsp
7074 735ef5ac 2019-08-03 stsp p = in_repo_path;
7075 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7076 735ef5ac 2019-08-03 stsp p++;
7077 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7078 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7079 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7080 735ef5ac 2019-08-03 stsp done:
7081 735ef5ac 2019-08-03 stsp free(in_repo_path);
7082 dc424a06 2019-08-07 stsp return err;
7083 dc424a06 2019-08-07 stsp }
7084 dc424a06 2019-08-07 stsp
7085 2db2652d 2019-08-07 stsp struct stage_path_arg {
7086 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7087 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7088 2db2652d 2019-08-07 stsp struct got_repository *repo;
7089 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7090 2db2652d 2019-08-07 stsp void *status_arg;
7091 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7092 2db2652d 2019-08-07 stsp void *patch_arg;
7093 7b5dc508 2019-10-28 stsp int staged_something;
7094 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7095 2db2652d 2019-08-07 stsp };
7096 2db2652d 2019-08-07 stsp
7097 2db2652d 2019-08-07 stsp static const struct got_error *
7098 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7099 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7100 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7101 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7102 0cb83759 2019-08-03 stsp {
7103 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7104 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7105 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7106 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7107 0cb83759 2019-08-03 stsp uint32_t stage;
7108 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7109 0aeb8099 2020-07-23 stsp struct stat sb;
7110 8b13ce36 2019-08-08 stsp
7111 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7112 8b13ce36 2019-08-08 stsp return NULL;
7113 0cb83759 2019-08-03 stsp
7114 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7115 d3e7c587 2019-08-03 stsp if (ie == NULL)
7116 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7117 0cb83759 2019-08-03 stsp
7118 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7119 2db2652d 2019-08-07 stsp relpath)== -1)
7120 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7121 0cb83759 2019-08-03 stsp
7122 0cb83759 2019-08-03 stsp switch (status) {
7123 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7124 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7125 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7126 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7127 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7128 0aeb8099 2020-07-23 stsp break;
7129 0aeb8099 2020-07-23 stsp }
7130 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7131 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7132 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7133 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7134 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7135 dc424a06 2019-08-07 stsp if (err)
7136 dc424a06 2019-08-07 stsp break;
7137 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7138 dc424a06 2019-08-07 stsp break;
7139 dc424a06 2019-08-07 stsp } else {
7140 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7141 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7142 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7143 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7144 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7145 dc424a06 2019-08-07 stsp break;
7146 dc424a06 2019-08-07 stsp }
7147 dc424a06 2019-08-07 stsp }
7148 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7149 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7150 0cb83759 2019-08-03 stsp if (err)
7151 dc424a06 2019-08-07 stsp break;
7152 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7153 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7154 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7155 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7156 0cb83759 2019-08-03 stsp else
7157 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7158 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7159 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7160 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7161 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7162 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7163 35213c7c 2020-07-23 stsp ssize_t target_len;
7164 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7165 35213c7c 2020-07-23 stsp sizeof(target_path));
7166 35213c7c 2020-07-23 stsp if (target_len == -1) {
7167 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7168 35213c7c 2020-07-23 stsp ondisk_path);
7169 35213c7c 2020-07-23 stsp break;
7170 35213c7c 2020-07-23 stsp }
7171 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7172 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7173 35213c7c 2020-07-23 stsp a->worktree->root_path);
7174 35213c7c 2020-07-23 stsp if (err)
7175 35213c7c 2020-07-23 stsp break;
7176 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7177 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7178 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7179 35213c7c 2020-07-23 stsp break;
7180 35213c7c 2020-07-23 stsp }
7181 35213c7c 2020-07-23 stsp }
7182 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7183 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7184 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7185 35213c7c 2020-07-23 stsp else
7186 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7187 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7188 0aeb8099 2020-07-23 stsp } else {
7189 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7190 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7191 0aeb8099 2020-07-23 stsp }
7192 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7193 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7194 dc424a06 2019-08-07 stsp break;
7195 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7196 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7197 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7198 0cb83759 2019-08-03 stsp break;
7199 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7200 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7201 d3e7c587 2019-08-03 stsp break;
7202 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7203 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7204 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7205 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7206 dc424a06 2019-08-07 stsp if (err)
7207 dc424a06 2019-08-07 stsp break;
7208 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7209 88f33a19 2019-08-08 stsp break;
7210 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7211 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7212 dc424a06 2019-08-07 stsp break;
7213 88f33a19 2019-08-08 stsp }
7214 dc424a06 2019-08-07 stsp }
7215 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7216 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7217 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7218 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7219 dc424a06 2019-08-07 stsp break;
7220 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7221 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7222 12463d8b 2019-12-13 stsp de_name);
7223 0cb83759 2019-08-03 stsp break;
7224 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7225 d3e7c587 2019-08-03 stsp break;
7226 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7227 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7228 ebf48fd5 2019-08-03 stsp break;
7229 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7230 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7231 2a06fe5f 2019-08-24 stsp break;
7232 0cb83759 2019-08-03 stsp default:
7233 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7234 537ac44b 2019-08-03 stsp break;
7235 0cb83759 2019-08-03 stsp }
7236 dc424a06 2019-08-07 stsp
7237 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7238 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7239 dc424a06 2019-08-07 stsp free(path_content);
7240 2db2652d 2019-08-07 stsp free(ondisk_path);
7241 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7242 0ebf8283 2019-07-24 stsp return err;
7243 0ebf8283 2019-07-24 stsp }
7244 0cb83759 2019-08-03 stsp
7245 0cb83759 2019-08-03 stsp const struct got_error *
7246 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7247 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7248 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7249 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7250 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7251 0cb83759 2019-08-03 stsp {
7252 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7253 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7254 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7255 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7256 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7257 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7258 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7259 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7260 0cb83759 2019-08-03 stsp
7261 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7262 0cb83759 2019-08-03 stsp if (err)
7263 0cb83759 2019-08-03 stsp return err;
7264 0cb83759 2019-08-03 stsp
7265 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7266 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7267 735ef5ac 2019-08-03 stsp if (err)
7268 735ef5ac 2019-08-03 stsp goto done;
7269 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7270 735ef5ac 2019-08-03 stsp if (err)
7271 735ef5ac 2019-08-03 stsp goto done;
7272 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7273 0cb83759 2019-08-03 stsp if (err)
7274 0cb83759 2019-08-03 stsp goto done;
7275 0cb83759 2019-08-03 stsp
7276 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7277 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7278 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7279 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7280 2db2652d 2019-08-07 stsp oka.repo = repo;
7281 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7282 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7283 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7284 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7285 735ef5ac 2019-08-03 stsp if (err)
7286 735ef5ac 2019-08-03 stsp goto done;
7287 735ef5ac 2019-08-03 stsp }
7288 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7289 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7290 2db2652d 2019-08-07 stsp goto done;
7291 2db2652d 2019-08-07 stsp }
7292 735ef5ac 2019-08-03 stsp
7293 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7294 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7295 2db2652d 2019-08-07 stsp spa.repo = repo;
7296 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7297 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7298 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7299 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7300 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7301 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7302 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7303 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7304 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7305 42005733 2019-08-03 stsp if (err)
7306 2db2652d 2019-08-07 stsp goto done;
7307 7b5dc508 2019-10-28 stsp }
7308 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7309 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7310 7b5dc508 2019-10-28 stsp goto done;
7311 0cb83759 2019-08-03 stsp }
7312 0cb83759 2019-08-03 stsp
7313 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7314 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7315 0cb83759 2019-08-03 stsp err = sync_err;
7316 0cb83759 2019-08-03 stsp done:
7317 735ef5ac 2019-08-03 stsp if (head_ref)
7318 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7319 735ef5ac 2019-08-03 stsp free(head_commit_id);
7320 0cb83759 2019-08-03 stsp free(fileindex_path);
7321 0cb83759 2019-08-03 stsp if (fileindex)
7322 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7323 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7324 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7325 0cb83759 2019-08-03 stsp err = unlockerr;
7326 0cb83759 2019-08-03 stsp return err;
7327 0cb83759 2019-08-03 stsp }
7328 ad493afc 2019-08-03 stsp
7329 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7330 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7331 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7332 ad493afc 2019-08-03 stsp struct got_repository *repo;
7333 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7334 ad493afc 2019-08-03 stsp void *progress_arg;
7335 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7336 2e1f37b0 2019-08-08 stsp void *patch_arg;
7337 ad493afc 2019-08-03 stsp };
7338 2e1f37b0 2019-08-08 stsp
7339 2e1f37b0 2019-08-08 stsp static const struct got_error *
7340 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7341 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7342 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7343 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7344 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7345 2e1f37b0 2019-08-08 stsp {
7346 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7347 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7348 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7349 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7350 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7351 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7352 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7353 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7354 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7355 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7356 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7357 2e1f37b0 2019-08-08 stsp
7358 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7359 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7360 2e1f37b0 2019-08-08 stsp
7361 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7362 2e1f37b0 2019-08-08 stsp if (err)
7363 2e1f37b0 2019-08-08 stsp return err;
7364 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7365 2e1f37b0 2019-08-08 stsp if (err)
7366 2e1f37b0 2019-08-08 stsp goto done;
7367 2e1f37b0 2019-08-08 stsp
7368 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7369 2e1f37b0 2019-08-08 stsp if (err)
7370 2e1f37b0 2019-08-08 stsp goto done;
7371 2e1f37b0 2019-08-08 stsp
7372 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7373 2e1f37b0 2019-08-08 stsp if (err)
7374 2e1f37b0 2019-08-08 stsp goto done;
7375 2e1f37b0 2019-08-08 stsp
7376 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7377 2e1f37b0 2019-08-08 stsp if (err)
7378 2e1f37b0 2019-08-08 stsp goto done;
7379 2e1f37b0 2019-08-08 stsp
7380 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7381 2e1f37b0 2019-08-08 stsp if (err)
7382 2e1f37b0 2019-08-08 stsp goto done;
7383 ad493afc 2019-08-03 stsp
7384 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7385 2e1f37b0 2019-08-08 stsp if (err)
7386 2e1f37b0 2019-08-08 stsp goto done;
7387 2e1f37b0 2019-08-08 stsp
7388 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7389 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7390 2e1f37b0 2019-08-08 stsp goto done;
7391 2e1f37b0 2019-08-08 stsp }
7392 2e1f37b0 2019-08-08 stsp
7393 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7394 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7395 2e1f37b0 2019-08-08 stsp goto done;
7396 2e1f37b0 2019-08-08 stsp }
7397 2e1f37b0 2019-08-08 stsp
7398 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7399 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7400 2e1f37b0 2019-08-08 stsp if (err)
7401 2e1f37b0 2019-08-08 stsp goto done;
7402 2e1f37b0 2019-08-08 stsp
7403 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7404 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7405 2e1f37b0 2019-08-08 stsp if (err)
7406 2e1f37b0 2019-08-08 stsp goto done;
7407 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7408 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7409 2e1f37b0 2019-08-08 stsp if (err)
7410 2e1f37b0 2019-08-08 stsp goto done;
7411 2e1f37b0 2019-08-08 stsp
7412 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7413 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7414 2e1f37b0 2019-08-08 stsp goto done;
7415 2e1f37b0 2019-08-08 stsp }
7416 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7417 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7418 2e1f37b0 2019-08-08 stsp goto done;
7419 2e1f37b0 2019-08-08 stsp }
7420 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7421 2e1f37b0 2019-08-08 stsp int choice;
7422 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7423 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7424 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7425 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7426 2e1f37b0 2019-08-08 stsp if (err)
7427 2e1f37b0 2019-08-08 stsp goto done;
7428 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7429 2e1f37b0 2019-08-08 stsp have_content = 1;
7430 19e4b907 2019-08-08 stsp else
7431 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7432 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7433 2e1f37b0 2019-08-08 stsp break;
7434 2e1f37b0 2019-08-08 stsp }
7435 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7436 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7437 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7438 2e1f37b0 2019-08-08 stsp done:
7439 2e1f37b0 2019-08-08 stsp free(label1);
7440 2e1f37b0 2019-08-08 stsp if (blob)
7441 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7442 2e1f37b0 2019-08-08 stsp if (staged_blob)
7443 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7444 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7445 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7446 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7447 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7448 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7449 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7450 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7451 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7452 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7453 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7454 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7455 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7456 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7457 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7458 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7459 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7460 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7461 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7462 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7463 2e1f37b0 2019-08-08 stsp }
7464 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7465 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7466 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7467 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7468 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7469 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7470 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7471 2e1f37b0 2019-08-08 stsp }
7472 2e1f37b0 2019-08-08 stsp free(args);
7473 2e1f37b0 2019-08-08 stsp if (ds) {
7474 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7475 2e1f37b0 2019-08-08 stsp free(ds);
7476 2e1f37b0 2019-08-08 stsp }
7477 2e1f37b0 2019-08-08 stsp if (changes)
7478 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7479 2e1f37b0 2019-08-08 stsp free(path1);
7480 2e1f37b0 2019-08-08 stsp free(path2);
7481 fda8017d 2020-07-23 stsp return err;
7482 fda8017d 2020-07-23 stsp }
7483 fda8017d 2020-07-23 stsp
7484 fda8017d 2020-07-23 stsp static const struct got_error *
7485 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7486 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7487 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7488 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7489 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7490 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7491 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7492 fda8017d 2020-07-23 stsp {
7493 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7494 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7495 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7496 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7497 fda8017d 2020-07-23 stsp FILE *f = NULL;
7498 fda8017d 2020-07-23 stsp struct stat sb;
7499 fda8017d 2020-07-23 stsp
7500 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7501 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7502 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7503 fda8017d 2020-07-23 stsp if (err)
7504 fda8017d 2020-07-23 stsp return err;
7505 fda8017d 2020-07-23 stsp
7506 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7507 fda8017d 2020-07-23 stsp return NULL;
7508 fda8017d 2020-07-23 stsp
7509 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7510 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7511 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7512 fda8017d 2020-07-23 stsp if (err)
7513 fda8017d 2020-07-23 stsp goto done;
7514 fda8017d 2020-07-23 stsp }
7515 fda8017d 2020-07-23 stsp
7516 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7517 fda8017d 2020-07-23 stsp if (f == NULL) {
7518 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7519 fda8017d 2020-07-23 stsp path_unstaged_content);
7520 fda8017d 2020-07-23 stsp goto done;
7521 fda8017d 2020-07-23 stsp }
7522 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7523 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7524 fda8017d 2020-07-23 stsp goto done;
7525 fda8017d 2020-07-23 stsp }
7526 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7527 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7528 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7529 fda8017d 2020-07-23 stsp size_t r;
7530 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7531 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7532 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7533 fda8017d 2020-07-23 stsp goto done;
7534 fda8017d 2020-07-23 stsp }
7535 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7536 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7537 fda8017d 2020-07-23 stsp goto done;
7538 fda8017d 2020-07-23 stsp }
7539 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7540 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7541 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7542 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7543 fda8017d 2020-07-23 stsp progress_arg);
7544 fda8017d 2020-07-23 stsp } else {
7545 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7546 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7547 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7548 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7549 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7550 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7551 fda8017d 2020-07-23 stsp }
7552 fda8017d 2020-07-23 stsp if (err)
7553 fda8017d 2020-07-23 stsp goto done;
7554 fda8017d 2020-07-23 stsp
7555 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7556 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7557 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7558 fda8017d 2020-07-23 stsp } else
7559 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7560 fda8017d 2020-07-23 stsp done:
7561 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7562 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7563 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7564 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7565 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7566 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7567 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7568 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7569 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7570 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7571 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7572 2e1f37b0 2019-08-08 stsp return err;
7573 2e1f37b0 2019-08-08 stsp }
7574 2e1f37b0 2019-08-08 stsp
7575 ad493afc 2019-08-03 stsp static const struct got_error *
7576 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7577 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7578 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7579 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7580 ad493afc 2019-08-03 stsp {
7581 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7582 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7583 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7584 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7585 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7586 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7587 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7588 9bc94a15 2019-08-03 stsp struct stat sb;
7589 ad493afc 2019-08-03 stsp
7590 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7591 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7592 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7593 2e1f37b0 2019-08-08 stsp return NULL;
7594 2e1f37b0 2019-08-08 stsp
7595 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7596 ad493afc 2019-08-03 stsp if (ie == NULL)
7597 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7598 9bc94a15 2019-08-03 stsp
7599 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7600 9bc94a15 2019-08-03 stsp == -1)
7601 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7602 ad493afc 2019-08-03 stsp
7603 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7604 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7605 f69721c3 2019-10-21 stsp if (err)
7606 f69721c3 2019-10-21 stsp goto done;
7607 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7608 f69721c3 2019-10-21 stsp id_str) == -1) {
7609 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7610 f69721c3 2019-10-21 stsp goto done;
7611 f69721c3 2019-10-21 stsp }
7612 f69721c3 2019-10-21 stsp
7613 ad493afc 2019-08-03 stsp switch (staged_status) {
7614 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7615 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7616 ad493afc 2019-08-03 stsp blob_id, 8192);
7617 ad493afc 2019-08-03 stsp if (err)
7618 ad493afc 2019-08-03 stsp break;
7619 ad493afc 2019-08-03 stsp /* fall through */
7620 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7621 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7622 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7623 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7624 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7625 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7626 2e1f37b0 2019-08-08 stsp if (err)
7627 2e1f37b0 2019-08-08 stsp break;
7628 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7629 2e1f37b0 2019-08-08 stsp break;
7630 2e1f37b0 2019-08-08 stsp } else {
7631 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7632 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7633 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7634 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7635 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7636 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7637 2e1f37b0 2019-08-08 stsp }
7638 2e1f37b0 2019-08-08 stsp }
7639 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7640 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7641 ad493afc 2019-08-03 stsp if (err)
7642 ad493afc 2019-08-03 stsp break;
7643 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7644 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7645 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7646 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7647 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7648 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7649 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7650 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7651 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7652 ea7786be 2020-07-23 stsp break;
7653 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7654 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7655 36bf999c 2020-07-23 stsp char *staged_target;
7656 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7657 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7658 36bf999c 2020-07-23 stsp if (err)
7659 36bf999c 2020-07-23 stsp goto done;
7660 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7661 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7662 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7663 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7664 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7665 36bf999c 2020-07-23 stsp free(staged_target);
7666 dfe9fba0 2020-07-23 stsp } else {
7667 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7668 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7669 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7670 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7671 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7672 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7673 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7674 dfe9fba0 2020-07-23 stsp }
7675 ea7786be 2020-07-23 stsp break;
7676 ea7786be 2020-07-23 stsp default:
7677 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7678 ea7786be 2020-07-23 stsp break;
7679 ea7786be 2020-07-23 stsp }
7680 ad493afc 2019-08-03 stsp if (err == NULL)
7681 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7682 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7683 ad493afc 2019-08-03 stsp break;
7684 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7685 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7686 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7687 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7688 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7689 2e1f37b0 2019-08-08 stsp if (err)
7690 2e1f37b0 2019-08-08 stsp break;
7691 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7692 2e1f37b0 2019-08-08 stsp break;
7693 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7694 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7695 2e1f37b0 2019-08-08 stsp break;
7696 2e1f37b0 2019-08-08 stsp }
7697 2e1f37b0 2019-08-08 stsp }
7698 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7699 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7700 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7701 9bc94a15 2019-08-03 stsp if (err)
7702 9bc94a15 2019-08-03 stsp break;
7703 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7704 ad493afc 2019-08-03 stsp break;
7705 ad493afc 2019-08-03 stsp }
7706 f69721c3 2019-10-21 stsp done:
7707 ad493afc 2019-08-03 stsp free(ondisk_path);
7708 ad493afc 2019-08-03 stsp if (blob_base)
7709 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7710 ad493afc 2019-08-03 stsp if (blob_staged)
7711 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7712 f69721c3 2019-10-21 stsp free(id_str);
7713 f69721c3 2019-10-21 stsp free(label_orig);
7714 ad493afc 2019-08-03 stsp return err;
7715 ad493afc 2019-08-03 stsp }
7716 ad493afc 2019-08-03 stsp
7717 ad493afc 2019-08-03 stsp const struct got_error *
7718 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7719 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7720 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7721 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7722 ad493afc 2019-08-03 stsp struct got_repository *repo)
7723 ad493afc 2019-08-03 stsp {
7724 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7725 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7726 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7727 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7728 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7729 ad493afc 2019-08-03 stsp
7730 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7731 ad493afc 2019-08-03 stsp if (err)
7732 ad493afc 2019-08-03 stsp return err;
7733 ad493afc 2019-08-03 stsp
7734 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7735 ad493afc 2019-08-03 stsp if (err)
7736 ad493afc 2019-08-03 stsp goto done;
7737 ad493afc 2019-08-03 stsp
7738 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7739 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7740 ad493afc 2019-08-03 stsp upa.repo = repo;
7741 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7742 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7743 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7744 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7745 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7746 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7747 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7748 ad493afc 2019-08-03 stsp if (err)
7749 ad493afc 2019-08-03 stsp goto done;
7750 ad493afc 2019-08-03 stsp }
7751 ad493afc 2019-08-03 stsp
7752 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7753 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7754 ad493afc 2019-08-03 stsp err = sync_err;
7755 ad493afc 2019-08-03 stsp done:
7756 ad493afc 2019-08-03 stsp free(fileindex_path);
7757 ad493afc 2019-08-03 stsp if (fileindex)
7758 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7759 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7760 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7761 ad493afc 2019-08-03 stsp err = unlockerr;
7762 ad493afc 2019-08-03 stsp return err;
7763 ad493afc 2019-08-03 stsp }
7764 b2118c49 2020-07-28 stsp
7765 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7766 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7767 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7768 b2118c49 2020-07-28 stsp void *info_arg;
7769 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7770 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7771 b2118c49 2020-07-28 stsp void *cancel_arg;
7772 b2118c49 2020-07-28 stsp };
7773 b2118c49 2020-07-28 stsp
7774 b2118c49 2020-07-28 stsp static const struct got_error *
7775 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7776 b2118c49 2020-07-28 stsp {
7777 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7778 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7779 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7780 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7781 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7782 b2118c49 2020-07-28 stsp int stage;
7783 b2118c49 2020-07-28 stsp
7784 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7785 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7786 b2118c49 2020-07-28 stsp
7787 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7788 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7789 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7790 b2118c49 2020-07-28 stsp break;
7791 b2118c49 2020-07-28 stsp }
7792 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7793 b2118c49 2020-07-28 stsp return NULL;
7794 b2118c49 2020-07-28 stsp
7795 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7796 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7797 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7798 b2118c49 2020-07-28 stsp }
7799 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7800 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7801 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7802 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7803 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7804 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7805 b2118c49 2020-07-28 stsp }
7806 b2118c49 2020-07-28 stsp
7807 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7808 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7809 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7810 b2118c49 2020-07-28 stsp }
7811 b2118c49 2020-07-28 stsp
7812 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7813 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7814 b2118c49 2020-07-28 stsp }
7815 b2118c49 2020-07-28 stsp
7816 b2118c49 2020-07-28 stsp const struct got_error *
7817 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7818 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7819 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7820 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7821 b2118c49 2020-07-28 stsp
7822 b2118c49 2020-07-28 stsp {
7823 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7824 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7825 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7826 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7827 b2118c49 2020-07-28 stsp
7828 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7829 b2118c49 2020-07-28 stsp if (err)
7830 b2118c49 2020-07-28 stsp return err;
7831 b2118c49 2020-07-28 stsp
7832 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7833 b2118c49 2020-07-28 stsp if (err)
7834 b2118c49 2020-07-28 stsp goto done;
7835 b2118c49 2020-07-28 stsp
7836 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7837 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7838 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7839 b2118c49 2020-07-28 stsp arg.paths = paths;
7840 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7841 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7842 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7843 b2118c49 2020-07-28 stsp &arg);
7844 b2118c49 2020-07-28 stsp done:
7845 b2118c49 2020-07-28 stsp free(fileindex_path);
7846 b2118c49 2020-07-28 stsp if (fileindex)
7847 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7848 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7849 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7850 b2118c49 2020-07-28 stsp err = unlockerr;
7851 b2118c49 2020-07-28 stsp return err;
7852 b2118c49 2020-07-28 stsp }