Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp
59 9d31a1d8 2018-03-11 stsp #ifndef MIN
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 9d31a1d8 2018-03-11 stsp #endif
62 f69721c3 2019-10-21 stsp
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 86c3caaf 2018-03-09 stsp
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
68 99724ed4 2018-03-10 stsp {
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
70 99724ed4 2018-03-10 stsp char *path;
71 99724ed4 2018-03-10 stsp
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
76 99724ed4 2018-03-10 stsp free(path);
77 507dc3bb 2018-12-29 stsp return err;
78 507dc3bb 2018-12-29 stsp }
79 507dc3bb 2018-12-29 stsp
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
82 507dc3bb 2018-12-29 stsp {
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
87 507dc3bb 2018-12-29 stsp
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp }
93 507dc3bb 2018-12-29 stsp
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
95 507dc3bb 2018-12-29 stsp if (err)
96 507dc3bb 2018-12-29 stsp goto done;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
102 507dc3bb 2018-12-29 stsp goto done;
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
109 507dc3bb 2018-12-29 stsp goto done;
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp
112 507dc3bb 2018-12-29 stsp done:
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
117 99724ed4 2018-03-10 stsp }
118 99724ed4 2018-03-10 stsp
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
121 09fe317a 2018-03-11 stsp {
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
125 09fe317a 2018-03-11 stsp ssize_t n;
126 09fe317a 2018-03-11 stsp struct stat sb;
127 09fe317a 2018-03-11 stsp
128 09fe317a 2018-03-11 stsp *content = NULL;
129 09fe317a 2018-03-11 stsp
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
133 09fe317a 2018-03-11 stsp goto done;
134 09fe317a 2018-03-11 stsp }
135 09fe317a 2018-03-11 stsp
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 f02eaa22 2019-03-11 stsp else
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
142 ef99fdb1 2018-03-11 stsp goto done;
143 ef99fdb1 2018-03-11 stsp }
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
147 09fe317a 2018-03-11 stsp goto done;
148 09fe317a 2018-03-11 stsp }
149 09fe317a 2018-03-11 stsp
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
152 d10c9b58 2019-02-19 stsp goto done;
153 d10c9b58 2019-02-19 stsp }
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
157 09fe317a 2018-03-11 stsp goto done;
158 09fe317a 2018-03-11 stsp }
159 09fe317a 2018-03-11 stsp
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
164 09fe317a 2018-03-11 stsp goto done;
165 09fe317a 2018-03-11 stsp }
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 09fe317a 2018-03-11 stsp goto done;
169 09fe317a 2018-03-11 stsp }
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
171 09fe317a 2018-03-11 stsp
172 09fe317a 2018-03-11 stsp done:
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
176 09fe317a 2018-03-11 stsp if (err) {
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
179 09fe317a 2018-03-11 stsp }
180 09fe317a 2018-03-11 stsp return err;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
185 024e9686 2019-05-14 stsp {
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
188 024e9686 2019-05-14 stsp
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
193 024e9686 2019-05-14 stsp } else {
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
197 024e9686 2019-05-14 stsp }
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
201 024e9686 2019-05-14 stsp }
202 024e9686 2019-05-14 stsp
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
206 86c3caaf 2018-03-09 stsp {
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
217 65596e15 2018-12-24 stsp
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
220 0c48fee2 2019-03-11 stsp goto done;
221 0c48fee2 2019-03-11 stsp }
222 0c48fee2 2019-03-11 stsp
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
224 65596e15 2018-12-24 stsp if (err)
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
227 65596e15 2018-12-24 stsp if (err)
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
231 86c3caaf 2018-03-09 stsp
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
235 0bb8a95e 2018-03-12 stsp }
236 577ec78f 2018-03-11 stsp
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
240 86c3caaf 2018-03-09 stsp goto done;
241 86c3caaf 2018-03-09 stsp }
242 86c3caaf 2018-03-09 stsp
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 86c3caaf 2018-03-09 stsp goto done;
247 86c3caaf 2018-03-09 stsp }
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
250 86c3caaf 2018-03-09 stsp goto done;
251 86c3caaf 2018-03-09 stsp }
252 86c3caaf 2018-03-09 stsp
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 056e7441 2018-03-11 stsp if (err)
256 056e7441 2018-03-11 stsp goto done;
257 056e7441 2018-03-11 stsp
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 99724ed4 2018-03-10 stsp if (err)
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 65596e15 2018-12-24 stsp
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
270 65596e15 2018-12-24 stsp if (err)
271 65596e15 2018-12-24 stsp goto done;
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 99724ed4 2018-03-10 stsp if (err)
274 86c3caaf 2018-03-09 stsp goto done;
275 86c3caaf 2018-03-09 stsp
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
279 99724ed4 2018-03-10 stsp if (err)
280 86c3caaf 2018-03-09 stsp goto done;
281 86c3caaf 2018-03-09 stsp
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
285 ec22038e 2019-03-10 stsp if (err)
286 ec22038e 2019-03-10 stsp goto done;
287 ec22038e 2019-03-10 stsp
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
297 ec22038e 2019-03-10 stsp goto done;
298 ec22038e 2019-03-10 stsp }
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 577ec78f 2018-03-11 stsp if (err)
301 577ec78f 2018-03-11 stsp goto done;
302 577ec78f 2018-03-11 stsp
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
306 1451e70d 2018-03-10 stsp goto done;
307 1451e70d 2018-03-10 stsp }
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 99724ed4 2018-03-10 stsp if (err)
310 1451e70d 2018-03-10 stsp goto done;
311 1451e70d 2018-03-10 stsp
312 86c3caaf 2018-03-09 stsp done:
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
324 86c3caaf 2018-03-09 stsp {
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 6d9d28c3 2018-03-11 stsp
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
341 6d9d28c3 2018-03-11 stsp goto done;
342 6d9d28c3 2018-03-11 stsp }
343 6d9d28c3 2018-03-11 stsp
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
347 6d9d28c3 2018-03-11 stsp goto done;
348 6d9d28c3 2018-03-11 stsp }
349 6d9d28c3 2018-03-11 stsp
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp }
356 6d9d28c3 2018-03-11 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 6d9d28c3 2018-03-11 stsp if (err)
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
365 6d9d28c3 2018-03-11 stsp goto done;
366 6d9d28c3 2018-03-11 stsp }
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 6d9d28c3 2018-03-11 stsp
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
375 6d9d28c3 2018-03-11 stsp goto done;
376 6d9d28c3 2018-03-11 stsp }
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
378 6d9d28c3 2018-03-11 stsp
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
382 6d9d28c3 2018-03-11 stsp goto done;
383 6d9d28c3 2018-03-11 stsp }
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
386 6d9d28c3 2018-03-11 stsp if (err)
387 6d9d28c3 2018-03-11 stsp goto done;
388 eaccb85f 2018-12-25 stsp
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
391 93a30277 2018-12-24 stsp if (err)
392 93a30277 2018-12-24 stsp goto done;
393 93a30277 2018-12-24 stsp
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
396 c442a90d 2019-03-10 stsp if (err)
397 c442a90d 2019-03-10 stsp goto done;
398 c442a90d 2019-03-10 stsp
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 eaccb85f 2018-12-25 stsp if (err)
401 c442a90d 2019-03-10 stsp goto done;
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
405 eaccb85f 2018-12-25 stsp goto done;
406 c442a90d 2019-03-10 stsp }
407 eaccb85f 2018-12-25 stsp
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 eaccb85f 2018-12-25 stsp if (err)
410 eaccb85f 2018-12-25 stsp goto done;
411 eaccb85f 2018-12-25 stsp
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
414 f5baf295 2018-03-11 stsp if (err)
415 6d9d28c3 2018-03-11 stsp goto done;
416 6d9d28c3 2018-03-11 stsp
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
419 6d9d28c3 2018-03-11 stsp done:
420 eaccb85f 2018-12-25 stsp if (repo)
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
427 6d9d28c3 2018-03-11 stsp if (err) {
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
429 6d9d28c3 2018-03-11 stsp close(fd);
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
433 6d9d28c3 2018-03-11 stsp } else
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
435 6d9d28c3 2018-03-11 stsp
436 6d9d28c3 2018-03-11 stsp return err;
437 86c3caaf 2018-03-09 stsp }
438 247140b2 2019-02-05 stsp
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
441 247140b2 2019-02-05 stsp {
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
443 86c3caaf 2018-03-09 stsp
444 247140b2 2019-02-05 stsp do {
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
454 247140b2 2019-02-05 stsp
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
456 247140b2 2019-02-05 stsp }
457 247140b2 2019-02-05 stsp
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
460 86c3caaf 2018-03-09 stsp {
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
473 86c3caaf 2018-03-09 stsp }
474 86c3caaf 2018-03-09 stsp
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
477 c7f4312f 2019-02-05 stsp {
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
479 c7f4312f 2019-02-05 stsp }
480 c7f4312f 2019-02-05 stsp
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
483 86c3caaf 2018-03-09 stsp {
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
485 49520a32 2018-12-29 stsp }
486 49520a32 2018-12-29 stsp
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
489 49520a32 2018-12-29 stsp {
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
491 e5dc7198 2018-12-29 stsp }
492 e5dc7198 2018-12-29 stsp
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
496 e5dc7198 2018-12-29 stsp {
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
498 e5dc7198 2018-12-29 stsp
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
502 e5dc7198 2018-12-29 stsp }
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
507 86c3caaf 2018-03-09 stsp }
508 86c3caaf 2018-03-09 stsp
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
511 86c3caaf 2018-03-09 stsp {
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
513 024e9686 2019-05-14 stsp }
514 024e9686 2019-05-14 stsp
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
518 024e9686 2019-05-14 stsp {
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
521 024e9686 2019-05-14 stsp
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
526 024e9686 2019-05-14 stsp goto done;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp }
534 024e9686 2019-05-14 stsp
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
536 024e9686 2019-05-14 stsp if (err)
537 024e9686 2019-05-14 stsp goto done;
538 024e9686 2019-05-14 stsp
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
541 024e9686 2019-05-14 stsp done:
542 024e9686 2019-05-14 stsp free(path_got);
543 024e9686 2019-05-14 stsp if (err)
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
546 507dc3bb 2018-12-29 stsp }
547 507dc3bb 2018-12-29 stsp
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
550 507dc3bb 2018-12-29 stsp {
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
552 86c3caaf 2018-03-09 stsp }
553 86c3caaf 2018-03-09 stsp
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
557 507dc3bb 2018-12-29 stsp {
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
562 507dc3bb 2018-12-29 stsp
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
567 507dc3bb 2018-12-29 stsp goto done;
568 507dc3bb 2018-12-29 stsp }
569 507dc3bb 2018-12-29 stsp
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
571 507dc3bb 2018-12-29 stsp if (err)
572 507dc3bb 2018-12-29 stsp return err;
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
576 507dc3bb 2018-12-29 stsp goto done;
577 507dc3bb 2018-12-29 stsp }
578 507dc3bb 2018-12-29 stsp
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
581 507dc3bb 2018-12-29 stsp if (err)
582 507dc3bb 2018-12-29 stsp goto done;
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 507dc3bb 2018-12-29 stsp if (err)
585 507dc3bb 2018-12-29 stsp goto done;
586 507dc3bb 2018-12-29 stsp
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
591 507dc3bb 2018-12-29 stsp goto done;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp done:
594 507dc3bb 2018-12-29 stsp if (obj)
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
599 507dc3bb 2018-12-29 stsp }
600 507dc3bb 2018-12-29 stsp
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
603 86c3caaf 2018-03-09 stsp {
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
609 21908da4 2019-01-13 stsp }
610 21908da4 2019-01-13 stsp
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
613 4a1ddfc2 2019-01-12 stsp {
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
616 4a1ddfc2 2019-01-12 stsp
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
619 4a1ddfc2 2019-01-12 stsp
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
629 ddcd8544 2019-03-11 stsp }
630 ddcd8544 2019-03-11 stsp }
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
633 68c76935 2019-02-19 stsp }
634 68c76935 2019-02-19 stsp
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
637 68c76935 2019-02-19 stsp {
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
642 68c76935 2019-02-19 stsp
643 68c76935 2019-02-19 stsp *same = 1;
644 68c76935 2019-02-19 stsp
645 230a42bd 2019-05-11 jcs for (;;) {
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp }
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
658 68c76935 2019-02-19 stsp *same = 0;
659 68c76935 2019-02-19 stsp break;
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
662 68c76935 2019-02-19 stsp *same = 0;
663 68c76935 2019-02-19 stsp break;
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 68c76935 2019-02-19 stsp *same = 0;
667 68c76935 2019-02-19 stsp break;
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp } else {
670 68c76935 2019-02-19 stsp *same = 0;
671 68c76935 2019-02-19 stsp break;
672 68c76935 2019-02-19 stsp }
673 68c76935 2019-02-19 stsp }
674 68c76935 2019-02-19 stsp
675 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp }
677 68c76935 2019-02-19 stsp
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
680 68c76935 2019-02-19 stsp {
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
685 68c76935 2019-02-19 stsp
686 68c76935 2019-02-19 stsp *same = 1;
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
690 68c76935 2019-02-19 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
693 68c76935 2019-02-19 stsp
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
696 68c76935 2019-02-19 stsp goto done;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp if (size1 != size2) {
701 68c76935 2019-02-19 stsp *same = 0;
702 68c76935 2019-02-19 stsp return NULL;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
712 68c76935 2019-02-19 stsp goto done;
713 68c76935 2019-02-19 stsp }
714 68c76935 2019-02-19 stsp
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
716 68c76935 2019-02-19 stsp done:
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
721 68c76935 2019-02-19 stsp
722 6353ad76 2019-02-08 stsp return err;
723 6353ad76 2019-02-08 stsp }
724 6353ad76 2019-02-08 stsp
725 6353ad76 2019-02-08 stsp /*
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
729 6353ad76 2019-02-08 stsp */
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
737 6353ad76 2019-02-08 stsp {
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
745 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
746 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
747 6353ad76 2019-02-08 stsp
748 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
749 234035bc 2019-06-01 stsp
750 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
751 af54ae4a 2019-02-19 stsp if (parent == NULL)
752 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
753 af54ae4a 2019-02-19 stsp
754 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
756 af54ae4a 2019-02-19 stsp
757 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 6353ad76 2019-02-08 stsp if (err)
759 6353ad76 2019-02-08 stsp goto done;
760 6353ad76 2019-02-08 stsp
761 af54ae4a 2019-02-19 stsp free(base_path);
762 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
764 af54ae4a 2019-02-19 stsp base_path = NULL;
765 af54ae4a 2019-02-19 stsp goto done;
766 af54ae4a 2019-02-19 stsp }
767 af54ae4a 2019-02-19 stsp
768 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 6353ad76 2019-02-08 stsp if (err)
770 6353ad76 2019-02-08 stsp goto done;
771 46b6ee73 2019-06-04 stsp if (blob_orig) {
772 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 46b6ee73 2019-06-04 stsp blob_orig);
774 1430b4e0 2019-03-27 stsp if (err)
775 1430b4e0 2019-03-27 stsp goto done;
776 1430b4e0 2019-03-27 stsp } else {
777 1430b4e0 2019-03-27 stsp /*
778 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
779 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
780 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
781 1430b4e0 2019-03-27 stsp */
782 1430b4e0 2019-03-27 stsp }
783 6353ad76 2019-02-08 stsp
784 3b9f0f87 2020-07-23 stsp /*
785 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
786 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
787 3b9f0f87 2020-07-23 stsp */
788 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
789 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
790 3b9f0f87 2020-07-23 stsp ssize_t target_len;
791 3b9f0f87 2020-07-23 stsp size_t n;
792 3b9f0f87 2020-07-23 stsp
793 3b9f0f87 2020-07-23 stsp free(base_path);
794 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
795 3b9f0f87 2020-07-23 stsp parent) == -1) {
796 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
797 3b9f0f87 2020-07-23 stsp base_path = NULL;
798 3b9f0f87 2020-07-23 stsp goto done;
799 3b9f0f87 2020-07-23 stsp }
800 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 3b9f0f87 2020-07-23 stsp if (err)
802 3b9f0f87 2020-07-23 stsp goto done;
803 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
804 3b9f0f87 2020-07-23 stsp sizeof(target_path));
805 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
806 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
807 3b9f0f87 2020-07-23 stsp goto done;
808 3b9f0f87 2020-07-23 stsp }
809 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
810 3b9f0f87 2020-07-23 stsp if (n != target_len) {
811 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
812 3b9f0f87 2020-07-23 stsp goto done;
813 3b9f0f87 2020-07-23 stsp }
814 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
815 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
816 3b9f0f87 2020-07-23 stsp goto done;
817 3b9f0f87 2020-07-23 stsp }
818 3b9f0f87 2020-07-23 stsp }
819 3b9f0f87 2020-07-23 stsp
820 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
823 6353ad76 2019-02-08 stsp if (err)
824 6353ad76 2019-02-08 stsp goto done;
825 6353ad76 2019-02-08 stsp
826 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
827 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 1ee397ad 2019-07-12 stsp if (err)
829 1ee397ad 2019-07-12 stsp goto done;
830 6353ad76 2019-02-08 stsp
831 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
832 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
833 816dc654 2019-02-16 stsp goto done;
834 68c76935 2019-02-19 stsp }
835 68c76935 2019-02-19 stsp
836 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
837 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
838 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
839 68c76935 2019-02-19 stsp merged_path);
840 68c76935 2019-02-19 stsp if (err)
841 68c76935 2019-02-19 stsp goto done;
842 816dc654 2019-02-16 stsp }
843 6353ad76 2019-02-08 stsp
844 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
845 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
846 70a0c8ec 2019-02-20 stsp goto done;
847 70a0c8ec 2019-02-20 stsp }
848 70a0c8ec 2019-02-20 stsp
849 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
850 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
851 230a42bd 2019-05-11 jcs ondisk_path);
852 6353ad76 2019-02-08 stsp goto done;
853 6353ad76 2019-02-08 stsp }
854 6353ad76 2019-02-08 stsp done:
855 fdcb7daf 2019-12-15 stsp if (err) {
856 fdcb7daf 2019-12-15 stsp if (merged_path)
857 fdcb7daf 2019-12-15 stsp unlink(merged_path);
858 3b9f0f87 2020-07-23 stsp }
859 3b9f0f87 2020-07-23 stsp if (symlink_path) {
860 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
861 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
862 fdcb7daf 2019-12-15 stsp }
863 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
865 3b9f0f87 2020-07-23 stsp free(symlink_path);
866 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
868 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
870 6353ad76 2019-02-08 stsp free(merged_path);
871 af54ae4a 2019-02-19 stsp free(base_path);
872 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
873 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
874 46b6ee73 2019-06-04 stsp free(blob_orig_path);
875 af57b12a 2020-07-23 stsp }
876 af57b12a 2020-07-23 stsp return err;
877 af57b12a 2020-07-23 stsp }
878 af57b12a 2020-07-23 stsp
879 af57b12a 2020-07-23 stsp static const struct got_error *
880 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
881 af57b12a 2020-07-23 stsp size_t target_len)
882 af57b12a 2020-07-23 stsp {
883 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
884 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
885 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
886 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
887 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
888 af57b12a 2020-07-23 stsp ondisk_path);
889 af57b12a 2020-07-23 stsp return NULL;
890 af57b12a 2020-07-23 stsp }
891 af57b12a 2020-07-23 stsp
892 af57b12a 2020-07-23 stsp /*
893 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
895 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
896 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
897 993e2a1b 2020-07-23 stsp *
898 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
899 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
900 993e2a1b 2020-07-23 stsp *
901 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
902 993e2a1b 2020-07-23 stsp * has deleted this symlink.
903 11cc08c1 2020-07-23 stsp */
904 11cc08c1 2020-07-23 stsp static const struct got_error *
905 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
906 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
908 11cc08c1 2020-07-23 stsp {
909 11cc08c1 2020-07-23 stsp const struct got_error *err;
910 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 11cc08c1 2020-07-23 stsp FILE *f = NULL;
912 11cc08c1 2020-07-23 stsp
913 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
914 11cc08c1 2020-07-23 stsp if (err)
915 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
916 11cc08c1 2020-07-23 stsp
917 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
918 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
920 11cc08c1 2020-07-23 stsp goto done;
921 11cc08c1 2020-07-23 stsp }
922 11cc08c1 2020-07-23 stsp
923 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 11cc08c1 2020-07-23 stsp if (err)
925 11cc08c1 2020-07-23 stsp goto done;
926 11cc08c1 2020-07-23 stsp
927 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
929 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
930 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
931 11cc08c1 2020-07-23 stsp "target paths were found:\n"
932 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
935 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
936 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
937 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
938 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
939 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
940 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
942 11cc08c1 2020-07-23 stsp goto done;
943 11cc08c1 2020-07-23 stsp }
944 11cc08c1 2020-07-23 stsp
945 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
946 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
947 11cc08c1 2020-07-23 stsp goto done;
948 11cc08c1 2020-07-23 stsp }
949 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
950 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
951 11cc08c1 2020-07-23 stsp goto done;
952 11cc08c1 2020-07-23 stsp }
953 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
955 11cc08c1 2020-07-23 stsp goto done;
956 11cc08c1 2020-07-23 stsp }
957 11cc08c1 2020-07-23 stsp done:
958 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
959 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
960 11cc08c1 2020-07-23 stsp free(path);
961 11cc08c1 2020-07-23 stsp free(id_str);
962 11cc08c1 2020-07-23 stsp free(label_deriv);
963 11cc08c1 2020-07-23 stsp return err;
964 11cc08c1 2020-07-23 stsp }
965 d219f183 2020-07-23 stsp
966 d219f183 2020-07-23 stsp /* forward declaration */
967 d219f183 2020-07-23 stsp static const struct got_error *
968 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
970 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
971 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
972 11cc08c1 2020-07-23 stsp
973 11cc08c1 2020-07-23 stsp /*
974 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
975 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
976 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
977 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
978 af57b12a 2020-07-23 stsp */
979 af57b12a 2020-07-23 stsp static const struct got_error *
980 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
981 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
982 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
983 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
984 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
986 af57b12a 2020-07-23 stsp {
987 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
988 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
989 af57b12a 2020-07-23 stsp struct stat sb;
990 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
991 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
992 11cc08c1 2020-07-23 stsp int have_local_change = 0;
993 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
994 af57b12a 2020-07-23 stsp
995 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
996 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
997 af57b12a 2020-07-23 stsp
998 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
999 d219f183 2020-07-23 stsp /*
1000 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
1001 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
1002 d219f183 2020-07-23 stsp * a merge conflict.
1003 d219f183 2020-07-23 stsp */
1004 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
1005 d219f183 2020-07-23 stsp int local_changes_subsumed;
1006 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
1007 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
1009 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
1010 d219f183 2020-07-23 stsp }
1011 d219f183 2020-07-23 stsp
1012 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
1013 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1014 af57b12a 2020-07-23 stsp }
1015 af57b12a 2020-07-23 stsp
1016 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1017 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1018 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1019 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1020 af57b12a 2020-07-23 stsp ondisk_path);
1021 af57b12a 2020-07-23 stsp goto done;
1022 af57b12a 2020-07-23 stsp }
1023 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1024 af57b12a 2020-07-23 stsp
1025 526a746f 2020-07-23 stsp if (blob_orig) {
1026 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1027 526a746f 2020-07-23 stsp if (err)
1028 526a746f 2020-07-23 stsp goto done;
1029 526a746f 2020-07-23 stsp }
1030 af57b12a 2020-07-23 stsp
1031 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1032 af57b12a 2020-07-23 stsp if (err)
1033 af57b12a 2020-07-23 stsp goto done;
1034 af57b12a 2020-07-23 stsp
1035 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1036 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1037 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 11cc08c1 2020-07-23 stsp have_local_change = 1;
1039 11cc08c1 2020-07-23 stsp
1040 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1041 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1042 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1043 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1045 11cc08c1 2020-07-23 stsp
1046 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1047 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1048 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1049 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 11cc08c1 2020-07-23 stsp path);
1051 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1052 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1054 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1055 11cc08c1 2020-07-23 stsp path);
1056 11cc08c1 2020-07-23 stsp } else {
1057 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1058 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1059 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1060 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1061 11cc08c1 2020-07-23 stsp if (err)
1062 11cc08c1 2020-07-23 stsp goto done;
1063 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1064 11cc08c1 2020-07-23 stsp path);
1065 11cc08c1 2020-07-23 stsp }
1066 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1067 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1068 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1069 af57b12a 2020-07-23 stsp strlen(deriv_target));
1070 af57b12a 2020-07-23 stsp if (err)
1071 af57b12a 2020-07-23 stsp goto done;
1072 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1074 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1075 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1076 11cc08c1 2020-07-23 stsp ondisk_target, ondisk_path);
1077 11cc08c1 2020-07-23 stsp if (err)
1078 11cc08c1 2020-07-23 stsp goto done;
1079 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 11cc08c1 2020-07-23 stsp path);
1081 6353ad76 2019-02-08 stsp }
1082 11cc08c1 2020-07-23 stsp
1083 af57b12a 2020-07-23 stsp done:
1084 af57b12a 2020-07-23 stsp free(ancestor_target);
1085 af57b12a 2020-07-23 stsp free(deriv_target);
1086 14c901f1 2019-08-08 stsp return err;
1087 14c901f1 2019-08-08 stsp }
1088 14c901f1 2019-08-08 stsp
1089 14c901f1 2019-08-08 stsp /*
1090 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1091 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1092 14c901f1 2019-08-08 stsp * acts as the second derived version.
1093 14c901f1 2019-08-08 stsp */
1094 14c901f1 2019-08-08 stsp static const struct got_error *
1095 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1096 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1097 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1098 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1099 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1100 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1101 14c901f1 2019-08-08 stsp {
1102 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1103 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1104 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1105 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1106 14c901f1 2019-08-08 stsp
1107 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1108 14c901f1 2019-08-08 stsp
1109 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1110 14c901f1 2019-08-08 stsp if (parent == NULL)
1111 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1112 14c901f1 2019-08-08 stsp
1113 14c901f1 2019-08-08 stsp free(base_path);
1114 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1115 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1116 14c901f1 2019-08-08 stsp base_path = NULL;
1117 14c901f1 2019-08-08 stsp goto done;
1118 14c901f1 2019-08-08 stsp }
1119 14c901f1 2019-08-08 stsp
1120 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1121 14c901f1 2019-08-08 stsp if (err)
1122 14c901f1 2019-08-08 stsp goto done;
1123 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1124 14c901f1 2019-08-08 stsp blob_deriv);
1125 14c901f1 2019-08-08 stsp if (err)
1126 14c901f1 2019-08-08 stsp goto done;
1127 14c901f1 2019-08-08 stsp
1128 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1129 14c901f1 2019-08-08 stsp if (err)
1130 14c901f1 2019-08-08 stsp goto done;
1131 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1132 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1133 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1134 14c901f1 2019-08-08 stsp goto done;
1135 14c901f1 2019-08-08 stsp }
1136 14c901f1 2019-08-08 stsp
1137 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1138 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1139 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1140 14c901f1 2019-08-08 stsp done:
1141 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1142 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1143 14c901f1 2019-08-08 stsp free(base_path);
1144 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1145 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1146 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1147 14c901f1 2019-08-08 stsp }
1148 6353ad76 2019-02-08 stsp free(id_str);
1149 818c7501 2019-07-11 stsp free(label_deriv);
1150 4a1ddfc2 2019-01-12 stsp return err;
1151 4a1ddfc2 2019-01-12 stsp }
1152 4a1ddfc2 2019-01-12 stsp
1153 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1154 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1155 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1156 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1157 13d9040b 2019-03-27 stsp {
1158 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1159 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1160 13d9040b 2019-03-27 stsp
1161 65b05cec 2020-07-23 stsp *new_iep = NULL;
1162 65b05cec 2020-07-23 stsp
1163 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1164 054041d0 2020-06-24 stsp if (err)
1165 054041d0 2020-06-24 stsp return err;
1166 054041d0 2020-06-24 stsp
1167 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1168 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1169 054041d0 2020-06-24 stsp if (err)
1170 054041d0 2020-06-24 stsp goto done;
1171 054041d0 2020-06-24 stsp
1172 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1173 054041d0 2020-06-24 stsp done:
1174 054041d0 2020-06-24 stsp if (err)
1175 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1176 65b05cec 2020-07-23 stsp else
1177 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1178 13d9040b 2019-03-27 stsp return err;
1179 1ebedb77 2019-10-19 stsp }
1180 1ebedb77 2019-10-19 stsp
1181 1ebedb77 2019-10-19 stsp static mode_t
1182 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1183 1ebedb77 2019-10-19 stsp {
1184 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1185 1ebedb77 2019-10-19 stsp
1186 1ebedb77 2019-10-19 stsp if (executable) {
1187 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1188 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1189 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1190 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1191 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1192 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1193 1ebedb77 2019-10-19 stsp }
1194 1ebedb77 2019-10-19 stsp
1195 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1196 13d9040b 2019-03-27 stsp }
1197 13d9040b 2019-03-27 stsp
1198 8ba819a3 2020-07-23 stsp /* forward declaration */
1199 13d9040b 2019-03-27 stsp static const struct got_error *
1200 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1201 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1202 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1203 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1204 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1205 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1206 8ba819a3 2020-07-23 stsp
1207 5a1fbc73 2020-07-23 stsp /*
1208 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1209 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1210 5a1fbc73 2020-07-23 stsp */
1211 8ba819a3 2020-07-23 stsp static const struct got_error *
1212 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1213 5a1fbc73 2020-07-23 stsp size_t target_len)
1214 5a1fbc73 2020-07-23 stsp {
1215 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1216 5a1fbc73 2020-07-23 stsp ssize_t elen;
1217 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1218 5a1fbc73 2020-07-23 stsp int fd;
1219 5a1fbc73 2020-07-23 stsp
1220 5a1fbc73 2020-07-23 stsp /*
1221 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1222 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1223 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1224 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1225 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1226 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1227 5a1fbc73 2020-07-23 stsp */
1228 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1229 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1230 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1231 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1232 5a1fbc73 2020-07-23 stsp
1233 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1234 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1235 5a1fbc73 2020-07-23 stsp if (elen == -1)
1236 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1237 5a1fbc73 2020-07-23 stsp
1238 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1239 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1240 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1241 5a1fbc73 2020-07-23 stsp }
1242 5a1fbc73 2020-07-23 stsp
1243 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1244 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1245 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1246 5a1fbc73 2020-07-23 stsp return err;
1247 5a1fbc73 2020-07-23 stsp }
1248 5a1fbc73 2020-07-23 stsp
1249 5a1fbc73 2020-07-23 stsp static const struct got_error *
1250 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1251 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1252 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1253 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1254 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1255 9d31a1d8 2018-03-11 stsp {
1256 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1257 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1258 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1259 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
1260 906c123b 2020-07-23 stsp char *path_got = NULL;
1261 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1262 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 8ba819a3 2020-07-23 stsp
1264 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1265 2e1fa222 2020-07-23 stsp
1266 8ba819a3 2020-07-23 stsp /*
1267 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1268 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1269 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1270 8ba819a3 2020-07-23 stsp * in the blob object.
1271 8ba819a3 2020-07-23 stsp */
1272 8ba819a3 2020-07-23 stsp do {
1273 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1274 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1275 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1276 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1277 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1278 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1279 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1280 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1281 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1282 3b9f0f87 2020-07-23 stsp progress_arg);
1283 8ba819a3 2020-07-23 stsp }
1284 8ba819a3 2020-07-23 stsp if (len > 0) {
1285 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1286 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1287 8ba819a3 2020-07-23 stsp len - hdrlen);
1288 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1289 8ba819a3 2020-07-23 stsp hdrlen = 0;
1290 8ba819a3 2020-07-23 stsp }
1291 8ba819a3 2020-07-23 stsp } while (len != 0);
1292 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1293 8ba819a3 2020-07-23 stsp
1294 8ba819a3 2020-07-23 stsp /*
1295 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1296 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1297 8ba819a3 2020-07-23 stsp */
1298 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1299 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1300 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1301 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1302 ef68ca6f 2020-07-23 stsp goto done;
1303 ef68ca6f 2020-07-23 stsp }
1304 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1305 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1306 8ba819a3 2020-07-23 stsp goto done;
1307 8ba819a3 2020-07-23 stsp }
1308 8ba819a3 2020-07-23 stsp }
1309 8ba819a3 2020-07-23 stsp
1310 8ba819a3 2020-07-23 stsp /*
1311 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1312 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1313 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1314 8ba819a3 2020-07-23 stsp */
1315 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1316 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1317 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1318 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1319 b15bc87b 2020-07-23 stsp goto done;
1320 b15bc87b 2020-07-23 stsp }
1321 8ba819a3 2020-07-23 stsp }
1322 8ba819a3 2020-07-23 stsp
1323 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1324 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1325 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1326 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1327 8ba819a3 2020-07-23 stsp /* install as a regular file */
1328 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1329 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1330 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1331 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1332 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1333 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1334 8ba819a3 2020-07-23 stsp goto done;
1335 8ba819a3 2020-07-23 stsp }
1336 8ba819a3 2020-07-23 stsp
1337 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1338 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1339 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1340 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1341 906c123b 2020-07-23 stsp goto done;
1342 906c123b 2020-07-23 stsp }
1343 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1344 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1345 906c123b 2020-07-23 stsp /* install as a regular file */
1346 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1347 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1348 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1349 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1350 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1351 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1352 906c123b 2020-07-23 stsp goto done;
1353 906c123b 2020-07-23 stsp }
1354 906c123b 2020-07-23 stsp
1355 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1356 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1357 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1358 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1359 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1360 c90c8ce3 2020-07-23 stsp goto done;
1361 c90c8ce3 2020-07-23 stsp }
1362 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1363 5a1fbc73 2020-07-23 stsp target_path, target_len);
1364 5a1fbc73 2020-07-23 stsp if (err)
1365 f35fa46a 2020-07-23 stsp goto done;
1366 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1367 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1368 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1369 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1370 6e1eade5 2020-07-23 stsp path);
1371 f35fa46a 2020-07-23 stsp }
1372 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1373 f35fa46a 2020-07-23 stsp }
1374 f35fa46a 2020-07-23 stsp
1375 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1376 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1377 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1378 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1379 f35fa46a 2020-07-23 stsp ondisk_path);
1380 f35fa46a 2020-07-23 stsp goto done;
1381 f35fa46a 2020-07-23 stsp }
1382 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1383 f35fa46a 2020-07-23 stsp if (err)
1384 f35fa46a 2020-07-23 stsp goto done;
1385 f35fa46a 2020-07-23 stsp /*
1386 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1387 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1388 f35fa46a 2020-07-23 stsp */
1389 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1390 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1391 f35fa46a 2020-07-23 stsp goto done;
1392 f35fa46a 2020-07-23 stsp }
1393 f35fa46a 2020-07-23 stsp }
1394 f35fa46a 2020-07-23 stsp
1395 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1396 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1397 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1398 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1399 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1400 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1401 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1403 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1404 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1405 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1406 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1407 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1408 8ba819a3 2020-07-23 stsp } else {
1409 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1410 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1411 8ba819a3 2020-07-23 stsp }
1412 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1413 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1414 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1415 8ba819a3 2020-07-23 stsp done:
1416 8ba819a3 2020-07-23 stsp free(resolved_path);
1417 8ba819a3 2020-07-23 stsp free(abspath);
1418 906c123b 2020-07-23 stsp free(path_got);
1419 8ba819a3 2020-07-23 stsp return err;
1420 8ba819a3 2020-07-23 stsp }
1421 8ba819a3 2020-07-23 stsp
1422 8ba819a3 2020-07-23 stsp static const struct got_error *
1423 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1424 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1425 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1426 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1427 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1428 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1429 8ba819a3 2020-07-23 stsp {
1430 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1431 507dc3bb 2018-12-29 stsp int fd = -1;
1432 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1433 507dc3bb 2018-12-29 stsp int update = 0;
1434 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1435 8ba819a3 2020-07-23 stsp
1436 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1437 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1438 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1439 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1440 21908da4 2019-01-13 stsp char *parent = dirname(path);
1441 21908da4 2019-01-13 stsp if (parent == NULL)
1442 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1443 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1444 21908da4 2019-01-13 stsp if (err)
1445 21908da4 2019-01-13 stsp return err;
1446 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1447 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1449 21908da4 2019-01-13 stsp if (fd == -1)
1450 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1451 230a42bd 2019-05-11 jcs ondisk_path);
1452 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1453 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1454 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1455 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1456 3b9f0f87 2020-07-23 stsp goto done;
1457 3b9f0f87 2020-07-23 stsp }
1458 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1459 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1460 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1461 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1462 507dc3bb 2018-12-29 stsp goto done;
1463 d70b8e30 2018-12-27 stsp } else {
1464 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1465 507dc3bb 2018-12-29 stsp ondisk_path);
1466 507dc3bb 2018-12-29 stsp if (err)
1467 507dc3bb 2018-12-29 stsp goto done;
1468 507dc3bb 2018-12-29 stsp update = 1;
1469 9d31a1d8 2018-03-11 stsp }
1470 507dc3bb 2018-12-29 stsp } else
1471 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1472 9d31a1d8 2018-03-11 stsp }
1473 9d31a1d8 2018-03-11 stsp
1474 bd6aa359 2020-07-23 stsp if (progress_cb) {
1475 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1476 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1477 bd6aa359 2020-07-23 stsp path);
1478 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1480 bd6aa359 2020-07-23 stsp path);
1481 bd6aa359 2020-07-23 stsp else
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1483 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 bd6aa359 2020-07-23 stsp if (err)
1485 bd6aa359 2020-07-23 stsp goto done;
1486 bd6aa359 2020-07-23 stsp }
1487 d7b62c98 2018-12-27 stsp
1488 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1489 9d31a1d8 2018-03-11 stsp do {
1490 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1492 9d31a1d8 2018-03-11 stsp if (err)
1493 9d31a1d8 2018-03-11 stsp break;
1494 9d31a1d8 2018-03-11 stsp if (len > 0) {
1495 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1496 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1499 b87c6f83 2018-12-24 stsp goto done;
1500 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1501 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1502 b87c6f83 2018-12-24 stsp goto done;
1503 9d31a1d8 2018-03-11 stsp }
1504 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1505 9d31a1d8 2018-03-11 stsp }
1506 9d31a1d8 2018-03-11 stsp } while (len != 0);
1507 9d31a1d8 2018-03-11 stsp
1508 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1510 816dc654 2019-02-16 stsp goto done;
1511 816dc654 2019-02-16 stsp }
1512 9d31a1d8 2018-03-11 stsp
1513 507dc3bb 2018-12-29 stsp if (update) {
1514 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1515 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1516 230a42bd 2019-05-11 jcs ondisk_path);
1517 2a57020b 2019-02-20 stsp unlink(tmppath);
1518 68ed9ba5 2019-02-10 stsp goto done;
1519 68ed9ba5 2019-02-10 stsp }
1520 68ed9ba5 2019-02-10 stsp }
1521 ba8a0d4d 2019-02-10 stsp
1522 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1523 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1525 1ebedb77 2019-10-19 stsp goto done;
1526 507dc3bb 2018-12-29 stsp }
1527 507dc3bb 2018-12-29 stsp
1528 9d31a1d8 2018-03-11 stsp done:
1529 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1530 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1531 507dc3bb 2018-12-29 stsp free(tmppath);
1532 6353ad76 2019-02-08 stsp return err;
1533 6353ad76 2019-02-08 stsp }
1534 6353ad76 2019-02-08 stsp
1535 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1536 6353ad76 2019-02-08 stsp static const struct got_error *
1537 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1538 7154f6ce 2019-03-27 stsp {
1539 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1540 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1541 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1543 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1544 7154f6ce 2019-03-27 stsp };
1545 7154f6ce 2019-03-27 stsp int i = 0;
1546 7154f6ce 2019-03-27 stsp char *line;
1547 7154f6ce 2019-03-27 stsp size_t len;
1548 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1549 7154f6ce 2019-03-27 stsp
1550 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1551 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1552 7154f6ce 2019-03-27 stsp if (line == NULL) {
1553 7154f6ce 2019-03-27 stsp if (feof(f))
1554 7154f6ce 2019-03-27 stsp break;
1555 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1556 7154f6ce 2019-03-27 stsp break;
1557 7154f6ce 2019-03-27 stsp }
1558 7154f6ce 2019-03-27 stsp
1559 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1560 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1561 19332e6d 2019-05-13 stsp == 0)
1562 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1563 7154f6ce 2019-03-27 stsp else
1564 7154f6ce 2019-03-27 stsp i++;
1565 7154f6ce 2019-03-27 stsp }
1566 7154f6ce 2019-03-27 stsp }
1567 7154f6ce 2019-03-27 stsp
1568 7154f6ce 2019-03-27 stsp return err;
1569 e2b1e152 2019-07-13 stsp }
1570 e2b1e152 2019-07-13 stsp
1571 e2b1e152 2019-07-13 stsp static int
1572 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1573 1ebedb77 2019-10-19 stsp {
1574 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1575 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1576 1ebedb77 2019-10-19 stsp }
1577 1ebedb77 2019-10-19 stsp
1578 1ebedb77 2019-10-19 stsp static int
1579 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1580 e2b1e152 2019-07-13 stsp {
1581 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1582 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1583 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1584 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1585 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1586 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1587 c363b2c1 2019-08-03 stsp }
1588 c363b2c1 2019-08-03 stsp
1589 c363b2c1 2019-08-03 stsp static unsigned char
1590 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1591 c363b2c1 2019-08-03 stsp {
1592 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1593 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1594 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1595 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1596 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1597 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1598 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1599 c363b2c1 2019-08-03 stsp default:
1600 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1601 a919d5c4 2020-07-23 stsp }
1602 a919d5c4 2020-07-23 stsp }
1603 a919d5c4 2020-07-23 stsp
1604 a919d5c4 2020-07-23 stsp static const struct got_error *
1605 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1606 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1607 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1608 a919d5c4 2020-07-23 stsp {
1609 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1610 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1611 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1612 a919d5c4 2020-07-23 stsp ssize_t elen;
1613 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1614 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1615 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1616 a919d5c4 2020-07-23 stsp
1617 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1618 a919d5c4 2020-07-23 stsp
1619 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1620 a919d5c4 2020-07-23 stsp do {
1621 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1622 a919d5c4 2020-07-23 stsp if (err)
1623 a919d5c4 2020-07-23 stsp return err;
1624 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1625 a919d5c4 2020-07-23 stsp /*
1626 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1627 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1628 a919d5c4 2020-07-23 stsp */
1629 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1630 a919d5c4 2020-07-23 stsp }
1631 a919d5c4 2020-07-23 stsp if (len > 0) {
1632 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1633 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1634 a919d5c4 2020-07-23 stsp len - hdrlen);
1635 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1636 a919d5c4 2020-07-23 stsp hdrlen = 0;
1637 a919d5c4 2020-07-23 stsp }
1638 a919d5c4 2020-07-23 stsp } while (len != 0);
1639 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1640 a919d5c4 2020-07-23 stsp
1641 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1642 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1643 a919d5c4 2020-07-23 stsp if (elen == -1)
1644 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1645 a919d5c4 2020-07-23 stsp } else {
1646 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1647 a919d5c4 2020-07-23 stsp if (elen == -1)
1648 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1649 c363b2c1 2019-08-03 stsp }
1650 a919d5c4 2020-07-23 stsp
1651 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1652 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1653 a919d5c4 2020-07-23 stsp
1654 a919d5c4 2020-07-23 stsp return NULL;
1655 7154f6ce 2019-03-27 stsp }
1656 7154f6ce 2019-03-27 stsp
1657 7154f6ce 2019-03-27 stsp static const struct got_error *
1658 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1659 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1660 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1661 6353ad76 2019-02-08 stsp {
1662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1663 6353ad76 2019-02-08 stsp struct got_object_id id;
1664 6353ad76 2019-02-08 stsp size_t hdrlen;
1665 1338848f 2019-12-13 stsp int fd = -1;
1666 6353ad76 2019-02-08 stsp FILE *f = NULL;
1667 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1668 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1669 6353ad76 2019-02-08 stsp size_t flen, blen;
1670 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1671 6353ad76 2019-02-08 stsp
1672 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1673 6353ad76 2019-02-08 stsp
1674 7f91a133 2019-12-13 stsp /*
1675 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1676 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1677 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1678 7f91a133 2019-12-13 stsp */
1679 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1680 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1681 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1682 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1683 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1684 882ef1b9 2019-12-13 stsp else
1685 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1686 882ef1b9 2019-12-13 stsp goto done;
1687 882ef1b9 2019-12-13 stsp }
1688 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1689 3d35a492 2019-12-13 stsp goto done;
1690 3d35a492 2019-12-13 stsp }
1691 7f91a133 2019-12-13 stsp } else {
1692 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1693 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1694 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1695 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1696 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1697 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1698 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1699 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1700 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1701 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1702 3d35a492 2019-12-13 stsp else
1703 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1704 3d35a492 2019-12-13 stsp goto done;
1705 3d35a492 2019-12-13 stsp }
1706 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1707 1338848f 2019-12-13 stsp goto done;
1708 a378724f 2019-02-10 stsp }
1709 a378724f 2019-02-10 stsp }
1710 6353ad76 2019-02-08 stsp
1711 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1712 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1713 1338848f 2019-12-13 stsp goto done;
1714 3f148bc6 2019-07-27 stsp }
1715 efdd40df 2019-07-27 stsp
1716 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1717 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1718 1338848f 2019-12-13 stsp goto done;
1719 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1720 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1721 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1722 1338848f 2019-12-13 stsp goto done;
1723 d00136be 2019-03-26 stsp }
1724 b8f41171 2019-02-10 stsp
1725 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1726 1338848f 2019-12-13 stsp goto done;
1727 6353ad76 2019-02-08 stsp
1728 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1729 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1730 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1731 c363b2c1 2019-08-03 stsp else
1732 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1733 c363b2c1 2019-08-03 stsp
1734 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1735 6353ad76 2019-02-08 stsp if (err)
1736 1338848f 2019-12-13 stsp goto done;
1737 6353ad76 2019-02-08 stsp
1738 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1739 a919d5c4 2020-07-23 stsp /* Staging changes to symlinks is not yet(?) supported. */
1740 a919d5c4 2020-07-23 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
1741 a919d5c4 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1742 a919d5c4 2020-07-23 stsp goto done;
1743 a919d5c4 2020-07-23 stsp }
1744 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1745 a919d5c4 2020-07-23 stsp de_name, blob);
1746 a919d5c4 2020-07-23 stsp goto done;
1747 a919d5c4 2020-07-23 stsp }
1748 a919d5c4 2020-07-23 stsp
1749 a919d5c4 2020-07-23 stsp
1750 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1751 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1752 ab0d4361 2019-12-13 stsp if (fd == -1) {
1753 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1754 ab0d4361 2019-12-13 stsp goto done;
1755 ab0d4361 2019-12-13 stsp }
1756 3d35a492 2019-12-13 stsp }
1757 3d35a492 2019-12-13 stsp
1758 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1759 6353ad76 2019-02-08 stsp if (f == NULL) {
1760 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1761 6353ad76 2019-02-08 stsp goto done;
1762 6353ad76 2019-02-08 stsp }
1763 1338848f 2019-12-13 stsp fd = -1;
1764 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1765 656b1f76 2019-05-11 jcs for (;;) {
1766 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1767 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1768 6353ad76 2019-02-08 stsp if (err)
1769 7154f6ce 2019-03-27 stsp goto done;
1770 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1771 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1772 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1773 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1774 7154f6ce 2019-03-27 stsp goto done;
1775 80c5c120 2019-02-19 stsp }
1776 6353ad76 2019-02-08 stsp if (blen == 0) {
1777 6353ad76 2019-02-08 stsp if (flen != 0)
1778 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1779 6353ad76 2019-02-08 stsp break;
1780 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1781 6353ad76 2019-02-08 stsp if (blen != 0)
1782 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1783 6353ad76 2019-02-08 stsp break;
1784 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1785 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1786 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1787 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1788 6353ad76 2019-02-08 stsp break;
1789 6353ad76 2019-02-08 stsp }
1790 6353ad76 2019-02-08 stsp } else {
1791 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1792 6353ad76 2019-02-08 stsp break;
1793 6353ad76 2019-02-08 stsp }
1794 6353ad76 2019-02-08 stsp hdrlen = 0;
1795 6353ad76 2019-02-08 stsp }
1796 7154f6ce 2019-03-27 stsp
1797 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1798 7154f6ce 2019-03-27 stsp rewind(f);
1799 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1800 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1801 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1802 6353ad76 2019-02-08 stsp done:
1803 6353ad76 2019-02-08 stsp if (blob)
1804 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1805 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1806 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1807 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1808 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1809 9d31a1d8 2018-03-11 stsp return err;
1810 e2b1e152 2019-07-13 stsp }
1811 e2b1e152 2019-07-13 stsp
1812 e2b1e152 2019-07-13 stsp /*
1813 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1814 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1815 e2b1e152 2019-07-13 stsp */
1816 e2b1e152 2019-07-13 stsp static const struct got_error *
1817 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1818 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1819 e2b1e152 2019-07-13 stsp {
1820 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1821 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1822 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1823 e2b1e152 2019-07-13 stsp
1824 e2b1e152 2019-07-13 stsp return NULL;
1825 9d31a1d8 2018-03-11 stsp }
1826 9d31a1d8 2018-03-11 stsp
1827 9d31a1d8 2018-03-11 stsp static const struct got_error *
1828 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1829 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1830 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1831 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1832 0584f854 2019-04-06 stsp void *progress_arg)
1833 9d31a1d8 2018-03-11 stsp {
1834 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1835 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1836 6353ad76 2019-02-08 stsp char *ondisk_path;
1837 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1838 b8f41171 2019-02-10 stsp struct stat sb;
1839 9d31a1d8 2018-03-11 stsp
1840 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1841 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1842 6353ad76 2019-02-08 stsp
1843 abb4604f 2019-07-27 stsp if (ie) {
1844 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1845 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1846 a76c42e6 2019-08-03 stsp goto done;
1847 a76c42e6 2019-08-03 stsp }
1848 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1849 7f91a133 2019-12-13 stsp repo);
1850 abb4604f 2019-07-27 stsp if (err)
1851 abb4604f 2019-07-27 stsp goto done;
1852 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1853 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1854 c90c8ce3 2020-07-23 stsp } else {
1855 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1856 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1857 c90c8ce3 2020-07-23 stsp }
1858 6353ad76 2019-02-08 stsp
1859 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1860 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1861 b8f41171 2019-02-10 stsp goto done;
1862 b8f41171 2019-02-10 stsp }
1863 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1864 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1865 5036ab18 2020-04-18 stsp path);
1866 5036ab18 2020-04-18 stsp goto done;
1867 5036ab18 2020-04-18 stsp }
1868 b8f41171 2019-02-10 stsp
1869 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1870 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1871 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1872 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1874 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1875 e2b1e152 2019-07-13 stsp if (err)
1876 e2b1e152 2019-07-13 stsp goto done;
1877 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 b8f41171 2019-02-10 stsp path);
1879 6353ad76 2019-02-08 stsp goto done;
1880 a378724f 2019-02-10 stsp }
1881 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1882 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1883 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1884 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1885 b8f41171 2019-02-10 stsp goto done;
1886 e2b1e152 2019-07-13 stsp }
1887 9d31a1d8 2018-03-11 stsp }
1888 9d31a1d8 2018-03-11 stsp
1889 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 8da9e5f4 2019-01-12 stsp if (err)
1891 6353ad76 2019-02-08 stsp goto done;
1892 512f0d0e 2019-01-02 stsp
1893 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 234035bc 2019-06-01 stsp int update_timestamps;
1895 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1896 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1897 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1898 234035bc 2019-06-01 stsp struct got_object_id id2;
1899 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 234035bc 2019-06-01 stsp if (err)
1902 f69721c3 2019-10-21 stsp goto done;
1903 f69721c3 2019-10-21 stsp }
1904 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1905 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1908 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1909 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1910 f69721c3 2019-10-21 stsp goto done;
1911 f69721c3 2019-10-21 stsp }
1912 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1913 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1915 234035bc 2019-06-01 stsp goto done;
1916 f69721c3 2019-10-21 stsp }
1917 234035bc 2019-06-01 stsp }
1918 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1919 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1920 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1921 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1922 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1923 993e2a1b 2020-07-23 stsp } else {
1924 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1925 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1926 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1927 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1928 993e2a1b 2020-07-23 stsp }
1929 f69721c3 2019-10-21 stsp free(label_orig);
1930 234035bc 2019-06-01 stsp if (blob2)
1931 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1932 909d120e 2019-09-22 stsp if (err)
1933 909d120e 2019-09-22 stsp goto done;
1934 234035bc 2019-06-01 stsp /*
1935 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1936 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1937 234035bc 2019-06-01 stsp * unmodified files again.
1938 234035bc 2019-06-01 stsp */
1939 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1940 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1941 234035bc 2019-06-01 stsp update_timestamps);
1942 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1943 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1944 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1945 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1946 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1947 1ee397ad 2019-07-12 stsp if (err)
1948 1ee397ad 2019-07-12 stsp goto done;
1949 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1950 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1951 13d9040b 2019-03-27 stsp if (err)
1952 13d9040b 2019-03-27 stsp goto done;
1953 13d9040b 2019-03-27 stsp } else {
1954 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1955 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1956 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1957 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1958 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1959 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1960 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1961 2e1fa222 2020-07-23 stsp } else {
1962 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1963 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1964 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1965 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1966 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1967 2e1fa222 2020-07-23 stsp }
1968 13d9040b 2019-03-27 stsp if (err)
1969 13d9040b 2019-03-27 stsp goto done;
1970 65b05cec 2020-07-23 stsp
1971 054041d0 2020-06-24 stsp if (ie) {
1972 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1973 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1974 054041d0 2020-06-24 stsp } else {
1975 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1976 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1977 054041d0 2020-06-24 stsp &blob->id);
1978 054041d0 2020-06-24 stsp }
1979 13d9040b 2019-03-27 stsp if (err)
1980 13d9040b 2019-03-27 stsp goto done;
1981 65b05cec 2020-07-23 stsp
1982 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1983 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
1984 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1985 2e1fa222 2020-07-23 stsp if (err)
1986 2e1fa222 2020-07-23 stsp goto done;
1987 2e1fa222 2020-07-23 stsp }
1988 13d9040b 2019-03-27 stsp }
1989 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1990 6353ad76 2019-02-08 stsp done:
1991 6353ad76 2019-02-08 stsp free(ondisk_path);
1992 8da9e5f4 2019-01-12 stsp return err;
1993 90285c3b 2019-01-08 stsp }
1994 90285c3b 2019-01-08 stsp
1995 90285c3b 2019-01-08 stsp static const struct got_error *
1996 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1997 90285c3b 2019-01-08 stsp {
1998 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1999 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2000 90285c3b 2019-01-08 stsp
2001 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2003 90285c3b 2019-01-08 stsp
2004 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2005 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2006 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2007 c97665ae 2019-01-13 stsp } else {
2008 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2009 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2010 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2011 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2012 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2013 230a42bd 2019-05-11 jcs parent);
2014 90285c3b 2019-01-08 stsp break;
2015 90285c3b 2019-01-08 stsp }
2016 90285c3b 2019-01-08 stsp parent = dirname(parent);
2017 90285c3b 2019-01-08 stsp }
2018 90285c3b 2019-01-08 stsp }
2019 90285c3b 2019-01-08 stsp free(ondisk_path);
2020 90285c3b 2019-01-08 stsp return err;
2021 512f0d0e 2019-01-02 stsp }
2022 512f0d0e 2019-01-02 stsp
2023 708d8e67 2019-03-27 stsp static const struct got_error *
2024 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2025 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2026 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2027 708d8e67 2019-03-27 stsp {
2028 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2029 708d8e67 2019-03-27 stsp unsigned char status;
2030 708d8e67 2019-03-27 stsp struct stat sb;
2031 708d8e67 2019-03-27 stsp char *ondisk_path;
2032 708d8e67 2019-03-27 stsp
2033 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2034 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2035 a76c42e6 2019-08-03 stsp
2036 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2037 708d8e67 2019-03-27 stsp == -1)
2038 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2039 708d8e67 2019-03-27 stsp
2040 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2041 708d8e67 2019-03-27 stsp if (err)
2042 5a58a424 2020-06-23 stsp goto done;
2043 708d8e67 2019-03-27 stsp
2044 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2045 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2046 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2047 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2048 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2049 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2050 993e2a1b 2020-07-23 stsp goto done;
2051 993e2a1b 2020-07-23 stsp }
2052 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2053 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2054 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2055 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2056 993e2a1b 2020-07-23 stsp if (err)
2057 993e2a1b 2020-07-23 stsp goto done;
2058 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2059 993e2a1b 2020-07-23 stsp ie->path);
2060 993e2a1b 2020-07-23 stsp goto done;
2061 993e2a1b 2020-07-23 stsp }
2062 993e2a1b 2020-07-23 stsp
2063 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2064 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2065 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2066 1ee397ad 2019-07-12 stsp if (err)
2067 5a58a424 2020-06-23 stsp goto done;
2068 fc6346c4 2019-03-27 stsp /*
2069 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2070 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2071 fc6346c4 2019-03-27 stsp */
2072 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2073 fc6346c4 2019-03-27 stsp 0);
2074 fc6346c4 2019-03-27 stsp } else {
2075 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2076 1ee397ad 2019-07-12 stsp if (err)
2077 5a58a424 2020-06-23 stsp goto done;
2078 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2079 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2080 fc6346c4 2019-03-27 stsp if (err)
2081 5a58a424 2020-06-23 stsp goto done;
2082 fc6346c4 2019-03-27 stsp }
2083 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2084 708d8e67 2019-03-27 stsp }
2085 5a58a424 2020-06-23 stsp done:
2086 5a58a424 2020-06-23 stsp free(ondisk_path);
2087 fc6346c4 2019-03-27 stsp return err;
2088 708d8e67 2019-03-27 stsp }
2089 708d8e67 2019-03-27 stsp
2090 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2091 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2092 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2093 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2094 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2095 8da9e5f4 2019-01-12 stsp void *progress_arg;
2096 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2097 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2098 8da9e5f4 2019-01-12 stsp };
2099 8da9e5f4 2019-01-12 stsp
2100 512f0d0e 2019-01-02 stsp static const struct got_error *
2101 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2102 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2103 512f0d0e 2019-01-02 stsp {
2104 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2105 0584f854 2019-04-06 stsp
2106 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2107 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2108 512f0d0e 2019-01-02 stsp
2109 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2110 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2111 8da9e5f4 2019-01-12 stsp }
2112 512f0d0e 2019-01-02 stsp
2113 8da9e5f4 2019-01-12 stsp static const struct got_error *
2114 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2115 8da9e5f4 2019-01-12 stsp {
2116 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2117 7a9df742 2019-01-08 stsp
2118 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2120 0584f854 2019-04-06 stsp
2121 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2122 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2123 9d31a1d8 2018-03-11 stsp }
2124 9d31a1d8 2018-03-11 stsp
2125 9d31a1d8 2018-03-11 stsp static const struct got_error *
2126 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2127 9d31a1d8 2018-03-11 stsp {
2128 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2129 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2130 8da9e5f4 2019-01-12 stsp char *path;
2131 9d31a1d8 2018-03-11 stsp
2132 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2134 0584f854 2019-04-06 stsp
2135 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2136 63c5ca5d 2019-08-24 stsp return NULL;
2137 63c5ca5d 2019-08-24 stsp
2138 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2139 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2140 8da9e5f4 2019-01-12 stsp == -1)
2141 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2142 9d31a1d8 2018-03-11 stsp
2143 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2144 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2145 8da9e5f4 2019-01-12 stsp else
2146 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2147 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2148 9d31a1d8 2018-03-11 stsp
2149 8da9e5f4 2019-01-12 stsp free(path);
2150 0cd1c46a 2019-03-11 stsp return err;
2151 0cd1c46a 2019-03-11 stsp }
2152 0cd1c46a 2019-03-11 stsp
2153 818c7501 2019-07-11 stsp static const struct got_error *
2154 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2155 0cd1c46a 2019-03-11 stsp {
2156 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2157 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2158 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2159 0cd1c46a 2019-03-11 stsp
2160 517bab73 2019-03-11 stsp *refname = NULL;
2161 517bab73 2019-03-11 stsp
2162 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2163 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2164 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2165 0cd1c46a 2019-03-11 stsp
2166 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2167 0647c563 2019-03-11 stsp == -1) {
2168 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2169 0647c563 2019-03-11 stsp *refname = NULL;
2170 0cd1c46a 2019-03-11 stsp }
2171 517bab73 2019-03-11 stsp free(uuidstr);
2172 517bab73 2019-03-11 stsp return err;
2173 517bab73 2019-03-11 stsp }
2174 517bab73 2019-03-11 stsp
2175 818c7501 2019-07-11 stsp const struct got_error *
2176 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2177 818c7501 2019-07-11 stsp {
2178 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2179 818c7501 2019-07-11 stsp }
2180 818c7501 2019-07-11 stsp
2181 818c7501 2019-07-11 stsp static const struct got_error *
2182 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2183 818c7501 2019-07-11 stsp {
2184 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2185 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2186 818c7501 2019-07-11 stsp }
2187 818c7501 2019-07-11 stsp
2188 818c7501 2019-07-11 stsp static const struct got_error *
2189 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2190 818c7501 2019-07-11 stsp {
2191 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2192 818c7501 2019-07-11 stsp }
2193 818c7501 2019-07-11 stsp
2194 818c7501 2019-07-11 stsp static const struct got_error *
2195 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2196 818c7501 2019-07-11 stsp {
2197 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2198 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2199 818c7501 2019-07-11 stsp }
2200 818c7501 2019-07-11 stsp
2201 818c7501 2019-07-11 stsp static const struct got_error *
2202 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2203 818c7501 2019-07-11 stsp {
2204 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2205 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2206 0ebf8283 2019-07-24 stsp }
2207 0ebf8283 2019-07-24 stsp
2208 0ebf8283 2019-07-24 stsp static const struct got_error *
2209 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 0ebf8283 2019-07-24 stsp {
2211 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2212 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2213 0ebf8283 2019-07-24 stsp }
2214 0ebf8283 2019-07-24 stsp
2215 0ebf8283 2019-07-24 stsp static const struct got_error *
2216 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2217 0ebf8283 2019-07-24 stsp {
2218 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2219 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2220 0ebf8283 2019-07-24 stsp }
2221 0ebf8283 2019-07-24 stsp
2222 0ebf8283 2019-07-24 stsp static const struct got_error *
2223 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2224 0ebf8283 2019-07-24 stsp {
2225 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2226 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2227 818c7501 2019-07-11 stsp }
2228 818c7501 2019-07-11 stsp
2229 0ebf8283 2019-07-24 stsp static const struct got_error *
2230 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2231 0ebf8283 2019-07-24 stsp {
2232 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2233 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2234 0ebf8283 2019-07-24 stsp }
2235 818c7501 2019-07-11 stsp
2236 0ebf8283 2019-07-24 stsp const struct got_error *
2237 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2238 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2239 0ebf8283 2019-07-24 stsp {
2240 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2241 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2242 0ebf8283 2019-07-24 stsp *path = NULL;
2243 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2244 0ebf8283 2019-07-24 stsp }
2245 0ebf8283 2019-07-24 stsp return NULL;
2246 0ebf8283 2019-07-24 stsp }
2247 0ebf8283 2019-07-24 stsp
2248 517bab73 2019-03-11 stsp /*
2249 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2250 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2251 517bab73 2019-03-11 stsp */
2252 517bab73 2019-03-11 stsp static const struct got_error *
2253 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2254 517bab73 2019-03-11 stsp {
2255 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2256 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2257 517bab73 2019-03-11 stsp char *refname;
2258 517bab73 2019-03-11 stsp
2259 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2260 517bab73 2019-03-11 stsp if (err)
2261 517bab73 2019-03-11 stsp return err;
2262 0cd1c46a 2019-03-11 stsp
2263 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2264 0cd1c46a 2019-03-11 stsp if (err)
2265 0cd1c46a 2019-03-11 stsp goto done;
2266 0cd1c46a 2019-03-11 stsp
2267 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2268 0cd1c46a 2019-03-11 stsp done:
2269 0cd1c46a 2019-03-11 stsp free(refname);
2270 0cd1c46a 2019-03-11 stsp if (ref)
2271 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2272 3e3a69f1 2019-07-25 stsp return err;
2273 3e3a69f1 2019-07-25 stsp }
2274 3e3a69f1 2019-07-25 stsp
2275 3e3a69f1 2019-07-25 stsp static const struct got_error *
2276 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2277 3e3a69f1 2019-07-25 stsp {
2278 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2279 3e3a69f1 2019-07-25 stsp
2280 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2281 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2282 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2283 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2284 3e3a69f1 2019-07-25 stsp }
2285 9d31a1d8 2018-03-11 stsp return err;
2286 9d31a1d8 2018-03-11 stsp }
2287 9d31a1d8 2018-03-11 stsp
2288 3e3a69f1 2019-07-25 stsp
2289 ebf99748 2019-05-09 stsp static const struct got_error *
2290 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2291 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2292 ebf99748 2019-05-09 stsp {
2293 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2294 ebf99748 2019-05-09 stsp FILE *index = NULL;
2295 0cd1c46a 2019-03-11 stsp
2296 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2297 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2298 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2299 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2300 ebf99748 2019-05-09 stsp
2301 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2302 3e3a69f1 2019-07-25 stsp if (err)
2303 ebf99748 2019-05-09 stsp goto done;
2304 ebf99748 2019-05-09 stsp
2305 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2306 ebf99748 2019-05-09 stsp if (index == NULL) {
2307 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2308 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2309 ebf99748 2019-05-09 stsp } else {
2310 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2311 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2312 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2313 ebf99748 2019-05-09 stsp }
2314 ebf99748 2019-05-09 stsp done:
2315 ebf99748 2019-05-09 stsp if (err) {
2316 ebf99748 2019-05-09 stsp free(*fileindex_path);
2317 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2318 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2319 ebf99748 2019-05-09 stsp *fileindex = NULL;
2320 ebf99748 2019-05-09 stsp }
2321 ebf99748 2019-05-09 stsp return err;
2322 ebf99748 2019-05-09 stsp }
2323 c932eeeb 2019-05-22 stsp
2324 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2325 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2326 c932eeeb 2019-05-22 stsp const char *path;
2327 c932eeeb 2019-05-22 stsp size_t path_len;
2328 c932eeeb 2019-05-22 stsp const char *entry_name;
2329 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2330 a484d721 2019-06-10 stsp void *progress_arg;
2331 c932eeeb 2019-05-22 stsp };
2332 ebf99748 2019-05-09 stsp
2333 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2334 c932eeeb 2019-05-22 stsp static const struct got_error *
2335 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2336 c932eeeb 2019-05-22 stsp {
2337 1ee397ad 2019-07-12 stsp const struct got_error *err;
2338 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2339 c932eeeb 2019-05-22 stsp
2340 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2341 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2342 c932eeeb 2019-05-22 stsp return NULL;
2343 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2344 102ff934 2019-06-11 stsp return NULL;
2345 102ff934 2019-06-11 stsp
2346 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2347 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2348 c932eeeb 2019-05-22 stsp return NULL;
2349 c932eeeb 2019-05-22 stsp
2350 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2351 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2352 edd02c5e 2019-07-11 stsp ie->path);
2353 1ee397ad 2019-07-12 stsp if (err)
2354 1ee397ad 2019-07-12 stsp return err;
2355 1ee397ad 2019-07-12 stsp }
2356 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2357 c932eeeb 2019-05-22 stsp return NULL;
2358 9c6338c4 2019-06-02 stsp }
2359 9c6338c4 2019-06-02 stsp
2360 9c6338c4 2019-06-02 stsp static const struct got_error *
2361 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2362 9c6338c4 2019-06-02 stsp {
2363 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2364 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2365 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2366 867630bb 2020-01-17 stsp struct timespec timeout;
2367 9c6338c4 2019-06-02 stsp
2368 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2369 9c6338c4 2019-06-02 stsp fileindex_path);
2370 9c6338c4 2019-06-02 stsp if (err)
2371 9c6338c4 2019-06-02 stsp goto done;
2372 9c6338c4 2019-06-02 stsp
2373 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2374 9c6338c4 2019-06-02 stsp if (err)
2375 9c6338c4 2019-06-02 stsp goto done;
2376 9c6338c4 2019-06-02 stsp
2377 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2378 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2379 9c6338c4 2019-06-02 stsp fileindex_path);
2380 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2381 9c6338c4 2019-06-02 stsp }
2382 867630bb 2020-01-17 stsp
2383 867630bb 2020-01-17 stsp /*
2384 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2385 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2386 867630bb 2020-01-17 stsp * was recorded in the file index.
2387 867630bb 2020-01-17 stsp */
2388 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2389 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2390 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2391 9c6338c4 2019-06-02 stsp done:
2392 9c6338c4 2019-06-02 stsp if (new_index)
2393 9c6338c4 2019-06-02 stsp fclose(new_index);
2394 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2395 9c6338c4 2019-06-02 stsp return err;
2396 c932eeeb 2019-05-22 stsp }
2397 6ced4176 2019-07-12 stsp
2398 6ced4176 2019-07-12 stsp static const struct got_error *
2399 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2400 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2401 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2402 6ced4176 2019-07-12 stsp {
2403 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2404 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2405 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2406 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2407 6ced4176 2019-07-12 stsp
2408 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2409 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2410 6ced4176 2019-07-12 stsp *tree_id = NULL;
2411 6ced4176 2019-07-12 stsp
2412 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2413 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2414 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2415 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2416 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2417 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2418 6ced4176 2019-07-12 stsp goto done;
2419 6ced4176 2019-07-12 stsp }
2420 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2421 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2422 6ced4176 2019-07-12 stsp if (err)
2423 6ced4176 2019-07-12 stsp goto done;
2424 6ced4176 2019-07-12 stsp return NULL;
2425 6ced4176 2019-07-12 stsp }
2426 6ced4176 2019-07-12 stsp
2427 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2428 6ced4176 2019-07-12 stsp
2429 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2430 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2431 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2432 6ced4176 2019-07-12 stsp goto done;
2433 6ced4176 2019-07-12 stsp }
2434 6ced4176 2019-07-12 stsp
2435 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2436 6ced4176 2019-07-12 stsp in_repo_path);
2437 6ced4176 2019-07-12 stsp if (err)
2438 6ced4176 2019-07-12 stsp goto done;
2439 6ced4176 2019-07-12 stsp
2440 6ced4176 2019-07-12 stsp free(in_repo_path);
2441 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2442 6ced4176 2019-07-12 stsp
2443 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2444 6ced4176 2019-07-12 stsp if (err)
2445 6ced4176 2019-07-12 stsp goto done;
2446 c932eeeb 2019-05-22 stsp
2447 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2448 6ced4176 2019-07-12 stsp /* Check out a single file. */
2449 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2450 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2451 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2452 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2453 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2454 6ced4176 2019-07-12 stsp goto done;
2455 6ced4176 2019-07-12 stsp }
2456 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2457 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2458 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2459 6ced4176 2019-07-12 stsp goto done;
2460 6ced4176 2019-07-12 stsp }
2461 6ced4176 2019-07-12 stsp } else {
2462 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2463 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2464 6ced4176 2019-07-12 stsp if (err)
2465 6ced4176 2019-07-12 stsp return err;
2466 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2467 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2468 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2469 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2470 6ced4176 2019-07-12 stsp goto done;
2471 6ced4176 2019-07-12 stsp }
2472 6ced4176 2019-07-12 stsp }
2473 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2474 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2475 6ced4176 2019-07-12 stsp } else {
2476 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2477 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2478 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2479 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2480 6ced4176 2019-07-12 stsp goto done;
2481 6ced4176 2019-07-12 stsp }
2482 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2483 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2484 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2485 6ced4176 2019-07-12 stsp goto done;
2486 6ced4176 2019-07-12 stsp }
2487 6ced4176 2019-07-12 stsp }
2488 6ced4176 2019-07-12 stsp done:
2489 6ced4176 2019-07-12 stsp free(id);
2490 6ced4176 2019-07-12 stsp free(in_repo_path);
2491 6ced4176 2019-07-12 stsp if (err) {
2492 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2493 6ced4176 2019-07-12 stsp free(*tree_relpath);
2494 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2495 6ced4176 2019-07-12 stsp free(*tree_id);
2496 6ced4176 2019-07-12 stsp *tree_id = NULL;
2497 6ced4176 2019-07-12 stsp }
2498 07ed472d 2019-07-12 stsp return err;
2499 07ed472d 2019-07-12 stsp }
2500 07ed472d 2019-07-12 stsp
2501 07ed472d 2019-07-12 stsp static const struct got_error *
2502 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2503 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2504 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2505 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2506 07ed472d 2019-07-12 stsp {
2507 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2508 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2509 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2510 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2511 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2512 07ed472d 2019-07-12 stsp
2513 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2514 7f47418f 2019-12-20 stsp if (err) {
2515 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2516 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2517 7f47418f 2019-12-20 stsp goto done;
2518 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2519 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2520 7f47418f 2019-12-20 stsp if (err)
2521 7f47418f 2019-12-20 stsp return err;
2522 7f47418f 2019-12-20 stsp }
2523 07ed472d 2019-07-12 stsp
2524 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2525 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2526 07ed472d 2019-07-12 stsp if (err)
2527 07ed472d 2019-07-12 stsp goto done;
2528 07ed472d 2019-07-12 stsp
2529 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2530 07ed472d 2019-07-12 stsp if (err)
2531 07ed472d 2019-07-12 stsp goto done;
2532 07ed472d 2019-07-12 stsp
2533 07ed472d 2019-07-12 stsp if (entry_name &&
2534 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2535 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2536 07ed472d 2019-07-12 stsp goto done;
2537 07ed472d 2019-07-12 stsp }
2538 07ed472d 2019-07-12 stsp
2539 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2540 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2541 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2542 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2543 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2544 07ed472d 2019-07-12 stsp arg.repo = repo;
2545 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2546 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2547 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2548 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2549 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2550 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2551 07ed472d 2019-07-12 stsp done:
2552 07ed472d 2019-07-12 stsp if (tree)
2553 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2554 07ed472d 2019-07-12 stsp if (commit)
2555 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2556 6ced4176 2019-07-12 stsp return err;
2557 6ced4176 2019-07-12 stsp }
2558 6ced4176 2019-07-12 stsp
2559 9d31a1d8 2018-03-11 stsp const struct got_error *
2560 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2561 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2562 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2563 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2564 9d31a1d8 2018-03-11 stsp {
2565 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2566 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2567 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2568 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2569 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2570 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2571 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2572 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2573 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2574 f2ea84fa 2019-07-27 stsp int entry_type;
2575 f2ea84fa 2019-07-27 stsp char *relpath;
2576 f2ea84fa 2019-07-27 stsp char *entry_name;
2577 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2578 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2579 9d31a1d8 2018-03-11 stsp
2580 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2581 f2ea84fa 2019-07-27 stsp
2582 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2583 9d31a1d8 2018-03-11 stsp if (err)
2584 9d31a1d8 2018-03-11 stsp return err;
2585 93a30277 2018-12-24 stsp
2586 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2587 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2588 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2589 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2590 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2591 f2ea84fa 2019-07-27 stsp goto done;
2592 f2ea84fa 2019-07-27 stsp }
2593 6ced4176 2019-07-12 stsp
2594 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2595 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2596 f2ea84fa 2019-07-27 stsp if (err) {
2597 f2ea84fa 2019-07-27 stsp free(tpd);
2598 6ced4176 2019-07-12 stsp goto done;
2599 6ced4176 2019-07-12 stsp }
2600 f2ea84fa 2019-07-27 stsp
2601 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2602 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2603 f2ea84fa 2019-07-27 stsp if (err) {
2604 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2605 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2606 f2ea84fa 2019-07-27 stsp free(tpd);
2607 f2ea84fa 2019-07-27 stsp goto done;
2608 f2ea84fa 2019-07-27 stsp }
2609 f2ea84fa 2019-07-27 stsp } else
2610 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2611 f2ea84fa 2019-07-27 stsp
2612 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2613 6ced4176 2019-07-12 stsp }
2614 6ced4176 2019-07-12 stsp
2615 51514078 2018-12-25 stsp /*
2616 51514078 2018-12-25 stsp * Read the file index.
2617 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2618 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2619 51514078 2018-12-25 stsp */
2620 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2621 8da9e5f4 2019-01-12 stsp if (err)
2622 c4cdcb68 2019-04-03 stsp goto done;
2623 c4cdcb68 2019-04-03 stsp
2624 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2625 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2626 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2627 f2ea84fa 2019-07-27 stsp
2628 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2629 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2630 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2631 f2ea84fa 2019-07-27 stsp if (err)
2632 f2ea84fa 2019-07-27 stsp break;
2633 f2ea84fa 2019-07-27 stsp
2634 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2635 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2636 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2637 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2638 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2639 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2640 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2641 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2642 f2ea84fa 2019-07-27 stsp if (err)
2643 f2ea84fa 2019-07-27 stsp break;
2644 f2ea84fa 2019-07-27 stsp
2645 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2646 711d9cd9 2019-07-12 stsp }
2647 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2648 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2649 af12c6b9 2019-06-04 stsp err = sync_err;
2650 9d31a1d8 2018-03-11 stsp done:
2651 9c6338c4 2019-06-02 stsp free(fileindex_path);
2652 edfa7d7f 2018-09-11 stsp if (tree)
2653 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2654 9d31a1d8 2018-03-11 stsp if (commit)
2655 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2656 5ade8233 2019-07-12 stsp if (fileindex)
2657 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2658 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2659 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2660 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2661 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2662 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2663 f2ea84fa 2019-07-27 stsp free(tpd);
2664 f2ea84fa 2019-07-27 stsp }
2665 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2666 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2667 9d31a1d8 2018-03-11 stsp err = unlockerr;
2668 f8d1f275 2019-02-04 stsp return err;
2669 f8d1f275 2019-02-04 stsp }
2670 f8d1f275 2019-02-04 stsp
2671 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2672 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2673 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2674 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2675 234035bc 2019-06-01 stsp void *progress_arg;
2676 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2677 234035bc 2019-06-01 stsp void *cancel_arg;
2678 f69721c3 2019-10-21 stsp const char *label_orig;
2679 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2680 234035bc 2019-06-01 stsp };
2681 234035bc 2019-06-01 stsp
2682 234035bc 2019-06-01 stsp static const struct got_error *
2683 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2684 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2685 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2686 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2687 234035bc 2019-06-01 stsp {
2688 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2689 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2690 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2691 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2692 234035bc 2019-06-01 stsp struct stat sb;
2693 234035bc 2019-06-01 stsp unsigned char status;
2694 234035bc 2019-06-01 stsp int local_changes_subsumed;
2695 234035bc 2019-06-01 stsp
2696 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2697 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2698 d6c87207 2019-08-02 stsp strlen(path2));
2699 1ee397ad 2019-07-12 stsp if (ie == NULL)
2700 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2701 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2702 234035bc 2019-06-01 stsp
2703 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2704 234035bc 2019-06-01 stsp path2) == -1)
2705 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2706 234035bc 2019-06-01 stsp
2707 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2708 7f91a133 2019-12-13 stsp repo);
2709 234035bc 2019-06-01 stsp if (err)
2710 234035bc 2019-06-01 stsp goto done;
2711 234035bc 2019-06-01 stsp
2712 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2713 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2714 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2715 234035bc 2019-06-01 stsp goto done;
2716 234035bc 2019-06-01 stsp }
2717 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2718 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2719 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2720 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2721 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2722 234035bc 2019-06-01 stsp goto done;
2723 234035bc 2019-06-01 stsp }
2724 234035bc 2019-06-01 stsp
2725 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2726 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2727 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2728 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2729 af57b12a 2020-07-23 stsp a->progress_arg);
2730 af57b12a 2020-07-23 stsp } else {
2731 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2732 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2733 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2734 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2735 af57b12a 2020-07-23 stsp }
2736 234035bc 2019-06-01 stsp } else if (blob1) {
2737 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2738 d6c87207 2019-08-02 stsp strlen(path1));
2739 1ee397ad 2019-07-12 stsp if (ie == NULL)
2740 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2741 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2742 2b92fad7 2019-06-02 stsp
2743 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2744 2b92fad7 2019-06-02 stsp path1) == -1)
2745 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2746 2b92fad7 2019-06-02 stsp
2747 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2748 7f91a133 2019-12-13 stsp repo);
2749 2b92fad7 2019-06-02 stsp if (err)
2750 2b92fad7 2019-06-02 stsp goto done;
2751 2b92fad7 2019-06-02 stsp
2752 2b92fad7 2019-06-02 stsp switch (status) {
2753 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2754 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2755 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2756 1ee397ad 2019-07-12 stsp if (err)
2757 1ee397ad 2019-07-12 stsp goto done;
2758 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2759 2b92fad7 2019-06-02 stsp if (err)
2760 2b92fad7 2019-06-02 stsp goto done;
2761 2b92fad7 2019-06-02 stsp if (ie)
2762 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2763 2b92fad7 2019-06-02 stsp break;
2764 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2765 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2766 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2767 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2768 1ee397ad 2019-07-12 stsp if (err)
2769 1ee397ad 2019-07-12 stsp goto done;
2770 2b92fad7 2019-06-02 stsp if (ie)
2771 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2772 2b92fad7 2019-06-02 stsp break;
2773 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2774 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2775 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2776 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2777 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2778 1ee397ad 2019-07-12 stsp if (err)
2779 1ee397ad 2019-07-12 stsp goto done;
2780 2b92fad7 2019-06-02 stsp break;
2781 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2782 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2783 1ee397ad 2019-07-12 stsp if (err)
2784 1ee397ad 2019-07-12 stsp goto done;
2785 2b92fad7 2019-06-02 stsp break;
2786 2b92fad7 2019-06-02 stsp default:
2787 2b92fad7 2019-06-02 stsp break;
2788 2b92fad7 2019-06-02 stsp }
2789 234035bc 2019-06-01 stsp } else if (blob2) {
2790 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2791 234035bc 2019-06-01 stsp path2) == -1)
2792 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2793 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2794 d6c87207 2019-08-02 stsp strlen(path2));
2795 234035bc 2019-06-01 stsp if (ie) {
2796 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2797 7f91a133 2019-12-13 stsp -1, NULL, repo);
2798 234035bc 2019-06-01 stsp if (err)
2799 234035bc 2019-06-01 stsp goto done;
2800 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2801 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2802 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2803 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2804 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2805 1ee397ad 2019-07-12 stsp status, path2);
2806 234035bc 2019-06-01 stsp goto done;
2807 234035bc 2019-06-01 stsp }
2808 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2809 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2810 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2811 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2812 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2813 526a746f 2020-07-23 stsp if (err)
2814 526a746f 2020-07-23 stsp goto done;
2815 526a746f 2020-07-23 stsp } else {
2816 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2817 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2818 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2819 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2820 526a746f 2020-07-23 stsp a->progress_arg);
2821 526a746f 2020-07-23 stsp if (err)
2822 526a746f 2020-07-23 stsp goto done;
2823 526a746f 2020-07-23 stsp }
2824 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2825 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2826 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2827 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2828 234035bc 2019-06-01 stsp if (err)
2829 234035bc 2019-06-01 stsp goto done;
2830 234035bc 2019-06-01 stsp }
2831 234035bc 2019-06-01 stsp } else {
2832 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2833 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2834 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2835 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2836 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2837 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2838 2e1fa222 2020-07-23 stsp } else {
2839 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2840 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2841 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2842 2e1fa222 2020-07-23 stsp }
2843 234035bc 2019-06-01 stsp if (err)
2844 234035bc 2019-06-01 stsp goto done;
2845 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2846 234035bc 2019-06-01 stsp if (err)
2847 234035bc 2019-06-01 stsp goto done;
2848 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2849 3969253a 2020-03-07 stsp NULL, NULL, 1);
2850 3969253a 2020-03-07 stsp if (err) {
2851 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2852 3969253a 2020-03-07 stsp goto done;
2853 3969253a 2020-03-07 stsp }
2854 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2855 2b92fad7 2019-06-02 stsp if (err) {
2856 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2857 2b92fad7 2019-06-02 stsp goto done;
2858 2b92fad7 2019-06-02 stsp }
2859 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2860 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
2861 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2862 2e1fa222 2020-07-23 stsp if (err)
2863 2e1fa222 2020-07-23 stsp goto done;
2864 2e1fa222 2020-07-23 stsp }
2865 234035bc 2019-06-01 stsp }
2866 234035bc 2019-06-01 stsp }
2867 234035bc 2019-06-01 stsp done:
2868 234035bc 2019-06-01 stsp free(ondisk_path);
2869 234035bc 2019-06-01 stsp return err;
2870 234035bc 2019-06-01 stsp }
2871 234035bc 2019-06-01 stsp
2872 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2873 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2874 234035bc 2019-06-01 stsp struct got_repository *repo;
2875 234035bc 2019-06-01 stsp };
2876 234035bc 2019-06-01 stsp
2877 234035bc 2019-06-01 stsp static const struct got_error *
2878 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2879 234035bc 2019-06-01 stsp {
2880 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2881 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2882 234035bc 2019-06-01 stsp unsigned char status;
2883 234035bc 2019-06-01 stsp struct stat sb;
2884 234035bc 2019-06-01 stsp char *ondisk_path;
2885 234035bc 2019-06-01 stsp
2886 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2887 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2888 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2889 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2890 234035bc 2019-06-01 stsp
2891 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2892 234035bc 2019-06-01 stsp == -1)
2893 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2894 234035bc 2019-06-01 stsp
2895 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2896 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2897 234035bc 2019-06-01 stsp if (err)
2898 234035bc 2019-06-01 stsp return err;
2899 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2900 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2901 234035bc 2019-06-01 stsp
2902 234035bc 2019-06-01 stsp return NULL;
2903 234035bc 2019-06-01 stsp }
2904 234035bc 2019-06-01 stsp
2905 818c7501 2019-07-11 stsp static const struct got_error *
2906 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2907 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2908 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2909 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2910 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2911 234035bc 2019-06-01 stsp {
2912 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2913 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2914 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2915 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2916 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2917 234035bc 2019-06-01 stsp
2918 03415a1a 2019-06-02 stsp if (commit_id1) {
2919 f69721c3 2019-10-21 stsp char *id_str;
2920 f69721c3 2019-10-21 stsp
2921 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2922 03415a1a 2019-06-02 stsp worktree->path_prefix);
2923 03415a1a 2019-06-02 stsp if (err)
2924 03415a1a 2019-06-02 stsp goto done;
2925 03415a1a 2019-06-02 stsp
2926 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2927 03415a1a 2019-06-02 stsp if (err)
2928 03415a1a 2019-06-02 stsp goto done;
2929 f69721c3 2019-10-21 stsp
2930 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2931 f69721c3 2019-10-21 stsp if (err)
2932 f69721c3 2019-10-21 stsp goto done;
2933 f69721c3 2019-10-21 stsp
2934 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2935 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2936 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2937 f69721c3 2019-10-21 stsp free(id_str);
2938 f69721c3 2019-10-21 stsp goto done;
2939 f69721c3 2019-10-21 stsp }
2940 f69721c3 2019-10-21 stsp free(id_str);
2941 03415a1a 2019-06-02 stsp }
2942 234035bc 2019-06-01 stsp
2943 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2944 234035bc 2019-06-01 stsp worktree->path_prefix);
2945 234035bc 2019-06-01 stsp if (err)
2946 234035bc 2019-06-01 stsp goto done;
2947 234035bc 2019-06-01 stsp
2948 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2949 234035bc 2019-06-01 stsp if (err)
2950 234035bc 2019-06-01 stsp goto done;
2951 234035bc 2019-06-01 stsp
2952 234035bc 2019-06-01 stsp arg.worktree = worktree;
2953 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2954 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2955 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2956 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2957 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2958 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2959 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2960 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2961 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2962 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2963 af12c6b9 2019-06-04 stsp err = sync_err;
2964 234035bc 2019-06-01 stsp done:
2965 234035bc 2019-06-01 stsp if (tree1)
2966 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2967 234035bc 2019-06-01 stsp if (tree2)
2968 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2969 f69721c3 2019-10-21 stsp free(label_orig);
2970 818c7501 2019-07-11 stsp return err;
2971 818c7501 2019-07-11 stsp }
2972 234035bc 2019-06-01 stsp
2973 818c7501 2019-07-11 stsp const struct got_error *
2974 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2975 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2976 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2977 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2978 818c7501 2019-07-11 stsp {
2979 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2980 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2981 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2982 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2983 818c7501 2019-07-11 stsp
2984 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2985 818c7501 2019-07-11 stsp if (err)
2986 818c7501 2019-07-11 stsp return err;
2987 818c7501 2019-07-11 stsp
2988 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2989 818c7501 2019-07-11 stsp if (err)
2990 818c7501 2019-07-11 stsp goto done;
2991 818c7501 2019-07-11 stsp
2992 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2993 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2994 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2995 818c7501 2019-07-11 stsp &mok_arg);
2996 818c7501 2019-07-11 stsp if (err)
2997 818c7501 2019-07-11 stsp goto done;
2998 818c7501 2019-07-11 stsp
2999 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3000 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3001 818c7501 2019-07-11 stsp done:
3002 818c7501 2019-07-11 stsp if (fileindex)
3003 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3004 818c7501 2019-07-11 stsp free(fileindex_path);
3005 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3006 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3007 234035bc 2019-06-01 stsp err = unlockerr;
3008 234035bc 2019-06-01 stsp return err;
3009 234035bc 2019-06-01 stsp }
3010 234035bc 2019-06-01 stsp
3011 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3012 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3013 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3014 927df6b7 2019-02-10 stsp const char *status_path;
3015 927df6b7 2019-02-10 stsp size_t status_path_len;
3016 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3017 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3018 f8d1f275 2019-02-04 stsp void *status_arg;
3019 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3020 f8d1f275 2019-02-04 stsp void *cancel_arg;
3021 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3022 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3023 f2a9dc41 2019-12-13 tracey int report_unchanged;
3024 3143d852 2020-06-25 stsp int no_ignores;
3025 f8d1f275 2019-02-04 stsp };
3026 88d0e355 2019-08-03 stsp
3027 f8d1f275 2019-02-04 stsp static const struct got_error *
3028 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3029 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3030 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3031 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3032 927df6b7 2019-02-10 stsp {
3033 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3034 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3035 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3036 927df6b7 2019-02-10 stsp struct stat sb;
3037 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3038 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3039 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3040 927df6b7 2019-02-10 stsp
3041 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3042 98eaaa12 2019-08-03 stsp if (err)
3043 98eaaa12 2019-08-03 stsp return err;
3044 98eaaa12 2019-08-03 stsp
3045 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3046 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3047 98eaaa12 2019-08-03 stsp return NULL;
3048 98eaaa12 2019-08-03 stsp
3049 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3050 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3051 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3052 98eaaa12 2019-08-03 stsp }
3053 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3054 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3055 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3056 927df6b7 2019-02-10 stsp }
3057 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3058 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3059 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3060 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3061 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3062 98eaaa12 2019-08-03 stsp }
3063 98eaaa12 2019-08-03 stsp
3064 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3065 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3066 927df6b7 2019-02-10 stsp }
3067 927df6b7 2019-02-10 stsp
3068 927df6b7 2019-02-10 stsp static const struct got_error *
3069 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3070 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3071 f8d1f275 2019-02-04 stsp {
3072 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3073 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3074 f8d1f275 2019-02-04 stsp char *abspath;
3075 f8d1f275 2019-02-04 stsp
3076 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3077 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3078 0584f854 2019-04-06 stsp
3079 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3080 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3081 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3082 927df6b7 2019-02-10 stsp return NULL;
3083 927df6b7 2019-02-10 stsp
3084 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3085 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3086 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3087 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3088 f8d1f275 2019-02-04 stsp } else {
3089 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3090 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3091 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3092 f8d1f275 2019-02-04 stsp }
3093 f8d1f275 2019-02-04 stsp
3094 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3095 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3096 f8d1f275 2019-02-04 stsp free(abspath);
3097 9d31a1d8 2018-03-11 stsp return err;
3098 9d31a1d8 2018-03-11 stsp }
3099 f8d1f275 2019-02-04 stsp
3100 f8d1f275 2019-02-04 stsp static const struct got_error *
3101 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3102 f8d1f275 2019-02-04 stsp {
3103 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3104 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3105 2ec1f75b 2019-03-26 stsp unsigned char status;
3106 927df6b7 2019-02-10 stsp
3107 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3108 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3109 0584f854 2019-04-06 stsp
3110 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3111 927df6b7 2019-02-10 stsp return NULL;
3112 927df6b7 2019-02-10 stsp
3113 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3114 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3115 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3116 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3117 2ec1f75b 2019-03-26 stsp else
3118 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3119 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3120 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3121 6841da00 2019-08-08 stsp }
3122 6841da00 2019-08-08 stsp
3123 6841da00 2019-08-08 stsp void
3124 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3125 6841da00 2019-08-08 stsp {
3126 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3127 6841da00 2019-08-08 stsp
3128 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3129 6841da00 2019-08-08 stsp free((char *)pe->path);
3130 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3131 6841da00 2019-08-08 stsp }
3132 6841da00 2019-08-08 stsp
3133 6841da00 2019-08-08 stsp void
3134 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3135 6841da00 2019-08-08 stsp {
3136 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3137 6841da00 2019-08-08 stsp
3138 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3139 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3140 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3141 6841da00 2019-08-08 stsp free((char *)pe->path);
3142 6841da00 2019-08-08 stsp }
3143 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3144 6841da00 2019-08-08 stsp }
3145 6841da00 2019-08-08 stsp
3146 6841da00 2019-08-08 stsp static const struct got_error *
3147 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3148 6841da00 2019-08-08 stsp {
3149 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3150 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3151 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3152 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3153 6841da00 2019-08-08 stsp size_t linesize = 0;
3154 6841da00 2019-08-08 stsp ssize_t linelen;
3155 6841da00 2019-08-08 stsp
3156 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3157 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3158 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3159 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3160 6841da00 2019-08-08 stsp
3161 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3162 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3163 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3164 bd8de430 2019-10-04 stsp
3165 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3166 bd8de430 2019-10-04 stsp if (line[0] == '#')
3167 bd8de430 2019-10-04 stsp continue;
3168 bd8de430 2019-10-04 stsp
3169 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3170 bd8de430 2019-10-04 stsp if (line[0] == '!')
3171 bd8de430 2019-10-04 stsp continue;
3172 bd8de430 2019-10-04 stsp
3173 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3174 6841da00 2019-08-08 stsp line) == -1) {
3175 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3176 6841da00 2019-08-08 stsp goto done;
3177 6841da00 2019-08-08 stsp }
3178 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3179 6841da00 2019-08-08 stsp if (err)
3180 6841da00 2019-08-08 stsp goto done;
3181 6841da00 2019-08-08 stsp }
3182 6841da00 2019-08-08 stsp if (ferror(f)) {
3183 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3184 6841da00 2019-08-08 stsp goto done;
3185 6841da00 2019-08-08 stsp }
3186 6841da00 2019-08-08 stsp
3187 6841da00 2019-08-08 stsp dirpath = strdup(path);
3188 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3189 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3190 6841da00 2019-08-08 stsp goto done;
3191 6841da00 2019-08-08 stsp }
3192 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3193 6841da00 2019-08-08 stsp done:
3194 6841da00 2019-08-08 stsp free(line);
3195 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3196 6841da00 2019-08-08 stsp free(dirpath);
3197 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3198 6841da00 2019-08-08 stsp }
3199 6841da00 2019-08-08 stsp return err;
3200 6841da00 2019-08-08 stsp }
3201 6841da00 2019-08-08 stsp
3202 6841da00 2019-08-08 stsp int
3203 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3204 6841da00 2019-08-08 stsp {
3205 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3206 bd8de430 2019-10-04 stsp
3207 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3208 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3209 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3210 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3211 bd8de430 2019-10-04 stsp
3212 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3213 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3214 6841da00 2019-08-08 stsp
3215 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3216 bd8de430 2019-10-04 stsp continue;
3217 bd8de430 2019-10-04 stsp pattern += 3;
3218 bd8de430 2019-10-04 stsp p = path;
3219 bd8de430 2019-10-04 stsp while (*p) {
3220 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3221 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3222 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3223 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3224 bd8de430 2019-10-04 stsp p++;
3225 bd8de430 2019-10-04 stsp while (*p == '/')
3226 bd8de430 2019-10-04 stsp p++;
3227 bd8de430 2019-10-04 stsp continue;
3228 bd8de430 2019-10-04 stsp }
3229 bd8de430 2019-10-04 stsp return 1;
3230 bd8de430 2019-10-04 stsp }
3231 bd8de430 2019-10-04 stsp }
3232 bd8de430 2019-10-04 stsp }
3233 bd8de430 2019-10-04 stsp
3234 6841da00 2019-08-08 stsp /*
3235 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3236 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3237 6841da00 2019-08-08 stsp * ignores backwards.
3238 6841da00 2019-08-08 stsp */
3239 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3240 6841da00 2019-08-08 stsp while (pe) {
3241 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3242 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3243 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3244 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3245 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3246 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3247 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3248 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3249 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3250 6841da00 2019-08-08 stsp continue;
3251 6841da00 2019-08-08 stsp return 1;
3252 6841da00 2019-08-08 stsp }
3253 6841da00 2019-08-08 stsp }
3254 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3255 6841da00 2019-08-08 stsp }
3256 6841da00 2019-08-08 stsp
3257 6841da00 2019-08-08 stsp return 0;
3258 6841da00 2019-08-08 stsp }
3259 6841da00 2019-08-08 stsp
3260 6841da00 2019-08-08 stsp static const struct got_error *
3261 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3262 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3263 6841da00 2019-08-08 stsp {
3264 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3265 6841da00 2019-08-08 stsp char *ignorespath;
3266 886cec17 2019-12-15 stsp int fd = -1;
3267 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3268 6841da00 2019-08-08 stsp
3269 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3270 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3271 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3272 6841da00 2019-08-08 stsp
3273 886cec17 2019-12-15 stsp if (dirfd != -1) {
3274 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3275 886cec17 2019-12-15 stsp if (fd == -1) {
3276 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3277 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3278 886cec17 2019-12-15 stsp ignorespath);
3279 886cec17 2019-12-15 stsp } else {
3280 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3281 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3282 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3283 886cec17 2019-12-15 stsp ignorespath);
3284 886cec17 2019-12-15 stsp else {
3285 886cec17 2019-12-15 stsp fd = -1;
3286 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3287 886cec17 2019-12-15 stsp }
3288 886cec17 2019-12-15 stsp }
3289 886cec17 2019-12-15 stsp } else {
3290 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3291 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3292 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3293 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3294 886cec17 2019-12-15 stsp ignorespath);
3295 886cec17 2019-12-15 stsp } else
3296 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3297 886cec17 2019-12-15 stsp }
3298 6841da00 2019-08-08 stsp
3299 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3300 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3301 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3302 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3303 6841da00 2019-08-08 stsp free(ignorespath);
3304 6841da00 2019-08-08 stsp return err;
3305 f8d1f275 2019-02-04 stsp }
3306 f8d1f275 2019-02-04 stsp
3307 f8d1f275 2019-02-04 stsp static const struct got_error *
3308 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3309 f8d1f275 2019-02-04 stsp {
3310 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3311 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3312 f8d1f275 2019-02-04 stsp char *path = NULL;
3313 f8d1f275 2019-02-04 stsp
3314 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3315 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3316 0584f854 2019-04-06 stsp
3317 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3318 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3319 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3320 f8d1f275 2019-02-04 stsp } else {
3321 f8d1f275 2019-02-04 stsp path = de->d_name;
3322 f8d1f275 2019-02-04 stsp }
3323 f8d1f275 2019-02-04 stsp
3324 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3325 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3326 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3327 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3328 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3329 f8d1f275 2019-02-04 stsp if (parent_path[0])
3330 f8d1f275 2019-02-04 stsp free(path);
3331 b72f483a 2019-02-05 stsp return err;
3332 f8d1f275 2019-02-04 stsp }
3333 f8d1f275 2019-02-04 stsp
3334 347d1d3e 2019-07-12 stsp static const struct got_error *
3335 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3336 3143d852 2020-06-25 stsp {
3337 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3338 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3339 3143d852 2020-06-25 stsp
3340 3143d852 2020-06-25 stsp if (a->no_ignores)
3341 3143d852 2020-06-25 stsp return NULL;
3342 3143d852 2020-06-25 stsp
3343 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3344 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3345 3143d852 2020-06-25 stsp if (err)
3346 3143d852 2020-06-25 stsp return err;
3347 3143d852 2020-06-25 stsp
3348 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3349 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3350 3143d852 2020-06-25 stsp
3351 3143d852 2020-06-25 stsp return err;
3352 3143d852 2020-06-25 stsp }
3353 3143d852 2020-06-25 stsp
3354 3143d852 2020-06-25 stsp static const struct got_error *
3355 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3356 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3357 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3358 abb4604f 2019-07-27 stsp {
3359 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3360 abb4604f 2019-07-27 stsp struct stat sb;
3361 abb4604f 2019-07-27 stsp
3362 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3363 abb4604f 2019-07-27 stsp if (ie)
3364 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3365 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3366 abb4604f 2019-07-27 stsp
3367 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3368 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3369 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3370 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3371 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3372 abb4604f 2019-07-27 stsp return NULL;
3373 abb4604f 2019-07-27 stsp }
3374 abb4604f 2019-07-27 stsp
3375 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3376 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3377 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3378 abb4604f 2019-07-27 stsp
3379 abb4604f 2019-07-27 stsp return NULL;
3380 3143d852 2020-06-25 stsp }
3381 3143d852 2020-06-25 stsp
3382 3143d852 2020-06-25 stsp static const struct got_error *
3383 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3384 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3385 3143d852 2020-06-25 stsp {
3386 3143d852 2020-06-25 stsp const struct got_error *err;
3387 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3388 3143d852 2020-06-25 stsp
3389 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3390 3143d852 2020-06-25 stsp ".cvsignore");
3391 3143d852 2020-06-25 stsp if (err)
3392 3143d852 2020-06-25 stsp return err;
3393 3143d852 2020-06-25 stsp
3394 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3395 3143d852 2020-06-25 stsp ".gitignore");
3396 3143d852 2020-06-25 stsp if (err)
3397 3143d852 2020-06-25 stsp return err;
3398 3143d852 2020-06-25 stsp
3399 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3400 3143d852 2020-06-25 stsp if (err) {
3401 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3402 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3403 3143d852 2020-06-25 stsp return err;
3404 3143d852 2020-06-25 stsp }
3405 3143d852 2020-06-25 stsp for (;;) {
3406 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3407 3143d852 2020-06-25 stsp ".cvsignore");
3408 3143d852 2020-06-25 stsp if (err)
3409 3143d852 2020-06-25 stsp break;
3410 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3411 3143d852 2020-06-25 stsp ".gitignore");
3412 3143d852 2020-06-25 stsp if (err)
3413 3143d852 2020-06-25 stsp break;
3414 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3415 3143d852 2020-06-25 stsp if (err) {
3416 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3417 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3418 3143d852 2020-06-25 stsp break;
3419 3143d852 2020-06-25 stsp }
3420 b737c85a 2020-06-26 stsp free(parent_path);
3421 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3422 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3423 3143d852 2020-06-25 stsp }
3424 3143d852 2020-06-25 stsp
3425 b737c85a 2020-06-26 stsp free(parent_path);
3426 b737c85a 2020-06-26 stsp free(next_parent_path);
3427 3143d852 2020-06-25 stsp return err;
3428 abb4604f 2019-07-27 stsp }
3429 abb4604f 2019-07-27 stsp
3430 abb4604f 2019-07-27 stsp static const struct got_error *
3431 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3432 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3433 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3434 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3435 f2a9dc41 2019-12-13 tracey int report_unchanged)
3436 f8d1f275 2019-02-04 stsp {
3437 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3438 6fc93f37 2019-12-13 stsp int fd = -1;
3439 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3440 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3441 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3442 3143d852 2020-06-25 stsp
3443 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3444 f8d1f275 2019-02-04 stsp
3445 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3446 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3447 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3448 8dc303cc 2019-07-27 stsp
3449 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3450 6fc93f37 2019-12-13 stsp if (fd == -1) {
3451 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3452 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3453 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3454 abb4604f 2019-07-27 stsp else
3455 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3456 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3457 f2a9dc41 2019-12-13 tracey report_unchanged);
3458 abb4604f 2019-07-27 stsp } else {
3459 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3460 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3461 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3462 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3463 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3464 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3465 abb4604f 2019-07-27 stsp arg.status_path = path;
3466 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3467 abb4604f 2019-07-27 stsp arg.repo = repo;
3468 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3469 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3470 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3471 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3472 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3473 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3474 022fae89 2019-12-06 tracey if (!no_ignores) {
3475 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3476 3143d852 2020-06-25 stsp worktree->root_path, path);
3477 3143d852 2020-06-25 stsp if (err)
3478 3143d852 2020-06-25 stsp goto done;
3479 022fae89 2019-12-06 tracey }
3480 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3481 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3482 927df6b7 2019-02-10 stsp }
3483 3143d852 2020-06-25 stsp done:
3484 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3485 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3486 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3487 927df6b7 2019-02-10 stsp free(ondisk_path);
3488 347d1d3e 2019-07-12 stsp return err;
3489 347d1d3e 2019-07-12 stsp }
3490 347d1d3e 2019-07-12 stsp
3491 347d1d3e 2019-07-12 stsp const struct got_error *
3492 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3493 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3494 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3495 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3496 347d1d3e 2019-07-12 stsp {
3497 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3498 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3499 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3500 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3501 347d1d3e 2019-07-12 stsp
3502 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3503 347d1d3e 2019-07-12 stsp if (err)
3504 347d1d3e 2019-07-12 stsp return err;
3505 347d1d3e 2019-07-12 stsp
3506 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3507 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3508 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3509 72ea6654 2019-07-27 stsp if (err)
3510 72ea6654 2019-07-27 stsp break;
3511 72ea6654 2019-07-27 stsp }
3512 f8d1f275 2019-02-04 stsp free(fileindex_path);
3513 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3514 f8d1f275 2019-02-04 stsp return err;
3515 f8d1f275 2019-02-04 stsp }
3516 6c7ab921 2019-03-18 stsp
3517 6c7ab921 2019-03-18 stsp const struct got_error *
3518 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3519 6c7ab921 2019-03-18 stsp const char *arg)
3520 6c7ab921 2019-03-18 stsp {
3521 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3522 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3523 6c7ab921 2019-03-18 stsp size_t len;
3524 00bb5ea0 2020-07-23 stsp struct stat sb;
3525 6c7ab921 2019-03-18 stsp
3526 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3527 6c7ab921 2019-03-18 stsp
3528 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3529 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3530 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3531 00bb5ea0 2020-07-23 stsp
3532 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3533 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3534 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3535 00bb5ea0 2020-07-23 stsp goto done;
3536 00bb5ea0 2020-07-23 stsp }
3537 00bb5ea0 2020-07-23 stsp }
3538 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3539 00bb5ea0 2020-07-23 stsp /*
3540 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3541 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3542 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3543 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3544 00bb5ea0 2020-07-23 stsp */
3545 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3546 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3547 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3548 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3549 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3550 00bb5ea0 2020-07-23 stsp goto done;
3551 00bb5ea0 2020-07-23 stsp }
3552 00bb5ea0 2020-07-23 stsp
3553 00bb5ea0 2020-07-23 stsp }
3554 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3555 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3556 00bb5ea0 2020-07-23 stsp if (err)
3557 d0710d08 2019-07-22 stsp goto done;
3558 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3559 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3560 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3561 00bb5ea0 2020-07-23 stsp goto done;
3562 00bb5ea0 2020-07-23 stsp }
3563 00bb5ea0 2020-07-23 stsp } else {
3564 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3565 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3566 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3567 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3568 00bb5ea0 2020-07-23 stsp goto done;
3569 00bb5ea0 2020-07-23 stsp }
3570 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3571 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3572 00bb5ea0 2020-07-23 stsp goto done;
3573 00bb5ea0 2020-07-23 stsp }
3574 d0710d08 2019-07-22 stsp }
3575 d0710d08 2019-07-22 stsp }
3576 6c7ab921 2019-03-18 stsp
3577 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3578 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3579 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3580 6c7ab921 2019-03-18 stsp goto done;
3581 6c7ab921 2019-03-18 stsp }
3582 6c7ab921 2019-03-18 stsp
3583 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3584 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3585 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3586 f6d88e1a 2019-05-29 stsp if (err)
3587 f6d88e1a 2019-05-29 stsp goto done;
3588 f6d88e1a 2019-05-29 stsp } else {
3589 f6d88e1a 2019-05-29 stsp path = strdup("");
3590 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3591 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3592 f6d88e1a 2019-05-29 stsp goto done;
3593 f6d88e1a 2019-05-29 stsp }
3594 6c7ab921 2019-03-18 stsp }
3595 6c7ab921 2019-03-18 stsp
3596 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3597 6c7ab921 2019-03-18 stsp len = strlen(path);
3598 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3599 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3600 6c7ab921 2019-03-18 stsp len--;
3601 6c7ab921 2019-03-18 stsp }
3602 6c7ab921 2019-03-18 stsp done:
3603 6c7ab921 2019-03-18 stsp free(resolved);
3604 d0710d08 2019-07-22 stsp free(cwd);
3605 6c7ab921 2019-03-18 stsp if (err == NULL)
3606 6c7ab921 2019-03-18 stsp *wt_path = path;
3607 6c7ab921 2019-03-18 stsp else
3608 6c7ab921 2019-03-18 stsp free(path);
3609 d00136be 2019-03-26 stsp return err;
3610 d00136be 2019-03-26 stsp }
3611 d00136be 2019-03-26 stsp
3612 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3613 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3614 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3615 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3616 4e68cba3 2019-11-23 stsp void *progress_arg;
3617 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3618 4e68cba3 2019-11-23 stsp };
3619 4e68cba3 2019-11-23 stsp
3620 4e68cba3 2019-11-23 stsp static const struct got_error *
3621 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3622 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3623 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3624 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3625 07f5b47a 2019-06-02 stsp {
3626 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3627 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3628 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3629 6d022e97 2019-08-04 stsp struct stat sb;
3630 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3631 07f5b47a 2019-06-02 stsp
3632 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3633 dbb83fbd 2019-12-12 stsp relpath) == -1)
3634 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3635 dbb83fbd 2019-12-12 stsp
3636 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3637 6d022e97 2019-08-04 stsp if (ie) {
3638 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3639 12463d8b 2019-12-13 stsp de_name, a->repo);
3640 6d022e97 2019-08-04 stsp if (err)
3641 dbb83fbd 2019-12-12 stsp goto done;
3642 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3643 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3644 dbb83fbd 2019-12-12 stsp goto done;
3645 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3646 dbb83fbd 2019-12-12 stsp if (err)
3647 dbb83fbd 2019-12-12 stsp goto done;
3648 6d022e97 2019-08-04 stsp }
3649 07f5b47a 2019-06-02 stsp
3650 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3651 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3652 dbb83fbd 2019-12-12 stsp goto done;
3653 4e68cba3 2019-11-23 stsp }
3654 07f5b47a 2019-06-02 stsp
3655 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3656 4e68cba3 2019-11-23 stsp if (err)
3657 4e68cba3 2019-11-23 stsp goto done;
3658 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3659 3969253a 2020-03-07 stsp if (err) {
3660 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3661 3969253a 2020-03-07 stsp goto done;
3662 3969253a 2020-03-07 stsp }
3663 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3664 07f5b47a 2019-06-02 stsp if (err) {
3665 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3666 4e68cba3 2019-11-23 stsp goto done;
3667 07f5b47a 2019-06-02 stsp }
3668 4e68cba3 2019-11-23 stsp done:
3669 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3670 dbb83fbd 2019-12-12 stsp if (err)
3671 dbb83fbd 2019-12-12 stsp return err;
3672 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3673 dbb83fbd 2019-12-12 stsp return NULL;
3674 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3675 07f5b47a 2019-06-02 stsp }
3676 07f5b47a 2019-06-02 stsp
3677 d00136be 2019-03-26 stsp const struct got_error *
3678 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3679 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3680 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3681 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3682 d00136be 2019-03-26 stsp {
3683 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3684 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3685 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3686 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3687 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3688 d00136be 2019-03-26 stsp
3689 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3690 d00136be 2019-03-26 stsp if (err)
3691 d00136be 2019-03-26 stsp return err;
3692 d00136be 2019-03-26 stsp
3693 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3694 d00136be 2019-03-26 stsp if (err)
3695 d00136be 2019-03-26 stsp goto done;
3696 d00136be 2019-03-26 stsp
3697 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3698 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3699 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3700 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3701 4e68cba3 2019-11-23 stsp saa.repo = repo;
3702 4e68cba3 2019-11-23 stsp
3703 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3704 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3705 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3706 1dd54920 2019-05-11 stsp if (err)
3707 af12c6b9 2019-06-04 stsp break;
3708 1dd54920 2019-05-11 stsp }
3709 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3710 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3711 af12c6b9 2019-06-04 stsp err = sync_err;
3712 d00136be 2019-03-26 stsp done:
3713 fb399478 2019-07-12 stsp free(fileindex_path);
3714 d00136be 2019-03-26 stsp if (fileindex)
3715 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3716 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3717 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3718 d00136be 2019-03-26 stsp err = unlockerr;
3719 6c7ab921 2019-03-18 stsp return err;
3720 6c7ab921 2019-03-18 stsp }
3721 17ed4618 2019-06-02 stsp
3722 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3723 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3724 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3725 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3726 f2a9dc41 2019-12-13 tracey void *progress_arg;
3727 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3728 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3729 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3730 f2a9dc41 2019-12-13 tracey };
3731 f2a9dc41 2019-12-13 tracey
3732 f2a9dc41 2019-12-13 tracey static const struct got_error *
3733 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3734 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3735 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3736 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3737 17ed4618 2019-06-02 stsp {
3738 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3739 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3740 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3741 17ed4618 2019-06-02 stsp struct stat sb;
3742 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3743 17ed4618 2019-06-02 stsp
3744 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3745 17ed4618 2019-06-02 stsp if (ie == NULL)
3746 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3747 2ec1f75b 2019-03-26 stsp
3748 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3749 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3750 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3751 9acbc4fa 2019-08-03 stsp return NULL;
3752 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3753 9acbc4fa 2019-08-03 stsp }
3754 9acbc4fa 2019-08-03 stsp
3755 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3756 f2a9dc41 2019-12-13 tracey relpath) == -1)
3757 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3758 f2a9dc41 2019-12-13 tracey
3759 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3760 12463d8b 2019-12-13 stsp a->repo);
3761 17ed4618 2019-06-02 stsp if (err)
3762 f2a9dc41 2019-12-13 tracey goto done;
3763 17ed4618 2019-06-02 stsp
3764 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3765 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3766 f2a9dc41 2019-12-13 tracey goto done;
3767 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3768 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3769 f2a9dc41 2019-12-13 tracey goto done;
3770 f2a9dc41 2019-12-13 tracey }
3771 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3772 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3773 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3774 f2a9dc41 2019-12-13 tracey goto done;
3775 f2a9dc41 2019-12-13 tracey }
3776 17ed4618 2019-06-02 stsp }
3777 17ed4618 2019-06-02 stsp
3778 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3779 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3780 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3781 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3782 12463d8b 2019-12-13 stsp ondisk_path);
3783 12463d8b 2019-12-13 stsp goto done;
3784 12463d8b 2019-12-13 stsp }
3785 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3786 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3787 12463d8b 2019-12-13 stsp goto done;
3788 15341bfd 2020-03-05 tracey }
3789 15341bfd 2020-03-05 tracey
3790 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3791 15341bfd 2020-03-05 tracey
3792 15341bfd 2020-03-05 tracey if (parent == NULL) {
3793 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3794 15341bfd 2020-03-05 tracey goto done;
3795 15341bfd 2020-03-05 tracey }
3796 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3797 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3798 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3799 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3800 15341bfd 2020-03-05 tracey parent);
3801 15341bfd 2020-03-05 tracey break;
3802 15341bfd 2020-03-05 tracey }
3803 15341bfd 2020-03-05 tracey parent = dirname(parent);
3804 15341bfd 2020-03-05 tracey if (parent == NULL) {
3805 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3806 15341bfd 2020-03-05 tracey goto done;
3807 15341bfd 2020-03-05 tracey }
3808 12463d8b 2019-12-13 stsp }
3809 f2a9dc41 2019-12-13 tracey }
3810 17ed4618 2019-06-02 stsp
3811 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3812 f2a9dc41 2019-12-13 tracey done:
3813 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3814 f2a9dc41 2019-12-13 tracey if (err)
3815 f2a9dc41 2019-12-13 tracey return err;
3816 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3817 f2a9dc41 2019-12-13 tracey return NULL;
3818 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3819 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3820 17ed4618 2019-06-02 stsp }
3821 17ed4618 2019-06-02 stsp
3822 2ec1f75b 2019-03-26 stsp const struct got_error *
3823 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3824 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3825 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3826 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3827 2ec1f75b 2019-03-26 stsp {
3828 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3829 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3830 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3831 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3832 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3833 2ec1f75b 2019-03-26 stsp
3834 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3835 2ec1f75b 2019-03-26 stsp if (err)
3836 2ec1f75b 2019-03-26 stsp return err;
3837 2ec1f75b 2019-03-26 stsp
3838 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3839 2ec1f75b 2019-03-26 stsp if (err)
3840 2ec1f75b 2019-03-26 stsp goto done;
3841 f2a9dc41 2019-12-13 tracey
3842 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3843 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3844 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3845 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3846 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3847 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3848 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3849 2ec1f75b 2019-03-26 stsp
3850 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3851 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3852 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3853 17ed4618 2019-06-02 stsp if (err)
3854 af12c6b9 2019-06-04 stsp break;
3855 2ec1f75b 2019-03-26 stsp }
3856 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3857 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3858 af12c6b9 2019-06-04 stsp err = sync_err;
3859 2ec1f75b 2019-03-26 stsp done:
3860 fb399478 2019-07-12 stsp free(fileindex_path);
3861 2ec1f75b 2019-03-26 stsp if (fileindex)
3862 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3863 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3864 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3865 2ec1f75b 2019-03-26 stsp err = unlockerr;
3866 33aa809d 2019-08-08 stsp return err;
3867 33aa809d 2019-08-08 stsp }
3868 33aa809d 2019-08-08 stsp
3869 33aa809d 2019-08-08 stsp static const struct got_error *
3870 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3871 33aa809d 2019-08-08 stsp {
3872 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3873 33aa809d 2019-08-08 stsp char *line = NULL;
3874 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3875 33aa809d 2019-08-08 stsp ssize_t linelen;
3876 33aa809d 2019-08-08 stsp
3877 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3878 33aa809d 2019-08-08 stsp if (linelen == -1) {
3879 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3880 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3881 33aa809d 2019-08-08 stsp goto done;
3882 33aa809d 2019-08-08 stsp }
3883 33aa809d 2019-08-08 stsp return NULL;
3884 33aa809d 2019-08-08 stsp }
3885 33aa809d 2019-08-08 stsp if (outfile) {
3886 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3887 33aa809d 2019-08-08 stsp if (n != linelen) {
3888 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3889 33aa809d 2019-08-08 stsp goto done;
3890 33aa809d 2019-08-08 stsp }
3891 33aa809d 2019-08-08 stsp }
3892 33aa809d 2019-08-08 stsp if (rejectfile) {
3893 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3894 33aa809d 2019-08-08 stsp if (n != linelen)
3895 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3896 33aa809d 2019-08-08 stsp }
3897 33aa809d 2019-08-08 stsp done:
3898 33aa809d 2019-08-08 stsp free(line);
3899 2ec1f75b 2019-03-26 stsp return err;
3900 2ec1f75b 2019-03-26 stsp }
3901 1f1abb7e 2019-08-08 stsp
3902 33aa809d 2019-08-08 stsp static const struct got_error *
3903 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3904 33aa809d 2019-08-08 stsp {
3905 33aa809d 2019-08-08 stsp char *line = NULL;
3906 33aa809d 2019-08-08 stsp size_t linesize = 0;
3907 33aa809d 2019-08-08 stsp ssize_t linelen;
3908 33aa809d 2019-08-08 stsp
3909 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3910 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3911 500467ff 2019-09-25 hiltjo if (ferror(f))
3912 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3913 500467ff 2019-09-25 hiltjo return NULL;
3914 500467ff 2019-09-25 hiltjo }
3915 33aa809d 2019-08-08 stsp free(line);
3916 33aa809d 2019-08-08 stsp return NULL;
3917 33aa809d 2019-08-08 stsp }
3918 33aa809d 2019-08-08 stsp
3919 33aa809d 2019-08-08 stsp static const struct got_error *
3920 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3921 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3922 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3923 33aa809d 2019-08-08 stsp {
3924 33aa809d 2019-08-08 stsp const struct got_error *err;
3925 33aa809d 2019-08-08 stsp
3926 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3927 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3928 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3929 33aa809d 2019-08-08 stsp if (err)
3930 33aa809d 2019-08-08 stsp return err;
3931 33aa809d 2019-08-08 stsp (*line_cur1)++;
3932 33aa809d 2019-08-08 stsp }
3933 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3934 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3935 33aa809d 2019-08-08 stsp if (rejectfile)
3936 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3937 33aa809d 2019-08-08 stsp else
3938 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3939 33aa809d 2019-08-08 stsp if (err)
3940 33aa809d 2019-08-08 stsp return err;
3941 33aa809d 2019-08-08 stsp (*line_cur2)++;
3942 33aa809d 2019-08-08 stsp }
3943 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3944 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3945 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3946 33aa809d 2019-08-08 stsp if (err)
3947 33aa809d 2019-08-08 stsp return err;
3948 33aa809d 2019-08-08 stsp (*line_cur2)++;
3949 33aa809d 2019-08-08 stsp }
3950 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3951 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3952 33aa809d 2019-08-08 stsp if (rejectfile)
3953 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3954 33aa809d 2019-08-08 stsp else
3955 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3956 33aa809d 2019-08-08 stsp if (err)
3957 33aa809d 2019-08-08 stsp return err;
3958 33aa809d 2019-08-08 stsp (*line_cur1)++;
3959 33aa809d 2019-08-08 stsp }
3960 f1e81a05 2019-08-10 stsp
3961 f1e81a05 2019-08-10 stsp return NULL;
3962 f1e81a05 2019-08-10 stsp }
3963 f1e81a05 2019-08-10 stsp
3964 f1e81a05 2019-08-10 stsp static const struct got_error *
3965 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3966 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3967 f1e81a05 2019-08-10 stsp {
3968 f1e81a05 2019-08-10 stsp const struct got_error *err;
3969 f1e81a05 2019-08-10 stsp
3970 f1e81a05 2019-08-10 stsp if (outfile) {
3971 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3972 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3973 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3974 f1e81a05 2019-08-10 stsp if (err)
3975 f1e81a05 2019-08-10 stsp return err;
3976 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3977 f1e81a05 2019-08-10 stsp }
3978 f1e81a05 2019-08-10 stsp }
3979 f1e81a05 2019-08-10 stsp if (rejectfile) {
3980 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3981 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3982 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3983 f1e81a05 2019-08-10 stsp if (err)
3984 f1e81a05 2019-08-10 stsp return err;
3985 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3986 f1e81a05 2019-08-10 stsp }
3987 33aa809d 2019-08-08 stsp }
3988 33aa809d 2019-08-08 stsp
3989 33aa809d 2019-08-08 stsp return NULL;
3990 33aa809d 2019-08-08 stsp }
3991 33aa809d 2019-08-08 stsp
3992 33aa809d 2019-08-08 stsp static const struct got_error *
3993 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3994 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3995 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3996 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3997 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3998 33aa809d 2019-08-08 stsp {
3999 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4000 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4001 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4002 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4003 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4004 33aa809d 2019-08-08 stsp long pos1, pos2;
4005 33aa809d 2019-08-08 stsp FILE *hunkfile;
4006 33aa809d 2019-08-08 stsp
4007 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4008 33aa809d 2019-08-08 stsp
4009 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4010 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4011 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4012 33aa809d 2019-08-08 stsp
4013 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4014 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4015 33aa809d 2019-08-08 stsp
4016 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4017 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4018 33aa809d 2019-08-08 stsp
4019 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4020 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4021 33aa809d 2019-08-08 stsp goto done;
4022 33aa809d 2019-08-08 stsp }
4023 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4024 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4025 33aa809d 2019-08-08 stsp goto done;
4026 33aa809d 2019-08-08 stsp }
4027 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4028 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4029 33aa809d 2019-08-08 stsp goto done;
4030 33aa809d 2019-08-08 stsp }
4031 33aa809d 2019-08-08 stsp
4032 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4033 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4034 33aa809d 2019-08-08 stsp if (err)
4035 33aa809d 2019-08-08 stsp goto done;
4036 33aa809d 2019-08-08 stsp
4037 33aa809d 2019-08-08 stsp switch (*choice) {
4038 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4039 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4040 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4041 33aa809d 2019-08-08 stsp break;
4042 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4043 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4044 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4045 33aa809d 2019-08-08 stsp break;
4046 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4047 33aa809d 2019-08-08 stsp break;
4048 33aa809d 2019-08-08 stsp default:
4049 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4050 33aa809d 2019-08-08 stsp break;
4051 33aa809d 2019-08-08 stsp }
4052 33aa809d 2019-08-08 stsp done:
4053 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4054 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4055 33aa809d 2019-08-08 stsp return err;
4056 33aa809d 2019-08-08 stsp }
4057 33aa809d 2019-08-08 stsp
4058 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4059 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4060 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4061 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4062 1f1abb7e 2019-08-08 stsp void *progress_arg;
4063 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4064 33aa809d 2019-08-08 stsp void *patch_arg;
4065 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4066 1f1abb7e 2019-08-08 stsp };
4067 a129376b 2019-03-28 stsp
4068 e20a8b6f 2019-06-04 stsp static const struct got_error *
4069 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4070 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4071 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4072 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4073 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4074 33aa809d 2019-08-08 stsp {
4075 33aa809d 2019-08-08 stsp const struct got_error *err;
4076 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4077 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4078 1ebedb77 2019-10-19 stsp int fd2 = -1;
4079 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4080 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4081 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4082 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4083 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4084 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4085 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4086 33aa809d 2019-08-08 stsp int n = 0;
4087 33aa809d 2019-08-08 stsp
4088 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4089 33aa809d 2019-08-08 stsp
4090 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4091 33aa809d 2019-08-08 stsp if (err)
4092 33aa809d 2019-08-08 stsp return err;
4093 33aa809d 2019-08-08 stsp
4094 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4095 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4096 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4097 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
4098 12463d8b 2019-12-13 stsp goto done;
4099 12463d8b 2019-12-13 stsp }
4100 12463d8b 2019-12-13 stsp } else {
4101 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4102 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4103 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
4104 12463d8b 2019-12-13 stsp goto done;
4105 12463d8b 2019-12-13 stsp }
4106 1ebedb77 2019-10-19 stsp }
4107 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
4108 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
4109 1ebedb77 2019-10-19 stsp goto done;
4110 1ebedb77 2019-10-19 stsp }
4111 1ebedb77 2019-10-19 stsp
4112 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
4113 33aa809d 2019-08-08 stsp if (f2 == NULL) {
4114 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
4115 33aa809d 2019-08-08 stsp goto done;
4116 33aa809d 2019-08-08 stsp }
4117 1ebedb77 2019-10-19 stsp fd2 = -1;
4118 33aa809d 2019-08-08 stsp
4119 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4120 33aa809d 2019-08-08 stsp if (err)
4121 33aa809d 2019-08-08 stsp goto done;
4122 33aa809d 2019-08-08 stsp
4123 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4124 33aa809d 2019-08-08 stsp if (err)
4125 33aa809d 2019-08-08 stsp goto done;
4126 33aa809d 2019-08-08 stsp
4127 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4128 33aa809d 2019-08-08 stsp if (err)
4129 33aa809d 2019-08-08 stsp goto done;
4130 33aa809d 2019-08-08 stsp
4131 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4132 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4133 33aa809d 2019-08-08 stsp goto done;
4134 33aa809d 2019-08-08 stsp }
4135 33aa809d 2019-08-08 stsp
4136 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4137 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4138 33aa809d 2019-08-08 stsp if (err)
4139 33aa809d 2019-08-08 stsp goto done;
4140 33aa809d 2019-08-08 stsp
4141 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4142 33aa809d 2019-08-08 stsp if (err)
4143 33aa809d 2019-08-08 stsp goto done;
4144 33aa809d 2019-08-08 stsp
4145 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4146 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4147 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4148 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4149 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4150 33aa809d 2019-08-08 stsp int choice;
4151 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4152 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4153 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4154 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4155 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4156 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4157 33aa809d 2019-08-08 stsp if (err)
4158 33aa809d 2019-08-08 stsp goto done;
4159 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4160 33aa809d 2019-08-08 stsp have_content = 1;
4161 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4162 33aa809d 2019-08-08 stsp break;
4163 33aa809d 2019-08-08 stsp }
4164 1ebedb77 2019-10-19 stsp if (have_content) {
4165 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4166 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4167 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4168 1ebedb77 2019-10-19 stsp if (err)
4169 1ebedb77 2019-10-19 stsp goto done;
4170 1ebedb77 2019-10-19 stsp
4171 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4172 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
4173 1ebedb77 2019-10-19 stsp goto done;
4174 1ebedb77 2019-10-19 stsp }
4175 1ebedb77 2019-10-19 stsp }
4176 33aa809d 2019-08-08 stsp done:
4177 33aa809d 2019-08-08 stsp free(id_str);
4178 33aa809d 2019-08-08 stsp if (blob)
4179 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4180 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4181 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4182 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4183 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4184 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4185 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4186 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4187 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4188 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4189 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4190 33aa809d 2019-08-08 stsp if (err || !have_content) {
4191 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4192 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4193 33aa809d 2019-08-08 stsp free(*path_outfile);
4194 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4195 33aa809d 2019-08-08 stsp }
4196 33aa809d 2019-08-08 stsp free(args);
4197 33aa809d 2019-08-08 stsp if (ds) {
4198 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4199 33aa809d 2019-08-08 stsp free(ds);
4200 33aa809d 2019-08-08 stsp }
4201 33aa809d 2019-08-08 stsp if (changes)
4202 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4203 33aa809d 2019-08-08 stsp free(path1);
4204 33aa809d 2019-08-08 stsp return err;
4205 33aa809d 2019-08-08 stsp }
4206 33aa809d 2019-08-08 stsp
4207 33aa809d 2019-08-08 stsp static const struct got_error *
4208 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4209 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4210 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4211 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4212 a129376b 2019-03-28 stsp {
4213 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4214 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4215 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4216 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4217 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4218 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4219 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4220 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4221 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4222 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4223 a129376b 2019-03-28 stsp
4224 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4225 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4226 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4227 d3bcc3d1 2019-08-08 stsp return NULL;
4228 3d69ad8d 2019-08-17 semarie
4229 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4230 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4231 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4232 d3bcc3d1 2019-08-08 stsp
4233 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4234 65084dad 2019-08-08 stsp if (ie == NULL)
4235 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4236 a129376b 2019-03-28 stsp
4237 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4238 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4239 a129376b 2019-03-28 stsp if (err) {
4240 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4241 a129376b 2019-03-28 stsp goto done;
4242 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4243 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4244 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4245 e20a8b6f 2019-06-04 stsp goto done;
4246 e20a8b6f 2019-06-04 stsp }
4247 a129376b 2019-03-28 stsp }
4248 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4249 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4250 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4251 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4252 a129376b 2019-03-28 stsp goto done;
4253 a129376b 2019-03-28 stsp }
4254 a129376b 2019-03-28 stsp } else {
4255 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4256 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4257 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4258 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4259 a129376b 2019-03-28 stsp goto done;
4260 a129376b 2019-03-28 stsp }
4261 a129376b 2019-03-28 stsp } else {
4262 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4263 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4264 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4265 a129376b 2019-03-28 stsp goto done;
4266 a129376b 2019-03-28 stsp }
4267 a129376b 2019-03-28 stsp }
4268 a129376b 2019-03-28 stsp }
4269 a129376b 2019-03-28 stsp
4270 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4271 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4272 a9fa2909 2019-07-27 stsp if (err) {
4273 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4274 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4275 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4276 a9fa2909 2019-07-27 stsp goto done;
4277 a9fa2909 2019-07-27 stsp } else {
4278 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4279 a9fa2909 2019-07-27 stsp if (err)
4280 a9fa2909 2019-07-27 stsp goto done;
4281 a9fa2909 2019-07-27 stsp
4282 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4283 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4284 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4285 a9fa2909 2019-07-27 stsp goto done;
4286 a9fa2909 2019-07-27 stsp }
4287 a9fa2909 2019-07-27 stsp
4288 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4289 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4290 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4291 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4292 a9fa2909 2019-07-27 stsp goto done;
4293 a9fa2909 2019-07-27 stsp }
4294 a129376b 2019-03-28 stsp }
4295 a129376b 2019-03-28 stsp
4296 a129376b 2019-03-28 stsp switch (status) {
4297 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4298 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4299 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4300 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4301 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4302 33aa809d 2019-08-08 stsp if (err)
4303 33aa809d 2019-08-08 stsp goto done;
4304 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4305 33aa809d 2019-08-08 stsp break;
4306 33aa809d 2019-08-08 stsp }
4307 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4308 1f1abb7e 2019-08-08 stsp ie->path);
4309 1ee397ad 2019-07-12 stsp if (err)
4310 1ee397ad 2019-07-12 stsp goto done;
4311 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4312 a129376b 2019-03-28 stsp break;
4313 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4314 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4315 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4316 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4317 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4318 33aa809d 2019-08-08 stsp if (err)
4319 33aa809d 2019-08-08 stsp goto done;
4320 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4321 33aa809d 2019-08-08 stsp break;
4322 33aa809d 2019-08-08 stsp }
4323 33aa809d 2019-08-08 stsp /* fall through */
4324 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4325 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4326 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4327 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4328 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4329 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4330 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4331 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4332 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4333 24278f30 2019-08-03 stsp } else
4334 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4335 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4336 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4337 a129376b 2019-03-28 stsp if (err)
4338 65084dad 2019-08-08 stsp goto done;
4339 65084dad 2019-08-08 stsp
4340 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4341 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4342 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4343 a129376b 2019-03-28 stsp goto done;
4344 65084dad 2019-08-08 stsp }
4345 65084dad 2019-08-08 stsp
4346 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4347 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4348 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4349 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4350 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4351 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4352 33aa809d 2019-08-08 stsp break;
4353 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
4354 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
4355 33aa809d 2019-08-08 stsp path_content, ondisk_path);
4356 33aa809d 2019-08-08 stsp goto done;
4357 33aa809d 2019-08-08 stsp }
4358 33aa809d 2019-08-08 stsp } else {
4359 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4360 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4361 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4362 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4363 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4364 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4365 2e1fa222 2020-07-23 stsp } else {
4366 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4367 2e1fa222 2020-07-23 stsp ie->path,
4368 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4369 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4370 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4371 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4372 2e1fa222 2020-07-23 stsp }
4373 a129376b 2019-03-28 stsp if (err)
4374 a129376b 2019-03-28 stsp goto done;
4375 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4376 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4377 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4378 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4379 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4380 2e1fa222 2020-07-23 stsp if (err)
4381 2e1fa222 2020-07-23 stsp goto done;
4382 2e1fa222 2020-07-23 stsp }
4383 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4384 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
4385 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4386 33aa809d 2019-08-08 stsp if (err)
4387 33aa809d 2019-08-08 stsp goto done;
4388 33aa809d 2019-08-08 stsp }
4389 a129376b 2019-03-28 stsp }
4390 a129376b 2019-03-28 stsp break;
4391 e20a8b6f 2019-06-04 stsp }
4392 a129376b 2019-03-28 stsp default:
4393 1f1abb7e 2019-08-08 stsp break;
4394 a129376b 2019-03-28 stsp }
4395 a129376b 2019-03-28 stsp done:
4396 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4397 33aa809d 2019-08-08 stsp free(path_content);
4398 e20a8b6f 2019-06-04 stsp free(parent_path);
4399 a129376b 2019-03-28 stsp free(tree_path);
4400 a129376b 2019-03-28 stsp if (blob)
4401 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4402 a129376b 2019-03-28 stsp if (tree)
4403 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4404 a129376b 2019-03-28 stsp free(tree_id);
4405 e20a8b6f 2019-06-04 stsp return err;
4406 e20a8b6f 2019-06-04 stsp }
4407 e20a8b6f 2019-06-04 stsp
4408 e20a8b6f 2019-06-04 stsp const struct got_error *
4409 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4410 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4411 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4412 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4413 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4414 e20a8b6f 2019-06-04 stsp {
4415 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4416 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4417 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4418 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4419 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4420 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4421 e20a8b6f 2019-06-04 stsp
4422 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4423 e20a8b6f 2019-06-04 stsp if (err)
4424 e20a8b6f 2019-06-04 stsp return err;
4425 e20a8b6f 2019-06-04 stsp
4426 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4427 e20a8b6f 2019-06-04 stsp if (err)
4428 e20a8b6f 2019-06-04 stsp goto done;
4429 e20a8b6f 2019-06-04 stsp
4430 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4431 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4432 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4433 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4434 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4435 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4436 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4437 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4438 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4439 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4440 e20a8b6f 2019-06-04 stsp if (err)
4441 af12c6b9 2019-06-04 stsp break;
4442 e20a8b6f 2019-06-04 stsp }
4443 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4444 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4445 e20a8b6f 2019-06-04 stsp err = sync_err;
4446 af12c6b9 2019-06-04 stsp done:
4447 fb399478 2019-07-12 stsp free(fileindex_path);
4448 a129376b 2019-03-28 stsp if (fileindex)
4449 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4450 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4451 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4452 a129376b 2019-03-28 stsp err = unlockerr;
4453 c4296144 2019-05-09 stsp return err;
4454 c4296144 2019-05-09 stsp }
4455 c4296144 2019-05-09 stsp
4456 cf066bf8 2019-05-09 stsp static void
4457 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4458 cf066bf8 2019-05-09 stsp {
4459 24519714 2019-05-09 stsp free(ct->path);
4460 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4461 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4462 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4463 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4464 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4465 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4466 cf066bf8 2019-05-09 stsp free(ct);
4467 cf066bf8 2019-05-09 stsp }
4468 24519714 2019-05-09 stsp
4469 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4470 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4471 24519714 2019-05-09 stsp struct got_repository *repo;
4472 24519714 2019-05-09 stsp struct got_worktree *worktree;
4473 5f8a88c6 2019-08-03 stsp int have_staged_files;
4474 24519714 2019-05-09 stsp };
4475 cf066bf8 2019-05-09 stsp
4476 c4296144 2019-05-09 stsp static const struct got_error *
4477 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4478 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4479 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4480 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4481 c4296144 2019-05-09 stsp {
4482 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4483 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4484 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4485 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4486 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4487 768aea60 2019-05-09 stsp struct stat sb;
4488 c4296144 2019-05-09 stsp
4489 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4490 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4491 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4492 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4493 5f8a88c6 2019-08-03 stsp return NULL;
4494 5f8a88c6 2019-08-03 stsp } else {
4495 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4496 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4497 c4296144 2019-05-09 stsp
4498 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4499 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4500 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4501 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4502 5f8a88c6 2019-08-03 stsp return NULL;
4503 5f8a88c6 2019-08-03 stsp }
4504 0b5cc0d6 2019-05-09 stsp
4505 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4506 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4507 036813ee 2019-05-09 stsp goto done;
4508 036813ee 2019-05-09 stsp }
4509 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4510 036813ee 2019-05-09 stsp parent_path = strdup("");
4511 69960a46 2019-05-09 stsp if (parent_path == NULL)
4512 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4513 69960a46 2019-05-09 stsp } else {
4514 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4515 69960a46 2019-05-09 stsp if (err)
4516 69960a46 2019-05-09 stsp return err;
4517 69960a46 2019-05-09 stsp }
4518 c4296144 2019-05-09 stsp
4519 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4520 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4521 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4522 c4296144 2019-05-09 stsp goto done;
4523 768aea60 2019-05-09 stsp }
4524 768aea60 2019-05-09 stsp
4525 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4526 768aea60 2019-05-09 stsp relpath) == -1) {
4527 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4528 768aea60 2019-05-09 stsp goto done;
4529 768aea60 2019-05-09 stsp }
4530 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4531 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4532 768aea60 2019-05-09 stsp } else {
4533 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4534 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4535 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4536 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4537 12463d8b 2019-12-13 stsp ct->ondisk_path);
4538 12463d8b 2019-12-13 stsp goto done;
4539 12463d8b 2019-12-13 stsp }
4540 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4541 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4542 768aea60 2019-05-09 stsp goto done;
4543 768aea60 2019-05-09 stsp }
4544 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4545 c4296144 2019-05-09 stsp }
4546 c4296144 2019-05-09 stsp
4547 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4548 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4549 44d03001 2019-05-09 stsp relpath) == -1) {
4550 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4551 44d03001 2019-05-09 stsp goto done;
4552 44d03001 2019-05-09 stsp }
4553 44d03001 2019-05-09 stsp
4554 cf066bf8 2019-05-09 stsp ct->status = status;
4555 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4556 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4557 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4558 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4559 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4560 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4561 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4562 b416585c 2019-05-13 stsp goto done;
4563 b416585c 2019-05-13 stsp }
4564 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4565 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4566 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4567 f0b75401 2019-08-03 stsp goto done;
4568 f0b75401 2019-08-03 stsp }
4569 f0b75401 2019-08-03 stsp }
4570 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4571 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4572 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4573 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4574 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4575 036813ee 2019-05-09 stsp goto done;
4576 036813ee 2019-05-09 stsp }
4577 ca2503ea 2019-05-09 stsp }
4578 24519714 2019-05-09 stsp ct->path = strdup(path);
4579 24519714 2019-05-09 stsp if (ct->path == NULL) {
4580 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4581 24519714 2019-05-09 stsp goto done;
4582 24519714 2019-05-09 stsp }
4583 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4584 c4296144 2019-05-09 stsp done:
4585 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4586 ed175427 2019-05-09 stsp free_commitable(ct);
4587 24519714 2019-05-09 stsp free(parent_path);
4588 036813ee 2019-05-09 stsp free(path);
4589 a129376b 2019-03-28 stsp return err;
4590 a129376b 2019-03-28 stsp }
4591 c4296144 2019-05-09 stsp
4592 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4593 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4594 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4595 036813ee 2019-05-09 stsp struct got_repository *);
4596 ed175427 2019-05-09 stsp
4597 ed175427 2019-05-09 stsp static const struct got_error *
4598 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4599 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4600 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4601 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4602 afa376bf 2019-05-09 stsp struct got_repository *repo)
4603 ed175427 2019-05-09 stsp {
4604 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4605 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4606 036813ee 2019-05-09 stsp char *subpath;
4607 ed175427 2019-05-09 stsp
4608 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4609 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4610 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4611 ed175427 2019-05-09 stsp
4612 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4613 ed175427 2019-05-09 stsp if (err)
4614 ed175427 2019-05-09 stsp return err;
4615 ed175427 2019-05-09 stsp
4616 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4617 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4618 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4619 036813ee 2019-05-09 stsp free(subpath);
4620 036813ee 2019-05-09 stsp return err;
4621 036813ee 2019-05-09 stsp }
4622 ed175427 2019-05-09 stsp
4623 036813ee 2019-05-09 stsp static const struct got_error *
4624 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4625 036813ee 2019-05-09 stsp {
4626 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4627 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4628 ed175427 2019-05-09 stsp
4629 036813ee 2019-05-09 stsp *match = 0;
4630 ed175427 2019-05-09 stsp
4631 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4632 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4633 0f63689d 2019-05-10 stsp return NULL;
4634 036813ee 2019-05-09 stsp }
4635 0b5cc0d6 2019-05-09 stsp
4636 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4637 0f63689d 2019-05-10 stsp if (err)
4638 0f63689d 2019-05-10 stsp return err;
4639 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4640 036813ee 2019-05-09 stsp free(ct_parent_path);
4641 036813ee 2019-05-09 stsp return err;
4642 036813ee 2019-05-09 stsp }
4643 0b5cc0d6 2019-05-09 stsp
4644 768aea60 2019-05-09 stsp static mode_t
4645 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4646 768aea60 2019-05-09 stsp {
4647 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4648 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4649 3d9a4ec4 2020-07-23 stsp
4650 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4651 768aea60 2019-05-09 stsp }
4652 768aea60 2019-05-09 stsp
4653 036813ee 2019-05-09 stsp static const struct got_error *
4654 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4655 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4656 036813ee 2019-05-09 stsp {
4657 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4658 ca2503ea 2019-05-09 stsp
4659 036813ee 2019-05-09 stsp *new_te = NULL;
4660 0b5cc0d6 2019-05-09 stsp
4661 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4662 036813ee 2019-05-09 stsp if (err)
4663 036813ee 2019-05-09 stsp goto done;
4664 0b5cc0d6 2019-05-09 stsp
4665 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4666 036813ee 2019-05-09 stsp
4667 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4668 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4669 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4670 5f8a88c6 2019-08-03 stsp else
4671 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4672 036813ee 2019-05-09 stsp done:
4673 036813ee 2019-05-09 stsp if (err && *new_te) {
4674 56e0773d 2019-11-28 stsp free(*new_te);
4675 036813ee 2019-05-09 stsp *new_te = NULL;
4676 036813ee 2019-05-09 stsp }
4677 036813ee 2019-05-09 stsp return err;
4678 036813ee 2019-05-09 stsp }
4679 036813ee 2019-05-09 stsp
4680 036813ee 2019-05-09 stsp static const struct got_error *
4681 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4682 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4683 036813ee 2019-05-09 stsp {
4684 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4685 036813ee 2019-05-09 stsp char *ct_name;
4686 036813ee 2019-05-09 stsp
4687 036813ee 2019-05-09 stsp *new_te = NULL;
4688 036813ee 2019-05-09 stsp
4689 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4690 036813ee 2019-05-09 stsp if (*new_te == NULL)
4691 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4692 036813ee 2019-05-09 stsp
4693 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4694 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4695 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4696 036813ee 2019-05-09 stsp goto done;
4697 036813ee 2019-05-09 stsp }
4698 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4699 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4700 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4701 036813ee 2019-05-09 stsp goto done;
4702 036813ee 2019-05-09 stsp }
4703 036813ee 2019-05-09 stsp
4704 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4705 036813ee 2019-05-09 stsp
4706 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4707 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4708 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4709 5f8a88c6 2019-08-03 stsp else
4710 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4711 036813ee 2019-05-09 stsp done:
4712 036813ee 2019-05-09 stsp if (err && *new_te) {
4713 56e0773d 2019-11-28 stsp free(*new_te);
4714 036813ee 2019-05-09 stsp *new_te = NULL;
4715 036813ee 2019-05-09 stsp }
4716 036813ee 2019-05-09 stsp return err;
4717 036813ee 2019-05-09 stsp }
4718 036813ee 2019-05-09 stsp
4719 036813ee 2019-05-09 stsp static const struct got_error *
4720 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4721 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4722 036813ee 2019-05-09 stsp {
4723 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4724 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4725 036813ee 2019-05-09 stsp
4726 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4727 036813ee 2019-05-09 stsp if (err)
4728 036813ee 2019-05-09 stsp return err;
4729 036813ee 2019-05-09 stsp if (new_pe == NULL)
4730 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4731 036813ee 2019-05-09 stsp return NULL;
4732 afa376bf 2019-05-09 stsp }
4733 afa376bf 2019-05-09 stsp
4734 afa376bf 2019-05-09 stsp static const struct got_error *
4735 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4736 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4737 afa376bf 2019-05-09 stsp {
4738 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4739 5f8a88c6 2019-08-03 stsp unsigned char status;
4740 5f8a88c6 2019-08-03 stsp
4741 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4742 afa376bf 2019-05-09 stsp ct_path++;
4743 5f8a88c6 2019-08-03 stsp
4744 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4745 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4746 5f8a88c6 2019-08-03 stsp else
4747 5f8a88c6 2019-08-03 stsp status = ct->status;
4748 5f8a88c6 2019-08-03 stsp
4749 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4750 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4751 036813ee 2019-05-09 stsp }
4752 036813ee 2019-05-09 stsp
4753 036813ee 2019-05-09 stsp static const struct got_error *
4754 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4755 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4756 44d03001 2019-05-09 stsp {
4757 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4758 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4759 44d03001 2019-05-09 stsp char *te_path;
4760 44d03001 2019-05-09 stsp
4761 44d03001 2019-05-09 stsp *modified = 0;
4762 44d03001 2019-05-09 stsp
4763 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4764 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4765 44d03001 2019-05-09 stsp te->name) == -1)
4766 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4767 44d03001 2019-05-09 stsp
4768 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4769 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4770 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4771 44d03001 2019-05-09 stsp strlen(te_path));
4772 44d03001 2019-05-09 stsp if (*modified)
4773 44d03001 2019-05-09 stsp break;
4774 44d03001 2019-05-09 stsp }
4775 44d03001 2019-05-09 stsp
4776 44d03001 2019-05-09 stsp free(te_path);
4777 44d03001 2019-05-09 stsp return err;
4778 44d03001 2019-05-09 stsp }
4779 44d03001 2019-05-09 stsp
4780 44d03001 2019-05-09 stsp static const struct got_error *
4781 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4782 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4783 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4784 036813ee 2019-05-09 stsp {
4785 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4786 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4787 036813ee 2019-05-09 stsp
4788 036813ee 2019-05-09 stsp *ctp = NULL;
4789 036813ee 2019-05-09 stsp
4790 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4791 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4792 036813ee 2019-05-09 stsp char *ct_name = NULL;
4793 036813ee 2019-05-09 stsp int path_matches;
4794 036813ee 2019-05-09 stsp
4795 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4796 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4797 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4798 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4799 5f8a88c6 2019-08-03 stsp continue;
4800 5f8a88c6 2019-08-03 stsp } else {
4801 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4802 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4803 5f8a88c6 2019-08-03 stsp continue;
4804 5f8a88c6 2019-08-03 stsp }
4805 036813ee 2019-05-09 stsp
4806 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4807 036813ee 2019-05-09 stsp continue;
4808 036813ee 2019-05-09 stsp
4809 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4810 036813ee 2019-05-09 stsp if (err)
4811 036813ee 2019-05-09 stsp return err;
4812 036813ee 2019-05-09 stsp if (!path_matches)
4813 036813ee 2019-05-09 stsp continue;
4814 036813ee 2019-05-09 stsp
4815 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4816 036813ee 2019-05-09 stsp if (ct_name == NULL)
4817 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4818 036813ee 2019-05-09 stsp
4819 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4820 036813ee 2019-05-09 stsp continue;
4821 036813ee 2019-05-09 stsp
4822 036813ee 2019-05-09 stsp *ctp = ct;
4823 036813ee 2019-05-09 stsp break;
4824 036813ee 2019-05-09 stsp }
4825 2b496619 2019-07-10 stsp
4826 2b496619 2019-07-10 stsp return err;
4827 2b496619 2019-07-10 stsp }
4828 2b496619 2019-07-10 stsp
4829 2b496619 2019-07-10 stsp static const struct got_error *
4830 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4831 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4832 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4833 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4834 2b496619 2019-07-10 stsp struct got_repository *repo)
4835 2b496619 2019-07-10 stsp {
4836 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4837 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4838 2b496619 2019-07-10 stsp char *subtree_path;
4839 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4840 ba580f68 2020-03-22 stsp int nentries;
4841 2b496619 2019-07-10 stsp
4842 2b496619 2019-07-10 stsp *new_tep = NULL;
4843 2b496619 2019-07-10 stsp
4844 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4845 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4846 2b496619 2019-07-10 stsp child_path) == -1)
4847 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4848 036813ee 2019-05-09 stsp
4849 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4850 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4851 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4852 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4853 56e0773d 2019-11-28 stsp
4854 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4855 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4856 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4857 2b496619 2019-07-10 stsp goto done;
4858 2b496619 2019-07-10 stsp }
4859 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4860 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4861 2b496619 2019-07-10 stsp if (err) {
4862 56e0773d 2019-11-28 stsp free(new_te);
4863 2b496619 2019-07-10 stsp goto done;
4864 2b496619 2019-07-10 stsp }
4865 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4866 2b496619 2019-07-10 stsp done:
4867 56e0773d 2019-11-28 stsp free(id);
4868 2b496619 2019-07-10 stsp free(subtree_path);
4869 2b496619 2019-07-10 stsp if (err == NULL)
4870 2b496619 2019-07-10 stsp *new_tep = new_te;
4871 036813ee 2019-05-09 stsp return err;
4872 036813ee 2019-05-09 stsp }
4873 036813ee 2019-05-09 stsp
4874 036813ee 2019-05-09 stsp static const struct got_error *
4875 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4876 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4877 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4878 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4879 036813ee 2019-05-09 stsp struct got_repository *repo)
4880 036813ee 2019-05-09 stsp {
4881 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4882 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4883 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4884 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4885 036813ee 2019-05-09 stsp
4886 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4887 ba580f68 2020-03-22 stsp *nentries = 0;
4888 036813ee 2019-05-09 stsp
4889 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4890 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4891 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4892 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4893 036813ee 2019-05-09 stsp
4894 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4895 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4896 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4897 036813ee 2019-05-09 stsp continue;
4898 036813ee 2019-05-09 stsp
4899 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4900 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4901 036813ee 2019-05-09 stsp continue;
4902 036813ee 2019-05-09 stsp
4903 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4904 036813ee 2019-05-09 stsp pe->path);
4905 036813ee 2019-05-09 stsp if (err)
4906 036813ee 2019-05-09 stsp goto done;
4907 036813ee 2019-05-09 stsp
4908 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4909 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4910 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4911 036813ee 2019-05-09 stsp if (err)
4912 036813ee 2019-05-09 stsp goto done;
4913 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4914 afa376bf 2019-05-09 stsp if (err)
4915 afa376bf 2019-05-09 stsp goto done;
4916 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4917 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4918 2b496619 2019-07-10 stsp if (err)
4919 2f51b5b3 2019-05-09 stsp goto done;
4920 ba580f68 2020-03-22 stsp (*nentries)++;
4921 2b496619 2019-07-10 stsp } else {
4922 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4923 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4924 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4925 2b496619 2019-07-10 stsp == NULL) {
4926 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4927 2b496619 2019-07-10 stsp child_path, path_base_tree,
4928 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4929 2b496619 2019-07-10 stsp repo);
4930 2b496619 2019-07-10 stsp if (err)
4931 2b496619 2019-07-10 stsp goto done;
4932 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4933 2b496619 2019-07-10 stsp if (err)
4934 2b496619 2019-07-10 stsp goto done;
4935 ba580f68 2020-03-22 stsp (*nentries)++;
4936 9ba0479c 2019-05-10 stsp }
4937 ed175427 2019-05-09 stsp }
4938 2f51b5b3 2019-05-09 stsp }
4939 2f51b5b3 2019-05-09 stsp
4940 2f51b5b3 2019-05-09 stsp if (base_tree) {
4941 56e0773d 2019-11-28 stsp int i, nbase_entries;
4942 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4943 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4944 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4945 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4946 63c5ca5d 2019-08-24 stsp
4947 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4948 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4949 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4950 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4951 63c5ca5d 2019-08-24 stsp if (err)
4952 63c5ca5d 2019-08-24 stsp goto done;
4953 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4954 63c5ca5d 2019-08-24 stsp if (err)
4955 63c5ca5d 2019-08-24 stsp goto done;
4956 ba580f68 2020-03-22 stsp (*nentries)++;
4957 63c5ca5d 2019-08-24 stsp continue;
4958 63c5ca5d 2019-08-24 stsp }
4959 2f51b5b3 2019-05-09 stsp
4960 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4961 44d03001 2019-05-09 stsp int modified;
4962 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4963 036813ee 2019-05-09 stsp if (err)
4964 036813ee 2019-05-09 stsp goto done;
4965 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4966 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4967 2f51b5b3 2019-05-09 stsp if (err)
4968 2f51b5b3 2019-05-09 stsp goto done;
4969 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4970 44d03001 2019-05-09 stsp if (modified) {
4971 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4972 ba580f68 2020-03-22 stsp int nsubentries;
4973 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4974 ba580f68 2020-03-22 stsp &nsubentries, te,
4975 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4976 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4977 44d03001 2019-05-09 stsp if (err)
4978 44d03001 2019-05-09 stsp goto done;
4979 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4980 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4981 ba580f68 2020-03-22 stsp free(new_id);
4982 ba580f68 2020-03-22 stsp continue;
4983 ba580f68 2020-03-22 stsp }
4984 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4985 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4986 56e0773d 2019-11-28 stsp free(new_id);
4987 44d03001 2019-05-09 stsp }
4988 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4989 036813ee 2019-05-09 stsp if (err)
4990 036813ee 2019-05-09 stsp goto done;
4991 ba580f68 2020-03-22 stsp (*nentries)++;
4992 2f51b5b3 2019-05-09 stsp continue;
4993 036813ee 2019-05-09 stsp }
4994 2f51b5b3 2019-05-09 stsp
4995 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4996 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4997 f66c734c 2019-09-22 stsp if (err)
4998 f66c734c 2019-09-22 stsp goto done;
4999 2f51b5b3 2019-05-09 stsp if (ct) {
5000 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5001 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5002 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5003 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5004 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5005 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5006 2f51b5b3 2019-05-09 stsp if (err)
5007 2f51b5b3 2019-05-09 stsp goto done;
5008 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5009 2f51b5b3 2019-05-09 stsp if (err)
5010 2f51b5b3 2019-05-09 stsp goto done;
5011 ba580f68 2020-03-22 stsp (*nentries)++;
5012 2f51b5b3 2019-05-09 stsp }
5013 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5014 b416585c 2019-05-13 stsp status_arg);
5015 afa376bf 2019-05-09 stsp if (err)
5016 afa376bf 2019-05-09 stsp goto done;
5017 2f51b5b3 2019-05-09 stsp } else {
5018 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5019 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5020 2f51b5b3 2019-05-09 stsp if (err)
5021 2f51b5b3 2019-05-09 stsp goto done;
5022 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5023 2f51b5b3 2019-05-09 stsp if (err)
5024 2f51b5b3 2019-05-09 stsp goto done;
5025 ba580f68 2020-03-22 stsp (*nentries)++;
5026 2f51b5b3 2019-05-09 stsp }
5027 ca2503ea 2019-05-09 stsp }
5028 ed175427 2019-05-09 stsp }
5029 0b5cc0d6 2019-05-09 stsp
5030 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5031 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5032 ed175427 2019-05-09 stsp done:
5033 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5034 ed175427 2019-05-09 stsp return err;
5035 ed175427 2019-05-09 stsp }
5036 ed175427 2019-05-09 stsp
5037 ebf99748 2019-05-09 stsp static const struct got_error *
5038 2e1fa222 2020-07-23 stsp reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5039 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5040 2e1fa222 2020-07-23 stsp struct got_repository *repo)
5041 2e1fa222 2020-07-23 stsp {
5042 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5043 2e1fa222 2020-07-23 stsp struct got_blob_object *blob = NULL;
5044 2e1fa222 2020-07-23 stsp struct got_object_id *tree_id = NULL;
5045 2e1fa222 2020-07-23 stsp char *tree_path = NULL;
5046 2e1fa222 2020-07-23 stsp struct got_tree_object *tree = NULL;
5047 2e1fa222 2020-07-23 stsp struct got_tree_entry *te;
5048 2e1fa222 2020-07-23 stsp char *entry_name;
5049 2e1fa222 2020-07-23 stsp
5050 2e1fa222 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
5051 2e1fa222 2020-07-23 stsp if (err)
5052 2e1fa222 2020-07-23 stsp return err;
5053 2e1fa222 2020-07-23 stsp
5054 2e1fa222 2020-07-23 stsp err = got_path_dirname(&tree_path, ct->in_repo_path);
5055 2e1fa222 2020-07-23 stsp if (err) {
5056 2e1fa222 2020-07-23 stsp if (err->code != GOT_ERR_BAD_PATH)
5057 2e1fa222 2020-07-23 stsp goto done;
5058 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
5059 2e1fa222 2020-07-23 stsp new_base_commit_id, "");
5060 2e1fa222 2020-07-23 stsp if (err)
5061 2e1fa222 2020-07-23 stsp goto done;
5062 2e1fa222 2020-07-23 stsp } else {
5063 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
5064 2e1fa222 2020-07-23 stsp new_base_commit_id, tree_path);
5065 2e1fa222 2020-07-23 stsp if (err)
5066 2e1fa222 2020-07-23 stsp goto done;
5067 2e1fa222 2020-07-23 stsp }
5068 2e1fa222 2020-07-23 stsp
5069 2e1fa222 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5070 2e1fa222 2020-07-23 stsp if (err)
5071 2e1fa222 2020-07-23 stsp goto done;
5072 2e1fa222 2020-07-23 stsp
5073 2e1fa222 2020-07-23 stsp entry_name = basename(ct->path);
5074 2e1fa222 2020-07-23 stsp if (entry_name == NULL) {
5075 2e1fa222 2020-07-23 stsp err = got_error_from_errno2("basename", ct->path);
5076 2e1fa222 2020-07-23 stsp goto done;
5077 2e1fa222 2020-07-23 stsp }
5078 2e1fa222 2020-07-23 stsp
5079 2e1fa222 2020-07-23 stsp te = got_object_tree_find_entry(tree, entry_name);
5080 2e1fa222 2020-07-23 stsp if (te == NULL) {
5081 2e1fa222 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5082 2e1fa222 2020-07-23 stsp goto done;
5083 2e1fa222 2020-07-23 stsp }
5084 2e1fa222 2020-07-23 stsp
5085 2e1fa222 2020-07-23 stsp err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5086 c90c8ce3 2020-07-23 stsp ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5087 2e1fa222 2020-07-23 stsp done:
5088 2e1fa222 2020-07-23 stsp if (blob)
5089 2e1fa222 2020-07-23 stsp got_object_blob_close(blob);
5090 2e1fa222 2020-07-23 stsp if (tree)
5091 2e1fa222 2020-07-23 stsp got_object_tree_close(tree);
5092 2e1fa222 2020-07-23 stsp free(tree_id);
5093 2e1fa222 2020-07-23 stsp free(tree_path);
5094 2e1fa222 2020-07-23 stsp return err;
5095 2e1fa222 2020-07-23 stsp }
5096 2e1fa222 2020-07-23 stsp
5097 2e1fa222 2020-07-23 stsp /*
5098 2e1fa222 2020-07-23 stsp * After comitting a symlink we have a chance to convert "bad" symlinks
5099 2e1fa222 2020-07-23 stsp * (those which point outside the work tree or into .got) to regular files.
5100 2e1fa222 2020-07-23 stsp * This way, the post-commit work tree state matches a fresh checkout of
5101 2e1fa222 2020-07-23 stsp * the tree which was just committed. We also mark such newly committed
5102 2e1fa222 2020-07-23 stsp * symlinks as "bad" in the work tree's fileindex.
5103 2e1fa222 2020-07-23 stsp */
5104 2e1fa222 2020-07-23 stsp static const struct got_error *
5105 2e1fa222 2020-07-23 stsp reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5106 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5107 2e1fa222 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo)
5108 2e1fa222 2020-07-23 stsp {
5109 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5110 2e1fa222 2020-07-23 stsp struct got_pathlist_entry *pe;
5111 2e1fa222 2020-07-23 stsp
5112 2e1fa222 2020-07-23 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5113 2e1fa222 2020-07-23 stsp struct got_commitable *ct = pe->data;
5114 2e1fa222 2020-07-23 stsp struct got_fileindex_entry *ie;
5115 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5116 2e1fa222 2020-07-23 stsp
5117 2e1fa222 2020-07-23 stsp if (!S_ISLNK(get_ct_file_mode(ct)))
5118 2e1fa222 2020-07-23 stsp continue;
5119 2e1fa222 2020-07-23 stsp
5120 2e1fa222 2020-07-23 stsp err = reinstall_symlink_after_commit(&is_bad_symlink,
5121 2e1fa222 2020-07-23 stsp ct, new_base_commit_id, worktree, repo);
5122 2e1fa222 2020-07-23 stsp if (err)
5123 2e1fa222 2020-07-23 stsp break;
5124 2e1fa222 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, ct->path,
5125 2e1fa222 2020-07-23 stsp strlen(ct->path));
5126 2e1fa222 2020-07-23 stsp if (ie && is_bad_symlink) {
5127 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
5128 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5129 2e1fa222 2020-07-23 stsp if (err)
5130 2e1fa222 2020-07-23 stsp break;
5131 2e1fa222 2020-07-23 stsp }
5132 2e1fa222 2020-07-23 stsp }
5133 2e1fa222 2020-07-23 stsp
5134 2e1fa222 2020-07-23 stsp return err;
5135 2e1fa222 2020-07-23 stsp }
5136 2e1fa222 2020-07-23 stsp
5137 2e1fa222 2020-07-23 stsp static const struct got_error *
5138 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5139 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5140 72fd46fa 2019-09-06 stsp int have_staged_files)
5141 ebf99748 2019-05-09 stsp {
5142 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5143 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5144 ebf99748 2019-05-09 stsp
5145 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5146 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5147 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5148 ebf99748 2019-05-09 stsp
5149 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5150 ebf99748 2019-05-09 stsp if (ie) {
5151 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5152 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5153 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5154 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5155 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5156 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5157 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5158 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5159 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5160 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5161 72fd46fa 2019-09-06 stsp !have_staged_files);
5162 ebf99748 2019-05-09 stsp } else
5163 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5164 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5165 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5166 72fd46fa 2019-09-06 stsp !have_staged_files);
5167 ebf99748 2019-05-09 stsp } else {
5168 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5169 ebf99748 2019-05-09 stsp if (err)
5170 af12c6b9 2019-06-04 stsp break;
5171 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5172 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5173 3969253a 2020-03-07 stsp if (err) {
5174 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5175 3969253a 2020-03-07 stsp break;
5176 3969253a 2020-03-07 stsp }
5177 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5178 3969253a 2020-03-07 stsp if (err) {
5179 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5180 af12c6b9 2019-06-04 stsp break;
5181 3969253a 2020-03-07 stsp }
5182 ebf99748 2019-05-09 stsp }
5183 ebf99748 2019-05-09 stsp }
5184 d56d26ce 2019-05-10 stsp return err;
5185 d56d26ce 2019-05-10 stsp }
5186 735ef5ac 2019-08-03 stsp
5187 d56d26ce 2019-05-10 stsp
5188 d56d26ce 2019-05-10 stsp static const struct got_error *
5189 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5190 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5191 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5192 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5193 735ef5ac 2019-08-03 stsp int ood_errcode)
5194 d56d26ce 2019-05-10 stsp {
5195 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5196 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5197 1a36436d 2019-06-10 stsp
5198 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5199 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5200 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5201 1a36436d 2019-06-10 stsp return NULL;
5202 9bead371 2019-07-28 stsp /*
5203 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5204 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5205 9bead371 2019-07-28 stsp */
5206 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5207 735ef5ac 2019-08-03 stsp in_repo_path);
5208 9bead371 2019-07-28 stsp if (err) {
5209 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5210 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5211 1a36436d 2019-06-10 stsp goto done;
5212 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5213 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5214 1a36436d 2019-06-10 stsp } else {
5215 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5216 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5217 735ef5ac 2019-08-03 stsp in_repo_path);
5218 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5219 1a36436d 2019-06-10 stsp goto done;
5220 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5221 1a36436d 2019-06-10 stsp }
5222 1a36436d 2019-06-10 stsp done:
5223 1a36436d 2019-06-10 stsp free(id);
5224 1a36436d 2019-06-10 stsp return err;
5225 ebf99748 2019-05-09 stsp }
5226 ebf99748 2019-05-09 stsp
5227 c4296144 2019-05-09 stsp const struct got_error *
5228 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5229 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5230 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5231 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5232 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5233 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5234 afa376bf 2019-05-09 stsp struct got_repository *repo)
5235 c4296144 2019-05-09 stsp {
5236 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5237 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5238 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5239 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5240 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5241 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5242 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5243 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5244 ba580f68 2020-03-22 stsp int nentries;
5245 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5246 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5247 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5248 c4296144 2019-05-09 stsp
5249 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5250 c4296144 2019-05-09 stsp
5251 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5252 675c7539 2019-05-09 stsp
5253 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5254 588edf97 2019-05-10 stsp if (err)
5255 588edf97 2019-05-10 stsp goto done;
5256 de18fc63 2019-05-09 stsp
5257 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5258 588edf97 2019-05-10 stsp if (err)
5259 588edf97 2019-05-10 stsp goto done;
5260 588edf97 2019-05-10 stsp
5261 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5262 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5263 33ad4cbe 2019-05-12 jcs if (err)
5264 33ad4cbe 2019-05-12 jcs goto done;
5265 33ad4cbe 2019-05-12 jcs }
5266 c4296144 2019-05-09 stsp
5267 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5268 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5269 33ad4cbe 2019-05-12 jcs goto done;
5270 33ad4cbe 2019-05-12 jcs }
5271 33ad4cbe 2019-05-12 jcs
5272 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5273 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5275 cf066bf8 2019-05-09 stsp char *ondisk_path;
5276 cf066bf8 2019-05-09 stsp
5277 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5278 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5279 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5280 5f8a88c6 2019-08-03 stsp continue;
5281 5f8a88c6 2019-08-03 stsp
5282 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5283 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5284 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5285 cf066bf8 2019-05-09 stsp continue;
5286 cf066bf8 2019-05-09 stsp
5287 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5288 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5289 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5290 cf066bf8 2019-05-09 stsp goto done;
5291 cf066bf8 2019-05-09 stsp }
5292 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5293 2e1fa222 2020-07-23 stsp free(ondisk_path);
5294 2e1fa222 2020-07-23 stsp if (err)
5295 cf066bf8 2019-05-09 stsp goto done;
5296 cf066bf8 2019-05-09 stsp }
5297 036813ee 2019-05-09 stsp
5298 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5299 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5300 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5301 036813ee 2019-05-09 stsp if (err)
5302 036813ee 2019-05-09 stsp goto done;
5303 036813ee 2019-05-09 stsp
5304 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5305 de18fc63 2019-05-09 stsp if (err)
5306 de18fc63 2019-05-09 stsp goto done;
5307 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5308 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5309 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5310 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5311 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5312 33ad4cbe 2019-05-12 jcs free(logmsg);
5313 9d40349a 2019-05-09 stsp if (err)
5314 9d40349a 2019-05-09 stsp goto done;
5315 9d40349a 2019-05-09 stsp
5316 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5317 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5318 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5319 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5320 09f5bd90 2019-05-09 stsp goto done;
5321 09f5bd90 2019-05-09 stsp }
5322 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5323 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5324 09f5bd90 2019-05-09 stsp if (err)
5325 09f5bd90 2019-05-09 stsp goto done;
5326 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5327 ebf99748 2019-05-09 stsp if (err)
5328 ebf99748 2019-05-09 stsp goto done;
5329 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5330 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5331 09f5bd90 2019-05-09 stsp goto done;
5332 09f5bd90 2019-05-09 stsp }
5333 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5334 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5335 f2c16586 2019-05-09 stsp if (err)
5336 f2c16586 2019-05-09 stsp goto done;
5337 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5338 f2c16586 2019-05-09 stsp if (err)
5339 f2c16586 2019-05-09 stsp goto done;
5340 f2c16586 2019-05-09 stsp
5341 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5342 09f5bd90 2019-05-09 stsp if (err)
5343 09f5bd90 2019-05-09 stsp goto done;
5344 09f5bd90 2019-05-09 stsp
5345 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5346 ebf99748 2019-05-09 stsp if (err)
5347 ebf99748 2019-05-09 stsp goto done;
5348 c4296144 2019-05-09 stsp done:
5349 588edf97 2019-05-10 stsp if (head_tree)
5350 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5351 588edf97 2019-05-10 stsp if (head_commit)
5352 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5353 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5354 2f17228e 2019-05-12 stsp if (head_ref2) {
5355 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5356 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5357 2f17228e 2019-05-12 stsp err = unlockerr;
5358 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5359 39cd0ff6 2019-07-12 stsp }
5360 39cd0ff6 2019-07-12 stsp return err;
5361 39cd0ff6 2019-07-12 stsp }
5362 5c1e53bc 2019-07-28 stsp
5363 5c1e53bc 2019-07-28 stsp static const struct got_error *
5364 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5365 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5366 5c1e53bc 2019-07-28 stsp {
5367 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5368 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5369 39cd0ff6 2019-07-12 stsp
5370 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5371 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5372 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5373 5c1e53bc 2019-07-28 stsp
5374 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5375 5c1e53bc 2019-07-28 stsp ct_path++;
5376 5c1e53bc 2019-07-28 stsp
5377 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5378 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5379 5c1e53bc 2019-07-28 stsp break;
5380 5c1e53bc 2019-07-28 stsp }
5381 5c1e53bc 2019-07-28 stsp
5382 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5383 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5384 5c1e53bc 2019-07-28 stsp
5385 5c1e53bc 2019-07-28 stsp return NULL;
5386 5c1e53bc 2019-07-28 stsp }
5387 5c1e53bc 2019-07-28 stsp
5388 f0b75401 2019-08-03 stsp static const struct got_error *
5389 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5390 f0b75401 2019-08-03 stsp {
5391 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5392 f0b75401 2019-08-03 stsp
5393 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5394 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5395 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5396 f0b75401 2019-08-03 stsp }
5397 f0b75401 2019-08-03 stsp
5398 f0b75401 2019-08-03 stsp return NULL;
5399 f0b75401 2019-08-03 stsp }
5400 f0b75401 2019-08-03 stsp
5401 5f8a88c6 2019-08-03 stsp static const struct got_error *
5402 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5403 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5404 5f8a88c6 2019-08-03 stsp {
5405 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5406 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5407 5f8a88c6 2019-08-03 stsp
5408 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5409 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5410 5f8a88c6 2019-08-03 stsp continue;
5411 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5412 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5413 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5414 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5415 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5416 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5417 5f8a88c6 2019-08-03 stsp }
5418 5f8a88c6 2019-08-03 stsp
5419 5f8a88c6 2019-08-03 stsp return NULL;
5420 5f8a88c6 2019-08-03 stsp }
5421 5f8a88c6 2019-08-03 stsp
5422 39cd0ff6 2019-07-12 stsp const struct got_error *
5423 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5424 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5425 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5426 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5427 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5428 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5429 39cd0ff6 2019-07-12 stsp {
5430 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5431 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5432 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5433 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5434 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5435 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5436 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5437 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5438 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5439 39cd0ff6 2019-07-12 stsp
5440 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5441 39cd0ff6 2019-07-12 stsp
5442 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5443 39cd0ff6 2019-07-12 stsp
5444 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5445 39cd0ff6 2019-07-12 stsp if (err)
5446 39cd0ff6 2019-07-12 stsp goto done;
5447 39cd0ff6 2019-07-12 stsp
5448 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5449 39cd0ff6 2019-07-12 stsp if (err)
5450 39cd0ff6 2019-07-12 stsp goto done;
5451 39cd0ff6 2019-07-12 stsp
5452 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5453 39cd0ff6 2019-07-12 stsp if (err)
5454 39cd0ff6 2019-07-12 stsp goto done;
5455 39cd0ff6 2019-07-12 stsp
5456 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5457 39cd0ff6 2019-07-12 stsp if (err)
5458 39cd0ff6 2019-07-12 stsp goto done;
5459 39cd0ff6 2019-07-12 stsp
5460 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5461 5f8a88c6 2019-08-03 stsp &have_staged_files);
5462 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5463 5f8a88c6 2019-08-03 stsp goto done;
5464 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5465 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5466 5f8a88c6 2019-08-03 stsp if (err)
5467 5f8a88c6 2019-08-03 stsp goto done;
5468 5f8a88c6 2019-08-03 stsp }
5469 5f8a88c6 2019-08-03 stsp
5470 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5471 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5472 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5473 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5474 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5475 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5476 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5477 5c1e53bc 2019-07-28 stsp if (err)
5478 5c1e53bc 2019-07-28 stsp goto done;
5479 5c1e53bc 2019-07-28 stsp }
5480 39cd0ff6 2019-07-12 stsp
5481 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5482 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5483 39cd0ff6 2019-07-12 stsp goto done;
5484 5c1e53bc 2019-07-28 stsp }
5485 5c1e53bc 2019-07-28 stsp
5486 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5487 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5488 5c1e53bc 2019-07-28 stsp if (err)
5489 5c1e53bc 2019-07-28 stsp goto done;
5490 39cd0ff6 2019-07-12 stsp }
5491 f0b75401 2019-08-03 stsp
5492 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5493 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5494 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5495 f0b75401 2019-08-03 stsp
5496 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5497 f0b75401 2019-08-03 stsp ct_path++;
5498 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5499 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5500 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5501 b50cabdf 2019-07-12 stsp if (err)
5502 b50cabdf 2019-07-12 stsp goto done;
5503 f0b75401 2019-08-03 stsp
5504 b50cabdf 2019-07-12 stsp }
5505 b50cabdf 2019-07-12 stsp
5506 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5507 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5508 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5509 39cd0ff6 2019-07-12 stsp if (err)
5510 39cd0ff6 2019-07-12 stsp goto done;
5511 39cd0ff6 2019-07-12 stsp
5512 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5513 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5514 2e1fa222 2020-07-23 stsp if (err == NULL) {
5515 2e1fa222 2020-07-23 stsp err = reinstall_symlinks_after_commit(&commitable_paths,
5516 2e1fa222 2020-07-23 stsp *new_commit_id, fileindex, worktree, repo);
5517 2e1fa222 2020-07-23 stsp }
5518 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5519 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5520 39cd0ff6 2019-07-12 stsp err = sync_err;
5521 39cd0ff6 2019-07-12 stsp done:
5522 39cd0ff6 2019-07-12 stsp if (fileindex)
5523 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5524 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5525 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5526 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5527 39cd0ff6 2019-07-12 stsp err = unlockerr;
5528 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5529 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5530 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5531 39cd0ff6 2019-07-12 stsp }
5532 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5533 c4296144 2019-05-09 stsp return err;
5534 8656d6c4 2019-05-20 stsp }
5535 8656d6c4 2019-05-20 stsp
5536 8656d6c4 2019-05-20 stsp const char *
5537 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5538 8656d6c4 2019-05-20 stsp {
5539 8656d6c4 2019-05-20 stsp return ct->path;
5540 8656d6c4 2019-05-20 stsp }
5541 8656d6c4 2019-05-20 stsp
5542 8656d6c4 2019-05-20 stsp unsigned int
5543 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5544 8656d6c4 2019-05-20 stsp {
5545 8656d6c4 2019-05-20 stsp return ct->status;
5546 818c7501 2019-07-11 stsp }
5547 818c7501 2019-07-11 stsp
5548 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5549 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5550 818c7501 2019-07-11 stsp struct got_repository *repo;
5551 818c7501 2019-07-11 stsp };
5552 818c7501 2019-07-11 stsp
5553 818c7501 2019-07-11 stsp static const struct got_error *
5554 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5555 818c7501 2019-07-11 stsp {
5556 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5557 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5558 818c7501 2019-07-11 stsp unsigned char status;
5559 818c7501 2019-07-11 stsp struct stat sb;
5560 818c7501 2019-07-11 stsp char *ondisk_path;
5561 818c7501 2019-07-11 stsp
5562 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5563 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5564 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5565 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5566 818c7501 2019-07-11 stsp
5567 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5568 818c7501 2019-07-11 stsp == -1)
5569 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5570 818c7501 2019-07-11 stsp
5571 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5572 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5573 818c7501 2019-07-11 stsp free(ondisk_path);
5574 818c7501 2019-07-11 stsp if (err)
5575 818c7501 2019-07-11 stsp return err;
5576 818c7501 2019-07-11 stsp
5577 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5578 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5579 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5580 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5581 818c7501 2019-07-11 stsp
5582 818c7501 2019-07-11 stsp return NULL;
5583 818c7501 2019-07-11 stsp }
5584 818c7501 2019-07-11 stsp
5585 818c7501 2019-07-11 stsp const struct got_error *
5586 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5587 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5588 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5589 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5590 818c7501 2019-07-11 stsp {
5591 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5592 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5593 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5594 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5595 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5596 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5597 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5598 818c7501 2019-07-11 stsp
5599 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5600 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5601 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5602 818c7501 2019-07-11 stsp
5603 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5604 818c7501 2019-07-11 stsp if (err)
5605 818c7501 2019-07-11 stsp return err;
5606 818c7501 2019-07-11 stsp
5607 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5608 818c7501 2019-07-11 stsp if (err)
5609 818c7501 2019-07-11 stsp goto done;
5610 818c7501 2019-07-11 stsp
5611 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5612 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5613 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5614 818c7501 2019-07-11 stsp &ok_arg);
5615 818c7501 2019-07-11 stsp if (err)
5616 818c7501 2019-07-11 stsp goto done;
5617 818c7501 2019-07-11 stsp
5618 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5619 818c7501 2019-07-11 stsp if (err)
5620 818c7501 2019-07-11 stsp goto done;
5621 818c7501 2019-07-11 stsp
5622 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5623 818c7501 2019-07-11 stsp if (err)
5624 818c7501 2019-07-11 stsp goto done;
5625 818c7501 2019-07-11 stsp
5626 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5627 818c7501 2019-07-11 stsp if (err)
5628 818c7501 2019-07-11 stsp goto done;
5629 818c7501 2019-07-11 stsp
5630 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5631 818c7501 2019-07-11 stsp 0);
5632 e51d7b55 2020-01-04 stsp if (err)
5633 e51d7b55 2020-01-04 stsp goto done;
5634 e51d7b55 2020-01-04 stsp
5635 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5636 818c7501 2019-07-11 stsp if (err)
5637 818c7501 2019-07-11 stsp goto done;
5638 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5639 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5640 e51d7b55 2020-01-04 stsp goto done;
5641 e51d7b55 2020-01-04 stsp }
5642 818c7501 2019-07-11 stsp
5643 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5644 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5645 818c7501 2019-07-11 stsp if (err)
5646 818c7501 2019-07-11 stsp goto done;
5647 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5648 818c7501 2019-07-11 stsp if (err)
5649 818c7501 2019-07-11 stsp goto done;
5650 818c7501 2019-07-11 stsp
5651 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5652 818c7501 2019-07-11 stsp
5653 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5654 818c7501 2019-07-11 stsp if (err)
5655 818c7501 2019-07-11 stsp goto done;
5656 818c7501 2019-07-11 stsp
5657 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5658 818c7501 2019-07-11 stsp if (err)
5659 818c7501 2019-07-11 stsp goto done;
5660 818c7501 2019-07-11 stsp
5661 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5662 818c7501 2019-07-11 stsp worktree->base_commit_id);
5663 818c7501 2019-07-11 stsp if (err)
5664 818c7501 2019-07-11 stsp goto done;
5665 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5666 818c7501 2019-07-11 stsp if (err)
5667 818c7501 2019-07-11 stsp goto done;
5668 818c7501 2019-07-11 stsp
5669 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5670 818c7501 2019-07-11 stsp if (err)
5671 818c7501 2019-07-11 stsp goto done;
5672 818c7501 2019-07-11 stsp done:
5673 818c7501 2019-07-11 stsp free(fileindex_path);
5674 818c7501 2019-07-11 stsp free(tmp_branch_name);
5675 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5676 818c7501 2019-07-11 stsp free(branch_ref_name);
5677 818c7501 2019-07-11 stsp if (branch_ref)
5678 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5679 818c7501 2019-07-11 stsp if (wt_branch)
5680 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5681 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5682 818c7501 2019-07-11 stsp if (err) {
5683 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5684 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5685 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5686 818c7501 2019-07-11 stsp }
5687 818c7501 2019-07-11 stsp if (*tmp_branch) {
5688 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5689 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5690 818c7501 2019-07-11 stsp }
5691 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5692 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5693 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5694 3e3a69f1 2019-07-25 stsp }
5695 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5696 818c7501 2019-07-11 stsp }
5697 818c7501 2019-07-11 stsp return err;
5698 818c7501 2019-07-11 stsp }
5699 818c7501 2019-07-11 stsp
5700 818c7501 2019-07-11 stsp const struct got_error *
5701 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5702 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5703 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5704 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5705 818c7501 2019-07-11 stsp {
5706 818c7501 2019-07-11 stsp const struct got_error *err;
5707 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5708 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5709 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5710 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5711 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5712 818c7501 2019-07-11 stsp
5713 818c7501 2019-07-11 stsp *commit_id = NULL;
5714 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5715 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5716 3e3a69f1 2019-07-25 stsp *branch = NULL;
5717 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5718 3e3a69f1 2019-07-25 stsp
5719 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5720 3e3a69f1 2019-07-25 stsp if (err)
5721 3e3a69f1 2019-07-25 stsp return err;
5722 818c7501 2019-07-11 stsp
5723 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5724 3e3a69f1 2019-07-25 stsp if (err)
5725 f032f1f7 2019-08-04 stsp goto done;
5726 f032f1f7 2019-08-04 stsp
5727 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5728 f032f1f7 2019-08-04 stsp &have_staged_files);
5729 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5730 f032f1f7 2019-08-04 stsp goto done;
5731 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5732 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5733 3e3a69f1 2019-07-25 stsp goto done;
5734 f032f1f7 2019-08-04 stsp }
5735 3e3a69f1 2019-07-25 stsp
5736 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5737 818c7501 2019-07-11 stsp if (err)
5738 f032f1f7 2019-08-04 stsp goto done;
5739 818c7501 2019-07-11 stsp
5740 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
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 = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5745 818c7501 2019-07-11 stsp if (err)
5746 818c7501 2019-07-11 stsp goto done;
5747 818c7501 2019-07-11 stsp
5748 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
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_ref_open(&branch_ref, repo, branch_ref_name, 0);
5753 818c7501 2019-07-11 stsp if (err)
5754 818c7501 2019-07-11 stsp goto done;
5755 818c7501 2019-07-11 stsp
5756 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5757 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5758 818c7501 2019-07-11 stsp if (err)
5759 818c7501 2019-07-11 stsp goto done;
5760 818c7501 2019-07-11 stsp
5761 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5762 818c7501 2019-07-11 stsp if (err)
5763 818c7501 2019-07-11 stsp goto done;
5764 818c7501 2019-07-11 stsp
5765 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5766 818c7501 2019-07-11 stsp if (err)
5767 818c7501 2019-07-11 stsp goto done;
5768 818c7501 2019-07-11 stsp
5769 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5770 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5771 818c7501 2019-07-11 stsp if (err)
5772 818c7501 2019-07-11 stsp goto done;
5773 818c7501 2019-07-11 stsp
5774 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5775 818c7501 2019-07-11 stsp if (err)
5776 818c7501 2019-07-11 stsp goto done;
5777 818c7501 2019-07-11 stsp done:
5778 818c7501 2019-07-11 stsp free(commit_ref_name);
5779 818c7501 2019-07-11 stsp free(branch_ref_name);
5780 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5781 818c7501 2019-07-11 stsp if (commit_ref)
5782 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5783 818c7501 2019-07-11 stsp if (branch_ref)
5784 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5785 818c7501 2019-07-11 stsp if (err) {
5786 818c7501 2019-07-11 stsp free(*commit_id);
5787 818c7501 2019-07-11 stsp *commit_id = NULL;
5788 818c7501 2019-07-11 stsp if (*tmp_branch) {
5789 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5790 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5791 818c7501 2019-07-11 stsp }
5792 818c7501 2019-07-11 stsp if (*new_base_branch) {
5793 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5794 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5795 818c7501 2019-07-11 stsp }
5796 818c7501 2019-07-11 stsp if (*branch) {
5797 818c7501 2019-07-11 stsp got_ref_close(*branch);
5798 818c7501 2019-07-11 stsp *branch = NULL;
5799 818c7501 2019-07-11 stsp }
5800 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5801 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5802 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5803 3e3a69f1 2019-07-25 stsp }
5804 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5805 818c7501 2019-07-11 stsp }
5806 818c7501 2019-07-11 stsp return err;
5807 c4296144 2019-05-09 stsp }
5808 818c7501 2019-07-11 stsp
5809 818c7501 2019-07-11 stsp const struct got_error *
5810 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5811 818c7501 2019-07-11 stsp {
5812 818c7501 2019-07-11 stsp const struct got_error *err;
5813 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5814 818c7501 2019-07-11 stsp
5815 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5816 818c7501 2019-07-11 stsp if (err)
5817 818c7501 2019-07-11 stsp return err;
5818 818c7501 2019-07-11 stsp
5819 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5820 818c7501 2019-07-11 stsp free(tmp_branch_name);
5821 818c7501 2019-07-11 stsp return NULL;
5822 818c7501 2019-07-11 stsp }
5823 818c7501 2019-07-11 stsp
5824 818c7501 2019-07-11 stsp static const struct got_error *
5825 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5826 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5827 818c7501 2019-07-11 stsp {
5828 0ebf8283 2019-07-24 stsp *logmsg = arg;
5829 818c7501 2019-07-11 stsp return NULL;
5830 818c7501 2019-07-11 stsp }
5831 818c7501 2019-07-11 stsp
5832 818c7501 2019-07-11 stsp static const struct got_error *
5833 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5834 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5835 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5836 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5837 818c7501 2019-07-11 stsp {
5838 818c7501 2019-07-11 stsp return NULL;
5839 01757395 2019-07-12 stsp }
5840 01757395 2019-07-12 stsp
5841 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5842 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5843 01757395 2019-07-12 stsp void *progress_arg;
5844 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5845 01757395 2019-07-12 stsp };
5846 01757395 2019-07-12 stsp
5847 01757395 2019-07-12 stsp static const struct got_error *
5848 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5849 01757395 2019-07-12 stsp {
5850 01757395 2019-07-12 stsp const struct got_error *err;
5851 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5852 01757395 2019-07-12 stsp char *p;
5853 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5854 01757395 2019-07-12 stsp
5855 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5856 01757395 2019-07-12 stsp if (err)
5857 01757395 2019-07-12 stsp return err;
5858 01757395 2019-07-12 stsp
5859 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5860 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5861 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5862 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5863 01757395 2019-07-12 stsp return NULL;
5864 01757395 2019-07-12 stsp
5865 01757395 2019-07-12 stsp p = strdup(path);
5866 01757395 2019-07-12 stsp if (p == NULL)
5867 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5868 01757395 2019-07-12 stsp
5869 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5870 01757395 2019-07-12 stsp if (err || new == NULL)
5871 01757395 2019-07-12 stsp free(p);
5872 01757395 2019-07-12 stsp return err;
5873 01757395 2019-07-12 stsp }
5874 01757395 2019-07-12 stsp
5875 01757395 2019-07-12 stsp void
5876 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5877 01757395 2019-07-12 stsp {
5878 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5879 01757395 2019-07-12 stsp
5880 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5881 01757395 2019-07-12 stsp free((char *)pe->path);
5882 01757395 2019-07-12 stsp
5883 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5884 818c7501 2019-07-11 stsp }
5885 818c7501 2019-07-11 stsp
5886 0ebf8283 2019-07-24 stsp static const struct got_error *
5887 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5888 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5889 818c7501 2019-07-11 stsp {
5890 818c7501 2019-07-11 stsp const struct got_error *err;
5891 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5892 818c7501 2019-07-11 stsp
5893 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5894 818c7501 2019-07-11 stsp if (err) {
5895 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5896 818c7501 2019-07-11 stsp goto done;
5897 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5898 818c7501 2019-07-11 stsp if (err)
5899 818c7501 2019-07-11 stsp goto done;
5900 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5901 818c7501 2019-07-11 stsp if (err)
5902 818c7501 2019-07-11 stsp goto done;
5903 de05890f 2020-03-05 stsp } else if (is_rebase) {
5904 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5905 818c7501 2019-07-11 stsp int cmp;
5906 818c7501 2019-07-11 stsp
5907 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5908 818c7501 2019-07-11 stsp if (err)
5909 818c7501 2019-07-11 stsp goto done;
5910 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5911 818c7501 2019-07-11 stsp free(stored_id);
5912 818c7501 2019-07-11 stsp if (cmp != 0) {
5913 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5914 818c7501 2019-07-11 stsp goto done;
5915 818c7501 2019-07-11 stsp }
5916 818c7501 2019-07-11 stsp }
5917 0ebf8283 2019-07-24 stsp done:
5918 0ebf8283 2019-07-24 stsp if (commit_ref)
5919 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5920 0ebf8283 2019-07-24 stsp return err;
5921 0ebf8283 2019-07-24 stsp }
5922 0ebf8283 2019-07-24 stsp
5923 0ebf8283 2019-07-24 stsp static const struct got_error *
5924 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5925 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5926 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5927 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5928 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5929 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5930 0ebf8283 2019-07-24 stsp {
5931 0ebf8283 2019-07-24 stsp const struct got_error *err;
5932 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5933 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5934 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5935 818c7501 2019-07-11 stsp
5936 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5937 0ebf8283 2019-07-24 stsp
5938 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5939 0ebf8283 2019-07-24 stsp if (err)
5940 0ebf8283 2019-07-24 stsp return err;
5941 0ebf8283 2019-07-24 stsp
5942 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5943 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5944 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5945 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5946 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5947 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5948 818c7501 2019-07-11 stsp if (commit_ref)
5949 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5950 818c7501 2019-07-11 stsp return err;
5951 818c7501 2019-07-11 stsp }
5952 818c7501 2019-07-11 stsp
5953 818c7501 2019-07-11 stsp const struct got_error *
5954 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5955 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5956 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5957 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5958 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5959 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5960 818c7501 2019-07-11 stsp {
5961 0ebf8283 2019-07-24 stsp const struct got_error *err;
5962 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5963 0ebf8283 2019-07-24 stsp
5964 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5965 0ebf8283 2019-07-24 stsp if (err)
5966 0ebf8283 2019-07-24 stsp return err;
5967 0ebf8283 2019-07-24 stsp
5968 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5969 0ebf8283 2019-07-24 stsp if (err)
5970 0ebf8283 2019-07-24 stsp goto done;
5971 0ebf8283 2019-07-24 stsp
5972 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5973 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5974 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5975 0ebf8283 2019-07-24 stsp done:
5976 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5977 0ebf8283 2019-07-24 stsp return err;
5978 0ebf8283 2019-07-24 stsp }
5979 0ebf8283 2019-07-24 stsp
5980 0ebf8283 2019-07-24 stsp const struct got_error *
5981 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5982 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5983 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5984 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5985 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5986 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5987 0ebf8283 2019-07-24 stsp {
5988 0ebf8283 2019-07-24 stsp const struct got_error *err;
5989 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5990 0ebf8283 2019-07-24 stsp
5991 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5992 0ebf8283 2019-07-24 stsp if (err)
5993 0ebf8283 2019-07-24 stsp return err;
5994 0ebf8283 2019-07-24 stsp
5995 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5996 0ebf8283 2019-07-24 stsp if (err)
5997 0ebf8283 2019-07-24 stsp goto done;
5998 0ebf8283 2019-07-24 stsp
5999 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6000 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6001 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6002 0ebf8283 2019-07-24 stsp done:
6003 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6004 0ebf8283 2019-07-24 stsp return err;
6005 0ebf8283 2019-07-24 stsp }
6006 0ebf8283 2019-07-24 stsp
6007 0ebf8283 2019-07-24 stsp static const struct got_error *
6008 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6009 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6010 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6011 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6012 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6013 0ebf8283 2019-07-24 stsp {
6014 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6015 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6016 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6017 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6018 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6019 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6020 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6021 818c7501 2019-07-11 stsp
6022 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6023 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6024 a0e95631 2019-07-12 stsp
6025 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6026 818c7501 2019-07-11 stsp
6027 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6028 a0e95631 2019-07-12 stsp if (err)
6029 3e3a69f1 2019-07-25 stsp return err;
6030 a0e95631 2019-07-12 stsp
6031 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6032 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6033 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6034 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6035 01757395 2019-07-12 stsp /*
6036 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6037 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6038 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6039 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6040 01757395 2019-07-12 stsp */
6041 01757395 2019-07-12 stsp if (merged_paths) {
6042 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6043 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6044 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6045 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6046 f2a9dc41 2019-12-13 tracey 0);
6047 01757395 2019-07-12 stsp if (err)
6048 01757395 2019-07-12 stsp goto done;
6049 01757395 2019-07-12 stsp }
6050 01757395 2019-07-12 stsp } else {
6051 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6052 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6053 01757395 2019-07-12 stsp if (err)
6054 01757395 2019-07-12 stsp goto done;
6055 01757395 2019-07-12 stsp }
6056 a0e95631 2019-07-12 stsp
6057 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6058 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6059 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6060 a0e95631 2019-07-12 stsp if (err)
6061 a0e95631 2019-07-12 stsp goto done;
6062 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6063 a0e95631 2019-07-12 stsp goto done;
6064 ff0d2220 2019-07-11 stsp }
6065 818c7501 2019-07-11 stsp
6066 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6067 edd02c5e 2019-07-11 stsp if (err)
6068 edd02c5e 2019-07-11 stsp goto done;
6069 edd02c5e 2019-07-11 stsp
6070 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6071 818c7501 2019-07-11 stsp if (err)
6072 818c7501 2019-07-11 stsp goto done;
6073 818c7501 2019-07-11 stsp
6074 5943eee2 2019-08-13 stsp if (new_logmsg) {
6075 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6076 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6077 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6078 5943eee2 2019-08-13 stsp goto done;
6079 5943eee2 2019-08-13 stsp }
6080 5943eee2 2019-08-13 stsp } else {
6081 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6082 5943eee2 2019-08-13 stsp if (err)
6083 5943eee2 2019-08-13 stsp goto done;
6084 5943eee2 2019-08-13 stsp }
6085 0ebf8283 2019-07-24 stsp
6086 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6087 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6088 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6089 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6090 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6091 a0e95631 2019-07-12 stsp if (err)
6092 a0e95631 2019-07-12 stsp goto done;
6093 a0e95631 2019-07-12 stsp
6094 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6095 a0e95631 2019-07-12 stsp if (err)
6096 a0e95631 2019-07-12 stsp goto done;
6097 a0e95631 2019-07-12 stsp
6098 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6099 a0e95631 2019-07-12 stsp if (err)
6100 a0e95631 2019-07-12 stsp goto done;
6101 a0e95631 2019-07-12 stsp
6102 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6103 72fd46fa 2019-09-06 stsp fileindex, 0);
6104 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6105 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6106 a0e95631 2019-07-12 stsp err = sync_err;
6107 818c7501 2019-07-11 stsp done:
6108 a0e95631 2019-07-12 stsp free(fileindex_path);
6109 a0e95631 2019-07-12 stsp free(head_commit_id);
6110 a0e95631 2019-07-12 stsp if (head_ref)
6111 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6112 818c7501 2019-07-11 stsp if (err) {
6113 818c7501 2019-07-11 stsp free(*new_commit_id);
6114 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6115 818c7501 2019-07-11 stsp }
6116 818c7501 2019-07-11 stsp return err;
6117 818c7501 2019-07-11 stsp }
6118 818c7501 2019-07-11 stsp
6119 818c7501 2019-07-11 stsp const struct got_error *
6120 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6121 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6122 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6123 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6124 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6125 0ebf8283 2019-07-24 stsp {
6126 0ebf8283 2019-07-24 stsp const struct got_error *err;
6127 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6128 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6129 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6130 0ebf8283 2019-07-24 stsp
6131 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6132 0ebf8283 2019-07-24 stsp if (err)
6133 0ebf8283 2019-07-24 stsp return err;
6134 0ebf8283 2019-07-24 stsp
6135 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6136 0ebf8283 2019-07-24 stsp if (err)
6137 0ebf8283 2019-07-24 stsp goto done;
6138 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6139 0ebf8283 2019-07-24 stsp if (err)
6140 0ebf8283 2019-07-24 stsp goto done;
6141 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6142 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6143 0ebf8283 2019-07-24 stsp goto done;
6144 0ebf8283 2019-07-24 stsp }
6145 0ebf8283 2019-07-24 stsp
6146 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6147 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6148 0ebf8283 2019-07-24 stsp done:
6149 0ebf8283 2019-07-24 stsp if (commit_ref)
6150 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6151 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6152 0ebf8283 2019-07-24 stsp free(commit_id);
6153 0ebf8283 2019-07-24 stsp return err;
6154 0ebf8283 2019-07-24 stsp }
6155 0ebf8283 2019-07-24 stsp
6156 0ebf8283 2019-07-24 stsp const struct got_error *
6157 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6158 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6159 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6160 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6161 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6162 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6163 0ebf8283 2019-07-24 stsp {
6164 0ebf8283 2019-07-24 stsp const struct got_error *err;
6165 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6166 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6167 0ebf8283 2019-07-24 stsp
6168 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6169 0ebf8283 2019-07-24 stsp if (err)
6170 0ebf8283 2019-07-24 stsp return err;
6171 0ebf8283 2019-07-24 stsp
6172 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6173 0ebf8283 2019-07-24 stsp if (err)
6174 0ebf8283 2019-07-24 stsp goto done;
6175 0ebf8283 2019-07-24 stsp
6176 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6177 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6178 0ebf8283 2019-07-24 stsp done:
6179 0ebf8283 2019-07-24 stsp if (commit_ref)
6180 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6181 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6182 0ebf8283 2019-07-24 stsp return err;
6183 0ebf8283 2019-07-24 stsp }
6184 0ebf8283 2019-07-24 stsp
6185 0ebf8283 2019-07-24 stsp const struct got_error *
6186 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6187 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6188 818c7501 2019-07-11 stsp {
6189 3e3a69f1 2019-07-25 stsp if (fileindex)
6190 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6191 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6192 69844fba 2019-07-11 stsp }
6193 69844fba 2019-07-11 stsp
6194 69844fba 2019-07-11 stsp static const struct got_error *
6195 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6196 69844fba 2019-07-11 stsp {
6197 69844fba 2019-07-11 stsp const struct got_error *err;
6198 69844fba 2019-07-11 stsp struct got_reference *ref;
6199 69844fba 2019-07-11 stsp
6200 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6201 69844fba 2019-07-11 stsp if (err) {
6202 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6203 69844fba 2019-07-11 stsp return NULL;
6204 69844fba 2019-07-11 stsp return err;
6205 69844fba 2019-07-11 stsp }
6206 69844fba 2019-07-11 stsp
6207 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6208 69844fba 2019-07-11 stsp got_ref_close(ref);
6209 69844fba 2019-07-11 stsp return err;
6210 818c7501 2019-07-11 stsp }
6211 818c7501 2019-07-11 stsp
6212 69844fba 2019-07-11 stsp static const struct got_error *
6213 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6214 69844fba 2019-07-11 stsp {
6215 69844fba 2019-07-11 stsp const struct got_error *err;
6216 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6217 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6218 69844fba 2019-07-11 stsp
6219 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6220 69844fba 2019-07-11 stsp if (err)
6221 69844fba 2019-07-11 stsp goto done;
6222 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6223 69844fba 2019-07-11 stsp if (err)
6224 69844fba 2019-07-11 stsp goto done;
6225 69844fba 2019-07-11 stsp
6226 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6227 69844fba 2019-07-11 stsp if (err)
6228 69844fba 2019-07-11 stsp goto done;
6229 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6230 69844fba 2019-07-11 stsp if (err)
6231 69844fba 2019-07-11 stsp goto done;
6232 69844fba 2019-07-11 stsp
6233 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6234 69844fba 2019-07-11 stsp if (err)
6235 69844fba 2019-07-11 stsp goto done;
6236 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6237 69844fba 2019-07-11 stsp if (err)
6238 69844fba 2019-07-11 stsp goto done;
6239 69844fba 2019-07-11 stsp
6240 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6241 69844fba 2019-07-11 stsp if (err)
6242 69844fba 2019-07-11 stsp goto done;
6243 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6244 69844fba 2019-07-11 stsp if (err)
6245 69844fba 2019-07-11 stsp goto done;
6246 69844fba 2019-07-11 stsp
6247 69844fba 2019-07-11 stsp done:
6248 69844fba 2019-07-11 stsp free(tmp_branch_name);
6249 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6250 69844fba 2019-07-11 stsp free(branch_ref_name);
6251 69844fba 2019-07-11 stsp free(commit_ref_name);
6252 69844fba 2019-07-11 stsp return err;
6253 69844fba 2019-07-11 stsp }
6254 69844fba 2019-07-11 stsp
6255 818c7501 2019-07-11 stsp const struct got_error *
6256 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6257 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6258 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6259 818c7501 2019-07-11 stsp struct got_repository *repo)
6260 818c7501 2019-07-11 stsp {
6261 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6262 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6263 818c7501 2019-07-11 stsp
6264 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6265 818c7501 2019-07-11 stsp if (err)
6266 818c7501 2019-07-11 stsp return err;
6267 818c7501 2019-07-11 stsp
6268 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6269 818c7501 2019-07-11 stsp if (err)
6270 818c7501 2019-07-11 stsp goto done;
6271 818c7501 2019-07-11 stsp
6272 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6273 818c7501 2019-07-11 stsp if (err)
6274 818c7501 2019-07-11 stsp goto done;
6275 818c7501 2019-07-11 stsp
6276 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6277 818c7501 2019-07-11 stsp if (err)
6278 818c7501 2019-07-11 stsp goto done;
6279 818c7501 2019-07-11 stsp
6280 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6281 818c7501 2019-07-11 stsp done:
6282 3e3a69f1 2019-07-25 stsp if (fileindex)
6283 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6284 818c7501 2019-07-11 stsp free(new_head_commit_id);
6285 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6286 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6287 818c7501 2019-07-11 stsp err = unlockerr;
6288 818c7501 2019-07-11 stsp return err;
6289 818c7501 2019-07-11 stsp }
6290 818c7501 2019-07-11 stsp
6291 818c7501 2019-07-11 stsp const struct got_error *
6292 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6293 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6294 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6295 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6296 818c7501 2019-07-11 stsp {
6297 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6298 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6299 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6300 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6301 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6302 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6303 818c7501 2019-07-11 stsp
6304 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6305 818c7501 2019-07-11 stsp if (err)
6306 818c7501 2019-07-11 stsp return err;
6307 818c7501 2019-07-11 stsp
6308 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6309 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6310 818c7501 2019-07-11 stsp if (err)
6311 818c7501 2019-07-11 stsp goto done;
6312 818c7501 2019-07-11 stsp
6313 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6314 818c7501 2019-07-11 stsp if (err)
6315 818c7501 2019-07-11 stsp goto done;
6316 818c7501 2019-07-11 stsp
6317 818c7501 2019-07-11 stsp /*
6318 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6319 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6320 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6321 818c7501 2019-07-11 stsp */
6322 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6323 818c7501 2019-07-11 stsp if (err)
6324 818c7501 2019-07-11 stsp goto done;
6325 818c7501 2019-07-11 stsp
6326 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6327 818c7501 2019-07-11 stsp if (err)
6328 818c7501 2019-07-11 stsp goto done;
6329 818c7501 2019-07-11 stsp
6330 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6331 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6332 ca355955 2019-07-12 stsp if (err)
6333 ca355955 2019-07-12 stsp goto done;
6334 ca355955 2019-07-12 stsp
6335 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6336 ca355955 2019-07-12 stsp if (err)
6337 ca355955 2019-07-12 stsp goto done;
6338 ca355955 2019-07-12 stsp
6339 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6340 818c7501 2019-07-11 stsp if (err)
6341 818c7501 2019-07-11 stsp goto done;
6342 818c7501 2019-07-11 stsp
6343 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6344 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6345 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6346 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6347 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6348 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6349 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6350 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6351 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6352 55bd499d 2019-07-12 stsp if (err)
6353 1f1abb7e 2019-08-08 stsp goto sync;
6354 55bd499d 2019-07-12 stsp
6355 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6356 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6357 ca355955 2019-07-12 stsp sync:
6358 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6359 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6360 ca355955 2019-07-12 stsp err = sync_err;
6361 818c7501 2019-07-11 stsp done:
6362 818c7501 2019-07-11 stsp got_ref_close(resolved);
6363 a3a2faf2 2019-07-12 stsp free(tree_id);
6364 818c7501 2019-07-11 stsp free(commit_id);
6365 0ebf8283 2019-07-24 stsp if (fileindex)
6366 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6367 0ebf8283 2019-07-24 stsp free(fileindex_path);
6368 0ebf8283 2019-07-24 stsp
6369 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6370 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6371 0ebf8283 2019-07-24 stsp err = unlockerr;
6372 0ebf8283 2019-07-24 stsp return err;
6373 0ebf8283 2019-07-24 stsp }
6374 0ebf8283 2019-07-24 stsp
6375 0ebf8283 2019-07-24 stsp const struct got_error *
6376 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6377 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6378 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6379 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6380 0ebf8283 2019-07-24 stsp {
6381 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6382 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6383 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6384 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6385 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6386 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6387 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6388 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6389 0ebf8283 2019-07-24 stsp
6390 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6391 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6392 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6393 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6394 0ebf8283 2019-07-24 stsp
6395 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6396 0ebf8283 2019-07-24 stsp if (err)
6397 0ebf8283 2019-07-24 stsp return err;
6398 0ebf8283 2019-07-24 stsp
6399 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6400 0ebf8283 2019-07-24 stsp if (err)
6401 0ebf8283 2019-07-24 stsp goto done;
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6404 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6405 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6406 0ebf8283 2019-07-24 stsp &ok_arg);
6407 0ebf8283 2019-07-24 stsp if (err)
6408 0ebf8283 2019-07-24 stsp goto done;
6409 0ebf8283 2019-07-24 stsp
6410 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6411 0ebf8283 2019-07-24 stsp if (err)
6412 0ebf8283 2019-07-24 stsp goto done;
6413 0ebf8283 2019-07-24 stsp
6414 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6415 0ebf8283 2019-07-24 stsp if (err)
6416 0ebf8283 2019-07-24 stsp goto done;
6417 0ebf8283 2019-07-24 stsp
6418 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6419 0ebf8283 2019-07-24 stsp worktree);
6420 0ebf8283 2019-07-24 stsp if (err)
6421 0ebf8283 2019-07-24 stsp goto done;
6422 0ebf8283 2019-07-24 stsp
6423 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6424 0ebf8283 2019-07-24 stsp 0);
6425 0ebf8283 2019-07-24 stsp if (err)
6426 0ebf8283 2019-07-24 stsp goto done;
6427 0ebf8283 2019-07-24 stsp
6428 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6429 0ebf8283 2019-07-24 stsp if (err)
6430 0ebf8283 2019-07-24 stsp goto done;
6431 0ebf8283 2019-07-24 stsp
6432 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6433 0ebf8283 2019-07-24 stsp if (err)
6434 0ebf8283 2019-07-24 stsp goto done;
6435 0ebf8283 2019-07-24 stsp
6436 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6437 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6438 0ebf8283 2019-07-24 stsp if (err)
6439 0ebf8283 2019-07-24 stsp goto done;
6440 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6441 0ebf8283 2019-07-24 stsp if (err)
6442 0ebf8283 2019-07-24 stsp goto done;
6443 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6444 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6445 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6446 0ebf8283 2019-07-24 stsp goto done;
6447 0ebf8283 2019-07-24 stsp }
6448 0ebf8283 2019-07-24 stsp
6449 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6450 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6451 0ebf8283 2019-07-24 stsp if (err)
6452 0ebf8283 2019-07-24 stsp goto done;
6453 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6454 0ebf8283 2019-07-24 stsp if (err)
6455 0ebf8283 2019-07-24 stsp goto done;
6456 0ebf8283 2019-07-24 stsp
6457 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6458 0ebf8283 2019-07-24 stsp if (err)
6459 0ebf8283 2019-07-24 stsp goto done;
6460 0ebf8283 2019-07-24 stsp done:
6461 0ebf8283 2019-07-24 stsp free(fileindex_path);
6462 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6463 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6464 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6465 0ebf8283 2019-07-24 stsp if (wt_branch)
6466 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6467 0ebf8283 2019-07-24 stsp if (err) {
6468 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6469 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6470 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6471 0ebf8283 2019-07-24 stsp }
6472 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6473 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6474 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6475 0ebf8283 2019-07-24 stsp }
6476 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6477 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6478 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6479 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6480 3e3a69f1 2019-07-25 stsp }
6481 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6482 0ebf8283 2019-07-24 stsp }
6483 0ebf8283 2019-07-24 stsp return err;
6484 0ebf8283 2019-07-24 stsp }
6485 0ebf8283 2019-07-24 stsp
6486 0ebf8283 2019-07-24 stsp const struct got_error *
6487 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6488 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6489 0ebf8283 2019-07-24 stsp {
6490 3e3a69f1 2019-07-25 stsp if (fileindex)
6491 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6492 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6493 0ebf8283 2019-07-24 stsp }
6494 0ebf8283 2019-07-24 stsp
6495 0ebf8283 2019-07-24 stsp const struct got_error *
6496 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6497 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6498 0ebf8283 2019-07-24 stsp {
6499 0ebf8283 2019-07-24 stsp const struct got_error *err;
6500 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6501 0ebf8283 2019-07-24 stsp
6502 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6503 0ebf8283 2019-07-24 stsp if (err)
6504 0ebf8283 2019-07-24 stsp return err;
6505 0ebf8283 2019-07-24 stsp
6506 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6507 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6508 0ebf8283 2019-07-24 stsp return NULL;
6509 0ebf8283 2019-07-24 stsp }
6510 0ebf8283 2019-07-24 stsp
6511 0ebf8283 2019-07-24 stsp const struct got_error *
6512 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6513 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6514 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6515 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6516 0ebf8283 2019-07-24 stsp {
6517 0ebf8283 2019-07-24 stsp const struct got_error *err;
6518 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6519 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6520 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6521 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6522 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6523 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6524 0ebf8283 2019-07-24 stsp
6525 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6526 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6527 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6528 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6529 0ebf8283 2019-07-24 stsp
6530 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6531 3e3a69f1 2019-07-25 stsp if (err)
6532 3e3a69f1 2019-07-25 stsp return err;
6533 3e3a69f1 2019-07-25 stsp
6534 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6535 3e3a69f1 2019-07-25 stsp if (err)
6536 f032f1f7 2019-08-04 stsp goto done;
6537 f032f1f7 2019-08-04 stsp
6538 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6539 f032f1f7 2019-08-04 stsp &have_staged_files);
6540 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6541 f032f1f7 2019-08-04 stsp goto done;
6542 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6543 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6544 3e3a69f1 2019-07-25 stsp goto done;
6545 f032f1f7 2019-08-04 stsp }
6546 3e3a69f1 2019-07-25 stsp
6547 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6548 0ebf8283 2019-07-24 stsp if (err)
6549 f032f1f7 2019-08-04 stsp goto done;
6550 0ebf8283 2019-07-24 stsp
6551 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6552 0ebf8283 2019-07-24 stsp if (err)
6553 0ebf8283 2019-07-24 stsp goto done;
6554 0ebf8283 2019-07-24 stsp
6555 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6556 0ebf8283 2019-07-24 stsp if (err)
6557 0ebf8283 2019-07-24 stsp goto done;
6558 0ebf8283 2019-07-24 stsp
6559 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6560 0ebf8283 2019-07-24 stsp worktree);
6561 0ebf8283 2019-07-24 stsp if (err)
6562 0ebf8283 2019-07-24 stsp goto done;
6563 0ebf8283 2019-07-24 stsp
6564 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6565 0ebf8283 2019-07-24 stsp if (err)
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp
6568 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6569 0ebf8283 2019-07-24 stsp if (err)
6570 0ebf8283 2019-07-24 stsp goto done;
6571 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6572 0ebf8283 2019-07-24 stsp if (err)
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6576 0ebf8283 2019-07-24 stsp if (err)
6577 0ebf8283 2019-07-24 stsp goto done;
6578 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6579 0ebf8283 2019-07-24 stsp if (err)
6580 0ebf8283 2019-07-24 stsp goto done;
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6583 0ebf8283 2019-07-24 stsp if (err)
6584 0ebf8283 2019-07-24 stsp goto done;
6585 0ebf8283 2019-07-24 stsp done:
6586 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6587 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6588 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6589 0ebf8283 2019-07-24 stsp if (commit_ref)
6590 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6591 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6592 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6593 0ebf8283 2019-07-24 stsp if (err) {
6594 0ebf8283 2019-07-24 stsp free(*commit_id);
6595 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6596 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6597 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6598 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6599 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6600 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6601 0ebf8283 2019-07-24 stsp }
6602 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6603 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6604 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6605 3e3a69f1 2019-07-25 stsp }
6606 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6607 0ebf8283 2019-07-24 stsp }
6608 0ebf8283 2019-07-24 stsp return err;
6609 0ebf8283 2019-07-24 stsp }
6610 0ebf8283 2019-07-24 stsp
6611 0ebf8283 2019-07-24 stsp static const struct got_error *
6612 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6613 0ebf8283 2019-07-24 stsp {
6614 0ebf8283 2019-07-24 stsp const struct got_error *err;
6615 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6616 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6617 0ebf8283 2019-07-24 stsp
6618 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6619 0ebf8283 2019-07-24 stsp if (err)
6620 0ebf8283 2019-07-24 stsp goto done;
6621 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6622 0ebf8283 2019-07-24 stsp if (err)
6623 0ebf8283 2019-07-24 stsp goto done;
6624 0ebf8283 2019-07-24 stsp
6625 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6626 0ebf8283 2019-07-24 stsp worktree);
6627 0ebf8283 2019-07-24 stsp if (err)
6628 0ebf8283 2019-07-24 stsp goto done;
6629 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6630 0ebf8283 2019-07-24 stsp if (err)
6631 0ebf8283 2019-07-24 stsp goto done;
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6634 0ebf8283 2019-07-24 stsp if (err)
6635 0ebf8283 2019-07-24 stsp goto done;
6636 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6637 0ebf8283 2019-07-24 stsp if (err)
6638 0ebf8283 2019-07-24 stsp goto done;
6639 0ebf8283 2019-07-24 stsp
6640 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6641 0ebf8283 2019-07-24 stsp if (err)
6642 0ebf8283 2019-07-24 stsp goto done;
6643 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6644 0ebf8283 2019-07-24 stsp if (err)
6645 0ebf8283 2019-07-24 stsp goto done;
6646 0ebf8283 2019-07-24 stsp done:
6647 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6648 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6649 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6650 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6651 0ebf8283 2019-07-24 stsp return err;
6652 0ebf8283 2019-07-24 stsp }
6653 0ebf8283 2019-07-24 stsp
6654 0ebf8283 2019-07-24 stsp const struct got_error *
6655 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6656 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6657 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6658 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6659 0ebf8283 2019-07-24 stsp {
6660 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6661 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6662 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6663 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6664 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6665 0ebf8283 2019-07-24 stsp
6666 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6667 0ebf8283 2019-07-24 stsp if (err)
6668 0ebf8283 2019-07-24 stsp return err;
6669 0ebf8283 2019-07-24 stsp
6670 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6671 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6672 0ebf8283 2019-07-24 stsp if (err)
6673 0ebf8283 2019-07-24 stsp goto done;
6674 0ebf8283 2019-07-24 stsp
6675 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6676 0ebf8283 2019-07-24 stsp if (err)
6677 0ebf8283 2019-07-24 stsp goto done;
6678 0ebf8283 2019-07-24 stsp
6679 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6680 0ebf8283 2019-07-24 stsp if (err)
6681 0ebf8283 2019-07-24 stsp goto done;
6682 0ebf8283 2019-07-24 stsp
6683 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6684 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6685 0ebf8283 2019-07-24 stsp if (err)
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp
6688 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6689 0ebf8283 2019-07-24 stsp if (err)
6690 0ebf8283 2019-07-24 stsp goto done;
6691 0ebf8283 2019-07-24 stsp
6692 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6693 0ebf8283 2019-07-24 stsp if (err)
6694 0ebf8283 2019-07-24 stsp goto done;
6695 0ebf8283 2019-07-24 stsp
6696 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6697 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6698 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6699 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6700 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6701 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6702 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6703 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6704 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6705 0ebf8283 2019-07-24 stsp if (err)
6706 1f1abb7e 2019-08-08 stsp goto sync;
6707 0ebf8283 2019-07-24 stsp
6708 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6709 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6710 0ebf8283 2019-07-24 stsp sync:
6711 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6712 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6713 0ebf8283 2019-07-24 stsp err = sync_err;
6714 0ebf8283 2019-07-24 stsp done:
6715 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6716 0ebf8283 2019-07-24 stsp free(tree_id);
6717 818c7501 2019-07-11 stsp free(fileindex_path);
6718 818c7501 2019-07-11 stsp
6719 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6720 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6721 818c7501 2019-07-11 stsp err = unlockerr;
6722 818c7501 2019-07-11 stsp return err;
6723 818c7501 2019-07-11 stsp }
6724 0ebf8283 2019-07-24 stsp
6725 0ebf8283 2019-07-24 stsp const struct got_error *
6726 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6727 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6728 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6729 0ebf8283 2019-07-24 stsp {
6730 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6731 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6732 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6733 0ebf8283 2019-07-24 stsp
6734 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6735 0ebf8283 2019-07-24 stsp if (err)
6736 0ebf8283 2019-07-24 stsp return err;
6737 0ebf8283 2019-07-24 stsp
6738 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6739 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6740 0ebf8283 2019-07-24 stsp if (err)
6741 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp
6743 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6744 0ebf8283 2019-07-24 stsp if (err)
6745 0ebf8283 2019-07-24 stsp goto done;
6746 0ebf8283 2019-07-24 stsp
6747 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6748 0ebf8283 2019-07-24 stsp if (err)
6749 0ebf8283 2019-07-24 stsp goto done;
6750 0ebf8283 2019-07-24 stsp
6751 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6752 0ebf8283 2019-07-24 stsp if (err)
6753 0ebf8283 2019-07-24 stsp goto done;
6754 0ebf8283 2019-07-24 stsp
6755 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6756 0ebf8283 2019-07-24 stsp done:
6757 3e3a69f1 2019-07-25 stsp if (fileindex)
6758 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6759 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6760 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6761 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6762 0ebf8283 2019-07-24 stsp err = unlockerr;
6763 0ebf8283 2019-07-24 stsp return err;
6764 0ebf8283 2019-07-24 stsp }
6765 0ebf8283 2019-07-24 stsp
6766 0ebf8283 2019-07-24 stsp const struct got_error *
6767 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6768 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6769 0ebf8283 2019-07-24 stsp {
6770 0ebf8283 2019-07-24 stsp const struct got_error *err;
6771 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6772 0ebf8283 2019-07-24 stsp
6773 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6774 0ebf8283 2019-07-24 stsp if (err)
6775 0ebf8283 2019-07-24 stsp return err;
6776 0ebf8283 2019-07-24 stsp
6777 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6778 0ebf8283 2019-07-24 stsp if (err)
6779 0ebf8283 2019-07-24 stsp goto done;
6780 0ebf8283 2019-07-24 stsp
6781 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6782 0ebf8283 2019-07-24 stsp done:
6783 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6784 2822a352 2019-10-15 stsp return err;
6785 2822a352 2019-10-15 stsp }
6786 2822a352 2019-10-15 stsp
6787 2822a352 2019-10-15 stsp const struct got_error *
6788 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6789 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6790 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6791 2822a352 2019-10-15 stsp struct got_repository *repo)
6792 2822a352 2019-10-15 stsp {
6793 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6794 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6795 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6796 2822a352 2019-10-15 stsp
6797 2822a352 2019-10-15 stsp *fileindex = NULL;
6798 2822a352 2019-10-15 stsp *branch_ref = NULL;
6799 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6800 2822a352 2019-10-15 stsp
6801 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6802 2822a352 2019-10-15 stsp if (err)
6803 2822a352 2019-10-15 stsp return err;
6804 2822a352 2019-10-15 stsp
6805 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6806 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6807 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6808 2822a352 2019-10-15 stsp "update -b or different branch name required");
6809 2822a352 2019-10-15 stsp goto done;
6810 2822a352 2019-10-15 stsp }
6811 2822a352 2019-10-15 stsp
6812 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6813 2822a352 2019-10-15 stsp if (err)
6814 2822a352 2019-10-15 stsp goto done;
6815 2822a352 2019-10-15 stsp
6816 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6817 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6818 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6819 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6820 2822a352 2019-10-15 stsp &ok_arg);
6821 2822a352 2019-10-15 stsp if (err)
6822 2822a352 2019-10-15 stsp goto done;
6823 2822a352 2019-10-15 stsp
6824 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6825 2822a352 2019-10-15 stsp if (err)
6826 2822a352 2019-10-15 stsp goto done;
6827 2822a352 2019-10-15 stsp
6828 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6829 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6830 2822a352 2019-10-15 stsp done:
6831 2822a352 2019-10-15 stsp if (err) {
6832 2822a352 2019-10-15 stsp if (*branch_ref) {
6833 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6834 2822a352 2019-10-15 stsp *branch_ref = NULL;
6835 2822a352 2019-10-15 stsp }
6836 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6837 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6838 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6839 2822a352 2019-10-15 stsp }
6840 2822a352 2019-10-15 stsp if (*fileindex) {
6841 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6842 2822a352 2019-10-15 stsp *fileindex = NULL;
6843 2822a352 2019-10-15 stsp }
6844 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6845 2822a352 2019-10-15 stsp }
6846 0cb83759 2019-08-03 stsp return err;
6847 0cb83759 2019-08-03 stsp }
6848 0cb83759 2019-08-03 stsp
6849 2822a352 2019-10-15 stsp const struct got_error *
6850 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6851 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6852 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6853 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6854 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6855 2822a352 2019-10-15 stsp {
6856 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6857 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6858 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6859 2822a352 2019-10-15 stsp
6860 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6861 2822a352 2019-10-15 stsp if (err)
6862 2822a352 2019-10-15 stsp goto done;
6863 2822a352 2019-10-15 stsp
6864 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6865 2822a352 2019-10-15 stsp if (err)
6866 2822a352 2019-10-15 stsp goto done;
6867 2822a352 2019-10-15 stsp
6868 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6869 2822a352 2019-10-15 stsp worktree->path_prefix);
6870 2822a352 2019-10-15 stsp if (err)
6871 2822a352 2019-10-15 stsp goto done;
6872 2822a352 2019-10-15 stsp
6873 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6874 2822a352 2019-10-15 stsp if (err)
6875 2822a352 2019-10-15 stsp goto done;
6876 2822a352 2019-10-15 stsp
6877 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6878 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6879 2822a352 2019-10-15 stsp if (err)
6880 2822a352 2019-10-15 stsp goto sync;
6881 2822a352 2019-10-15 stsp
6882 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6883 2822a352 2019-10-15 stsp if (err)
6884 2822a352 2019-10-15 stsp goto sync;
6885 2822a352 2019-10-15 stsp
6886 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6887 2822a352 2019-10-15 stsp sync:
6888 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6889 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6890 2822a352 2019-10-15 stsp err = sync_err;
6891 2822a352 2019-10-15 stsp
6892 2822a352 2019-10-15 stsp done:
6893 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6894 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6895 2822a352 2019-10-15 stsp err = unlockerr;
6896 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6897 2822a352 2019-10-15 stsp
6898 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6899 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6900 2822a352 2019-10-15 stsp err = unlockerr;
6901 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6902 2822a352 2019-10-15 stsp
6903 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6904 2822a352 2019-10-15 stsp free(fileindex_path);
6905 2822a352 2019-10-15 stsp free(tree_id);
6906 2822a352 2019-10-15 stsp
6907 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6908 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6909 2822a352 2019-10-15 stsp err = unlockerr;
6910 2822a352 2019-10-15 stsp return err;
6911 2822a352 2019-10-15 stsp }
6912 2822a352 2019-10-15 stsp
6913 2822a352 2019-10-15 stsp const struct got_error *
6914 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6915 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6916 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6917 2822a352 2019-10-15 stsp {
6918 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6919 8b692cd0 2019-10-21 stsp
6920 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6921 8b692cd0 2019-10-21 stsp
6922 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6923 8b692cd0 2019-10-21 stsp
6924 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6925 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6926 8b692cd0 2019-10-21 stsp err = unlockerr;
6927 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6928 8b692cd0 2019-10-21 stsp
6929 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6930 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6931 8b692cd0 2019-10-21 stsp err = unlockerr;
6932 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6933 8b692cd0 2019-10-21 stsp
6934 8b692cd0 2019-10-21 stsp return err;
6935 2822a352 2019-10-15 stsp }
6936 2822a352 2019-10-15 stsp
6937 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6938 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6939 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6940 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6941 2db2652d 2019-08-07 stsp struct got_repository *repo;
6942 2db2652d 2019-08-07 stsp int have_changes;
6943 2db2652d 2019-08-07 stsp };
6944 2db2652d 2019-08-07 stsp
6945 2db2652d 2019-08-07 stsp const struct got_error *
6946 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6947 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6948 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6949 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6950 735ef5ac 2019-08-03 stsp {
6951 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6952 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6953 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6954 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6955 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6956 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6957 8b13ce36 2019-08-08 stsp
6958 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6959 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6960 8b13ce36 2019-08-08 stsp return NULL;
6961 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6962 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6963 735ef5ac 2019-08-03 stsp
6964 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6965 735ef5ac 2019-08-03 stsp if (ie == NULL)
6966 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6967 735ef5ac 2019-08-03 stsp
6968 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6969 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6970 735ef5ac 2019-08-03 stsp relpath) == -1)
6971 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6972 735ef5ac 2019-08-03 stsp
6973 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6974 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6975 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6976 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6977 735ef5ac 2019-08-03 stsp }
6978 735ef5ac 2019-08-03 stsp
6979 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6980 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6981 3aa5969e 2019-08-06 stsp goto done;
6982 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6983 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6984 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6985 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6986 735ef5ac 2019-08-03 stsp goto done;
6987 3aa5969e 2019-08-06 stsp }
6988 735ef5ac 2019-08-03 stsp
6989 2db2652d 2019-08-07 stsp a->have_changes = 1;
6990 2db2652d 2019-08-07 stsp
6991 735ef5ac 2019-08-03 stsp p = in_repo_path;
6992 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6993 735ef5ac 2019-08-03 stsp p++;
6994 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6995 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6996 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6997 735ef5ac 2019-08-03 stsp done:
6998 735ef5ac 2019-08-03 stsp free(in_repo_path);
6999 dc424a06 2019-08-07 stsp return err;
7000 dc424a06 2019-08-07 stsp }
7001 dc424a06 2019-08-07 stsp
7002 2db2652d 2019-08-07 stsp struct stage_path_arg {
7003 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7004 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7005 2db2652d 2019-08-07 stsp struct got_repository *repo;
7006 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7007 2db2652d 2019-08-07 stsp void *status_arg;
7008 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7009 2db2652d 2019-08-07 stsp void *patch_arg;
7010 7b5dc508 2019-10-28 stsp int staged_something;
7011 2db2652d 2019-08-07 stsp };
7012 2db2652d 2019-08-07 stsp
7013 2db2652d 2019-08-07 stsp static const struct got_error *
7014 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7015 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7016 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7017 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7018 0cb83759 2019-08-03 stsp {
7019 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7020 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7021 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7022 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7023 0cb83759 2019-08-03 stsp uint32_t stage;
7024 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7025 8b13ce36 2019-08-08 stsp
7026 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7027 8b13ce36 2019-08-08 stsp return NULL;
7028 0cb83759 2019-08-03 stsp
7029 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7030 d3e7c587 2019-08-03 stsp if (ie == NULL)
7031 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7032 0cb83759 2019-08-03 stsp
7033 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7034 2db2652d 2019-08-07 stsp relpath)== -1)
7035 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7036 0cb83759 2019-08-03 stsp
7037 0cb83759 2019-08-03 stsp switch (status) {
7038 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7039 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7040 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7041 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7042 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7043 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7044 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7045 dc424a06 2019-08-07 stsp if (err)
7046 dc424a06 2019-08-07 stsp break;
7047 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7048 dc424a06 2019-08-07 stsp break;
7049 dc424a06 2019-08-07 stsp } else {
7050 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7051 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7052 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7053 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7054 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7055 dc424a06 2019-08-07 stsp break;
7056 dc424a06 2019-08-07 stsp }
7057 dc424a06 2019-08-07 stsp }
7058 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7059 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7060 0cb83759 2019-08-03 stsp if (err)
7061 dc424a06 2019-08-07 stsp break;
7062 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7063 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7064 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7065 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7066 0cb83759 2019-08-03 stsp else
7067 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7068 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7069 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7070 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7071 dc424a06 2019-08-07 stsp break;
7072 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7073 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7074 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7075 0cb83759 2019-08-03 stsp break;
7076 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7077 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7078 d3e7c587 2019-08-03 stsp break;
7079 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7080 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7081 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7082 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7083 dc424a06 2019-08-07 stsp if (err)
7084 dc424a06 2019-08-07 stsp break;
7085 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7086 88f33a19 2019-08-08 stsp break;
7087 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7088 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7089 dc424a06 2019-08-07 stsp break;
7090 88f33a19 2019-08-08 stsp }
7091 dc424a06 2019-08-07 stsp }
7092 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7093 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7094 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7095 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7096 dc424a06 2019-08-07 stsp break;
7097 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7098 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7099 12463d8b 2019-12-13 stsp de_name);
7100 0cb83759 2019-08-03 stsp break;
7101 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7102 d3e7c587 2019-08-03 stsp break;
7103 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7104 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7105 ebf48fd5 2019-08-03 stsp break;
7106 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7107 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7108 2a06fe5f 2019-08-24 stsp break;
7109 0cb83759 2019-08-03 stsp default:
7110 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7111 537ac44b 2019-08-03 stsp break;
7112 0cb83759 2019-08-03 stsp }
7113 dc424a06 2019-08-07 stsp
7114 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7115 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7116 dc424a06 2019-08-07 stsp free(path_content);
7117 2db2652d 2019-08-07 stsp free(ondisk_path);
7118 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7119 0ebf8283 2019-07-24 stsp return err;
7120 0ebf8283 2019-07-24 stsp }
7121 0cb83759 2019-08-03 stsp
7122 0cb83759 2019-08-03 stsp const struct got_error *
7123 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7124 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7125 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7126 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7127 1e71573e 2019-08-03 stsp struct got_repository *repo)
7128 0cb83759 2019-08-03 stsp {
7129 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7130 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7131 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7132 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7133 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7134 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7135 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7136 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7137 0cb83759 2019-08-03 stsp
7138 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7139 0cb83759 2019-08-03 stsp if (err)
7140 0cb83759 2019-08-03 stsp return err;
7141 0cb83759 2019-08-03 stsp
7142 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7143 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7144 735ef5ac 2019-08-03 stsp if (err)
7145 735ef5ac 2019-08-03 stsp goto done;
7146 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7147 735ef5ac 2019-08-03 stsp if (err)
7148 735ef5ac 2019-08-03 stsp goto done;
7149 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7150 0cb83759 2019-08-03 stsp if (err)
7151 0cb83759 2019-08-03 stsp goto done;
7152 0cb83759 2019-08-03 stsp
7153 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7154 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7155 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7156 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7157 2db2652d 2019-08-07 stsp oka.repo = repo;
7158 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7159 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7160 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7161 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7162 735ef5ac 2019-08-03 stsp if (err)
7163 735ef5ac 2019-08-03 stsp goto done;
7164 735ef5ac 2019-08-03 stsp }
7165 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7166 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7167 2db2652d 2019-08-07 stsp goto done;
7168 2db2652d 2019-08-07 stsp }
7169 735ef5ac 2019-08-03 stsp
7170 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7171 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7172 2db2652d 2019-08-07 stsp spa.repo = repo;
7173 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7174 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7175 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7176 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7177 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7178 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7179 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7180 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7181 42005733 2019-08-03 stsp if (err)
7182 2db2652d 2019-08-07 stsp goto done;
7183 7b5dc508 2019-10-28 stsp }
7184 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7185 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7186 7b5dc508 2019-10-28 stsp goto done;
7187 0cb83759 2019-08-03 stsp }
7188 0cb83759 2019-08-03 stsp
7189 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7190 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7191 0cb83759 2019-08-03 stsp err = sync_err;
7192 0cb83759 2019-08-03 stsp done:
7193 735ef5ac 2019-08-03 stsp if (head_ref)
7194 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7195 735ef5ac 2019-08-03 stsp free(head_commit_id);
7196 0cb83759 2019-08-03 stsp free(fileindex_path);
7197 0cb83759 2019-08-03 stsp if (fileindex)
7198 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7199 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7200 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7201 0cb83759 2019-08-03 stsp err = unlockerr;
7202 0cb83759 2019-08-03 stsp return err;
7203 0cb83759 2019-08-03 stsp }
7204 ad493afc 2019-08-03 stsp
7205 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7206 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7207 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7208 ad493afc 2019-08-03 stsp struct got_repository *repo;
7209 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7210 ad493afc 2019-08-03 stsp void *progress_arg;
7211 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7212 2e1f37b0 2019-08-08 stsp void *patch_arg;
7213 ad493afc 2019-08-03 stsp };
7214 2e1f37b0 2019-08-08 stsp
7215 2e1f37b0 2019-08-08 stsp static const struct got_error *
7216 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7217 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7218 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7219 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7220 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7221 2e1f37b0 2019-08-08 stsp {
7222 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7223 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7224 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7225 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7226 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7227 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7228 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7229 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7230 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7231 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7232 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7233 2e1f37b0 2019-08-08 stsp
7234 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7235 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7236 2e1f37b0 2019-08-08 stsp
7237 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7238 2e1f37b0 2019-08-08 stsp if (err)
7239 2e1f37b0 2019-08-08 stsp return err;
7240 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7241 2e1f37b0 2019-08-08 stsp if (err)
7242 2e1f37b0 2019-08-08 stsp goto done;
7243 2e1f37b0 2019-08-08 stsp
7244 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7245 2e1f37b0 2019-08-08 stsp if (err)
7246 2e1f37b0 2019-08-08 stsp goto done;
7247 2e1f37b0 2019-08-08 stsp
7248 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7249 2e1f37b0 2019-08-08 stsp if (err)
7250 2e1f37b0 2019-08-08 stsp goto done;
7251 2e1f37b0 2019-08-08 stsp
7252 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7253 2e1f37b0 2019-08-08 stsp if (err)
7254 2e1f37b0 2019-08-08 stsp goto done;
7255 2e1f37b0 2019-08-08 stsp
7256 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7257 2e1f37b0 2019-08-08 stsp if (err)
7258 2e1f37b0 2019-08-08 stsp goto done;
7259 ad493afc 2019-08-03 stsp
7260 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7261 2e1f37b0 2019-08-08 stsp if (err)
7262 2e1f37b0 2019-08-08 stsp goto done;
7263 2e1f37b0 2019-08-08 stsp
7264 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7265 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7266 2e1f37b0 2019-08-08 stsp goto done;
7267 2e1f37b0 2019-08-08 stsp }
7268 2e1f37b0 2019-08-08 stsp
7269 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7270 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7271 2e1f37b0 2019-08-08 stsp goto done;
7272 2e1f37b0 2019-08-08 stsp }
7273 2e1f37b0 2019-08-08 stsp
7274 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7275 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7276 2e1f37b0 2019-08-08 stsp if (err)
7277 2e1f37b0 2019-08-08 stsp goto done;
7278 2e1f37b0 2019-08-08 stsp
7279 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7280 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7281 2e1f37b0 2019-08-08 stsp if (err)
7282 2e1f37b0 2019-08-08 stsp goto done;
7283 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7284 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7285 2e1f37b0 2019-08-08 stsp if (err)
7286 2e1f37b0 2019-08-08 stsp goto done;
7287 2e1f37b0 2019-08-08 stsp
7288 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7289 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7290 2e1f37b0 2019-08-08 stsp goto done;
7291 2e1f37b0 2019-08-08 stsp }
7292 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7293 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7294 2e1f37b0 2019-08-08 stsp goto done;
7295 2e1f37b0 2019-08-08 stsp }
7296 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7297 2e1f37b0 2019-08-08 stsp int choice;
7298 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7299 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7300 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7301 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7302 2e1f37b0 2019-08-08 stsp if (err)
7303 2e1f37b0 2019-08-08 stsp goto done;
7304 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7305 2e1f37b0 2019-08-08 stsp have_content = 1;
7306 19e4b907 2019-08-08 stsp else
7307 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7308 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7309 2e1f37b0 2019-08-08 stsp break;
7310 2e1f37b0 2019-08-08 stsp }
7311 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7312 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7313 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7314 2e1f37b0 2019-08-08 stsp done:
7315 2e1f37b0 2019-08-08 stsp free(label1);
7316 2e1f37b0 2019-08-08 stsp if (blob)
7317 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7318 2e1f37b0 2019-08-08 stsp if (staged_blob)
7319 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7320 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7321 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7322 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7323 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7324 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7325 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7326 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7327 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7328 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7329 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7330 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7331 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7332 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7333 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7334 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7335 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7336 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7337 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7338 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7339 2e1f37b0 2019-08-08 stsp }
7340 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7341 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7342 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7343 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7344 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7345 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7346 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7347 2e1f37b0 2019-08-08 stsp }
7348 2e1f37b0 2019-08-08 stsp free(args);
7349 2e1f37b0 2019-08-08 stsp if (ds) {
7350 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7351 2e1f37b0 2019-08-08 stsp free(ds);
7352 2e1f37b0 2019-08-08 stsp }
7353 2e1f37b0 2019-08-08 stsp if (changes)
7354 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7355 2e1f37b0 2019-08-08 stsp free(path1);
7356 2e1f37b0 2019-08-08 stsp free(path2);
7357 2e1f37b0 2019-08-08 stsp return err;
7358 2e1f37b0 2019-08-08 stsp }
7359 2e1f37b0 2019-08-08 stsp
7360 ad493afc 2019-08-03 stsp static const struct got_error *
7361 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7362 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7363 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7364 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7365 ad493afc 2019-08-03 stsp {
7366 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7367 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7368 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7369 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7370 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7371 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7372 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7373 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7374 9bc94a15 2019-08-03 stsp struct stat sb;
7375 ad493afc 2019-08-03 stsp
7376 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7377 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7378 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7379 2e1f37b0 2019-08-08 stsp return NULL;
7380 2e1f37b0 2019-08-08 stsp
7381 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7382 ad493afc 2019-08-03 stsp if (ie == NULL)
7383 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7384 9bc94a15 2019-08-03 stsp
7385 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7386 9bc94a15 2019-08-03 stsp == -1)
7387 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7388 ad493afc 2019-08-03 stsp
7389 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7390 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7391 f69721c3 2019-10-21 stsp if (err)
7392 f69721c3 2019-10-21 stsp goto done;
7393 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7394 f69721c3 2019-10-21 stsp id_str) == -1) {
7395 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7396 f69721c3 2019-10-21 stsp goto done;
7397 f69721c3 2019-10-21 stsp }
7398 f69721c3 2019-10-21 stsp
7399 ad493afc 2019-08-03 stsp switch (staged_status) {
7400 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7401 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7402 ad493afc 2019-08-03 stsp blob_id, 8192);
7403 ad493afc 2019-08-03 stsp if (err)
7404 ad493afc 2019-08-03 stsp break;
7405 ad493afc 2019-08-03 stsp /* fall through */
7406 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7407 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7408 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7409 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7410 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7411 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7412 2e1f37b0 2019-08-08 stsp if (err)
7413 2e1f37b0 2019-08-08 stsp break;
7414 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7415 2e1f37b0 2019-08-08 stsp break;
7416 2e1f37b0 2019-08-08 stsp } else {
7417 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7418 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7419 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7420 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7421 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7422 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7423 2e1f37b0 2019-08-08 stsp break;
7424 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7425 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7426 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7427 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7428 2e1f37b0 2019-08-08 stsp a->repo);
7429 2e1f37b0 2019-08-08 stsp if (err)
7430 2e1f37b0 2019-08-08 stsp break;
7431 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7432 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7433 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7434 2e1f37b0 2019-08-08 stsp }
7435 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7436 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7437 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7438 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7439 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7440 f69721c3 2019-10-21 stsp a->progress_arg);
7441 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7442 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7443 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7444 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7445 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7446 2e1f37b0 2019-08-08 stsp }
7447 2e1f37b0 2019-08-08 stsp }
7448 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7449 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7450 ad493afc 2019-08-03 stsp if (err)
7451 ad493afc 2019-08-03 stsp break;
7452 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7453 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
7454 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7455 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
7456 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
7457 ad493afc 2019-08-03 stsp if (err == NULL)
7458 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7459 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7460 ad493afc 2019-08-03 stsp break;
7461 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7462 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7463 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7464 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7465 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7466 2e1f37b0 2019-08-08 stsp if (err)
7467 2e1f37b0 2019-08-08 stsp break;
7468 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7469 2e1f37b0 2019-08-08 stsp break;
7470 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7471 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7472 2e1f37b0 2019-08-08 stsp break;
7473 2e1f37b0 2019-08-08 stsp }
7474 2e1f37b0 2019-08-08 stsp }
7475 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7476 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7477 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7478 9bc94a15 2019-08-03 stsp if (err)
7479 9bc94a15 2019-08-03 stsp break;
7480 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7481 ad493afc 2019-08-03 stsp break;
7482 ad493afc 2019-08-03 stsp }
7483 f69721c3 2019-10-21 stsp done:
7484 ad493afc 2019-08-03 stsp free(ondisk_path);
7485 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7486 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7487 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7488 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7489 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7490 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7491 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7492 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7493 ad493afc 2019-08-03 stsp if (blob_base)
7494 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7495 ad493afc 2019-08-03 stsp if (blob_staged)
7496 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7497 f69721c3 2019-10-21 stsp free(id_str);
7498 f69721c3 2019-10-21 stsp free(label_orig);
7499 ad493afc 2019-08-03 stsp return err;
7500 ad493afc 2019-08-03 stsp }
7501 ad493afc 2019-08-03 stsp
7502 ad493afc 2019-08-03 stsp const struct got_error *
7503 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7504 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7505 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7506 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7507 ad493afc 2019-08-03 stsp struct got_repository *repo)
7508 ad493afc 2019-08-03 stsp {
7509 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7510 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7511 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7512 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7513 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7514 ad493afc 2019-08-03 stsp
7515 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7516 ad493afc 2019-08-03 stsp if (err)
7517 ad493afc 2019-08-03 stsp return err;
7518 ad493afc 2019-08-03 stsp
7519 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7520 ad493afc 2019-08-03 stsp if (err)
7521 ad493afc 2019-08-03 stsp goto done;
7522 ad493afc 2019-08-03 stsp
7523 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7524 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7525 ad493afc 2019-08-03 stsp upa.repo = repo;
7526 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7527 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7528 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7529 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7530 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7531 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7532 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7533 ad493afc 2019-08-03 stsp if (err)
7534 ad493afc 2019-08-03 stsp goto done;
7535 ad493afc 2019-08-03 stsp }
7536 ad493afc 2019-08-03 stsp
7537 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7538 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7539 ad493afc 2019-08-03 stsp err = sync_err;
7540 ad493afc 2019-08-03 stsp done:
7541 ad493afc 2019-08-03 stsp free(fileindex_path);
7542 ad493afc 2019-08-03 stsp if (fileindex)
7543 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7544 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7545 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7546 ad493afc 2019-08-03 stsp err = unlockerr;
7547 ad493afc 2019-08-03 stsp return err;
7548 ad493afc 2019-08-03 stsp }