Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 eaccb85f 2018-12-25 stsp if (repo)
440 eaccb85f 2018-12-25 stsp got_repo_close(repo);
441 7ac97322 2018-03-11 stsp free(path_got);
442 056e7441 2018-03-11 stsp free(path_lock);
443 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
444 c442a90d 2019-03-10 stsp free(uuidstr);
445 bd165944 2019-03-10 stsp free(formatstr);
446 6d9d28c3 2018-03-11 stsp if (err) {
447 6d9d28c3 2018-03-11 stsp if (fd != -1)
448 6d9d28c3 2018-03-11 stsp close(fd);
449 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
450 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
451 6d9d28c3 2018-03-11 stsp *worktree = NULL;
452 6d9d28c3 2018-03-11 stsp } else
453 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
454 6d9d28c3 2018-03-11 stsp
455 6d9d28c3 2018-03-11 stsp return err;
456 86c3caaf 2018-03-09 stsp }
457 247140b2 2019-02-05 stsp
458 247140b2 2019-02-05 stsp const struct got_error *
459 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
460 247140b2 2019-02-05 stsp {
461 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
462 aedea96d 2020-10-20 stsp char *worktree_path;
463 86c3caaf 2018-03-09 stsp
464 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
465 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
466 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
467 aedea96d 2020-10-20 stsp
468 aedea96d 2020-10-20 stsp for (;;) {
469 aedea96d 2020-10-20 stsp char *parent_path;
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
472 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 aedea96d 2020-10-20 stsp free(worktree_path);
474 247140b2 2019-02-05 stsp return err;
475 aedea96d 2020-10-20 stsp }
476 aedea96d 2020-10-20 stsp if (*worktree) {
477 aedea96d 2020-10-20 stsp free(worktree_path);
478 aedea96d 2020-10-20 stsp return NULL;
479 aedea96d 2020-10-20 stsp }
480 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 aedea96d 2020-10-20 stsp break;
482 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
483 aedea96d 2020-10-20 stsp if (err) {
484 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
485 aedea96d 2020-10-20 stsp free(worktree_path);
486 aedea96d 2020-10-20 stsp return err;
487 aedea96d 2020-10-20 stsp }
488 aedea96d 2020-10-20 stsp break;
489 aedea96d 2020-10-20 stsp }
490 aedea96d 2020-10-20 stsp free(worktree_path);
491 aedea96d 2020-10-20 stsp worktree_path = parent_path;
492 aedea96d 2020-10-20 stsp }
493 247140b2 2019-02-05 stsp
494 aedea96d 2020-10-20 stsp free(worktree_path);
495 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
496 247140b2 2019-02-05 stsp }
497 247140b2 2019-02-05 stsp
498 3a6ce05a 2019-02-11 stsp const struct got_error *
499 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
500 86c3caaf 2018-03-09 stsp {
501 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
502 cde76477 2018-03-11 stsp free(worktree->repo_path);
503 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
504 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
505 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
506 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
507 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
510 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
511 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
512 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
513 6d9d28c3 2018-03-11 stsp free(worktree);
514 437adc9d 2020-12-10 yzhong close(worktree->root_fd);
515 3a6ce05a 2019-02-11 stsp return err;
516 86c3caaf 2018-03-09 stsp }
517 86c3caaf 2018-03-09 stsp
518 2fbdb5ae 2018-12-29 stsp const char *
519 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
520 c7f4312f 2019-02-05 stsp {
521 c7f4312f 2019-02-05 stsp return worktree->root_path;
522 c7f4312f 2019-02-05 stsp }
523 c7f4312f 2019-02-05 stsp
524 c7f4312f 2019-02-05 stsp const char *
525 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
526 86c3caaf 2018-03-09 stsp {
527 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
528 49520a32 2018-12-29 stsp }
529 49520a32 2018-12-29 stsp const char *
530 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
531 49520a32 2018-12-29 stsp {
532 f609be2e 2018-12-29 stsp return worktree->path_prefix;
533 e5dc7198 2018-12-29 stsp }
534 e5dc7198 2018-12-29 stsp
535 e5dc7198 2018-12-29 stsp const struct got_error *
536 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
537 e5dc7198 2018-12-29 stsp const char *path_prefix)
538 e5dc7198 2018-12-29 stsp {
539 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
540 e5dc7198 2018-12-29 stsp
541 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
542 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
543 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
544 e5dc7198 2018-12-29 stsp }
545 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
546 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
547 e5dc7198 2018-12-29 stsp free(absprefix);
548 e5dc7198 2018-12-29 stsp return NULL;
549 86c3caaf 2018-03-09 stsp }
550 86c3caaf 2018-03-09 stsp
551 bc70eb79 2019-05-09 stsp const char *
552 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
553 86c3caaf 2018-03-09 stsp {
554 36a38700 2019-05-10 stsp return worktree->head_ref_name;
555 024e9686 2019-05-14 stsp }
556 024e9686 2019-05-14 stsp
557 024e9686 2019-05-14 stsp const struct got_error *
558 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
559 024e9686 2019-05-14 stsp struct got_reference *head_ref)
560 024e9686 2019-05-14 stsp {
561 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
562 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
563 024e9686 2019-05-14 stsp
564 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
565 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
566 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
567 024e9686 2019-05-14 stsp path_got = NULL;
568 024e9686 2019-05-14 stsp goto done;
569 024e9686 2019-05-14 stsp }
570 024e9686 2019-05-14 stsp
571 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
572 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
573 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
574 024e9686 2019-05-14 stsp goto done;
575 024e9686 2019-05-14 stsp }
576 024e9686 2019-05-14 stsp
577 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
578 024e9686 2019-05-14 stsp if (err)
579 024e9686 2019-05-14 stsp goto done;
580 024e9686 2019-05-14 stsp
581 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
582 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
583 024e9686 2019-05-14 stsp done:
584 024e9686 2019-05-14 stsp free(path_got);
585 024e9686 2019-05-14 stsp if (err)
586 024e9686 2019-05-14 stsp free(head_ref_name);
587 024e9686 2019-05-14 stsp return err;
588 507dc3bb 2018-12-29 stsp }
589 507dc3bb 2018-12-29 stsp
590 b72f483a 2019-02-05 stsp struct got_object_id *
591 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
592 507dc3bb 2018-12-29 stsp {
593 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
594 86c3caaf 2018-03-09 stsp }
595 86c3caaf 2018-03-09 stsp
596 507dc3bb 2018-12-29 stsp const struct got_error *
597 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
598 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
599 507dc3bb 2018-12-29 stsp {
600 507dc3bb 2018-12-29 stsp const struct got_error *err;
601 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
602 507dc3bb 2018-12-29 stsp char *id_str = NULL;
603 507dc3bb 2018-12-29 stsp char *path_got = NULL;
604 507dc3bb 2018-12-29 stsp
605 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
606 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
607 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
608 507dc3bb 2018-12-29 stsp path_got = NULL;
609 507dc3bb 2018-12-29 stsp goto done;
610 507dc3bb 2018-12-29 stsp }
611 507dc3bb 2018-12-29 stsp
612 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
613 507dc3bb 2018-12-29 stsp if (err)
614 507dc3bb 2018-12-29 stsp return err;
615 507dc3bb 2018-12-29 stsp
616 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
617 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
618 507dc3bb 2018-12-29 stsp goto done;
619 507dc3bb 2018-12-29 stsp }
620 507dc3bb 2018-12-29 stsp
621 507dc3bb 2018-12-29 stsp /* Record our base commit. */
622 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
623 507dc3bb 2018-12-29 stsp if (err)
624 507dc3bb 2018-12-29 stsp goto done;
625 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
626 507dc3bb 2018-12-29 stsp if (err)
627 507dc3bb 2018-12-29 stsp goto done;
628 507dc3bb 2018-12-29 stsp
629 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
630 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
631 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
632 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
633 507dc3bb 2018-12-29 stsp goto done;
634 507dc3bb 2018-12-29 stsp }
635 507dc3bb 2018-12-29 stsp done:
636 507dc3bb 2018-12-29 stsp if (obj)
637 507dc3bb 2018-12-29 stsp got_object_close(obj);
638 507dc3bb 2018-12-29 stsp free(id_str);
639 507dc3bb 2018-12-29 stsp free(path_got);
640 507dc3bb 2018-12-29 stsp return err;
641 50b0790e 2020-09-11 stsp }
642 50b0790e 2020-09-11 stsp
643 50b0790e 2020-09-11 stsp const struct got_gotconfig *
644 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
645 50b0790e 2020-09-11 stsp {
646 50b0790e 2020-09-11 stsp return worktree->gotconfig;
647 507dc3bb 2018-12-29 stsp }
648 507dc3bb 2018-12-29 stsp
649 9d31a1d8 2018-03-11 stsp static const struct got_error *
650 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
651 86c3caaf 2018-03-09 stsp {
652 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
653 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
654 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
655 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
656 86c3caaf 2018-03-09 stsp return NULL;
657 21908da4 2019-01-13 stsp }
658 21908da4 2019-01-13 stsp
659 21908da4 2019-01-13 stsp static const struct got_error *
660 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
661 4a1ddfc2 2019-01-12 stsp {
662 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
663 4a1ddfc2 2019-01-12 stsp char *abspath;
664 4a1ddfc2 2019-01-12 stsp
665 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
666 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
667 4a1ddfc2 2019-01-12 stsp
668 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
669 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
670 ddcd8544 2019-03-11 stsp struct stat sb;
671 ddcd8544 2019-03-11 stsp err = NULL;
672 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
673 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
674 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
675 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
676 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
677 ddcd8544 2019-03-11 stsp }
678 ddcd8544 2019-03-11 stsp }
679 4a1ddfc2 2019-01-12 stsp free(abspath);
680 68c76935 2019-02-19 stsp return err;
681 68c76935 2019-02-19 stsp }
682 68c76935 2019-02-19 stsp
683 68c76935 2019-02-19 stsp static const struct got_error *
684 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
685 68c76935 2019-02-19 stsp {
686 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
687 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
688 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
689 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
690 68c76935 2019-02-19 stsp
691 68c76935 2019-02-19 stsp *same = 1;
692 68c76935 2019-02-19 stsp
693 230a42bd 2019-05-11 jcs for (;;) {
694 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
695 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
696 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
697 68c76935 2019-02-19 stsp break;
698 68c76935 2019-02-19 stsp }
699 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
700 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
701 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
702 68c76935 2019-02-19 stsp break;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp if (flen1 == 0) {
705 68c76935 2019-02-19 stsp if (flen2 != 0)
706 68c76935 2019-02-19 stsp *same = 0;
707 68c76935 2019-02-19 stsp break;
708 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
709 68c76935 2019-02-19 stsp if (flen1 != 0)
710 68c76935 2019-02-19 stsp *same = 0;
711 68c76935 2019-02-19 stsp break;
712 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
713 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
714 68c76935 2019-02-19 stsp *same = 0;
715 68c76935 2019-02-19 stsp break;
716 68c76935 2019-02-19 stsp }
717 68c76935 2019-02-19 stsp } else {
718 68c76935 2019-02-19 stsp *same = 0;
719 68c76935 2019-02-19 stsp break;
720 68c76935 2019-02-19 stsp }
721 68c76935 2019-02-19 stsp }
722 68c76935 2019-02-19 stsp
723 68c76935 2019-02-19 stsp return err;
724 68c76935 2019-02-19 stsp }
725 68c76935 2019-02-19 stsp
726 68c76935 2019-02-19 stsp static const struct got_error *
727 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
728 68c76935 2019-02-19 stsp {
729 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
730 68c76935 2019-02-19 stsp struct stat sb;
731 68c76935 2019-02-19 stsp size_t size1, size2;
732 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
733 68c76935 2019-02-19 stsp
734 68c76935 2019-02-19 stsp *same = 1;
735 68c76935 2019-02-19 stsp
736 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
737 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
738 68c76935 2019-02-19 stsp goto done;
739 68c76935 2019-02-19 stsp }
740 68c76935 2019-02-19 stsp size1 = sb.st_size;
741 68c76935 2019-02-19 stsp
742 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
743 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
744 68c76935 2019-02-19 stsp goto done;
745 68c76935 2019-02-19 stsp }
746 68c76935 2019-02-19 stsp size2 = sb.st_size;
747 68c76935 2019-02-19 stsp
748 68c76935 2019-02-19 stsp if (size1 != size2) {
749 68c76935 2019-02-19 stsp *same = 0;
750 68c76935 2019-02-19 stsp return NULL;
751 68c76935 2019-02-19 stsp }
752 68c76935 2019-02-19 stsp
753 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
754 68c76935 2019-02-19 stsp if (f1 == NULL)
755 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
756 68c76935 2019-02-19 stsp
757 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
758 68c76935 2019-02-19 stsp if (f2 == NULL) {
759 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
760 68c76935 2019-02-19 stsp goto done;
761 68c76935 2019-02-19 stsp }
762 68c76935 2019-02-19 stsp
763 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
764 68c76935 2019-02-19 stsp done:
765 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
766 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
767 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
768 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
769 68c76935 2019-02-19 stsp
770 6353ad76 2019-02-08 stsp return err;
771 6353ad76 2019-02-08 stsp }
772 6353ad76 2019-02-08 stsp
773 6353ad76 2019-02-08 stsp /*
774 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
775 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
776 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
777 6353ad76 2019-02-08 stsp */
778 6353ad76 2019-02-08 stsp static const struct got_error *
779 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
780 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
781 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
782 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
783 f69721c3 2019-10-21 stsp struct got_repository *repo,
784 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
785 6353ad76 2019-02-08 stsp {
786 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
787 6353ad76 2019-02-08 stsp int merged_fd = -1;
788 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
789 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
790 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
791 234035bc 2019-06-01 stsp int overlapcnt = 0;
792 3524bbf9 2020-10-20 stsp char *parent = NULL;
793 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
794 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
795 6353ad76 2019-02-08 stsp
796 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
797 234035bc 2019-06-01 stsp
798 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
799 3524bbf9 2020-10-20 stsp if (err)
800 3524bbf9 2020-10-20 stsp return err;
801 af54ae4a 2019-02-19 stsp
802 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
803 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
804 3524bbf9 2020-10-20 stsp goto done;
805 3524bbf9 2020-10-20 stsp }
806 af54ae4a 2019-02-19 stsp
807 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
808 6353ad76 2019-02-08 stsp if (err)
809 6353ad76 2019-02-08 stsp goto done;
810 6353ad76 2019-02-08 stsp
811 af54ae4a 2019-02-19 stsp free(base_path);
812 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
813 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
814 af54ae4a 2019-02-19 stsp base_path = NULL;
815 af54ae4a 2019-02-19 stsp goto done;
816 af54ae4a 2019-02-19 stsp }
817 af54ae4a 2019-02-19 stsp
818 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
819 6353ad76 2019-02-08 stsp if (err)
820 6353ad76 2019-02-08 stsp goto done;
821 46b6ee73 2019-06-04 stsp if (blob_orig) {
822 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
823 46b6ee73 2019-06-04 stsp blob_orig);
824 1430b4e0 2019-03-27 stsp if (err)
825 1430b4e0 2019-03-27 stsp goto done;
826 1430b4e0 2019-03-27 stsp } else {
827 1430b4e0 2019-03-27 stsp /*
828 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
829 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
830 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
831 1430b4e0 2019-03-27 stsp */
832 1430b4e0 2019-03-27 stsp }
833 6353ad76 2019-02-08 stsp
834 3b9f0f87 2020-07-23 stsp /*
835 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
836 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
837 3b9f0f87 2020-07-23 stsp */
838 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
839 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
840 3b9f0f87 2020-07-23 stsp ssize_t target_len;
841 3b9f0f87 2020-07-23 stsp size_t n;
842 3b9f0f87 2020-07-23 stsp
843 3b9f0f87 2020-07-23 stsp free(base_path);
844 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
845 3b9f0f87 2020-07-23 stsp parent) == -1) {
846 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
847 3b9f0f87 2020-07-23 stsp base_path = NULL;
848 3b9f0f87 2020-07-23 stsp goto done;
849 3b9f0f87 2020-07-23 stsp }
850 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
851 3b9f0f87 2020-07-23 stsp if (err)
852 3b9f0f87 2020-07-23 stsp goto done;
853 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
854 3b9f0f87 2020-07-23 stsp sizeof(target_path));
855 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
856 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
857 3b9f0f87 2020-07-23 stsp goto done;
858 3b9f0f87 2020-07-23 stsp }
859 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
860 3b9f0f87 2020-07-23 stsp if (n != target_len) {
861 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
862 3b9f0f87 2020-07-23 stsp goto done;
863 3b9f0f87 2020-07-23 stsp }
864 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
865 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
866 3b9f0f87 2020-07-23 stsp goto done;
867 3b9f0f87 2020-07-23 stsp }
868 3b9f0f87 2020-07-23 stsp }
869 3b9f0f87 2020-07-23 stsp
870 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
871 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
872 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
873 6353ad76 2019-02-08 stsp if (err)
874 6353ad76 2019-02-08 stsp goto done;
875 6353ad76 2019-02-08 stsp
876 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
877 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
878 1ee397ad 2019-07-12 stsp if (err)
879 1ee397ad 2019-07-12 stsp goto done;
880 6353ad76 2019-02-08 stsp
881 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
882 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
883 816dc654 2019-02-16 stsp goto done;
884 68c76935 2019-02-19 stsp }
885 68c76935 2019-02-19 stsp
886 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
887 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
888 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
889 68c76935 2019-02-19 stsp merged_path);
890 68c76935 2019-02-19 stsp if (err)
891 68c76935 2019-02-19 stsp goto done;
892 816dc654 2019-02-16 stsp }
893 6353ad76 2019-02-08 stsp
894 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
895 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
896 70a0c8ec 2019-02-20 stsp goto done;
897 70a0c8ec 2019-02-20 stsp }
898 70a0c8ec 2019-02-20 stsp
899 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
900 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
901 230a42bd 2019-05-11 jcs ondisk_path);
902 6353ad76 2019-02-08 stsp goto done;
903 6353ad76 2019-02-08 stsp }
904 6353ad76 2019-02-08 stsp done:
905 fdcb7daf 2019-12-15 stsp if (err) {
906 fdcb7daf 2019-12-15 stsp if (merged_path)
907 fdcb7daf 2019-12-15 stsp unlink(merged_path);
908 3b9f0f87 2020-07-23 stsp }
909 3b9f0f87 2020-07-23 stsp if (symlink_path) {
910 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
911 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
912 fdcb7daf 2019-12-15 stsp }
913 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
914 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
915 3b9f0f87 2020-07-23 stsp free(symlink_path);
916 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
917 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
918 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
919 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
920 6353ad76 2019-02-08 stsp free(merged_path);
921 af54ae4a 2019-02-19 stsp free(base_path);
922 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
923 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
924 46b6ee73 2019-06-04 stsp free(blob_orig_path);
925 af57b12a 2020-07-23 stsp }
926 3524bbf9 2020-10-20 stsp free(parent);
927 af57b12a 2020-07-23 stsp return err;
928 af57b12a 2020-07-23 stsp }
929 af57b12a 2020-07-23 stsp
930 af57b12a 2020-07-23 stsp static const struct got_error *
931 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
932 af57b12a 2020-07-23 stsp size_t target_len)
933 af57b12a 2020-07-23 stsp {
934 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
935 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
936 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
937 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
938 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
939 af57b12a 2020-07-23 stsp ondisk_path);
940 af57b12a 2020-07-23 stsp return NULL;
941 af57b12a 2020-07-23 stsp }
942 af57b12a 2020-07-23 stsp
943 af57b12a 2020-07-23 stsp /*
944 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
945 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
946 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
947 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
948 993e2a1b 2020-07-23 stsp *
949 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
950 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
951 993e2a1b 2020-07-23 stsp *
952 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
953 993e2a1b 2020-07-23 stsp * has deleted this symlink.
954 11cc08c1 2020-07-23 stsp */
955 11cc08c1 2020-07-23 stsp static const struct got_error *
956 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
957 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
958 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
959 11cc08c1 2020-07-23 stsp {
960 11cc08c1 2020-07-23 stsp const struct got_error *err;
961 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
962 11cc08c1 2020-07-23 stsp FILE *f = NULL;
963 11cc08c1 2020-07-23 stsp
964 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
965 11cc08c1 2020-07-23 stsp if (err)
966 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
967 11cc08c1 2020-07-23 stsp
968 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
969 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
970 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
971 11cc08c1 2020-07-23 stsp goto done;
972 11cc08c1 2020-07-23 stsp }
973 11cc08c1 2020-07-23 stsp
974 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
975 11cc08c1 2020-07-23 stsp if (err)
976 3818e3c4 2020-11-01 naddy goto done;
977 3818e3c4 2020-11-01 naddy
978 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
979 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
980 11cc08c1 2020-07-23 stsp goto done;
981 3818e3c4 2020-11-01 naddy }
982 11cc08c1 2020-07-23 stsp
983 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
984 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
985 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
986 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
987 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
988 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
989 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
990 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
991 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
992 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
993 11cc08c1 2020-07-23 stsp goto done;
994 11cc08c1 2020-07-23 stsp }
995 11cc08c1 2020-07-23 stsp
996 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
997 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
998 11cc08c1 2020-07-23 stsp goto done;
999 11cc08c1 2020-07-23 stsp }
1000 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
1001 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
1002 11cc08c1 2020-07-23 stsp goto done;
1003 11cc08c1 2020-07-23 stsp }
1004 11cc08c1 2020-07-23 stsp done:
1005 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1006 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1007 11cc08c1 2020-07-23 stsp free(path);
1008 11cc08c1 2020-07-23 stsp free(id_str);
1009 11cc08c1 2020-07-23 stsp free(label_deriv);
1010 11cc08c1 2020-07-23 stsp return err;
1011 11cc08c1 2020-07-23 stsp }
1012 d219f183 2020-07-23 stsp
1013 d219f183 2020-07-23 stsp /* forward declaration */
1014 d219f183 2020-07-23 stsp static const struct got_error *
1015 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1016 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1017 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1018 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1019 11cc08c1 2020-07-23 stsp
1020 11cc08c1 2020-07-23 stsp /*
1021 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1022 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1023 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1024 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1025 af57b12a 2020-07-23 stsp */
1026 af57b12a 2020-07-23 stsp static const struct got_error *
1027 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1028 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1029 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1030 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1031 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1032 af57b12a 2020-07-23 stsp {
1033 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1034 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1035 af57b12a 2020-07-23 stsp struct stat sb;
1036 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1037 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1038 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1039 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1040 af57b12a 2020-07-23 stsp
1041 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1042 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1043 af57b12a 2020-07-23 stsp
1044 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1045 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1046 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1047 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1048 af57b12a 2020-07-23 stsp ondisk_path);
1049 af57b12a 2020-07-23 stsp goto done;
1050 af57b12a 2020-07-23 stsp }
1051 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1052 af57b12a 2020-07-23 stsp
1053 526a746f 2020-07-23 stsp if (blob_orig) {
1054 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1055 526a746f 2020-07-23 stsp if (err)
1056 526a746f 2020-07-23 stsp goto done;
1057 526a746f 2020-07-23 stsp }
1058 af57b12a 2020-07-23 stsp
1059 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1060 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1061 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1062 11cc08c1 2020-07-23 stsp have_local_change = 1;
1063 11cc08c1 2020-07-23 stsp
1064 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1065 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1066 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1067 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1068 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1069 11cc08c1 2020-07-23 stsp
1070 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1071 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1072 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1073 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1074 11cc08c1 2020-07-23 stsp path);
1075 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1076 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1077 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1078 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1079 11cc08c1 2020-07-23 stsp path);
1080 11cc08c1 2020-07-23 stsp } else {
1081 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1082 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1083 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1084 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1085 11cc08c1 2020-07-23 stsp if (err)
1086 11cc08c1 2020-07-23 stsp goto done;
1087 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1088 11cc08c1 2020-07-23 stsp path);
1089 11cc08c1 2020-07-23 stsp }
1090 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1091 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1092 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1093 af57b12a 2020-07-23 stsp strlen(deriv_target));
1094 af57b12a 2020-07-23 stsp if (err)
1095 af57b12a 2020-07-23 stsp goto done;
1096 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1097 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1098 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1099 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1100 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1101 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1102 ea7786be 2020-07-23 stsp path);
1103 ea7786be 2020-07-23 stsp } else {
1104 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1105 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1106 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1107 ea7786be 2020-07-23 stsp if (err)
1108 ea7786be 2020-07-23 stsp goto done;
1109 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1110 ea7786be 2020-07-23 stsp path);
1111 ea7786be 2020-07-23 stsp }
1112 6353ad76 2019-02-08 stsp }
1113 11cc08c1 2020-07-23 stsp
1114 af57b12a 2020-07-23 stsp done:
1115 af57b12a 2020-07-23 stsp free(ancestor_target);
1116 14c901f1 2019-08-08 stsp return err;
1117 14c901f1 2019-08-08 stsp }
1118 14c901f1 2019-08-08 stsp
1119 14c901f1 2019-08-08 stsp /*
1120 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1121 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1122 14c901f1 2019-08-08 stsp * acts as the second derived version.
1123 14c901f1 2019-08-08 stsp */
1124 14c901f1 2019-08-08 stsp static const struct got_error *
1125 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1126 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1127 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1128 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1129 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1130 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1131 14c901f1 2019-08-08 stsp {
1132 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1133 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1134 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1135 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1136 14c901f1 2019-08-08 stsp
1137 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1138 14c901f1 2019-08-08 stsp
1139 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1140 ed6b5030 2020-10-20 stsp if (err)
1141 ed6b5030 2020-10-20 stsp return err;
1142 14c901f1 2019-08-08 stsp
1143 14c901f1 2019-08-08 stsp free(base_path);
1144 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1145 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1146 14c901f1 2019-08-08 stsp base_path = NULL;
1147 14c901f1 2019-08-08 stsp goto done;
1148 14c901f1 2019-08-08 stsp }
1149 14c901f1 2019-08-08 stsp
1150 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1151 14c901f1 2019-08-08 stsp if (err)
1152 14c901f1 2019-08-08 stsp goto done;
1153 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1154 14c901f1 2019-08-08 stsp blob_deriv);
1155 14c901f1 2019-08-08 stsp if (err)
1156 14c901f1 2019-08-08 stsp goto done;
1157 14c901f1 2019-08-08 stsp
1158 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1159 14c901f1 2019-08-08 stsp if (err)
1160 14c901f1 2019-08-08 stsp goto done;
1161 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1162 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1163 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1164 14c901f1 2019-08-08 stsp goto done;
1165 14c901f1 2019-08-08 stsp }
1166 14c901f1 2019-08-08 stsp
1167 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1168 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1169 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1170 14c901f1 2019-08-08 stsp done:
1171 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1172 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1173 14c901f1 2019-08-08 stsp free(base_path);
1174 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1175 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1176 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1177 14c901f1 2019-08-08 stsp }
1178 6353ad76 2019-02-08 stsp free(id_str);
1179 818c7501 2019-07-11 stsp free(label_deriv);
1180 ed6b5030 2020-10-20 stsp free(parent);
1181 4a1ddfc2 2019-01-12 stsp return err;
1182 4a1ddfc2 2019-01-12 stsp }
1183 4a1ddfc2 2019-01-12 stsp
1184 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1185 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1186 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1187 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1188 13d9040b 2019-03-27 stsp {
1189 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1190 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1191 13d9040b 2019-03-27 stsp
1192 65b05cec 2020-07-23 stsp *new_iep = NULL;
1193 65b05cec 2020-07-23 stsp
1194 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1195 054041d0 2020-06-24 stsp if (err)
1196 054041d0 2020-06-24 stsp return err;
1197 054041d0 2020-06-24 stsp
1198 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1199 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1200 054041d0 2020-06-24 stsp if (err)
1201 054041d0 2020-06-24 stsp goto done;
1202 054041d0 2020-06-24 stsp
1203 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1204 054041d0 2020-06-24 stsp done:
1205 054041d0 2020-06-24 stsp if (err)
1206 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1207 65b05cec 2020-07-23 stsp else
1208 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1209 13d9040b 2019-03-27 stsp return err;
1210 1ebedb77 2019-10-19 stsp }
1211 1ebedb77 2019-10-19 stsp
1212 1ebedb77 2019-10-19 stsp static mode_t
1213 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1214 1ebedb77 2019-10-19 stsp {
1215 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1216 1ebedb77 2019-10-19 stsp
1217 1ebedb77 2019-10-19 stsp if (executable) {
1218 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1219 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1220 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1221 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1222 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1223 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1224 1ebedb77 2019-10-19 stsp }
1225 1ebedb77 2019-10-19 stsp
1226 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1227 13d9040b 2019-03-27 stsp }
1228 13d9040b 2019-03-27 stsp
1229 8ba819a3 2020-07-23 stsp /* forward declaration */
1230 13d9040b 2019-03-27 stsp static const struct got_error *
1231 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1232 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1233 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1234 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1235 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1236 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1237 8ba819a3 2020-07-23 stsp
1238 5a1fbc73 2020-07-23 stsp /*
1239 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1240 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1241 5a1fbc73 2020-07-23 stsp */
1242 8ba819a3 2020-07-23 stsp static const struct got_error *
1243 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1244 5a1fbc73 2020-07-23 stsp size_t target_len)
1245 5a1fbc73 2020-07-23 stsp {
1246 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1247 5a1fbc73 2020-07-23 stsp ssize_t elen;
1248 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1249 5a1fbc73 2020-07-23 stsp int fd;
1250 5a1fbc73 2020-07-23 stsp
1251 5a1fbc73 2020-07-23 stsp /*
1252 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1253 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1254 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1255 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1256 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1257 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1258 5a1fbc73 2020-07-23 stsp */
1259 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1260 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1261 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1262 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1263 5a1fbc73 2020-07-23 stsp
1264 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1265 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1266 5a1fbc73 2020-07-23 stsp if (elen == -1)
1267 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1268 5a1fbc73 2020-07-23 stsp
1269 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1270 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1271 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1272 5a1fbc73 2020-07-23 stsp }
1273 5a1fbc73 2020-07-23 stsp
1274 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1275 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1276 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1277 5a1fbc73 2020-07-23 stsp return err;
1278 3c1ec782 2020-07-23 stsp }
1279 3c1ec782 2020-07-23 stsp
1280 3c1ec782 2020-07-23 stsp static const struct got_error *
1281 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1282 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1283 3c1ec782 2020-07-23 stsp {
1284 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1285 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1286 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1287 3c1ec782 2020-07-23 stsp
1288 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1289 3c1ec782 2020-07-23 stsp
1290 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1291 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1292 3c1ec782 2020-07-23 stsp return NULL;
1293 3c1ec782 2020-07-23 stsp }
1294 3c1ec782 2020-07-23 stsp
1295 3c1ec782 2020-07-23 stsp /*
1296 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1297 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1298 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1299 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1300 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1301 3c1ec782 2020-07-23 stsp */
1302 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1303 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1304 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1305 ce031e9e 2020-10-20 stsp if (err)
1306 ce031e9e 2020-10-20 stsp return err;
1307 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1308 ce031e9e 2020-10-20 stsp free(parent);
1309 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1310 ce031e9e 2020-10-20 stsp }
1311 ce031e9e 2020-10-20 stsp free(parent);
1312 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1313 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1314 3c1ec782 2020-07-23 stsp free(abspath);
1315 3c1ec782 2020-07-23 stsp return err;
1316 3c1ec782 2020-07-23 stsp }
1317 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1318 3c1ec782 2020-07-23 stsp free(abspath);
1319 3c1ec782 2020-07-23 stsp if (err)
1320 3c1ec782 2020-07-23 stsp return err;
1321 3c1ec782 2020-07-23 stsp } else {
1322 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1323 3c1ec782 2020-07-23 stsp if (err)
1324 3c1ec782 2020-07-23 stsp return err;
1325 3c1ec782 2020-07-23 stsp }
1326 3c1ec782 2020-07-23 stsp
1327 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1328 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1329 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1330 3c1ec782 2020-07-23 stsp return NULL;
1331 3c1ec782 2020-07-23 stsp }
1332 3c1ec782 2020-07-23 stsp
1333 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1334 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1335 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1336 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1337 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1338 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1339 3c1ec782 2020-07-23 stsp
1340 3c1ec782 2020-07-23 stsp free(path_got);
1341 3c1ec782 2020-07-23 stsp return NULL;
1342 5a1fbc73 2020-07-23 stsp }
1343 5a1fbc73 2020-07-23 stsp
1344 5a1fbc73 2020-07-23 stsp static const struct got_error *
1345 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1346 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1347 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1348 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1349 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1350 9d31a1d8 2018-03-11 stsp {
1351 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1352 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1353 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1354 906c123b 2020-07-23 stsp char *path_got = NULL;
1355 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1356 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1357 8ba819a3 2020-07-23 stsp
1358 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1359 2e1fa222 2020-07-23 stsp
1360 8ba819a3 2020-07-23 stsp /*
1361 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1362 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1363 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1364 8ba819a3 2020-07-23 stsp * in the blob object.
1365 8ba819a3 2020-07-23 stsp */
1366 8ba819a3 2020-07-23 stsp do {
1367 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1368 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1369 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1370 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1371 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1372 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1373 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1375 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1376 3b9f0f87 2020-07-23 stsp progress_arg);
1377 8ba819a3 2020-07-23 stsp }
1378 8ba819a3 2020-07-23 stsp if (len > 0) {
1379 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1380 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1381 8ba819a3 2020-07-23 stsp len - hdrlen);
1382 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1383 8ba819a3 2020-07-23 stsp hdrlen = 0;
1384 8ba819a3 2020-07-23 stsp }
1385 8ba819a3 2020-07-23 stsp } while (len != 0);
1386 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1387 8ba819a3 2020-07-23 stsp
1388 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1389 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1390 3c1ec782 2020-07-23 stsp if (err)
1391 3c1ec782 2020-07-23 stsp return err;
1392 8ba819a3 2020-07-23 stsp
1393 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1394 906c123b 2020-07-23 stsp /* install as a regular file */
1395 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1396 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1397 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1398 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1400 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1401 906c123b 2020-07-23 stsp goto done;
1402 906c123b 2020-07-23 stsp }
1403 906c123b 2020-07-23 stsp
1404 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1405 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1406 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1407 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1408 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1409 c90c8ce3 2020-07-23 stsp goto done;
1410 c90c8ce3 2020-07-23 stsp }
1411 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1412 5a1fbc73 2020-07-23 stsp target_path, target_len);
1413 5a1fbc73 2020-07-23 stsp if (err)
1414 f35fa46a 2020-07-23 stsp goto done;
1415 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1416 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1417 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1418 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1419 6e1eade5 2020-07-23 stsp path);
1420 f35fa46a 2020-07-23 stsp }
1421 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1422 f35fa46a 2020-07-23 stsp }
1423 f35fa46a 2020-07-23 stsp
1424 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1425 f4994adc 2020-10-20 stsp char *parent;
1426 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1427 f4994adc 2020-10-20 stsp if (err)
1428 f4994adc 2020-10-20 stsp goto done;
1429 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1430 f4994adc 2020-10-20 stsp free(parent);
1431 f35fa46a 2020-07-23 stsp if (err)
1432 f35fa46a 2020-07-23 stsp goto done;
1433 f35fa46a 2020-07-23 stsp /*
1434 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1435 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1436 f35fa46a 2020-07-23 stsp */
1437 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1438 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1439 f35fa46a 2020-07-23 stsp goto done;
1440 f35fa46a 2020-07-23 stsp }
1441 f35fa46a 2020-07-23 stsp }
1442 f35fa46a 2020-07-23 stsp
1443 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1444 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1445 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1446 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1447 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1448 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1449 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1450 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1451 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1452 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1453 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1454 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1455 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1456 8ba819a3 2020-07-23 stsp } else {
1457 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1458 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1459 8ba819a3 2020-07-23 stsp }
1460 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1461 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1462 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1463 8ba819a3 2020-07-23 stsp done:
1464 906c123b 2020-07-23 stsp free(path_got);
1465 8ba819a3 2020-07-23 stsp return err;
1466 8ba819a3 2020-07-23 stsp }
1467 8ba819a3 2020-07-23 stsp
1468 8ba819a3 2020-07-23 stsp static const struct got_error *
1469 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1470 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1471 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1472 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1473 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1474 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1475 8ba819a3 2020-07-23 stsp {
1476 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1477 507dc3bb 2018-12-29 stsp int fd = -1;
1478 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1479 507dc3bb 2018-12-29 stsp int update = 0;
1480 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1481 8ba819a3 2020-07-23 stsp
1482 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1483 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1484 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1485 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1486 f5375317 2020-10-20 stsp char *parent;
1487 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1488 f5375317 2020-10-20 stsp if (err)
1489 f5375317 2020-10-20 stsp return err;
1490 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1491 f5375317 2020-10-20 stsp free(parent);
1492 21908da4 2019-01-13 stsp if (err)
1493 21908da4 2019-01-13 stsp return err;
1494 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1495 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1496 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1497 21908da4 2019-01-13 stsp if (fd == -1)
1498 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1499 230a42bd 2019-05-11 jcs ondisk_path);
1500 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1501 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1502 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1503 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1504 3b9f0f87 2020-07-23 stsp goto done;
1505 3b9f0f87 2020-07-23 stsp }
1506 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1507 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1508 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1509 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1510 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1511 507dc3bb 2018-12-29 stsp goto done;
1512 d70b8e30 2018-12-27 stsp } else {
1513 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1514 507dc3bb 2018-12-29 stsp ondisk_path);
1515 507dc3bb 2018-12-29 stsp if (err)
1516 507dc3bb 2018-12-29 stsp goto done;
1517 507dc3bb 2018-12-29 stsp update = 1;
1518 9d31a1d8 2018-03-11 stsp }
1519 507dc3bb 2018-12-29 stsp } else
1520 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1521 9d31a1d8 2018-03-11 stsp }
1522 9d31a1d8 2018-03-11 stsp
1523 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1525 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1526 3818e3c4 2020-11-01 naddy goto done;
1527 3818e3c4 2020-11-01 naddy }
1528 3818e3c4 2020-11-01 naddy
1529 bd6aa359 2020-07-23 stsp if (progress_cb) {
1530 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1531 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1532 bd6aa359 2020-07-23 stsp path);
1533 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1534 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1535 bd6aa359 2020-07-23 stsp path);
1536 bd6aa359 2020-07-23 stsp else
1537 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1538 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1539 bd6aa359 2020-07-23 stsp if (err)
1540 bd6aa359 2020-07-23 stsp goto done;
1541 bd6aa359 2020-07-23 stsp }
1542 d7b62c98 2018-12-27 stsp
1543 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1544 9d31a1d8 2018-03-11 stsp do {
1545 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1546 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1547 9d31a1d8 2018-03-11 stsp if (err)
1548 9d31a1d8 2018-03-11 stsp break;
1549 9d31a1d8 2018-03-11 stsp if (len > 0) {
1550 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1551 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1552 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1553 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1554 b87c6f83 2018-12-24 stsp goto done;
1555 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1556 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1557 b87c6f83 2018-12-24 stsp goto done;
1558 9d31a1d8 2018-03-11 stsp }
1559 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1560 9d31a1d8 2018-03-11 stsp }
1561 9d31a1d8 2018-03-11 stsp } while (len != 0);
1562 9d31a1d8 2018-03-11 stsp
1563 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1564 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1565 816dc654 2019-02-16 stsp goto done;
1566 816dc654 2019-02-16 stsp }
1567 9d31a1d8 2018-03-11 stsp
1568 507dc3bb 2018-12-29 stsp if (update) {
1569 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1570 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1571 f6d8c0ac 2020-11-09 stsp goto done;
1572 f6d8c0ac 2020-11-09 stsp }
1573 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1574 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1575 230a42bd 2019-05-11 jcs ondisk_path);
1576 68ed9ba5 2019-02-10 stsp goto done;
1577 68ed9ba5 2019-02-10 stsp }
1578 63df146d 2020-11-09 stsp free(tmppath);
1579 63df146d 2020-11-09 stsp tmppath = NULL;
1580 507dc3bb 2018-12-29 stsp }
1581 507dc3bb 2018-12-29 stsp
1582 9d31a1d8 2018-03-11 stsp done:
1583 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1584 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1585 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1586 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1587 507dc3bb 2018-12-29 stsp free(tmppath);
1588 6353ad76 2019-02-08 stsp return err;
1589 6353ad76 2019-02-08 stsp }
1590 6353ad76 2019-02-08 stsp
1591 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1592 6353ad76 2019-02-08 stsp static const struct got_error *
1593 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1594 7154f6ce 2019-03-27 stsp {
1595 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1596 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1597 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1598 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1599 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1600 7154f6ce 2019-03-27 stsp };
1601 7154f6ce 2019-03-27 stsp int i = 0;
1602 9bdd68dd 2020-01-02 naddy char *line = NULL;
1603 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1604 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1605 7154f6ce 2019-03-27 stsp
1606 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1607 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1608 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1609 7154f6ce 2019-03-27 stsp if (feof(f))
1610 7154f6ce 2019-03-27 stsp break;
1611 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1612 7154f6ce 2019-03-27 stsp break;
1613 7154f6ce 2019-03-27 stsp }
1614 7154f6ce 2019-03-27 stsp
1615 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1616 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1617 19332e6d 2019-05-13 stsp == 0)
1618 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1619 7154f6ce 2019-03-27 stsp else
1620 7154f6ce 2019-03-27 stsp i++;
1621 7154f6ce 2019-03-27 stsp }
1622 7154f6ce 2019-03-27 stsp }
1623 9bdd68dd 2020-01-02 naddy free(line);
1624 7154f6ce 2019-03-27 stsp
1625 7154f6ce 2019-03-27 stsp return err;
1626 e2b1e152 2019-07-13 stsp }
1627 e2b1e152 2019-07-13 stsp
1628 e2b1e152 2019-07-13 stsp static int
1629 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1630 1ebedb77 2019-10-19 stsp {
1631 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1632 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1633 1ebedb77 2019-10-19 stsp }
1634 1ebedb77 2019-10-19 stsp
1635 1ebedb77 2019-10-19 stsp static int
1636 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1637 e2b1e152 2019-07-13 stsp {
1638 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1639 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1640 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1641 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1642 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1643 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1644 c363b2c1 2019-08-03 stsp }
1645 c363b2c1 2019-08-03 stsp
1646 c363b2c1 2019-08-03 stsp static unsigned char
1647 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1648 c363b2c1 2019-08-03 stsp {
1649 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1650 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1651 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1652 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1653 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1654 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1655 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1656 c363b2c1 2019-08-03 stsp default:
1657 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1658 a919d5c4 2020-07-23 stsp }
1659 a919d5c4 2020-07-23 stsp }
1660 a919d5c4 2020-07-23 stsp
1661 a919d5c4 2020-07-23 stsp static const struct got_error *
1662 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1663 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1664 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1665 a919d5c4 2020-07-23 stsp {
1666 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1667 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1668 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1669 a919d5c4 2020-07-23 stsp ssize_t elen;
1670 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1671 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1672 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1673 a919d5c4 2020-07-23 stsp
1674 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1675 a919d5c4 2020-07-23 stsp
1676 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1677 a919d5c4 2020-07-23 stsp do {
1678 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1679 a919d5c4 2020-07-23 stsp if (err)
1680 a919d5c4 2020-07-23 stsp return err;
1681 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1682 a919d5c4 2020-07-23 stsp /*
1683 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1684 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1685 a919d5c4 2020-07-23 stsp */
1686 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1687 a919d5c4 2020-07-23 stsp }
1688 a919d5c4 2020-07-23 stsp if (len > 0) {
1689 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1690 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1691 a919d5c4 2020-07-23 stsp len - hdrlen);
1692 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1693 a919d5c4 2020-07-23 stsp hdrlen = 0;
1694 a919d5c4 2020-07-23 stsp }
1695 a919d5c4 2020-07-23 stsp } while (len != 0);
1696 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1697 a919d5c4 2020-07-23 stsp
1698 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1699 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1700 a919d5c4 2020-07-23 stsp if (elen == -1)
1701 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1702 a919d5c4 2020-07-23 stsp } else {
1703 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1704 a919d5c4 2020-07-23 stsp if (elen == -1)
1705 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1706 c363b2c1 2019-08-03 stsp }
1707 a919d5c4 2020-07-23 stsp
1708 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1709 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1710 a919d5c4 2020-07-23 stsp
1711 a919d5c4 2020-07-23 stsp return NULL;
1712 7154f6ce 2019-03-27 stsp }
1713 7154f6ce 2019-03-27 stsp
1714 7154f6ce 2019-03-27 stsp static const struct got_error *
1715 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1716 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1717 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1718 6353ad76 2019-02-08 stsp {
1719 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1720 6353ad76 2019-02-08 stsp struct got_object_id id;
1721 6353ad76 2019-02-08 stsp size_t hdrlen;
1722 1338848f 2019-12-13 stsp int fd = -1;
1723 6353ad76 2019-02-08 stsp FILE *f = NULL;
1724 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1725 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1726 6353ad76 2019-02-08 stsp size_t flen, blen;
1727 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1728 6353ad76 2019-02-08 stsp
1729 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1730 6353ad76 2019-02-08 stsp
1731 7f91a133 2019-12-13 stsp /*
1732 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1733 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1734 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1735 7f91a133 2019-12-13 stsp */
1736 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1737 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1738 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1739 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1740 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1741 882ef1b9 2019-12-13 stsp else
1742 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1743 882ef1b9 2019-12-13 stsp goto done;
1744 882ef1b9 2019-12-13 stsp }
1745 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1746 3d35a492 2019-12-13 stsp goto done;
1747 3d35a492 2019-12-13 stsp }
1748 7f91a133 2019-12-13 stsp } else {
1749 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1750 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1751 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1752 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1753 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1754 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1755 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1756 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1757 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1758 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1759 3d35a492 2019-12-13 stsp else
1760 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1761 3d35a492 2019-12-13 stsp goto done;
1762 3d35a492 2019-12-13 stsp }
1763 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1764 1338848f 2019-12-13 stsp goto done;
1765 a378724f 2019-02-10 stsp }
1766 a378724f 2019-02-10 stsp }
1767 6353ad76 2019-02-08 stsp
1768 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1769 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1770 1338848f 2019-12-13 stsp goto done;
1771 3f148bc6 2019-07-27 stsp }
1772 efdd40df 2019-07-27 stsp
1773 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1774 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1775 1338848f 2019-12-13 stsp goto done;
1776 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1777 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1778 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1779 1338848f 2019-12-13 stsp goto done;
1780 d00136be 2019-03-26 stsp }
1781 b8f41171 2019-02-10 stsp
1782 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1783 f179e66d 2020-07-23 stsp goto done;
1784 f179e66d 2020-07-23 stsp
1785 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1786 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1787 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1788 1338848f 2019-12-13 stsp goto done;
1789 f179e66d 2020-07-23 stsp }
1790 6353ad76 2019-02-08 stsp
1791 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1792 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1793 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1794 c363b2c1 2019-08-03 stsp else
1795 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1796 c363b2c1 2019-08-03 stsp
1797 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1798 6353ad76 2019-02-08 stsp if (err)
1799 1338848f 2019-12-13 stsp goto done;
1800 6353ad76 2019-02-08 stsp
1801 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1802 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1803 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1804 a919d5c4 2020-07-23 stsp goto done;
1805 a919d5c4 2020-07-23 stsp }
1806 a919d5c4 2020-07-23 stsp
1807 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1808 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1809 ab0d4361 2019-12-13 stsp if (fd == -1) {
1810 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1811 ab0d4361 2019-12-13 stsp goto done;
1812 ab0d4361 2019-12-13 stsp }
1813 3d35a492 2019-12-13 stsp }
1814 3d35a492 2019-12-13 stsp
1815 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1816 6353ad76 2019-02-08 stsp if (f == NULL) {
1817 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1818 6353ad76 2019-02-08 stsp goto done;
1819 6353ad76 2019-02-08 stsp }
1820 1338848f 2019-12-13 stsp fd = -1;
1821 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1822 656b1f76 2019-05-11 jcs for (;;) {
1823 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1824 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1825 6353ad76 2019-02-08 stsp if (err)
1826 7154f6ce 2019-03-27 stsp goto done;
1827 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1828 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1829 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1830 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1831 7154f6ce 2019-03-27 stsp goto done;
1832 80c5c120 2019-02-19 stsp }
1833 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1834 6353ad76 2019-02-08 stsp if (flen != 0)
1835 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1836 6353ad76 2019-02-08 stsp break;
1837 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1838 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1839 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1840 6353ad76 2019-02-08 stsp break;
1841 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1842 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1843 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp }
1847 6353ad76 2019-02-08 stsp } else {
1848 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 6353ad76 2019-02-08 stsp break;
1850 6353ad76 2019-02-08 stsp }
1851 6353ad76 2019-02-08 stsp hdrlen = 0;
1852 6353ad76 2019-02-08 stsp }
1853 7154f6ce 2019-03-27 stsp
1854 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1855 7154f6ce 2019-03-27 stsp rewind(f);
1856 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1857 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1858 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1859 6353ad76 2019-02-08 stsp done:
1860 6353ad76 2019-02-08 stsp if (blob)
1861 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1862 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1863 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1864 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1865 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1866 9d31a1d8 2018-03-11 stsp return err;
1867 e2b1e152 2019-07-13 stsp }
1868 e2b1e152 2019-07-13 stsp
1869 e2b1e152 2019-07-13 stsp /*
1870 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1871 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1872 e2b1e152 2019-07-13 stsp */
1873 e2b1e152 2019-07-13 stsp static const struct got_error *
1874 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1876 e2b1e152 2019-07-13 stsp {
1877 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1879 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1880 e2b1e152 2019-07-13 stsp
1881 e2b1e152 2019-07-13 stsp return NULL;
1882 9d31a1d8 2018-03-11 stsp }
1883 9d31a1d8 2018-03-11 stsp
1884 9d31a1d8 2018-03-11 stsp static const struct got_error *
1885 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1886 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1888 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1889 0584f854 2019-04-06 stsp void *progress_arg)
1890 9d31a1d8 2018-03-11 stsp {
1891 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1892 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1893 6353ad76 2019-02-08 stsp char *ondisk_path;
1894 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1895 b8f41171 2019-02-10 stsp struct stat sb;
1896 9d31a1d8 2018-03-11 stsp
1897 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1899 6353ad76 2019-02-08 stsp
1900 abb4604f 2019-07-27 stsp if (ie) {
1901 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1903 a76c42e6 2019-08-03 stsp goto done;
1904 a76c42e6 2019-08-03 stsp }
1905 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1906 7f91a133 2019-12-13 stsp repo);
1907 abb4604f 2019-07-27 stsp if (err)
1908 abb4604f 2019-07-27 stsp goto done;
1909 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1911 c90c8ce3 2020-07-23 stsp } else {
1912 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1913 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1914 c90c8ce3 2020-07-23 stsp }
1915 6353ad76 2019-02-08 stsp
1916 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1917 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1918 b8f41171 2019-02-10 stsp goto done;
1919 b8f41171 2019-02-10 stsp }
1920 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1921 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1922 5036ab18 2020-04-18 stsp path);
1923 5036ab18 2020-04-18 stsp goto done;
1924 5036ab18 2020-04-18 stsp }
1925 b8f41171 2019-02-10 stsp
1926 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1927 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1928 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1929 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1930 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1931 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1932 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1933 e2b1e152 2019-07-13 stsp if (err)
1934 e2b1e152 2019-07-13 stsp goto done;
1935 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1936 b8f41171 2019-02-10 stsp path);
1937 6353ad76 2019-02-08 stsp goto done;
1938 a378724f 2019-02-10 stsp }
1939 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1940 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1941 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1942 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1943 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1944 b8f41171 2019-02-10 stsp goto done;
1945 e2b1e152 2019-07-13 stsp }
1946 9d31a1d8 2018-03-11 stsp }
1947 9d31a1d8 2018-03-11 stsp
1948 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1949 8da9e5f4 2019-01-12 stsp if (err)
1950 6353ad76 2019-02-08 stsp goto done;
1951 512f0d0e 2019-01-02 stsp
1952 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1953 234035bc 2019-06-01 stsp int update_timestamps;
1954 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1955 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1956 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1957 234035bc 2019-06-01 stsp struct got_object_id id2;
1958 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1959 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1960 234035bc 2019-06-01 stsp if (err)
1961 f69721c3 2019-10-21 stsp goto done;
1962 f69721c3 2019-10-21 stsp }
1963 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1964 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1965 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1966 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1967 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1968 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1969 f69721c3 2019-10-21 stsp goto done;
1970 f69721c3 2019-10-21 stsp }
1971 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1972 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1973 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1974 234035bc 2019-06-01 stsp goto done;
1975 f69721c3 2019-10-21 stsp }
1976 234035bc 2019-06-01 stsp }
1977 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1978 36bf999c 2020-07-23 stsp char *link_target;
1979 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1980 36bf999c 2020-07-23 stsp if (err)
1981 36bf999c 2020-07-23 stsp goto done;
1982 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1983 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1984 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1985 36bf999c 2020-07-23 stsp free(link_target);
1986 993e2a1b 2020-07-23 stsp } else {
1987 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1988 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1989 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1990 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1991 993e2a1b 2020-07-23 stsp }
1992 f69721c3 2019-10-21 stsp free(label_orig);
1993 234035bc 2019-06-01 stsp if (blob2)
1994 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1995 909d120e 2019-09-22 stsp if (err)
1996 909d120e 2019-09-22 stsp goto done;
1997 234035bc 2019-06-01 stsp /*
1998 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1999 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2000 234035bc 2019-06-01 stsp * unmodified files again.
2001 234035bc 2019-06-01 stsp */
2002 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2003 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2004 234035bc 2019-06-01 stsp update_timestamps);
2005 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2006 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2007 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2008 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2009 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2010 1ee397ad 2019-07-12 stsp if (err)
2011 1ee397ad 2019-07-12 stsp goto done;
2012 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2013 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2014 13d9040b 2019-03-27 stsp if (err)
2015 13d9040b 2019-03-27 stsp goto done;
2016 13d9040b 2019-03-27 stsp } else {
2017 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2018 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2019 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2020 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2021 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2022 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2023 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2024 2e1fa222 2020-07-23 stsp } else {
2025 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2026 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2027 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2028 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2029 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2030 2e1fa222 2020-07-23 stsp }
2031 13d9040b 2019-03-27 stsp if (err)
2032 13d9040b 2019-03-27 stsp goto done;
2033 65b05cec 2020-07-23 stsp
2034 054041d0 2020-06-24 stsp if (ie) {
2035 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2036 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2037 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2038 054041d0 2020-06-24 stsp } else {
2039 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2040 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2041 054041d0 2020-06-24 stsp &blob->id);
2042 054041d0 2020-06-24 stsp }
2043 13d9040b 2019-03-27 stsp if (err)
2044 13d9040b 2019-03-27 stsp goto done;
2045 65b05cec 2020-07-23 stsp
2046 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2047 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2048 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2049 2e1fa222 2020-07-23 stsp }
2050 13d9040b 2019-03-27 stsp }
2051 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2052 6353ad76 2019-02-08 stsp done:
2053 6353ad76 2019-02-08 stsp free(ondisk_path);
2054 8da9e5f4 2019-01-12 stsp return err;
2055 90285c3b 2019-01-08 stsp }
2056 90285c3b 2019-01-08 stsp
2057 90285c3b 2019-01-08 stsp static const struct got_error *
2058 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2059 90285c3b 2019-01-08 stsp {
2060 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2061 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2062 90285c3b 2019-01-08 stsp
2063 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2064 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2065 90285c3b 2019-01-08 stsp
2066 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2067 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2068 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2069 c97665ae 2019-01-13 stsp } else {
2070 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2071 fddefe3b 2020-10-20 stsp do {
2072 fddefe3b 2020-10-20 stsp char *parent;
2073 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2074 fddefe3b 2020-10-20 stsp if (err)
2075 2513f20a 2020-10-20 stsp break;
2076 fddefe3b 2020-10-20 stsp free(ondisk_path);
2077 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2078 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2079 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2080 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2081 fddefe3b 2020-10-20 stsp ondisk_path);
2082 90285c3b 2019-01-08 stsp break;
2083 90285c3b 2019-01-08 stsp }
2084 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2085 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2086 90285c3b 2019-01-08 stsp }
2087 90285c3b 2019-01-08 stsp free(ondisk_path);
2088 90285c3b 2019-01-08 stsp return err;
2089 512f0d0e 2019-01-02 stsp }
2090 512f0d0e 2019-01-02 stsp
2091 708d8e67 2019-03-27 stsp static const struct got_error *
2092 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2093 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2094 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2095 708d8e67 2019-03-27 stsp {
2096 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2097 708d8e67 2019-03-27 stsp unsigned char status;
2098 708d8e67 2019-03-27 stsp struct stat sb;
2099 708d8e67 2019-03-27 stsp char *ondisk_path;
2100 708d8e67 2019-03-27 stsp
2101 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2102 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2103 a76c42e6 2019-08-03 stsp
2104 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2105 708d8e67 2019-03-27 stsp == -1)
2106 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2107 708d8e67 2019-03-27 stsp
2108 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2109 708d8e67 2019-03-27 stsp if (err)
2110 5a58a424 2020-06-23 stsp goto done;
2111 708d8e67 2019-03-27 stsp
2112 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2113 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2114 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2115 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2116 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2117 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2118 993e2a1b 2020-07-23 stsp goto done;
2119 993e2a1b 2020-07-23 stsp }
2120 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2121 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2122 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2123 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2124 993e2a1b 2020-07-23 stsp if (err)
2125 993e2a1b 2020-07-23 stsp goto done;
2126 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2127 993e2a1b 2020-07-23 stsp ie->path);
2128 993e2a1b 2020-07-23 stsp goto done;
2129 993e2a1b 2020-07-23 stsp }
2130 993e2a1b 2020-07-23 stsp
2131 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2132 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2133 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2134 1ee397ad 2019-07-12 stsp if (err)
2135 5a58a424 2020-06-23 stsp goto done;
2136 fc6346c4 2019-03-27 stsp /*
2137 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2138 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2139 fc6346c4 2019-03-27 stsp */
2140 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2141 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2142 fc6346c4 2019-03-27 stsp } else {
2143 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2144 1ee397ad 2019-07-12 stsp if (err)
2145 5a58a424 2020-06-23 stsp goto done;
2146 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2147 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2148 fc6346c4 2019-03-27 stsp if (err)
2149 5a58a424 2020-06-23 stsp goto done;
2150 fc6346c4 2019-03-27 stsp }
2151 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2152 708d8e67 2019-03-27 stsp }
2153 5a58a424 2020-06-23 stsp done:
2154 5a58a424 2020-06-23 stsp free(ondisk_path);
2155 fc6346c4 2019-03-27 stsp return err;
2156 708d8e67 2019-03-27 stsp }
2157 708d8e67 2019-03-27 stsp
2158 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2159 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2160 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2161 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2162 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2163 8da9e5f4 2019-01-12 stsp void *progress_arg;
2164 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2165 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2166 8da9e5f4 2019-01-12 stsp };
2167 8da9e5f4 2019-01-12 stsp
2168 512f0d0e 2019-01-02 stsp static const struct got_error *
2169 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2170 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2171 512f0d0e 2019-01-02 stsp {
2172 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2173 0584f854 2019-04-06 stsp
2174 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2176 512f0d0e 2019-01-02 stsp
2177 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2178 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2179 8da9e5f4 2019-01-12 stsp }
2180 512f0d0e 2019-01-02 stsp
2181 8da9e5f4 2019-01-12 stsp static const struct got_error *
2182 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2183 8da9e5f4 2019-01-12 stsp {
2184 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2185 7a9df742 2019-01-08 stsp
2186 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2187 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2188 0584f854 2019-04-06 stsp
2189 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2190 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2191 9d31a1d8 2018-03-11 stsp }
2192 9d31a1d8 2018-03-11 stsp
2193 9d31a1d8 2018-03-11 stsp static const struct got_error *
2194 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2195 9d31a1d8 2018-03-11 stsp {
2196 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2197 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2198 8da9e5f4 2019-01-12 stsp char *path;
2199 9d31a1d8 2018-03-11 stsp
2200 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2201 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2202 0584f854 2019-04-06 stsp
2203 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2204 63c5ca5d 2019-08-24 stsp return NULL;
2205 63c5ca5d 2019-08-24 stsp
2206 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2207 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2208 8da9e5f4 2019-01-12 stsp == -1)
2209 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2210 9d31a1d8 2018-03-11 stsp
2211 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2212 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2213 8da9e5f4 2019-01-12 stsp else
2214 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2215 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2216 9d31a1d8 2018-03-11 stsp
2217 8da9e5f4 2019-01-12 stsp free(path);
2218 0cd1c46a 2019-03-11 stsp return err;
2219 0cd1c46a 2019-03-11 stsp }
2220 0cd1c46a 2019-03-11 stsp
2221 b2118c49 2020-07-28 stsp const struct got_error *
2222 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2223 b2118c49 2020-07-28 stsp {
2224 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2225 b2118c49 2020-07-28 stsp
2226 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2227 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2228 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2229 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2230 b2118c49 2020-07-28 stsp }
2231 b2118c49 2020-07-28 stsp
2232 b2118c49 2020-07-28 stsp return NULL;
2233 b2118c49 2020-07-28 stsp }
2234 b2118c49 2020-07-28 stsp
2235 818c7501 2019-07-11 stsp static const struct got_error *
2236 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2237 0cd1c46a 2019-03-11 stsp {
2238 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2239 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2240 0cd1c46a 2019-03-11 stsp
2241 517bab73 2019-03-11 stsp *refname = NULL;
2242 517bab73 2019-03-11 stsp
2243 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2244 b2118c49 2020-07-28 stsp if (err)
2245 b2118c49 2020-07-28 stsp return err;
2246 0cd1c46a 2019-03-11 stsp
2247 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2248 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2249 0647c563 2019-03-11 stsp *refname = NULL;
2250 0cd1c46a 2019-03-11 stsp }
2251 517bab73 2019-03-11 stsp free(uuidstr);
2252 517bab73 2019-03-11 stsp return err;
2253 517bab73 2019-03-11 stsp }
2254 517bab73 2019-03-11 stsp
2255 818c7501 2019-07-11 stsp const struct got_error *
2256 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2257 818c7501 2019-07-11 stsp {
2258 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2259 818c7501 2019-07-11 stsp }
2260 818c7501 2019-07-11 stsp
2261 818c7501 2019-07-11 stsp static const struct got_error *
2262 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2263 818c7501 2019-07-11 stsp {
2264 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2265 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2266 818c7501 2019-07-11 stsp }
2267 818c7501 2019-07-11 stsp
2268 818c7501 2019-07-11 stsp static const struct got_error *
2269 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2270 818c7501 2019-07-11 stsp {
2271 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2272 818c7501 2019-07-11 stsp }
2273 818c7501 2019-07-11 stsp
2274 818c7501 2019-07-11 stsp static const struct got_error *
2275 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2276 818c7501 2019-07-11 stsp {
2277 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2278 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2279 818c7501 2019-07-11 stsp }
2280 818c7501 2019-07-11 stsp
2281 818c7501 2019-07-11 stsp static const struct got_error *
2282 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 818c7501 2019-07-11 stsp {
2284 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2285 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2286 0ebf8283 2019-07-24 stsp }
2287 0ebf8283 2019-07-24 stsp
2288 0ebf8283 2019-07-24 stsp static const struct got_error *
2289 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2290 0ebf8283 2019-07-24 stsp {
2291 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2292 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2293 0ebf8283 2019-07-24 stsp }
2294 0ebf8283 2019-07-24 stsp
2295 0ebf8283 2019-07-24 stsp static const struct got_error *
2296 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2297 0ebf8283 2019-07-24 stsp {
2298 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2299 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2300 0ebf8283 2019-07-24 stsp }
2301 0ebf8283 2019-07-24 stsp
2302 0ebf8283 2019-07-24 stsp static const struct got_error *
2303 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2304 0ebf8283 2019-07-24 stsp {
2305 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2306 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2307 818c7501 2019-07-11 stsp }
2308 818c7501 2019-07-11 stsp
2309 0ebf8283 2019-07-24 stsp static const struct got_error *
2310 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2311 0ebf8283 2019-07-24 stsp {
2312 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2313 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2314 0ebf8283 2019-07-24 stsp }
2315 818c7501 2019-07-11 stsp
2316 0ebf8283 2019-07-24 stsp const struct got_error *
2317 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2318 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2319 0ebf8283 2019-07-24 stsp {
2320 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2321 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2322 0ebf8283 2019-07-24 stsp *path = NULL;
2323 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2324 0ebf8283 2019-07-24 stsp }
2325 0ebf8283 2019-07-24 stsp return NULL;
2326 0ebf8283 2019-07-24 stsp }
2327 0ebf8283 2019-07-24 stsp
2328 517bab73 2019-03-11 stsp /*
2329 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2330 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2331 517bab73 2019-03-11 stsp */
2332 517bab73 2019-03-11 stsp static const struct got_error *
2333 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2334 517bab73 2019-03-11 stsp {
2335 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2336 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2337 517bab73 2019-03-11 stsp char *refname;
2338 517bab73 2019-03-11 stsp
2339 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2340 517bab73 2019-03-11 stsp if (err)
2341 517bab73 2019-03-11 stsp return err;
2342 0cd1c46a 2019-03-11 stsp
2343 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2344 0cd1c46a 2019-03-11 stsp if (err)
2345 0cd1c46a 2019-03-11 stsp goto done;
2346 0cd1c46a 2019-03-11 stsp
2347 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2348 0cd1c46a 2019-03-11 stsp done:
2349 0cd1c46a 2019-03-11 stsp free(refname);
2350 0cd1c46a 2019-03-11 stsp if (ref)
2351 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2352 3e3a69f1 2019-07-25 stsp return err;
2353 3e3a69f1 2019-07-25 stsp }
2354 3e3a69f1 2019-07-25 stsp
2355 3e3a69f1 2019-07-25 stsp static const struct got_error *
2356 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2357 3e3a69f1 2019-07-25 stsp {
2358 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2359 3e3a69f1 2019-07-25 stsp
2360 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2361 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2362 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2363 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2364 3e3a69f1 2019-07-25 stsp }
2365 9d31a1d8 2018-03-11 stsp return err;
2366 9d31a1d8 2018-03-11 stsp }
2367 9d31a1d8 2018-03-11 stsp
2368 3e3a69f1 2019-07-25 stsp
2369 ebf99748 2019-05-09 stsp static const struct got_error *
2370 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2371 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2372 ebf99748 2019-05-09 stsp {
2373 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2374 ebf99748 2019-05-09 stsp FILE *index = NULL;
2375 0cd1c46a 2019-03-11 stsp
2376 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2377 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2378 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2379 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2380 ebf99748 2019-05-09 stsp
2381 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2382 3e3a69f1 2019-07-25 stsp if (err)
2383 ebf99748 2019-05-09 stsp goto done;
2384 ebf99748 2019-05-09 stsp
2385 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2386 ebf99748 2019-05-09 stsp if (index == NULL) {
2387 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2388 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2389 ebf99748 2019-05-09 stsp } else {
2390 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2391 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2392 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2393 ebf99748 2019-05-09 stsp }
2394 ebf99748 2019-05-09 stsp done:
2395 ebf99748 2019-05-09 stsp if (err) {
2396 ebf99748 2019-05-09 stsp free(*fileindex_path);
2397 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2398 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2399 ebf99748 2019-05-09 stsp *fileindex = NULL;
2400 ebf99748 2019-05-09 stsp }
2401 ebf99748 2019-05-09 stsp return err;
2402 ebf99748 2019-05-09 stsp }
2403 c932eeeb 2019-05-22 stsp
2404 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2405 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2406 c932eeeb 2019-05-22 stsp const char *path;
2407 c932eeeb 2019-05-22 stsp size_t path_len;
2408 c932eeeb 2019-05-22 stsp const char *entry_name;
2409 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2410 a484d721 2019-06-10 stsp void *progress_arg;
2411 c932eeeb 2019-05-22 stsp };
2412 ebf99748 2019-05-09 stsp
2413 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2414 c932eeeb 2019-05-22 stsp static const struct got_error *
2415 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2416 c932eeeb 2019-05-22 stsp {
2417 1ee397ad 2019-07-12 stsp const struct got_error *err;
2418 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2419 c932eeeb 2019-05-22 stsp
2420 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2421 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2422 c932eeeb 2019-05-22 stsp return NULL;
2423 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2424 102ff934 2019-06-11 stsp return NULL;
2425 102ff934 2019-06-11 stsp
2426 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2427 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2428 c932eeeb 2019-05-22 stsp return NULL;
2429 c932eeeb 2019-05-22 stsp
2430 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2431 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2432 edd02c5e 2019-07-11 stsp ie->path);
2433 1ee397ad 2019-07-12 stsp if (err)
2434 1ee397ad 2019-07-12 stsp return err;
2435 1ee397ad 2019-07-12 stsp }
2436 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2437 c932eeeb 2019-05-22 stsp return NULL;
2438 a615e0e7 2020-12-16 stsp }
2439 a615e0e7 2020-12-16 stsp
2440 a615e0e7 2020-12-16 stsp static const struct got_error *
2441 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2442 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2443 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2444 a615e0e7 2020-12-16 stsp {
2445 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2446 a615e0e7 2020-12-16 stsp
2447 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2448 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2449 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2450 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2451 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2452 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2453 a615e0e7 2020-12-16 stsp
2454 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2455 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2456 9c6338c4 2019-06-02 stsp }
2457 9c6338c4 2019-06-02 stsp
2458 9c6338c4 2019-06-02 stsp static const struct got_error *
2459 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2460 9c6338c4 2019-06-02 stsp {
2461 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2462 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2463 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2464 867630bb 2020-01-17 stsp struct timespec timeout;
2465 9c6338c4 2019-06-02 stsp
2466 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2467 9c6338c4 2019-06-02 stsp fileindex_path);
2468 9c6338c4 2019-06-02 stsp if (err)
2469 9c6338c4 2019-06-02 stsp goto done;
2470 9c6338c4 2019-06-02 stsp
2471 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2472 9c6338c4 2019-06-02 stsp if (err)
2473 9c6338c4 2019-06-02 stsp goto done;
2474 9c6338c4 2019-06-02 stsp
2475 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2476 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2477 9c6338c4 2019-06-02 stsp fileindex_path);
2478 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2479 9c6338c4 2019-06-02 stsp }
2480 867630bb 2020-01-17 stsp
2481 867630bb 2020-01-17 stsp /*
2482 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2483 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2484 867630bb 2020-01-17 stsp * was recorded in the file index.
2485 867630bb 2020-01-17 stsp */
2486 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2487 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2488 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2489 9c6338c4 2019-06-02 stsp done:
2490 9c6338c4 2019-06-02 stsp if (new_index)
2491 9c6338c4 2019-06-02 stsp fclose(new_index);
2492 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2493 9c6338c4 2019-06-02 stsp return err;
2494 c932eeeb 2019-05-22 stsp }
2495 6ced4176 2019-07-12 stsp
2496 6ced4176 2019-07-12 stsp static const struct got_error *
2497 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2498 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2499 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2500 6ced4176 2019-07-12 stsp {
2501 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2502 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2503 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2504 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2505 6ced4176 2019-07-12 stsp
2506 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2507 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2508 6ced4176 2019-07-12 stsp *tree_id = NULL;
2509 6ced4176 2019-07-12 stsp
2510 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2511 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2512 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2513 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2514 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2515 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2516 6ced4176 2019-07-12 stsp goto done;
2517 6ced4176 2019-07-12 stsp }
2518 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2519 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2520 6ced4176 2019-07-12 stsp if (err)
2521 6ced4176 2019-07-12 stsp goto done;
2522 6ced4176 2019-07-12 stsp return NULL;
2523 6ced4176 2019-07-12 stsp }
2524 6ced4176 2019-07-12 stsp
2525 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2526 6ced4176 2019-07-12 stsp
2527 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2528 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2529 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2530 6ced4176 2019-07-12 stsp goto done;
2531 6ced4176 2019-07-12 stsp }
2532 6ced4176 2019-07-12 stsp
2533 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2534 6ced4176 2019-07-12 stsp in_repo_path);
2535 6ced4176 2019-07-12 stsp if (err)
2536 6ced4176 2019-07-12 stsp goto done;
2537 6ced4176 2019-07-12 stsp
2538 6ced4176 2019-07-12 stsp free(in_repo_path);
2539 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2540 6ced4176 2019-07-12 stsp
2541 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2542 6ced4176 2019-07-12 stsp if (err)
2543 6ced4176 2019-07-12 stsp goto done;
2544 c932eeeb 2019-05-22 stsp
2545 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2546 6ced4176 2019-07-12 stsp /* Check out a single file. */
2547 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2548 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2549 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2550 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2551 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2552 6ced4176 2019-07-12 stsp goto done;
2553 6ced4176 2019-07-12 stsp }
2554 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2555 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2556 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2557 6ced4176 2019-07-12 stsp goto done;
2558 6ced4176 2019-07-12 stsp }
2559 6ced4176 2019-07-12 stsp } else {
2560 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2561 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2562 6ced4176 2019-07-12 stsp if (err)
2563 6ced4176 2019-07-12 stsp return err;
2564 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2565 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2566 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2567 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2568 6ced4176 2019-07-12 stsp goto done;
2569 6ced4176 2019-07-12 stsp }
2570 6ced4176 2019-07-12 stsp }
2571 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2572 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2573 6ced4176 2019-07-12 stsp } else {
2574 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2575 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2576 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2577 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2578 6ced4176 2019-07-12 stsp goto done;
2579 6ced4176 2019-07-12 stsp }
2580 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2581 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2582 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2583 6ced4176 2019-07-12 stsp goto done;
2584 6ced4176 2019-07-12 stsp }
2585 6ced4176 2019-07-12 stsp }
2586 6ced4176 2019-07-12 stsp done:
2587 6ced4176 2019-07-12 stsp free(id);
2588 6ced4176 2019-07-12 stsp free(in_repo_path);
2589 6ced4176 2019-07-12 stsp if (err) {
2590 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2591 6ced4176 2019-07-12 stsp free(*tree_relpath);
2592 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2593 6ced4176 2019-07-12 stsp free(*tree_id);
2594 6ced4176 2019-07-12 stsp *tree_id = NULL;
2595 6ced4176 2019-07-12 stsp }
2596 07ed472d 2019-07-12 stsp return err;
2597 07ed472d 2019-07-12 stsp }
2598 07ed472d 2019-07-12 stsp
2599 07ed472d 2019-07-12 stsp static const struct got_error *
2600 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2601 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2602 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2603 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2604 07ed472d 2019-07-12 stsp {
2605 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2606 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2607 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2608 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2609 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2610 07ed472d 2019-07-12 stsp
2611 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2612 7f47418f 2019-12-20 stsp if (err) {
2613 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2614 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2615 7f47418f 2019-12-20 stsp goto done;
2616 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2617 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2618 7f47418f 2019-12-20 stsp if (err)
2619 7f47418f 2019-12-20 stsp return err;
2620 7f47418f 2019-12-20 stsp }
2621 07ed472d 2019-07-12 stsp
2622 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2623 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2624 07ed472d 2019-07-12 stsp if (err)
2625 07ed472d 2019-07-12 stsp goto done;
2626 07ed472d 2019-07-12 stsp
2627 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2628 07ed472d 2019-07-12 stsp if (err)
2629 07ed472d 2019-07-12 stsp goto done;
2630 07ed472d 2019-07-12 stsp
2631 07ed472d 2019-07-12 stsp if (entry_name &&
2632 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2633 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2634 07ed472d 2019-07-12 stsp goto done;
2635 07ed472d 2019-07-12 stsp }
2636 07ed472d 2019-07-12 stsp
2637 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2638 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2639 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2640 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2641 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2642 07ed472d 2019-07-12 stsp arg.repo = repo;
2643 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2644 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2645 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2646 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2647 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2648 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2649 07ed472d 2019-07-12 stsp done:
2650 07ed472d 2019-07-12 stsp if (tree)
2651 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2652 07ed472d 2019-07-12 stsp if (commit)
2653 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2654 6ced4176 2019-07-12 stsp return err;
2655 6ced4176 2019-07-12 stsp }
2656 6ced4176 2019-07-12 stsp
2657 9d31a1d8 2018-03-11 stsp const struct got_error *
2658 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2659 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2660 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2661 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2662 9d31a1d8 2018-03-11 stsp {
2663 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2664 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2665 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2666 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2667 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2668 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2669 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2670 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2671 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2672 f2ea84fa 2019-07-27 stsp int entry_type;
2673 f2ea84fa 2019-07-27 stsp char *relpath;
2674 f2ea84fa 2019-07-27 stsp char *entry_name;
2675 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2676 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2677 9d31a1d8 2018-03-11 stsp
2678 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2679 f2ea84fa 2019-07-27 stsp
2680 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2681 9d31a1d8 2018-03-11 stsp if (err)
2682 9d31a1d8 2018-03-11 stsp return err;
2683 93a30277 2018-12-24 stsp
2684 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2685 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2686 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2687 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2688 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2689 f2ea84fa 2019-07-27 stsp goto done;
2690 f2ea84fa 2019-07-27 stsp }
2691 6ced4176 2019-07-12 stsp
2692 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2693 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2694 f2ea84fa 2019-07-27 stsp if (err) {
2695 f2ea84fa 2019-07-27 stsp free(tpd);
2696 6ced4176 2019-07-12 stsp goto done;
2697 6ced4176 2019-07-12 stsp }
2698 f2ea84fa 2019-07-27 stsp
2699 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2700 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2701 f2ea84fa 2019-07-27 stsp if (err) {
2702 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2703 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2704 f2ea84fa 2019-07-27 stsp free(tpd);
2705 f2ea84fa 2019-07-27 stsp goto done;
2706 f2ea84fa 2019-07-27 stsp }
2707 f2ea84fa 2019-07-27 stsp } else
2708 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2709 f2ea84fa 2019-07-27 stsp
2710 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2711 6ced4176 2019-07-12 stsp }
2712 6ced4176 2019-07-12 stsp
2713 51514078 2018-12-25 stsp /*
2714 51514078 2018-12-25 stsp * Read the file index.
2715 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2716 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2717 51514078 2018-12-25 stsp */
2718 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2719 8da9e5f4 2019-01-12 stsp if (err)
2720 c4cdcb68 2019-04-03 stsp goto done;
2721 c4cdcb68 2019-04-03 stsp
2722 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2723 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2724 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2725 f2ea84fa 2019-07-27 stsp
2726 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2727 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2728 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2729 f2ea84fa 2019-07-27 stsp if (err)
2730 f2ea84fa 2019-07-27 stsp break;
2731 f2ea84fa 2019-07-27 stsp
2732 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2733 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2734 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2735 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2736 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2737 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2738 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2739 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2740 f2ea84fa 2019-07-27 stsp if (err)
2741 f2ea84fa 2019-07-27 stsp break;
2742 f2ea84fa 2019-07-27 stsp
2743 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2744 711d9cd9 2019-07-12 stsp }
2745 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2746 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2747 af12c6b9 2019-06-04 stsp err = sync_err;
2748 9d31a1d8 2018-03-11 stsp done:
2749 9c6338c4 2019-06-02 stsp free(fileindex_path);
2750 edfa7d7f 2018-09-11 stsp if (tree)
2751 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2752 9d31a1d8 2018-03-11 stsp if (commit)
2753 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2754 5ade8233 2019-07-12 stsp if (fileindex)
2755 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2756 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2757 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2758 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2759 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2760 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2761 f2ea84fa 2019-07-27 stsp free(tpd);
2762 f2ea84fa 2019-07-27 stsp }
2763 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2764 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2765 9d31a1d8 2018-03-11 stsp err = unlockerr;
2766 f8d1f275 2019-02-04 stsp return err;
2767 f8d1f275 2019-02-04 stsp }
2768 f8d1f275 2019-02-04 stsp
2769 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2770 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2771 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2772 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2773 234035bc 2019-06-01 stsp void *progress_arg;
2774 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2775 234035bc 2019-06-01 stsp void *cancel_arg;
2776 f69721c3 2019-10-21 stsp const char *label_orig;
2777 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2778 234035bc 2019-06-01 stsp };
2779 234035bc 2019-06-01 stsp
2780 234035bc 2019-06-01 stsp static const struct got_error *
2781 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2782 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2783 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2784 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2785 234035bc 2019-06-01 stsp {
2786 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2787 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2788 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2789 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2790 234035bc 2019-06-01 stsp struct stat sb;
2791 234035bc 2019-06-01 stsp unsigned char status;
2792 234035bc 2019-06-01 stsp int local_changes_subsumed;
2793 234035bc 2019-06-01 stsp
2794 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2795 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2796 d6c87207 2019-08-02 stsp strlen(path2));
2797 1ee397ad 2019-07-12 stsp if (ie == NULL)
2798 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2799 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2800 234035bc 2019-06-01 stsp
2801 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2802 234035bc 2019-06-01 stsp path2) == -1)
2803 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2804 234035bc 2019-06-01 stsp
2805 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2806 7f91a133 2019-12-13 stsp repo);
2807 234035bc 2019-06-01 stsp if (err)
2808 234035bc 2019-06-01 stsp goto done;
2809 234035bc 2019-06-01 stsp
2810 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2811 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2812 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2813 234035bc 2019-06-01 stsp goto done;
2814 234035bc 2019-06-01 stsp }
2815 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2816 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2817 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2818 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2819 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2820 234035bc 2019-06-01 stsp goto done;
2821 234035bc 2019-06-01 stsp }
2822 234035bc 2019-06-01 stsp
2823 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2824 36bf999c 2020-07-23 stsp char *link_target2;
2825 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2826 36bf999c 2020-07-23 stsp if (err)
2827 36bf999c 2020-07-23 stsp goto done;
2828 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2829 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2830 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2831 36bf999c 2020-07-23 stsp free(link_target2);
2832 af57b12a 2020-07-23 stsp } else {
2833 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2834 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2835 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2836 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2837 af57b12a 2020-07-23 stsp }
2838 234035bc 2019-06-01 stsp } else if (blob1) {
2839 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2840 d6c87207 2019-08-02 stsp strlen(path1));
2841 1ee397ad 2019-07-12 stsp if (ie == NULL)
2842 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2843 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2844 2b92fad7 2019-06-02 stsp
2845 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2846 2b92fad7 2019-06-02 stsp path1) == -1)
2847 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2848 2b92fad7 2019-06-02 stsp
2849 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2850 7f91a133 2019-12-13 stsp repo);
2851 2b92fad7 2019-06-02 stsp if (err)
2852 2b92fad7 2019-06-02 stsp goto done;
2853 2b92fad7 2019-06-02 stsp
2854 2b92fad7 2019-06-02 stsp switch (status) {
2855 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2856 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2857 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2858 1ee397ad 2019-07-12 stsp if (err)
2859 1ee397ad 2019-07-12 stsp goto done;
2860 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2861 2b92fad7 2019-06-02 stsp if (err)
2862 2b92fad7 2019-06-02 stsp goto done;
2863 2b92fad7 2019-06-02 stsp if (ie)
2864 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2865 2b92fad7 2019-06-02 stsp break;
2866 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2867 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2868 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2869 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2870 1ee397ad 2019-07-12 stsp if (err)
2871 1ee397ad 2019-07-12 stsp goto done;
2872 2b92fad7 2019-06-02 stsp if (ie)
2873 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2874 2b92fad7 2019-06-02 stsp break;
2875 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2876 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2877 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2878 0a22ca1a 2020-09-23 stsp /*
2879 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2880 0a22ca1a 2020-09-23 stsp * exists in the repository.
2881 0a22ca1a 2020-09-23 stsp */
2882 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2883 0a22ca1a 2020-09-23 stsp if (err)
2884 0a22ca1a 2020-09-23 stsp goto done;
2885 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2886 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2887 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2888 0a22ca1a 2020-09-23 stsp if (err)
2889 0a22ca1a 2020-09-23 stsp goto done;
2890 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2891 0a22ca1a 2020-09-23 stsp path1);
2892 0a22ca1a 2020-09-23 stsp if (err)
2893 0a22ca1a 2020-09-23 stsp goto done;
2894 0a22ca1a 2020-09-23 stsp if (ie)
2895 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2896 0a22ca1a 2020-09-23 stsp ie);
2897 0a22ca1a 2020-09-23 stsp } else {
2898 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2899 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2900 0a22ca1a 2020-09-23 stsp }
2901 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2902 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2903 0a22ca1a 2020-09-23 stsp free(id);
2904 0a22ca1a 2020-09-23 stsp if (err)
2905 0a22ca1a 2020-09-23 stsp goto done;
2906 0a22ca1a 2020-09-23 stsp break;
2907 0a22ca1a 2020-09-23 stsp }
2908 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2909 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2910 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2911 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2912 1ee397ad 2019-07-12 stsp if (err)
2913 1ee397ad 2019-07-12 stsp goto done;
2914 2b92fad7 2019-06-02 stsp break;
2915 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2916 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2917 1ee397ad 2019-07-12 stsp if (err)
2918 1ee397ad 2019-07-12 stsp goto done;
2919 2b92fad7 2019-06-02 stsp break;
2920 2b92fad7 2019-06-02 stsp default:
2921 2b92fad7 2019-06-02 stsp break;
2922 2b92fad7 2019-06-02 stsp }
2923 234035bc 2019-06-01 stsp } else if (blob2) {
2924 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2925 234035bc 2019-06-01 stsp path2) == -1)
2926 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2927 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2928 d6c87207 2019-08-02 stsp strlen(path2));
2929 234035bc 2019-06-01 stsp if (ie) {
2930 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2931 7f91a133 2019-12-13 stsp -1, NULL, repo);
2932 234035bc 2019-06-01 stsp if (err)
2933 234035bc 2019-06-01 stsp goto done;
2934 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2935 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2936 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2937 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2938 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2939 1ee397ad 2019-07-12 stsp status, path2);
2940 234035bc 2019-06-01 stsp goto done;
2941 234035bc 2019-06-01 stsp }
2942 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2943 36bf999c 2020-07-23 stsp char *link_target2;
2944 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2945 36bf999c 2020-07-23 stsp blob2);
2946 36bf999c 2020-07-23 stsp if (err)
2947 36bf999c 2020-07-23 stsp goto done;
2948 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2949 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2950 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2951 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2952 36bf999c 2020-07-23 stsp free(link_target2);
2953 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2954 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2955 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2956 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2957 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2958 526a746f 2020-07-23 stsp a->progress_arg);
2959 dfe9fba0 2020-07-23 stsp } else {
2960 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2961 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2962 526a746f 2020-07-23 stsp }
2963 dfe9fba0 2020-07-23 stsp if (err)
2964 dfe9fba0 2020-07-23 stsp goto done;
2965 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2966 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2967 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2968 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2969 234035bc 2019-06-01 stsp if (err)
2970 234035bc 2019-06-01 stsp goto done;
2971 234035bc 2019-06-01 stsp }
2972 234035bc 2019-06-01 stsp } else {
2973 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2974 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2975 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2976 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2977 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2978 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2979 2e1fa222 2020-07-23 stsp } else {
2980 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2981 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2982 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2983 2e1fa222 2020-07-23 stsp }
2984 234035bc 2019-06-01 stsp if (err)
2985 234035bc 2019-06-01 stsp goto done;
2986 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2987 234035bc 2019-06-01 stsp if (err)
2988 234035bc 2019-06-01 stsp goto done;
2989 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2990 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
2991 3969253a 2020-03-07 stsp if (err) {
2992 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2993 3969253a 2020-03-07 stsp goto done;
2994 3969253a 2020-03-07 stsp }
2995 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2996 2b92fad7 2019-06-02 stsp if (err) {
2997 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2998 2b92fad7 2019-06-02 stsp goto done;
2999 2b92fad7 2019-06-02 stsp }
3000 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3001 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3002 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3003 2e1fa222 2020-07-23 stsp }
3004 234035bc 2019-06-01 stsp }
3005 234035bc 2019-06-01 stsp }
3006 234035bc 2019-06-01 stsp done:
3007 234035bc 2019-06-01 stsp free(ondisk_path);
3008 234035bc 2019-06-01 stsp return err;
3009 234035bc 2019-06-01 stsp }
3010 234035bc 2019-06-01 stsp
3011 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3012 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3013 234035bc 2019-06-01 stsp struct got_repository *repo;
3014 234035bc 2019-06-01 stsp };
3015 234035bc 2019-06-01 stsp
3016 234035bc 2019-06-01 stsp static const struct got_error *
3017 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3018 234035bc 2019-06-01 stsp {
3019 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3020 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3021 234035bc 2019-06-01 stsp unsigned char status;
3022 234035bc 2019-06-01 stsp struct stat sb;
3023 234035bc 2019-06-01 stsp char *ondisk_path;
3024 234035bc 2019-06-01 stsp
3025 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3026 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3027 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3028 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3029 234035bc 2019-06-01 stsp
3030 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3031 234035bc 2019-06-01 stsp == -1)
3032 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3033 234035bc 2019-06-01 stsp
3034 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3035 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3036 234035bc 2019-06-01 stsp if (err)
3037 234035bc 2019-06-01 stsp return err;
3038 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3039 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3040 234035bc 2019-06-01 stsp
3041 234035bc 2019-06-01 stsp return NULL;
3042 234035bc 2019-06-01 stsp }
3043 234035bc 2019-06-01 stsp
3044 818c7501 2019-07-11 stsp static const struct got_error *
3045 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3046 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3047 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3048 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3049 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3050 234035bc 2019-06-01 stsp {
3051 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3052 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3053 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3054 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3055 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3056 234035bc 2019-06-01 stsp
3057 03415a1a 2019-06-02 stsp if (commit_id1) {
3058 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3059 03415a1a 2019-06-02 stsp worktree->path_prefix);
3060 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3061 03415a1a 2019-06-02 stsp goto done;
3062 69d57f3d 2020-07-31 stsp }
3063 69d57f3d 2020-07-31 stsp if (tree_id1) {
3064 69d57f3d 2020-07-31 stsp char *id_str;
3065 03415a1a 2019-06-02 stsp
3066 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3067 03415a1a 2019-06-02 stsp if (err)
3068 03415a1a 2019-06-02 stsp goto done;
3069 f69721c3 2019-10-21 stsp
3070 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3071 f69721c3 2019-10-21 stsp if (err)
3072 f69721c3 2019-10-21 stsp goto done;
3073 f69721c3 2019-10-21 stsp
3074 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3075 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3076 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3077 f69721c3 2019-10-21 stsp free(id_str);
3078 f69721c3 2019-10-21 stsp goto done;
3079 f69721c3 2019-10-21 stsp }
3080 f69721c3 2019-10-21 stsp free(id_str);
3081 03415a1a 2019-06-02 stsp }
3082 234035bc 2019-06-01 stsp
3083 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3084 234035bc 2019-06-01 stsp worktree->path_prefix);
3085 234035bc 2019-06-01 stsp if (err)
3086 234035bc 2019-06-01 stsp goto done;
3087 234035bc 2019-06-01 stsp
3088 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3089 234035bc 2019-06-01 stsp if (err)
3090 234035bc 2019-06-01 stsp goto done;
3091 234035bc 2019-06-01 stsp
3092 234035bc 2019-06-01 stsp arg.worktree = worktree;
3093 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3094 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3095 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3096 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3097 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3098 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3099 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3100 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3101 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3102 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3103 af12c6b9 2019-06-04 stsp err = sync_err;
3104 234035bc 2019-06-01 stsp done:
3105 234035bc 2019-06-01 stsp if (tree1)
3106 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3107 234035bc 2019-06-01 stsp if (tree2)
3108 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3109 f69721c3 2019-10-21 stsp free(label_orig);
3110 818c7501 2019-07-11 stsp return err;
3111 818c7501 2019-07-11 stsp }
3112 234035bc 2019-06-01 stsp
3113 818c7501 2019-07-11 stsp const struct got_error *
3114 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3115 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3116 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3117 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3118 818c7501 2019-07-11 stsp {
3119 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3120 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3121 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3122 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3123 818c7501 2019-07-11 stsp
3124 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3125 818c7501 2019-07-11 stsp if (err)
3126 818c7501 2019-07-11 stsp return err;
3127 818c7501 2019-07-11 stsp
3128 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3129 818c7501 2019-07-11 stsp if (err)
3130 818c7501 2019-07-11 stsp goto done;
3131 818c7501 2019-07-11 stsp
3132 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3133 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3134 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3135 818c7501 2019-07-11 stsp &mok_arg);
3136 818c7501 2019-07-11 stsp if (err)
3137 818c7501 2019-07-11 stsp goto done;
3138 818c7501 2019-07-11 stsp
3139 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3140 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3141 818c7501 2019-07-11 stsp done:
3142 818c7501 2019-07-11 stsp if (fileindex)
3143 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3144 818c7501 2019-07-11 stsp free(fileindex_path);
3145 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3146 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3147 234035bc 2019-06-01 stsp err = unlockerr;
3148 234035bc 2019-06-01 stsp return err;
3149 234035bc 2019-06-01 stsp }
3150 234035bc 2019-06-01 stsp
3151 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3152 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3153 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3154 927df6b7 2019-02-10 stsp const char *status_path;
3155 927df6b7 2019-02-10 stsp size_t status_path_len;
3156 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3157 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3158 f8d1f275 2019-02-04 stsp void *status_arg;
3159 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3160 f8d1f275 2019-02-04 stsp void *cancel_arg;
3161 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3162 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3163 f2a9dc41 2019-12-13 tracey int report_unchanged;
3164 3143d852 2020-06-25 stsp int no_ignores;
3165 f8d1f275 2019-02-04 stsp };
3166 88d0e355 2019-08-03 stsp
3167 f8d1f275 2019-02-04 stsp static const struct got_error *
3168 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3169 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3170 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3171 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3172 927df6b7 2019-02-10 stsp {
3173 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3174 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3175 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3176 927df6b7 2019-02-10 stsp struct stat sb;
3177 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3178 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3179 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3180 927df6b7 2019-02-10 stsp
3181 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3182 98eaaa12 2019-08-03 stsp if (err)
3183 98eaaa12 2019-08-03 stsp return err;
3184 98eaaa12 2019-08-03 stsp
3185 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3186 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3187 98eaaa12 2019-08-03 stsp return NULL;
3188 98eaaa12 2019-08-03 stsp
3189 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3190 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3191 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3192 98eaaa12 2019-08-03 stsp }
3193 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3194 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3195 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3196 927df6b7 2019-02-10 stsp }
3197 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3198 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3199 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3200 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3201 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3202 98eaaa12 2019-08-03 stsp }
3203 98eaaa12 2019-08-03 stsp
3204 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3205 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3206 927df6b7 2019-02-10 stsp }
3207 927df6b7 2019-02-10 stsp
3208 927df6b7 2019-02-10 stsp static const struct got_error *
3209 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3210 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3211 f8d1f275 2019-02-04 stsp {
3212 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3213 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3214 f8d1f275 2019-02-04 stsp char *abspath;
3215 f8d1f275 2019-02-04 stsp
3216 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3217 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3218 0584f854 2019-04-06 stsp
3219 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3220 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3221 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3222 927df6b7 2019-02-10 stsp return NULL;
3223 927df6b7 2019-02-10 stsp
3224 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3225 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3226 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3227 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3228 f8d1f275 2019-02-04 stsp } else {
3229 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3230 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3231 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3232 f8d1f275 2019-02-04 stsp }
3233 f8d1f275 2019-02-04 stsp
3234 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3235 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3236 f8d1f275 2019-02-04 stsp free(abspath);
3237 9d31a1d8 2018-03-11 stsp return err;
3238 9d31a1d8 2018-03-11 stsp }
3239 f8d1f275 2019-02-04 stsp
3240 f8d1f275 2019-02-04 stsp static const struct got_error *
3241 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3242 f8d1f275 2019-02-04 stsp {
3243 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3244 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3245 2ec1f75b 2019-03-26 stsp unsigned char status;
3246 927df6b7 2019-02-10 stsp
3247 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3248 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3249 0584f854 2019-04-06 stsp
3250 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3251 927df6b7 2019-02-10 stsp return NULL;
3252 927df6b7 2019-02-10 stsp
3253 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3254 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3255 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3256 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3257 2ec1f75b 2019-03-26 stsp else
3258 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3259 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3260 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3261 6841da00 2019-08-08 stsp }
3262 6841da00 2019-08-08 stsp
3263 6841da00 2019-08-08 stsp void
3264 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3265 6841da00 2019-08-08 stsp {
3266 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3267 6841da00 2019-08-08 stsp
3268 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3269 6841da00 2019-08-08 stsp free((char *)pe->path);
3270 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3271 6841da00 2019-08-08 stsp }
3272 6841da00 2019-08-08 stsp
3273 6841da00 2019-08-08 stsp void
3274 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3275 6841da00 2019-08-08 stsp {
3276 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3277 6841da00 2019-08-08 stsp
3278 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3279 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3280 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3281 6841da00 2019-08-08 stsp free((char *)pe->path);
3282 6841da00 2019-08-08 stsp }
3283 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3284 6841da00 2019-08-08 stsp }
3285 6841da00 2019-08-08 stsp
3286 6841da00 2019-08-08 stsp static const struct got_error *
3287 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3288 6841da00 2019-08-08 stsp {
3289 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3290 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3291 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3292 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3293 6841da00 2019-08-08 stsp size_t linesize = 0;
3294 6841da00 2019-08-08 stsp ssize_t linelen;
3295 6841da00 2019-08-08 stsp
3296 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3297 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3298 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3299 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3300 6841da00 2019-08-08 stsp
3301 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3302 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3303 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3304 bd8de430 2019-10-04 stsp
3305 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3306 bd8de430 2019-10-04 stsp if (line[0] == '#')
3307 bd8de430 2019-10-04 stsp continue;
3308 bd8de430 2019-10-04 stsp
3309 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3310 bd8de430 2019-10-04 stsp if (line[0] == '!')
3311 bd8de430 2019-10-04 stsp continue;
3312 bd8de430 2019-10-04 stsp
3313 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3314 6841da00 2019-08-08 stsp line) == -1) {
3315 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3316 6841da00 2019-08-08 stsp goto done;
3317 6841da00 2019-08-08 stsp }
3318 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3319 6841da00 2019-08-08 stsp if (err)
3320 6841da00 2019-08-08 stsp goto done;
3321 6841da00 2019-08-08 stsp }
3322 6841da00 2019-08-08 stsp if (ferror(f)) {
3323 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3324 6841da00 2019-08-08 stsp goto done;
3325 6841da00 2019-08-08 stsp }
3326 6841da00 2019-08-08 stsp
3327 6841da00 2019-08-08 stsp dirpath = strdup(path);
3328 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3329 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3330 6841da00 2019-08-08 stsp goto done;
3331 6841da00 2019-08-08 stsp }
3332 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3333 6841da00 2019-08-08 stsp done:
3334 6841da00 2019-08-08 stsp free(line);
3335 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3336 6841da00 2019-08-08 stsp free(dirpath);
3337 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3338 6841da00 2019-08-08 stsp }
3339 6841da00 2019-08-08 stsp return err;
3340 6841da00 2019-08-08 stsp }
3341 6841da00 2019-08-08 stsp
3342 6841da00 2019-08-08 stsp int
3343 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3344 6841da00 2019-08-08 stsp {
3345 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3346 bd8de430 2019-10-04 stsp
3347 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3348 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3349 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3350 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3351 bd8de430 2019-10-04 stsp
3352 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3353 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3354 6841da00 2019-08-08 stsp
3355 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3356 bd8de430 2019-10-04 stsp continue;
3357 bd8de430 2019-10-04 stsp pattern += 3;
3358 bd8de430 2019-10-04 stsp p = path;
3359 bd8de430 2019-10-04 stsp while (*p) {
3360 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3361 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3362 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3363 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3364 bd8de430 2019-10-04 stsp p++;
3365 bd8de430 2019-10-04 stsp while (*p == '/')
3366 bd8de430 2019-10-04 stsp p++;
3367 bd8de430 2019-10-04 stsp continue;
3368 bd8de430 2019-10-04 stsp }
3369 bd8de430 2019-10-04 stsp return 1;
3370 bd8de430 2019-10-04 stsp }
3371 bd8de430 2019-10-04 stsp }
3372 bd8de430 2019-10-04 stsp }
3373 bd8de430 2019-10-04 stsp
3374 6841da00 2019-08-08 stsp /*
3375 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3376 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3377 6841da00 2019-08-08 stsp * ignores backwards.
3378 6841da00 2019-08-08 stsp */
3379 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3380 6841da00 2019-08-08 stsp while (pe) {
3381 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3382 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3383 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3384 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3385 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3386 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3387 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3388 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3389 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3390 6841da00 2019-08-08 stsp continue;
3391 6841da00 2019-08-08 stsp return 1;
3392 6841da00 2019-08-08 stsp }
3393 6841da00 2019-08-08 stsp }
3394 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3395 6841da00 2019-08-08 stsp }
3396 6841da00 2019-08-08 stsp
3397 6841da00 2019-08-08 stsp return 0;
3398 6841da00 2019-08-08 stsp }
3399 6841da00 2019-08-08 stsp
3400 6841da00 2019-08-08 stsp static const struct got_error *
3401 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3402 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3403 6841da00 2019-08-08 stsp {
3404 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3405 6841da00 2019-08-08 stsp char *ignorespath;
3406 886cec17 2019-12-15 stsp int fd = -1;
3407 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3408 6841da00 2019-08-08 stsp
3409 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3410 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3411 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3412 6841da00 2019-08-08 stsp
3413 886cec17 2019-12-15 stsp if (dirfd != -1) {
3414 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3415 886cec17 2019-12-15 stsp if (fd == -1) {
3416 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3417 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3418 886cec17 2019-12-15 stsp ignorespath);
3419 886cec17 2019-12-15 stsp } else {
3420 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3421 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3422 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3423 886cec17 2019-12-15 stsp ignorespath);
3424 886cec17 2019-12-15 stsp else {
3425 886cec17 2019-12-15 stsp fd = -1;
3426 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3427 886cec17 2019-12-15 stsp }
3428 886cec17 2019-12-15 stsp }
3429 886cec17 2019-12-15 stsp } else {
3430 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3431 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3432 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3433 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3434 886cec17 2019-12-15 stsp ignorespath);
3435 886cec17 2019-12-15 stsp } else
3436 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3437 886cec17 2019-12-15 stsp }
3438 6841da00 2019-08-08 stsp
3439 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3440 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3441 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3442 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3443 6841da00 2019-08-08 stsp free(ignorespath);
3444 6841da00 2019-08-08 stsp return err;
3445 f8d1f275 2019-02-04 stsp }
3446 f8d1f275 2019-02-04 stsp
3447 f8d1f275 2019-02-04 stsp static const struct got_error *
3448 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3449 f8d1f275 2019-02-04 stsp {
3450 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3451 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3452 f8d1f275 2019-02-04 stsp char *path = NULL;
3453 f8d1f275 2019-02-04 stsp
3454 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3455 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3456 0584f854 2019-04-06 stsp
3457 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3458 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3459 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3460 f8d1f275 2019-02-04 stsp } else {
3461 f8d1f275 2019-02-04 stsp path = de->d_name;
3462 f8d1f275 2019-02-04 stsp }
3463 f8d1f275 2019-02-04 stsp
3464 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3465 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3466 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3467 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3468 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3469 f8d1f275 2019-02-04 stsp if (parent_path[0])
3470 f8d1f275 2019-02-04 stsp free(path);
3471 b72f483a 2019-02-05 stsp return err;
3472 f8d1f275 2019-02-04 stsp }
3473 f8d1f275 2019-02-04 stsp
3474 347d1d3e 2019-07-12 stsp static const struct got_error *
3475 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3476 3143d852 2020-06-25 stsp {
3477 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3478 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3479 3143d852 2020-06-25 stsp
3480 3143d852 2020-06-25 stsp if (a->no_ignores)
3481 3143d852 2020-06-25 stsp return NULL;
3482 3143d852 2020-06-25 stsp
3483 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3484 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3485 3143d852 2020-06-25 stsp if (err)
3486 3143d852 2020-06-25 stsp return err;
3487 3143d852 2020-06-25 stsp
3488 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3489 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3490 3143d852 2020-06-25 stsp
3491 3143d852 2020-06-25 stsp return err;
3492 3143d852 2020-06-25 stsp }
3493 3143d852 2020-06-25 stsp
3494 3143d852 2020-06-25 stsp static const struct got_error *
3495 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3496 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3497 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3498 abb4604f 2019-07-27 stsp {
3499 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3500 abb4604f 2019-07-27 stsp struct stat sb;
3501 abb4604f 2019-07-27 stsp
3502 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3503 abb4604f 2019-07-27 stsp if (ie)
3504 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3505 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3506 abb4604f 2019-07-27 stsp
3507 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3508 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3509 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3510 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3511 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3512 abb4604f 2019-07-27 stsp return NULL;
3513 abb4604f 2019-07-27 stsp }
3514 abb4604f 2019-07-27 stsp
3515 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3516 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3517 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3518 abb4604f 2019-07-27 stsp
3519 abb4604f 2019-07-27 stsp return NULL;
3520 3143d852 2020-06-25 stsp }
3521 3143d852 2020-06-25 stsp
3522 3143d852 2020-06-25 stsp static const struct got_error *
3523 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3524 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3525 3143d852 2020-06-25 stsp {
3526 3143d852 2020-06-25 stsp const struct got_error *err;
3527 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3528 3143d852 2020-06-25 stsp
3529 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3530 3143d852 2020-06-25 stsp ".cvsignore");
3531 3143d852 2020-06-25 stsp if (err)
3532 3143d852 2020-06-25 stsp return err;
3533 3143d852 2020-06-25 stsp
3534 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3535 3143d852 2020-06-25 stsp ".gitignore");
3536 3143d852 2020-06-25 stsp if (err)
3537 3143d852 2020-06-25 stsp return err;
3538 3143d852 2020-06-25 stsp
3539 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3540 3143d852 2020-06-25 stsp if (err) {
3541 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3542 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3543 3143d852 2020-06-25 stsp return err;
3544 3143d852 2020-06-25 stsp }
3545 3143d852 2020-06-25 stsp for (;;) {
3546 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3547 3143d852 2020-06-25 stsp ".cvsignore");
3548 3143d852 2020-06-25 stsp if (err)
3549 3143d852 2020-06-25 stsp break;
3550 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3551 3143d852 2020-06-25 stsp ".gitignore");
3552 3143d852 2020-06-25 stsp if (err)
3553 3143d852 2020-06-25 stsp break;
3554 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3555 3143d852 2020-06-25 stsp if (err) {
3556 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3557 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3558 3143d852 2020-06-25 stsp break;
3559 3143d852 2020-06-25 stsp }
3560 b737c85a 2020-06-26 stsp free(parent_path);
3561 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3562 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3563 3143d852 2020-06-25 stsp }
3564 3143d852 2020-06-25 stsp
3565 b737c85a 2020-06-26 stsp free(parent_path);
3566 b737c85a 2020-06-26 stsp free(next_parent_path);
3567 3143d852 2020-06-25 stsp return err;
3568 abb4604f 2019-07-27 stsp }
3569 abb4604f 2019-07-27 stsp
3570 abb4604f 2019-07-27 stsp static const struct got_error *
3571 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3572 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3573 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3574 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3575 f2a9dc41 2019-12-13 tracey int report_unchanged)
3576 f8d1f275 2019-02-04 stsp {
3577 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3578 6fc93f37 2019-12-13 stsp int fd = -1;
3579 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3580 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3581 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3582 3143d852 2020-06-25 stsp
3583 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3584 f8d1f275 2019-02-04 stsp
3585 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3586 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3587 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3588 8dc303cc 2019-07-27 stsp
3589 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3590 6fc93f37 2019-12-13 stsp if (fd == -1) {
3591 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3592 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3593 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3594 abb4604f 2019-07-27 stsp else
3595 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3596 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3597 f2a9dc41 2019-12-13 tracey report_unchanged);
3598 abb4604f 2019-07-27 stsp } else {
3599 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3600 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3601 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3602 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3603 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3604 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3605 abb4604f 2019-07-27 stsp arg.status_path = path;
3606 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3607 abb4604f 2019-07-27 stsp arg.repo = repo;
3608 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3609 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3610 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3611 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3612 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3613 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3614 022fae89 2019-12-06 tracey if (!no_ignores) {
3615 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3616 3143d852 2020-06-25 stsp worktree->root_path, path);
3617 3143d852 2020-06-25 stsp if (err)
3618 3143d852 2020-06-25 stsp goto done;
3619 022fae89 2019-12-06 tracey }
3620 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3621 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3622 927df6b7 2019-02-10 stsp }
3623 3143d852 2020-06-25 stsp done:
3624 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3625 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3626 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3627 927df6b7 2019-02-10 stsp free(ondisk_path);
3628 347d1d3e 2019-07-12 stsp return err;
3629 347d1d3e 2019-07-12 stsp }
3630 347d1d3e 2019-07-12 stsp
3631 347d1d3e 2019-07-12 stsp const struct got_error *
3632 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3633 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3634 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3635 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3636 347d1d3e 2019-07-12 stsp {
3637 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3638 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3639 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3640 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3641 347d1d3e 2019-07-12 stsp
3642 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3643 347d1d3e 2019-07-12 stsp if (err)
3644 347d1d3e 2019-07-12 stsp return err;
3645 347d1d3e 2019-07-12 stsp
3646 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3647 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3648 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3649 72ea6654 2019-07-27 stsp if (err)
3650 72ea6654 2019-07-27 stsp break;
3651 72ea6654 2019-07-27 stsp }
3652 f8d1f275 2019-02-04 stsp free(fileindex_path);
3653 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3654 f8d1f275 2019-02-04 stsp return err;
3655 f8d1f275 2019-02-04 stsp }
3656 6c7ab921 2019-03-18 stsp
3657 6c7ab921 2019-03-18 stsp const struct got_error *
3658 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3659 6c7ab921 2019-03-18 stsp const char *arg)
3660 6c7ab921 2019-03-18 stsp {
3661 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3662 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3663 6c7ab921 2019-03-18 stsp size_t len;
3664 00bb5ea0 2020-07-23 stsp struct stat sb;
3665 01740607 2020-11-04 stsp char *abspath = NULL;
3666 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3667 6c7ab921 2019-03-18 stsp
3668 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3669 6c7ab921 2019-03-18 stsp
3670 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3671 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3672 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3673 00bb5ea0 2020-07-23 stsp
3674 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3675 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3676 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3677 00bb5ea0 2020-07-23 stsp goto done;
3678 00bb5ea0 2020-07-23 stsp }
3679 727173c3 2020-11-06 stsp sb.st_mode = 0;
3680 00bb5ea0 2020-07-23 stsp }
3681 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3682 00bb5ea0 2020-07-23 stsp /*
3683 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3684 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3685 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3686 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3687 00bb5ea0 2020-07-23 stsp */
3688 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3689 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3690 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3691 00bb5ea0 2020-07-23 stsp goto done;
3692 00bb5ea0 2020-07-23 stsp }
3693 00bb5ea0 2020-07-23 stsp
3694 00bb5ea0 2020-07-23 stsp }
3695 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3696 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3697 00bb5ea0 2020-07-23 stsp if (err)
3698 d0710d08 2019-07-22 stsp goto done;
3699 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3700 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3701 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3702 00bb5ea0 2020-07-23 stsp goto done;
3703 00bb5ea0 2020-07-23 stsp }
3704 00bb5ea0 2020-07-23 stsp } else {
3705 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3706 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3707 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3708 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3709 00bb5ea0 2020-07-23 stsp goto done;
3710 00bb5ea0 2020-07-23 stsp }
3711 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3712 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3713 01740607 2020-11-04 stsp goto done;
3714 01740607 2020-11-04 stsp }
3715 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3716 01740607 2020-11-04 stsp sizeof(canonpath));
3717 01740607 2020-11-04 stsp if (err)
3718 00bb5ea0 2020-07-23 stsp goto done;
3719 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3720 01740607 2020-11-04 stsp if (resolved == NULL) {
3721 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3722 01740607 2020-11-04 stsp goto done;
3723 00bb5ea0 2020-07-23 stsp }
3724 d0710d08 2019-07-22 stsp }
3725 d0710d08 2019-07-22 stsp }
3726 6c7ab921 2019-03-18 stsp
3727 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3728 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3729 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3730 6c7ab921 2019-03-18 stsp goto done;
3731 6c7ab921 2019-03-18 stsp }
3732 6c7ab921 2019-03-18 stsp
3733 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3734 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3735 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3736 f6d88e1a 2019-05-29 stsp if (err)
3737 f6d88e1a 2019-05-29 stsp goto done;
3738 f6d88e1a 2019-05-29 stsp } else {
3739 f6d88e1a 2019-05-29 stsp path = strdup("");
3740 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3741 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3742 f6d88e1a 2019-05-29 stsp goto done;
3743 f6d88e1a 2019-05-29 stsp }
3744 6c7ab921 2019-03-18 stsp }
3745 6c7ab921 2019-03-18 stsp
3746 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3747 6c7ab921 2019-03-18 stsp len = strlen(path);
3748 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3749 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3750 6c7ab921 2019-03-18 stsp len--;
3751 6c7ab921 2019-03-18 stsp }
3752 6c7ab921 2019-03-18 stsp done:
3753 01740607 2020-11-04 stsp free(abspath);
3754 6c7ab921 2019-03-18 stsp free(resolved);
3755 d0710d08 2019-07-22 stsp free(cwd);
3756 6c7ab921 2019-03-18 stsp if (err == NULL)
3757 6c7ab921 2019-03-18 stsp *wt_path = path;
3758 6c7ab921 2019-03-18 stsp else
3759 6c7ab921 2019-03-18 stsp free(path);
3760 d00136be 2019-03-26 stsp return err;
3761 d00136be 2019-03-26 stsp }
3762 d00136be 2019-03-26 stsp
3763 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3764 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3765 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3766 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3767 4e68cba3 2019-11-23 stsp void *progress_arg;
3768 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3769 4e68cba3 2019-11-23 stsp };
3770 4e68cba3 2019-11-23 stsp
3771 4e68cba3 2019-11-23 stsp static const struct got_error *
3772 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3773 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3774 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3775 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3776 07f5b47a 2019-06-02 stsp {
3777 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3778 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3779 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3780 6d022e97 2019-08-04 stsp struct stat sb;
3781 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3782 07f5b47a 2019-06-02 stsp
3783 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3784 dbb83fbd 2019-12-12 stsp relpath) == -1)
3785 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3786 dbb83fbd 2019-12-12 stsp
3787 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3788 6d022e97 2019-08-04 stsp if (ie) {
3789 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3790 12463d8b 2019-12-13 stsp de_name, a->repo);
3791 6d022e97 2019-08-04 stsp if (err)
3792 dbb83fbd 2019-12-12 stsp goto done;
3793 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3794 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3795 dbb83fbd 2019-12-12 stsp goto done;
3796 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3797 dbb83fbd 2019-12-12 stsp if (err)
3798 dbb83fbd 2019-12-12 stsp goto done;
3799 6d022e97 2019-08-04 stsp }
3800 07f5b47a 2019-06-02 stsp
3801 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3802 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3803 dbb83fbd 2019-12-12 stsp goto done;
3804 4e68cba3 2019-11-23 stsp }
3805 07f5b47a 2019-06-02 stsp
3806 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3807 4e68cba3 2019-11-23 stsp if (err)
3808 4e68cba3 2019-11-23 stsp goto done;
3809 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3810 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3811 3969253a 2020-03-07 stsp if (err) {
3812 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3813 3969253a 2020-03-07 stsp goto done;
3814 3969253a 2020-03-07 stsp }
3815 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3816 07f5b47a 2019-06-02 stsp if (err) {
3817 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3818 4e68cba3 2019-11-23 stsp goto done;
3819 07f5b47a 2019-06-02 stsp }
3820 4e68cba3 2019-11-23 stsp done:
3821 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3822 dbb83fbd 2019-12-12 stsp if (err)
3823 dbb83fbd 2019-12-12 stsp return err;
3824 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3825 dbb83fbd 2019-12-12 stsp return NULL;
3826 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3827 07f5b47a 2019-06-02 stsp }
3828 07f5b47a 2019-06-02 stsp
3829 d00136be 2019-03-26 stsp const struct got_error *
3830 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3831 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3832 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3833 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3834 d00136be 2019-03-26 stsp {
3835 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3836 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3837 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3838 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3839 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3840 d00136be 2019-03-26 stsp
3841 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3842 d00136be 2019-03-26 stsp if (err)
3843 d00136be 2019-03-26 stsp return err;
3844 d00136be 2019-03-26 stsp
3845 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3846 d00136be 2019-03-26 stsp if (err)
3847 d00136be 2019-03-26 stsp goto done;
3848 d00136be 2019-03-26 stsp
3849 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3850 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3851 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3852 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3853 4e68cba3 2019-11-23 stsp saa.repo = repo;
3854 4e68cba3 2019-11-23 stsp
3855 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3856 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3857 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3858 1dd54920 2019-05-11 stsp if (err)
3859 af12c6b9 2019-06-04 stsp break;
3860 1dd54920 2019-05-11 stsp }
3861 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3862 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3863 af12c6b9 2019-06-04 stsp err = sync_err;
3864 d00136be 2019-03-26 stsp done:
3865 fb399478 2019-07-12 stsp free(fileindex_path);
3866 d00136be 2019-03-26 stsp if (fileindex)
3867 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3868 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3869 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3870 d00136be 2019-03-26 stsp err = unlockerr;
3871 6c7ab921 2019-03-18 stsp return err;
3872 6c7ab921 2019-03-18 stsp }
3873 17ed4618 2019-06-02 stsp
3874 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3875 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3876 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3877 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3878 f2a9dc41 2019-12-13 tracey void *progress_arg;
3879 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3880 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3881 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3882 766841c2 2020-08-13 stsp const char *status_codes;
3883 f2a9dc41 2019-12-13 tracey };
3884 f2a9dc41 2019-12-13 tracey
3885 f2a9dc41 2019-12-13 tracey static const struct got_error *
3886 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3887 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3888 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3889 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3890 17ed4618 2019-06-02 stsp {
3891 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3892 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3893 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3894 17ed4618 2019-06-02 stsp struct stat sb;
3895 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3896 17ed4618 2019-06-02 stsp
3897 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3898 17ed4618 2019-06-02 stsp if (ie == NULL)
3899 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3900 2ec1f75b 2019-03-26 stsp
3901 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3902 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3903 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3904 9acbc4fa 2019-08-03 stsp return NULL;
3905 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3906 9acbc4fa 2019-08-03 stsp }
3907 9acbc4fa 2019-08-03 stsp
3908 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3909 f2a9dc41 2019-12-13 tracey relpath) == -1)
3910 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3911 f2a9dc41 2019-12-13 tracey
3912 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3913 12463d8b 2019-12-13 stsp a->repo);
3914 17ed4618 2019-06-02 stsp if (err)
3915 f2a9dc41 2019-12-13 tracey goto done;
3916 17ed4618 2019-06-02 stsp
3917 766841c2 2020-08-13 stsp if (a->status_codes) {
3918 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3919 766841c2 2020-08-13 stsp int i;
3920 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3921 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3922 766841c2 2020-08-13 stsp break;
3923 766841c2 2020-08-13 stsp }
3924 766841c2 2020-08-13 stsp if (i == ncodes) {
3925 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3926 766841c2 2020-08-13 stsp free(ondisk_path);
3927 766841c2 2020-08-13 stsp return NULL;
3928 766841c2 2020-08-13 stsp }
3929 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3930 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3931 766841c2 2020-08-13 stsp static char msg[64];
3932 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3933 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3934 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3935 766841c2 2020-08-13 stsp goto done;
3936 766841c2 2020-08-13 stsp }
3937 766841c2 2020-08-13 stsp }
3938 766841c2 2020-08-13 stsp
3939 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3940 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3941 f2a9dc41 2019-12-13 tracey goto done;
3942 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3943 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3944 f2a9dc41 2019-12-13 tracey goto done;
3945 f2a9dc41 2019-12-13 tracey }
3946 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3947 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3948 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3949 f2a9dc41 2019-12-13 tracey goto done;
3950 f2a9dc41 2019-12-13 tracey }
3951 17ed4618 2019-06-02 stsp }
3952 17ed4618 2019-06-02 stsp
3953 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3954 20a2ad1c 2020-10-20 stsp size_t root_len;
3955 20a2ad1c 2020-10-20 stsp
3956 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3957 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3958 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3959 12463d8b 2019-12-13 stsp ondisk_path);
3960 12463d8b 2019-12-13 stsp goto done;
3961 12463d8b 2019-12-13 stsp }
3962 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3963 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3964 12463d8b 2019-12-13 stsp goto done;
3965 15341bfd 2020-03-05 tracey }
3966 15341bfd 2020-03-05 tracey
3967 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3968 20a2ad1c 2020-10-20 stsp do {
3969 20a2ad1c 2020-10-20 stsp char *parent;
3970 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3971 20a2ad1c 2020-10-20 stsp if (err)
3972 2513f20a 2020-10-20 stsp goto done;
3973 20a2ad1c 2020-10-20 stsp free(ondisk_path);
3974 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
3975 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
3976 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3977 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3978 20a2ad1c 2020-10-20 stsp ondisk_path);
3979 15341bfd 2020-03-05 tracey break;
3980 15341bfd 2020-03-05 tracey }
3981 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3982 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
3983 f2a9dc41 2019-12-13 tracey }
3984 17ed4618 2019-06-02 stsp
3985 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3986 f2a9dc41 2019-12-13 tracey done:
3987 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3988 f2a9dc41 2019-12-13 tracey if (err)
3989 f2a9dc41 2019-12-13 tracey return err;
3990 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3991 f2a9dc41 2019-12-13 tracey return NULL;
3992 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3993 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3994 17ed4618 2019-06-02 stsp }
3995 17ed4618 2019-06-02 stsp
3996 2ec1f75b 2019-03-26 stsp const struct got_error *
3997 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3998 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3999 766841c2 2020-08-13 stsp const char *status_codes,
4000 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4001 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4002 2ec1f75b 2019-03-26 stsp {
4003 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4004 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4005 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4006 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4007 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4008 2ec1f75b 2019-03-26 stsp
4009 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4010 2ec1f75b 2019-03-26 stsp if (err)
4011 2ec1f75b 2019-03-26 stsp return err;
4012 2ec1f75b 2019-03-26 stsp
4013 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4014 2ec1f75b 2019-03-26 stsp if (err)
4015 2ec1f75b 2019-03-26 stsp goto done;
4016 f2a9dc41 2019-12-13 tracey
4017 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4018 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4019 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4020 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4021 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4022 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4023 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4024 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4025 2ec1f75b 2019-03-26 stsp
4026 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4027 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4028 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4029 17ed4618 2019-06-02 stsp if (err)
4030 af12c6b9 2019-06-04 stsp break;
4031 2ec1f75b 2019-03-26 stsp }
4032 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4033 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4034 af12c6b9 2019-06-04 stsp err = sync_err;
4035 2ec1f75b 2019-03-26 stsp done:
4036 fb399478 2019-07-12 stsp free(fileindex_path);
4037 2ec1f75b 2019-03-26 stsp if (fileindex)
4038 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4039 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4040 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4041 2ec1f75b 2019-03-26 stsp err = unlockerr;
4042 33aa809d 2019-08-08 stsp return err;
4043 33aa809d 2019-08-08 stsp }
4044 33aa809d 2019-08-08 stsp
4045 33aa809d 2019-08-08 stsp static const struct got_error *
4046 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4047 33aa809d 2019-08-08 stsp {
4048 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4049 33aa809d 2019-08-08 stsp char *line = NULL;
4050 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4051 33aa809d 2019-08-08 stsp ssize_t linelen;
4052 33aa809d 2019-08-08 stsp
4053 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4054 33aa809d 2019-08-08 stsp if (linelen == -1) {
4055 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4056 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4057 33aa809d 2019-08-08 stsp goto done;
4058 33aa809d 2019-08-08 stsp }
4059 33aa809d 2019-08-08 stsp return NULL;
4060 33aa809d 2019-08-08 stsp }
4061 33aa809d 2019-08-08 stsp if (outfile) {
4062 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4063 33aa809d 2019-08-08 stsp if (n != linelen) {
4064 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4065 33aa809d 2019-08-08 stsp goto done;
4066 33aa809d 2019-08-08 stsp }
4067 33aa809d 2019-08-08 stsp }
4068 33aa809d 2019-08-08 stsp if (rejectfile) {
4069 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4070 33aa809d 2019-08-08 stsp if (n != linelen)
4071 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4072 33aa809d 2019-08-08 stsp }
4073 33aa809d 2019-08-08 stsp done:
4074 33aa809d 2019-08-08 stsp free(line);
4075 2ec1f75b 2019-03-26 stsp return err;
4076 2ec1f75b 2019-03-26 stsp }
4077 1f1abb7e 2019-08-08 stsp
4078 33aa809d 2019-08-08 stsp static const struct got_error *
4079 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4080 33aa809d 2019-08-08 stsp {
4081 33aa809d 2019-08-08 stsp char *line = NULL;
4082 33aa809d 2019-08-08 stsp size_t linesize = 0;
4083 33aa809d 2019-08-08 stsp ssize_t linelen;
4084 33aa809d 2019-08-08 stsp
4085 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4086 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4087 500467ff 2019-09-25 hiltjo if (ferror(f))
4088 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4089 500467ff 2019-09-25 hiltjo return NULL;
4090 500467ff 2019-09-25 hiltjo }
4091 33aa809d 2019-08-08 stsp free(line);
4092 33aa809d 2019-08-08 stsp return NULL;
4093 33aa809d 2019-08-08 stsp }
4094 33aa809d 2019-08-08 stsp
4095 33aa809d 2019-08-08 stsp static const struct got_error *
4096 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4097 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4098 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4099 33aa809d 2019-08-08 stsp {
4100 33aa809d 2019-08-08 stsp const struct got_error *err;
4101 33aa809d 2019-08-08 stsp
4102 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4103 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4104 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4105 33aa809d 2019-08-08 stsp if (err)
4106 33aa809d 2019-08-08 stsp return err;
4107 33aa809d 2019-08-08 stsp (*line_cur1)++;
4108 33aa809d 2019-08-08 stsp }
4109 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4110 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4111 33aa809d 2019-08-08 stsp if (rejectfile)
4112 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4113 33aa809d 2019-08-08 stsp else
4114 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4115 33aa809d 2019-08-08 stsp if (err)
4116 33aa809d 2019-08-08 stsp return err;
4117 33aa809d 2019-08-08 stsp (*line_cur2)++;
4118 33aa809d 2019-08-08 stsp }
4119 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4120 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4121 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4122 33aa809d 2019-08-08 stsp if (err)
4123 33aa809d 2019-08-08 stsp return err;
4124 33aa809d 2019-08-08 stsp (*line_cur2)++;
4125 33aa809d 2019-08-08 stsp }
4126 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4127 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4128 33aa809d 2019-08-08 stsp if (rejectfile)
4129 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4130 33aa809d 2019-08-08 stsp else
4131 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4132 33aa809d 2019-08-08 stsp if (err)
4133 33aa809d 2019-08-08 stsp return err;
4134 33aa809d 2019-08-08 stsp (*line_cur1)++;
4135 33aa809d 2019-08-08 stsp }
4136 f1e81a05 2019-08-10 stsp
4137 f1e81a05 2019-08-10 stsp return NULL;
4138 f1e81a05 2019-08-10 stsp }
4139 f1e81a05 2019-08-10 stsp
4140 f1e81a05 2019-08-10 stsp static const struct got_error *
4141 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4142 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4143 f1e81a05 2019-08-10 stsp {
4144 f1e81a05 2019-08-10 stsp const struct got_error *err;
4145 f1e81a05 2019-08-10 stsp
4146 f1e81a05 2019-08-10 stsp if (outfile) {
4147 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4148 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4149 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4150 f1e81a05 2019-08-10 stsp if (err)
4151 f1e81a05 2019-08-10 stsp return err;
4152 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4153 f1e81a05 2019-08-10 stsp }
4154 f1e81a05 2019-08-10 stsp }
4155 f1e81a05 2019-08-10 stsp if (rejectfile) {
4156 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4157 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4158 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4159 f1e81a05 2019-08-10 stsp if (err)
4160 f1e81a05 2019-08-10 stsp return err;
4161 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4162 f1e81a05 2019-08-10 stsp }
4163 33aa809d 2019-08-08 stsp }
4164 33aa809d 2019-08-08 stsp
4165 33aa809d 2019-08-08 stsp return NULL;
4166 33aa809d 2019-08-08 stsp }
4167 33aa809d 2019-08-08 stsp
4168 33aa809d 2019-08-08 stsp static const struct got_error *
4169 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4170 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4171 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4172 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4173 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4174 33aa809d 2019-08-08 stsp {
4175 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4176 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4177 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4178 33aa809d 2019-08-08 stsp FILE *hunkfile;
4179 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4180 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4181 fe621944 2020-11-10 stsp int rc;
4182 33aa809d 2019-08-08 stsp
4183 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4184 33aa809d 2019-08-08 stsp
4185 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4186 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4187 fe621944 2020-11-10 stsp start_old = cc.left.start;
4188 fe621944 2020-11-10 stsp end_old = cc.left.end;
4189 fe621944 2020-11-10 stsp start_new = cc.right.start;
4190 fe621944 2020-11-10 stsp end_new = cc.right.end;
4191 33aa809d 2019-08-08 stsp
4192 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4193 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4194 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4195 33aa809d 2019-08-08 stsp
4196 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4197 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4198 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4199 33aa809d 2019-08-08 stsp
4200 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4201 fe621944 2020-11-10 stsp if (diff_state == NULL)
4202 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4203 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4204 fe621944 2020-11-10 stsp
4205 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4206 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4207 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4208 33aa809d 2019-08-08 stsp goto done;
4209 33aa809d 2019-08-08 stsp }
4210 fe621944 2020-11-10 stsp
4211 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4212 fe621944 2020-11-10 stsp diff_result, &cc);
4213 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4214 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4215 33aa809d 2019-08-08 stsp goto done;
4216 33aa809d 2019-08-08 stsp }
4217 fe621944 2020-11-10 stsp
4218 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4219 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4220 33aa809d 2019-08-08 stsp goto done;
4221 33aa809d 2019-08-08 stsp }
4222 33aa809d 2019-08-08 stsp
4223 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4224 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4225 33aa809d 2019-08-08 stsp if (err)
4226 33aa809d 2019-08-08 stsp goto done;
4227 33aa809d 2019-08-08 stsp
4228 33aa809d 2019-08-08 stsp switch (*choice) {
4229 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4230 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4231 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4232 33aa809d 2019-08-08 stsp break;
4233 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4234 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4235 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4236 33aa809d 2019-08-08 stsp break;
4237 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4238 33aa809d 2019-08-08 stsp break;
4239 33aa809d 2019-08-08 stsp default:
4240 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4241 33aa809d 2019-08-08 stsp break;
4242 33aa809d 2019-08-08 stsp }
4243 33aa809d 2019-08-08 stsp done:
4244 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4245 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4246 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4247 33aa809d 2019-08-08 stsp return err;
4248 33aa809d 2019-08-08 stsp }
4249 33aa809d 2019-08-08 stsp
4250 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4251 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4252 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4253 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4254 1f1abb7e 2019-08-08 stsp void *progress_arg;
4255 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4256 33aa809d 2019-08-08 stsp void *patch_arg;
4257 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4258 1f1abb7e 2019-08-08 stsp };
4259 a129376b 2019-03-28 stsp
4260 e20a8b6f 2019-06-04 stsp static const struct got_error *
4261 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4262 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4263 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4264 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4265 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4266 33aa809d 2019-08-08 stsp {
4267 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4268 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4269 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4270 1ebedb77 2019-10-19 stsp int fd2 = -1;
4271 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4272 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4273 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4274 fe621944 2020-11-10 stsp struct stat sb2;
4275 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4276 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4277 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4278 33aa809d 2019-08-08 stsp
4279 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4280 33aa809d 2019-08-08 stsp
4281 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4282 33aa809d 2019-08-08 stsp if (err)
4283 33aa809d 2019-08-08 stsp return err;
4284 33aa809d 2019-08-08 stsp
4285 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4286 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4287 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4288 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4289 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4290 fa3cef63 2020-07-23 stsp goto done;
4291 fa3cef63 2020-07-23 stsp }
4292 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4293 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4294 fa3cef63 2020-07-23 stsp if (link_len == -1)
4295 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4296 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4297 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4298 12463d8b 2019-12-13 stsp }
4299 12463d8b 2019-12-13 stsp } else {
4300 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4301 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4302 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4303 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4304 fa3cef63 2020-07-23 stsp goto done;
4305 fa3cef63 2020-07-23 stsp }
4306 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4307 fa3cef63 2020-07-23 stsp sizeof(link_target));
4308 fa3cef63 2020-07-23 stsp if (link_len == -1)
4309 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4310 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4311 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4312 12463d8b 2019-12-13 stsp }
4313 1ebedb77 2019-10-19 stsp }
4314 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4315 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4316 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4317 fa3cef63 2020-07-23 stsp goto done;
4318 fa3cef63 2020-07-23 stsp }
4319 1ebedb77 2019-10-19 stsp
4320 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4321 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4322 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4323 fa3cef63 2020-07-23 stsp goto done;
4324 fa3cef63 2020-07-23 stsp }
4325 fa3cef63 2020-07-23 stsp fd2 = -1;
4326 fa3cef63 2020-07-23 stsp } else {
4327 fa3cef63 2020-07-23 stsp size_t n;
4328 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4329 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4330 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4331 fa3cef63 2020-07-23 stsp goto done;
4332 fa3cef63 2020-07-23 stsp }
4333 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4334 fa3cef63 2020-07-23 stsp if (n != link_len) {
4335 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4336 fa3cef63 2020-07-23 stsp goto done;
4337 fa3cef63 2020-07-23 stsp }
4338 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4339 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4340 fa3cef63 2020-07-23 stsp goto done;
4341 fa3cef63 2020-07-23 stsp }
4342 fa3cef63 2020-07-23 stsp rewind(f2);
4343 33aa809d 2019-08-08 stsp }
4344 33aa809d 2019-08-08 stsp
4345 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4346 33aa809d 2019-08-08 stsp if (err)
4347 33aa809d 2019-08-08 stsp goto done;
4348 33aa809d 2019-08-08 stsp
4349 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4350 33aa809d 2019-08-08 stsp if (err)
4351 33aa809d 2019-08-08 stsp goto done;
4352 33aa809d 2019-08-08 stsp
4353 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4354 33aa809d 2019-08-08 stsp if (err)
4355 33aa809d 2019-08-08 stsp goto done;
4356 33aa809d 2019-08-08 stsp
4357 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4358 fe621944 2020-11-10 stsp NULL);
4359 33aa809d 2019-08-08 stsp if (err)
4360 33aa809d 2019-08-08 stsp goto done;
4361 33aa809d 2019-08-08 stsp
4362 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4363 33aa809d 2019-08-08 stsp if (err)
4364 33aa809d 2019-08-08 stsp goto done;
4365 33aa809d 2019-08-08 stsp
4366 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4367 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4368 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4369 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4370 fe621944 2020-11-10 stsp
4371 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4372 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4373 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4374 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4375 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4376 fe621944 2020-11-10 stsp nchanges++;
4377 fe621944 2020-11-10 stsp }
4378 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4379 33aa809d 2019-08-08 stsp int choice;
4380 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4381 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4382 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4383 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4384 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4385 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4386 33aa809d 2019-08-08 stsp if (err)
4387 33aa809d 2019-08-08 stsp goto done;
4388 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4389 33aa809d 2019-08-08 stsp have_content = 1;
4390 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4391 33aa809d 2019-08-08 stsp break;
4392 33aa809d 2019-08-08 stsp }
4393 1ebedb77 2019-10-19 stsp if (have_content) {
4394 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4395 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4396 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4397 1ebedb77 2019-10-19 stsp if (err)
4398 1ebedb77 2019-10-19 stsp goto done;
4399 1ebedb77 2019-10-19 stsp
4400 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4401 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4402 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4403 fa3cef63 2020-07-23 stsp goto done;
4404 fa3cef63 2020-07-23 stsp }
4405 1ebedb77 2019-10-19 stsp }
4406 1ebedb77 2019-10-19 stsp }
4407 33aa809d 2019-08-08 stsp done:
4408 33aa809d 2019-08-08 stsp free(id_str);
4409 33aa809d 2019-08-08 stsp if (blob)
4410 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4411 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4412 fe621944 2020-11-10 stsp if (err == NULL)
4413 fe621944 2020-11-10 stsp err = free_err;
4414 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4415 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4416 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4417 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4418 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4419 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4420 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4421 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4422 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4423 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4424 33aa809d 2019-08-08 stsp if (err || !have_content) {
4425 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4426 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4427 33aa809d 2019-08-08 stsp free(*path_outfile);
4428 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4429 33aa809d 2019-08-08 stsp }
4430 33aa809d 2019-08-08 stsp free(path1);
4431 33aa809d 2019-08-08 stsp return err;
4432 33aa809d 2019-08-08 stsp }
4433 33aa809d 2019-08-08 stsp
4434 33aa809d 2019-08-08 stsp static const struct got_error *
4435 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4436 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4437 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4438 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4439 a129376b 2019-03-28 stsp {
4440 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4441 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4442 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4443 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4444 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4445 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4446 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4447 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4448 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4449 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4450 a129376b 2019-03-28 stsp
4451 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4452 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4453 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4454 d3bcc3d1 2019-08-08 stsp return NULL;
4455 3d69ad8d 2019-08-17 semarie
4456 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4457 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4458 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4459 d3bcc3d1 2019-08-08 stsp
4460 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4461 65084dad 2019-08-08 stsp if (ie == NULL)
4462 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4463 a129376b 2019-03-28 stsp
4464 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4465 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4466 a129376b 2019-03-28 stsp if (err) {
4467 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4468 a129376b 2019-03-28 stsp goto done;
4469 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4470 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4471 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4472 e20a8b6f 2019-06-04 stsp goto done;
4473 e20a8b6f 2019-06-04 stsp }
4474 a129376b 2019-03-28 stsp }
4475 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4476 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4477 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4478 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4479 a129376b 2019-03-28 stsp goto done;
4480 a129376b 2019-03-28 stsp }
4481 a129376b 2019-03-28 stsp } else {
4482 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4483 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4484 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4485 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4486 a129376b 2019-03-28 stsp goto done;
4487 a129376b 2019-03-28 stsp }
4488 a129376b 2019-03-28 stsp } else {
4489 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4490 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4491 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4492 a129376b 2019-03-28 stsp goto done;
4493 a129376b 2019-03-28 stsp }
4494 a129376b 2019-03-28 stsp }
4495 a129376b 2019-03-28 stsp }
4496 a129376b 2019-03-28 stsp
4497 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4498 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4499 a9fa2909 2019-07-27 stsp if (err) {
4500 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4501 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4502 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4503 a9fa2909 2019-07-27 stsp goto done;
4504 a9fa2909 2019-07-27 stsp } else {
4505 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4506 a9fa2909 2019-07-27 stsp if (err)
4507 a9fa2909 2019-07-27 stsp goto done;
4508 a9fa2909 2019-07-27 stsp
4509 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4510 1233e6b6 2020-10-19 stsp if (err)
4511 a9fa2909 2019-07-27 stsp goto done;
4512 a9fa2909 2019-07-27 stsp
4513 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4514 1233e6b6 2020-10-19 stsp free(te_name);
4515 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4516 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4517 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4518 a9fa2909 2019-07-27 stsp goto done;
4519 a9fa2909 2019-07-27 stsp }
4520 a129376b 2019-03-28 stsp }
4521 a129376b 2019-03-28 stsp
4522 a129376b 2019-03-28 stsp switch (status) {
4523 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4524 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4525 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4526 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4527 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4528 33aa809d 2019-08-08 stsp if (err)
4529 33aa809d 2019-08-08 stsp goto done;
4530 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4531 33aa809d 2019-08-08 stsp break;
4532 33aa809d 2019-08-08 stsp }
4533 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4534 1f1abb7e 2019-08-08 stsp ie->path);
4535 1ee397ad 2019-07-12 stsp if (err)
4536 1ee397ad 2019-07-12 stsp goto done;
4537 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4538 a129376b 2019-03-28 stsp break;
4539 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4540 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4541 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4542 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4543 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4544 33aa809d 2019-08-08 stsp if (err)
4545 33aa809d 2019-08-08 stsp goto done;
4546 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4547 33aa809d 2019-08-08 stsp break;
4548 33aa809d 2019-08-08 stsp }
4549 33aa809d 2019-08-08 stsp /* fall through */
4550 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4551 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4552 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4553 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4554 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4555 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4556 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4557 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4558 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4559 24278f30 2019-08-03 stsp } else
4560 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4561 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4562 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4563 a129376b 2019-03-28 stsp if (err)
4564 65084dad 2019-08-08 stsp goto done;
4565 65084dad 2019-08-08 stsp
4566 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4567 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4568 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4569 a129376b 2019-03-28 stsp goto done;
4570 65084dad 2019-08-08 stsp }
4571 65084dad 2019-08-08 stsp
4572 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4573 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4574 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4575 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4576 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4577 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4578 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4579 33aa809d 2019-08-08 stsp break;
4580 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4581 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4582 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4583 369fd7e5 2020-07-23 stsp path_content);
4584 369fd7e5 2020-07-23 stsp break;
4585 369fd7e5 2020-07-23 stsp }
4586 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4587 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4588 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4589 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4590 369fd7e5 2020-07-23 stsp } else {
4591 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4592 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4593 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4594 369fd7e5 2020-07-23 stsp goto done;
4595 369fd7e5 2020-07-23 stsp }
4596 33aa809d 2019-08-08 stsp }
4597 33aa809d 2019-08-08 stsp } else {
4598 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4599 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4600 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4601 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4602 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4603 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4604 2e1fa222 2020-07-23 stsp } else {
4605 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4606 2e1fa222 2020-07-23 stsp ie->path,
4607 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4608 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4609 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4610 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4611 2e1fa222 2020-07-23 stsp }
4612 a129376b 2019-03-28 stsp if (err)
4613 a129376b 2019-03-28 stsp goto done;
4614 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4615 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4616 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4617 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4618 437adc9d 2020-12-10 yzhong blob->id.sha1,
4619 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4620 2e1fa222 2020-07-23 stsp if (err)
4621 2e1fa222 2020-07-23 stsp goto done;
4622 2e1fa222 2020-07-23 stsp }
4623 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4624 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4625 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4626 33aa809d 2019-08-08 stsp }
4627 a129376b 2019-03-28 stsp }
4628 a129376b 2019-03-28 stsp break;
4629 e20a8b6f 2019-06-04 stsp }
4630 a129376b 2019-03-28 stsp default:
4631 1f1abb7e 2019-08-08 stsp break;
4632 a129376b 2019-03-28 stsp }
4633 a129376b 2019-03-28 stsp done:
4634 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4635 33aa809d 2019-08-08 stsp free(path_content);
4636 e20a8b6f 2019-06-04 stsp free(parent_path);
4637 a129376b 2019-03-28 stsp free(tree_path);
4638 a129376b 2019-03-28 stsp if (blob)
4639 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4640 a129376b 2019-03-28 stsp if (tree)
4641 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4642 a129376b 2019-03-28 stsp free(tree_id);
4643 e20a8b6f 2019-06-04 stsp return err;
4644 e20a8b6f 2019-06-04 stsp }
4645 e20a8b6f 2019-06-04 stsp
4646 e20a8b6f 2019-06-04 stsp const struct got_error *
4647 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4648 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4649 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4650 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4651 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4652 e20a8b6f 2019-06-04 stsp {
4653 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4654 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4655 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4656 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4657 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4658 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4659 e20a8b6f 2019-06-04 stsp
4660 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4661 e20a8b6f 2019-06-04 stsp if (err)
4662 e20a8b6f 2019-06-04 stsp return err;
4663 e20a8b6f 2019-06-04 stsp
4664 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4665 e20a8b6f 2019-06-04 stsp if (err)
4666 e20a8b6f 2019-06-04 stsp goto done;
4667 e20a8b6f 2019-06-04 stsp
4668 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4669 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4670 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4671 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4672 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4673 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4674 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4675 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4676 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4677 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4678 e20a8b6f 2019-06-04 stsp if (err)
4679 af12c6b9 2019-06-04 stsp break;
4680 e20a8b6f 2019-06-04 stsp }
4681 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4682 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4683 e20a8b6f 2019-06-04 stsp err = sync_err;
4684 af12c6b9 2019-06-04 stsp done:
4685 fb399478 2019-07-12 stsp free(fileindex_path);
4686 a129376b 2019-03-28 stsp if (fileindex)
4687 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4688 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4689 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4690 a129376b 2019-03-28 stsp err = unlockerr;
4691 c4296144 2019-05-09 stsp return err;
4692 c4296144 2019-05-09 stsp }
4693 c4296144 2019-05-09 stsp
4694 cf066bf8 2019-05-09 stsp static void
4695 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4696 cf066bf8 2019-05-09 stsp {
4697 24519714 2019-05-09 stsp free(ct->path);
4698 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4699 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4700 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4701 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4702 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4703 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4704 cf066bf8 2019-05-09 stsp free(ct);
4705 cf066bf8 2019-05-09 stsp }
4706 24519714 2019-05-09 stsp
4707 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4708 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4709 24519714 2019-05-09 stsp struct got_repository *repo;
4710 24519714 2019-05-09 stsp struct got_worktree *worktree;
4711 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4712 5f8a88c6 2019-08-03 stsp int have_staged_files;
4713 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4714 24519714 2019-05-09 stsp };
4715 cf066bf8 2019-05-09 stsp
4716 c4296144 2019-05-09 stsp static const struct got_error *
4717 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4718 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4719 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4720 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4721 c4296144 2019-05-09 stsp {
4722 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4723 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4724 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4725 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4726 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4727 768aea60 2019-05-09 stsp struct stat sb;
4728 c4296144 2019-05-09 stsp
4729 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4730 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4731 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4732 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4733 5f8a88c6 2019-08-03 stsp return NULL;
4734 5f8a88c6 2019-08-03 stsp } else {
4735 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4736 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4737 c4296144 2019-05-09 stsp
4738 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4739 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4740 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4741 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4742 5f8a88c6 2019-08-03 stsp return NULL;
4743 5f8a88c6 2019-08-03 stsp }
4744 0b5cc0d6 2019-05-09 stsp
4745 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4746 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4747 036813ee 2019-05-09 stsp goto done;
4748 036813ee 2019-05-09 stsp }
4749 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4750 036813ee 2019-05-09 stsp parent_path = strdup("");
4751 69960a46 2019-05-09 stsp if (parent_path == NULL)
4752 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4753 69960a46 2019-05-09 stsp } else {
4754 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4755 69960a46 2019-05-09 stsp if (err)
4756 69960a46 2019-05-09 stsp return err;
4757 69960a46 2019-05-09 stsp }
4758 c4296144 2019-05-09 stsp
4759 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4760 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4761 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4762 c4296144 2019-05-09 stsp goto done;
4763 768aea60 2019-05-09 stsp }
4764 768aea60 2019-05-09 stsp
4765 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4766 768aea60 2019-05-09 stsp relpath) == -1) {
4767 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4768 768aea60 2019-05-09 stsp goto done;
4769 768aea60 2019-05-09 stsp }
4770 0aeb8099 2020-07-23 stsp
4771 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4772 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4773 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4774 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4775 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4776 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4777 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4778 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4779 0aeb8099 2020-07-23 stsp break;
4780 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4781 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4782 0aeb8099 2020-07-23 stsp break;
4783 0aeb8099 2020-07-23 stsp default:
4784 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4785 0aeb8099 2020-07-23 stsp goto done;
4786 0aeb8099 2020-07-23 stsp }
4787 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4788 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4789 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4790 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4791 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4792 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4793 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4794 12463d8b 2019-12-13 stsp ct->ondisk_path);
4795 12463d8b 2019-12-13 stsp goto done;
4796 12463d8b 2019-12-13 stsp }
4797 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4798 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4799 768aea60 2019-05-09 stsp goto done;
4800 768aea60 2019-05-09 stsp }
4801 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4802 c4296144 2019-05-09 stsp }
4803 c4296144 2019-05-09 stsp
4804 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4805 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4806 44d03001 2019-05-09 stsp relpath) == -1) {
4807 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4808 44d03001 2019-05-09 stsp goto done;
4809 44d03001 2019-05-09 stsp }
4810 44d03001 2019-05-09 stsp
4811 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4812 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4813 35213c7c 2020-07-23 stsp int is_bad_symlink;
4814 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4815 35213c7c 2020-07-23 stsp ssize_t target_len;
4816 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4817 35213c7c 2020-07-23 stsp sizeof(target_path));
4818 35213c7c 2020-07-23 stsp if (target_len == -1) {
4819 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4820 35213c7c 2020-07-23 stsp ct->ondisk_path);
4821 35213c7c 2020-07-23 stsp goto done;
4822 35213c7c 2020-07-23 stsp }
4823 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4824 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4825 35213c7c 2020-07-23 stsp if (err)
4826 35213c7c 2020-07-23 stsp goto done;
4827 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4828 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4829 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4830 35213c7c 2020-07-23 stsp goto done;
4831 35213c7c 2020-07-23 stsp }
4832 35213c7c 2020-07-23 stsp }
4833 35213c7c 2020-07-23 stsp
4834 35213c7c 2020-07-23 stsp
4835 cf066bf8 2019-05-09 stsp ct->status = status;
4836 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4837 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4838 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4839 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4840 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4841 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4842 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4843 b416585c 2019-05-13 stsp goto done;
4844 b416585c 2019-05-13 stsp }
4845 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4846 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4847 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4848 f0b75401 2019-08-03 stsp goto done;
4849 f0b75401 2019-08-03 stsp }
4850 f0b75401 2019-08-03 stsp }
4851 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4852 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4853 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4854 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4855 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4856 036813ee 2019-05-09 stsp goto done;
4857 036813ee 2019-05-09 stsp }
4858 ca2503ea 2019-05-09 stsp }
4859 24519714 2019-05-09 stsp ct->path = strdup(path);
4860 24519714 2019-05-09 stsp if (ct->path == NULL) {
4861 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4862 24519714 2019-05-09 stsp goto done;
4863 24519714 2019-05-09 stsp }
4864 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4865 c4296144 2019-05-09 stsp done:
4866 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4867 ed175427 2019-05-09 stsp free_commitable(ct);
4868 24519714 2019-05-09 stsp free(parent_path);
4869 036813ee 2019-05-09 stsp free(path);
4870 a129376b 2019-03-28 stsp return err;
4871 a129376b 2019-03-28 stsp }
4872 c4296144 2019-05-09 stsp
4873 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4874 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4875 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4876 036813ee 2019-05-09 stsp struct got_repository *);
4877 ed175427 2019-05-09 stsp
4878 ed175427 2019-05-09 stsp static const struct got_error *
4879 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4880 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4881 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4882 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4883 afa376bf 2019-05-09 stsp struct got_repository *repo)
4884 ed175427 2019-05-09 stsp {
4885 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4886 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4887 036813ee 2019-05-09 stsp char *subpath;
4888 ed175427 2019-05-09 stsp
4889 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4890 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4891 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4892 ed175427 2019-05-09 stsp
4893 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4894 ed175427 2019-05-09 stsp if (err)
4895 ed175427 2019-05-09 stsp return err;
4896 ed175427 2019-05-09 stsp
4897 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4898 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4899 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4900 036813ee 2019-05-09 stsp free(subpath);
4901 036813ee 2019-05-09 stsp return err;
4902 036813ee 2019-05-09 stsp }
4903 ed175427 2019-05-09 stsp
4904 036813ee 2019-05-09 stsp static const struct got_error *
4905 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4906 036813ee 2019-05-09 stsp {
4907 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4908 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4909 ed175427 2019-05-09 stsp
4910 036813ee 2019-05-09 stsp *match = 0;
4911 ed175427 2019-05-09 stsp
4912 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4913 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4914 0f63689d 2019-05-10 stsp return NULL;
4915 036813ee 2019-05-09 stsp }
4916 0b5cc0d6 2019-05-09 stsp
4917 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4918 0f63689d 2019-05-10 stsp if (err)
4919 0f63689d 2019-05-10 stsp return err;
4920 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4921 036813ee 2019-05-09 stsp free(ct_parent_path);
4922 036813ee 2019-05-09 stsp return err;
4923 036813ee 2019-05-09 stsp }
4924 0b5cc0d6 2019-05-09 stsp
4925 768aea60 2019-05-09 stsp static mode_t
4926 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4927 768aea60 2019-05-09 stsp {
4928 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4929 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4930 3d9a4ec4 2020-07-23 stsp
4931 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4932 768aea60 2019-05-09 stsp }
4933 768aea60 2019-05-09 stsp
4934 036813ee 2019-05-09 stsp static const struct got_error *
4935 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4936 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4937 036813ee 2019-05-09 stsp {
4938 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4939 ca2503ea 2019-05-09 stsp
4940 036813ee 2019-05-09 stsp *new_te = NULL;
4941 0b5cc0d6 2019-05-09 stsp
4942 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4943 036813ee 2019-05-09 stsp if (err)
4944 036813ee 2019-05-09 stsp goto done;
4945 0b5cc0d6 2019-05-09 stsp
4946 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4947 036813ee 2019-05-09 stsp
4948 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4949 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4950 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4951 5f8a88c6 2019-08-03 stsp else
4952 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4953 036813ee 2019-05-09 stsp done:
4954 036813ee 2019-05-09 stsp if (err && *new_te) {
4955 56e0773d 2019-11-28 stsp free(*new_te);
4956 036813ee 2019-05-09 stsp *new_te = NULL;
4957 036813ee 2019-05-09 stsp }
4958 036813ee 2019-05-09 stsp return err;
4959 036813ee 2019-05-09 stsp }
4960 036813ee 2019-05-09 stsp
4961 036813ee 2019-05-09 stsp static const struct got_error *
4962 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4963 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4964 036813ee 2019-05-09 stsp {
4965 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4966 102b254e 2020-10-19 stsp char *ct_name = NULL;
4967 036813ee 2019-05-09 stsp
4968 036813ee 2019-05-09 stsp *new_te = NULL;
4969 036813ee 2019-05-09 stsp
4970 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4971 036813ee 2019-05-09 stsp if (*new_te == NULL)
4972 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4973 036813ee 2019-05-09 stsp
4974 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4975 102b254e 2020-10-19 stsp if (err)
4976 036813ee 2019-05-09 stsp goto done;
4977 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4978 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4979 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4980 036813ee 2019-05-09 stsp goto done;
4981 036813ee 2019-05-09 stsp }
4982 036813ee 2019-05-09 stsp
4983 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4984 036813ee 2019-05-09 stsp
4985 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4986 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4987 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4988 5f8a88c6 2019-08-03 stsp else
4989 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4990 036813ee 2019-05-09 stsp done:
4991 102b254e 2020-10-19 stsp free(ct_name);
4992 036813ee 2019-05-09 stsp if (err && *new_te) {
4993 56e0773d 2019-11-28 stsp free(*new_te);
4994 036813ee 2019-05-09 stsp *new_te = NULL;
4995 036813ee 2019-05-09 stsp }
4996 036813ee 2019-05-09 stsp return err;
4997 036813ee 2019-05-09 stsp }
4998 036813ee 2019-05-09 stsp
4999 036813ee 2019-05-09 stsp static const struct got_error *
5000 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5001 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5002 036813ee 2019-05-09 stsp {
5003 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5004 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5005 036813ee 2019-05-09 stsp
5006 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5007 036813ee 2019-05-09 stsp if (err)
5008 036813ee 2019-05-09 stsp return err;
5009 036813ee 2019-05-09 stsp if (new_pe == NULL)
5010 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5011 036813ee 2019-05-09 stsp return NULL;
5012 afa376bf 2019-05-09 stsp }
5013 afa376bf 2019-05-09 stsp
5014 afa376bf 2019-05-09 stsp static const struct got_error *
5015 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5016 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5017 afa376bf 2019-05-09 stsp {
5018 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5019 5f8a88c6 2019-08-03 stsp unsigned char status;
5020 5f8a88c6 2019-08-03 stsp
5021 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5022 afa376bf 2019-05-09 stsp ct_path++;
5023 5f8a88c6 2019-08-03 stsp
5024 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5025 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5026 5f8a88c6 2019-08-03 stsp else
5027 5f8a88c6 2019-08-03 stsp status = ct->status;
5028 5f8a88c6 2019-08-03 stsp
5029 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5030 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5031 036813ee 2019-05-09 stsp }
5032 036813ee 2019-05-09 stsp
5033 036813ee 2019-05-09 stsp static const struct got_error *
5034 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5035 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5036 44d03001 2019-05-09 stsp {
5037 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5038 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5039 44d03001 2019-05-09 stsp char *te_path;
5040 44d03001 2019-05-09 stsp
5041 44d03001 2019-05-09 stsp *modified = 0;
5042 44d03001 2019-05-09 stsp
5043 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5044 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5045 44d03001 2019-05-09 stsp te->name) == -1)
5046 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5047 44d03001 2019-05-09 stsp
5048 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5049 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5050 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5051 44d03001 2019-05-09 stsp strlen(te_path));
5052 62d463ca 2020-10-20 naddy if (*modified)
5053 44d03001 2019-05-09 stsp break;
5054 44d03001 2019-05-09 stsp }
5055 44d03001 2019-05-09 stsp
5056 44d03001 2019-05-09 stsp free(te_path);
5057 44d03001 2019-05-09 stsp return err;
5058 44d03001 2019-05-09 stsp }
5059 44d03001 2019-05-09 stsp
5060 44d03001 2019-05-09 stsp static const struct got_error *
5061 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5062 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5063 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5064 036813ee 2019-05-09 stsp {
5065 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5066 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5067 036813ee 2019-05-09 stsp
5068 036813ee 2019-05-09 stsp *ctp = NULL;
5069 036813ee 2019-05-09 stsp
5070 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5071 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5072 036813ee 2019-05-09 stsp char *ct_name = NULL;
5073 036813ee 2019-05-09 stsp int path_matches;
5074 036813ee 2019-05-09 stsp
5075 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5076 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5077 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5078 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5079 5f8a88c6 2019-08-03 stsp continue;
5080 5f8a88c6 2019-08-03 stsp } else {
5081 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5082 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5083 5f8a88c6 2019-08-03 stsp continue;
5084 5f8a88c6 2019-08-03 stsp }
5085 036813ee 2019-05-09 stsp
5086 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5087 036813ee 2019-05-09 stsp continue;
5088 036813ee 2019-05-09 stsp
5089 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5090 62d463ca 2020-10-20 naddy if (err)
5091 036813ee 2019-05-09 stsp return err;
5092 036813ee 2019-05-09 stsp if (!path_matches)
5093 036813ee 2019-05-09 stsp continue;
5094 036813ee 2019-05-09 stsp
5095 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5096 d34b633e 2020-10-19 stsp if (err)
5097 d34b633e 2020-10-19 stsp return err;
5098 d34b633e 2020-10-19 stsp
5099 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5100 d34b633e 2020-10-19 stsp free(ct_name);
5101 036813ee 2019-05-09 stsp continue;
5102 d34b633e 2020-10-19 stsp }
5103 d34b633e 2020-10-19 stsp free(ct_name);
5104 036813ee 2019-05-09 stsp
5105 036813ee 2019-05-09 stsp *ctp = ct;
5106 036813ee 2019-05-09 stsp break;
5107 036813ee 2019-05-09 stsp }
5108 2b496619 2019-07-10 stsp
5109 2b496619 2019-07-10 stsp return err;
5110 2b496619 2019-07-10 stsp }
5111 2b496619 2019-07-10 stsp
5112 2b496619 2019-07-10 stsp static const struct got_error *
5113 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5114 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5115 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5116 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5117 2b496619 2019-07-10 stsp struct got_repository *repo)
5118 2b496619 2019-07-10 stsp {
5119 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5120 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5121 2b496619 2019-07-10 stsp char *subtree_path;
5122 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5123 ba580f68 2020-03-22 stsp int nentries;
5124 2b496619 2019-07-10 stsp
5125 2b496619 2019-07-10 stsp *new_tep = NULL;
5126 2b496619 2019-07-10 stsp
5127 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5128 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5129 2b496619 2019-07-10 stsp child_path) == -1)
5130 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5131 036813ee 2019-05-09 stsp
5132 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5133 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5134 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5135 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5136 56e0773d 2019-11-28 stsp
5137 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5138 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5139 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5140 2b496619 2019-07-10 stsp goto done;
5141 2b496619 2019-07-10 stsp }
5142 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5143 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5144 2b496619 2019-07-10 stsp if (err) {
5145 56e0773d 2019-11-28 stsp free(new_te);
5146 2b496619 2019-07-10 stsp goto done;
5147 2b496619 2019-07-10 stsp }
5148 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5149 2b496619 2019-07-10 stsp done:
5150 56e0773d 2019-11-28 stsp free(id);
5151 2b496619 2019-07-10 stsp free(subtree_path);
5152 2b496619 2019-07-10 stsp if (err == NULL)
5153 2b496619 2019-07-10 stsp *new_tep = new_te;
5154 036813ee 2019-05-09 stsp return err;
5155 036813ee 2019-05-09 stsp }
5156 036813ee 2019-05-09 stsp
5157 036813ee 2019-05-09 stsp static const struct got_error *
5158 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5159 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5160 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5161 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5162 036813ee 2019-05-09 stsp struct got_repository *repo)
5163 036813ee 2019-05-09 stsp {
5164 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5165 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5166 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5167 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5168 036813ee 2019-05-09 stsp
5169 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5170 ba580f68 2020-03-22 stsp *nentries = 0;
5171 036813ee 2019-05-09 stsp
5172 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5173 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5174 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5175 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5176 036813ee 2019-05-09 stsp
5177 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5178 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5179 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5180 036813ee 2019-05-09 stsp continue;
5181 036813ee 2019-05-09 stsp
5182 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5183 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5184 036813ee 2019-05-09 stsp continue;
5185 036813ee 2019-05-09 stsp
5186 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5187 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5188 036813ee 2019-05-09 stsp if (err)
5189 036813ee 2019-05-09 stsp goto done;
5190 036813ee 2019-05-09 stsp
5191 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5192 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5193 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5194 036813ee 2019-05-09 stsp if (err)
5195 036813ee 2019-05-09 stsp goto done;
5196 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5197 afa376bf 2019-05-09 stsp if (err)
5198 afa376bf 2019-05-09 stsp goto done;
5199 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5200 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5201 2b496619 2019-07-10 stsp if (err)
5202 2f51b5b3 2019-05-09 stsp goto done;
5203 ba580f68 2020-03-22 stsp (*nentries)++;
5204 2b496619 2019-07-10 stsp } else {
5205 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5206 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5207 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5208 2b496619 2019-07-10 stsp == NULL) {
5209 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5210 2b496619 2019-07-10 stsp child_path, path_base_tree,
5211 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5212 2b496619 2019-07-10 stsp repo);
5213 2b496619 2019-07-10 stsp if (err)
5214 2b496619 2019-07-10 stsp goto done;
5215 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5216 2b496619 2019-07-10 stsp if (err)
5217 2b496619 2019-07-10 stsp goto done;
5218 ba580f68 2020-03-22 stsp (*nentries)++;
5219 9ba0479c 2019-05-10 stsp }
5220 ed175427 2019-05-09 stsp }
5221 2f51b5b3 2019-05-09 stsp }
5222 2f51b5b3 2019-05-09 stsp
5223 2f51b5b3 2019-05-09 stsp if (base_tree) {
5224 56e0773d 2019-11-28 stsp int i, nbase_entries;
5225 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5226 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5227 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5228 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5229 63c5ca5d 2019-08-24 stsp
5230 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5231 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5232 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5233 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5234 63c5ca5d 2019-08-24 stsp if (err)
5235 63c5ca5d 2019-08-24 stsp goto done;
5236 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5237 63c5ca5d 2019-08-24 stsp if (err)
5238 63c5ca5d 2019-08-24 stsp goto done;
5239 ba580f68 2020-03-22 stsp (*nentries)++;
5240 63c5ca5d 2019-08-24 stsp continue;
5241 63c5ca5d 2019-08-24 stsp }
5242 2f51b5b3 2019-05-09 stsp
5243 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5244 44d03001 2019-05-09 stsp int modified;
5245 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5246 036813ee 2019-05-09 stsp if (err)
5247 036813ee 2019-05-09 stsp goto done;
5248 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5249 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5250 2f51b5b3 2019-05-09 stsp if (err)
5251 2f51b5b3 2019-05-09 stsp goto done;
5252 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5253 44d03001 2019-05-09 stsp if (modified) {
5254 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5255 ba580f68 2020-03-22 stsp int nsubentries;
5256 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5257 ba580f68 2020-03-22 stsp &nsubentries, te,
5258 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5259 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5260 44d03001 2019-05-09 stsp if (err)
5261 44d03001 2019-05-09 stsp goto done;
5262 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5263 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5264 ba580f68 2020-03-22 stsp free(new_id);
5265 ba580f68 2020-03-22 stsp continue;
5266 ba580f68 2020-03-22 stsp }
5267 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5268 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5269 56e0773d 2019-11-28 stsp free(new_id);
5270 44d03001 2019-05-09 stsp }
5271 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5272 036813ee 2019-05-09 stsp if (err)
5273 036813ee 2019-05-09 stsp goto done;
5274 ba580f68 2020-03-22 stsp (*nentries)++;
5275 2f51b5b3 2019-05-09 stsp continue;
5276 036813ee 2019-05-09 stsp }
5277 2f51b5b3 2019-05-09 stsp
5278 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5279 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5280 f66c734c 2019-09-22 stsp if (err)
5281 f66c734c 2019-09-22 stsp goto done;
5282 2f51b5b3 2019-05-09 stsp if (ct) {
5283 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5284 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5285 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5286 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5287 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5288 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5289 2f51b5b3 2019-05-09 stsp if (err)
5290 2f51b5b3 2019-05-09 stsp goto done;
5291 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5292 2f51b5b3 2019-05-09 stsp if (err)
5293 2f51b5b3 2019-05-09 stsp goto done;
5294 ba580f68 2020-03-22 stsp (*nentries)++;
5295 2f51b5b3 2019-05-09 stsp }
5296 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5297 b416585c 2019-05-13 stsp status_arg);
5298 afa376bf 2019-05-09 stsp if (err)
5299 afa376bf 2019-05-09 stsp goto done;
5300 2f51b5b3 2019-05-09 stsp } else {
5301 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5302 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5303 2f51b5b3 2019-05-09 stsp if (err)
5304 2f51b5b3 2019-05-09 stsp goto done;
5305 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5306 2f51b5b3 2019-05-09 stsp if (err)
5307 2f51b5b3 2019-05-09 stsp goto done;
5308 ba580f68 2020-03-22 stsp (*nentries)++;
5309 2f51b5b3 2019-05-09 stsp }
5310 ca2503ea 2019-05-09 stsp }
5311 ed175427 2019-05-09 stsp }
5312 0b5cc0d6 2019-05-09 stsp
5313 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5314 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5315 ed175427 2019-05-09 stsp done:
5316 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5317 2e1fa222 2020-07-23 stsp return err;
5318 2e1fa222 2020-07-23 stsp }
5319 2e1fa222 2020-07-23 stsp
5320 2e1fa222 2020-07-23 stsp static const struct got_error *
5321 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5322 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5323 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5324 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5325 ebf99748 2019-05-09 stsp {
5326 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5327 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5328 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5329 ebf99748 2019-05-09 stsp
5330 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5331 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5332 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5333 ebf99748 2019-05-09 stsp
5334 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5335 437adc9d 2020-12-10 yzhong
5336 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5337 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5338 437adc9d 2020-12-10 yzhong if (err)
5339 437adc9d 2020-12-10 yzhong goto done;
5340 437adc9d 2020-12-10 yzhong
5341 ebf99748 2019-05-09 stsp if (ie) {
5342 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5343 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5344 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5345 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5346 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5347 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5348 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5349 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5350 437adc9d 2020-12-10 yzhong
5351 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5352 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5353 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5354 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5355 72fd46fa 2019-09-06 stsp !have_staged_files);
5356 ebf99748 2019-05-09 stsp } else
5357 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5358 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5359 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5360 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5361 72fd46fa 2019-09-06 stsp !have_staged_files);
5362 ebf99748 2019-05-09 stsp } else {
5363 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5364 ebf99748 2019-05-09 stsp if (err)
5365 437adc9d 2020-12-10 yzhong goto done;
5366 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5367 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5368 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5369 3969253a 2020-03-07 stsp if (err) {
5370 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5371 437adc9d 2020-12-10 yzhong goto done;
5372 3969253a 2020-03-07 stsp }
5373 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5374 3969253a 2020-03-07 stsp if (err) {
5375 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5376 437adc9d 2020-12-10 yzhong goto done;
5377 3969253a 2020-03-07 stsp }
5378 ebf99748 2019-05-09 stsp }
5379 437adc9d 2020-12-10 yzhong free(relpath);
5380 437adc9d 2020-12-10 yzhong relpath = NULL;
5381 ebf99748 2019-05-09 stsp }
5382 437adc9d 2020-12-10 yzhong done:
5383 437adc9d 2020-12-10 yzhong free(relpath);
5384 d56d26ce 2019-05-10 stsp return err;
5385 d56d26ce 2019-05-10 stsp }
5386 735ef5ac 2019-08-03 stsp
5387 d56d26ce 2019-05-10 stsp
5388 d56d26ce 2019-05-10 stsp static const struct got_error *
5389 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5390 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5391 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5392 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5393 735ef5ac 2019-08-03 stsp int ood_errcode)
5394 d56d26ce 2019-05-10 stsp {
5395 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5396 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5397 1a36436d 2019-06-10 stsp
5398 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5399 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5400 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5401 1a36436d 2019-06-10 stsp return NULL;
5402 9bead371 2019-07-28 stsp /*
5403 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5404 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5405 9bead371 2019-07-28 stsp */
5406 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5407 735ef5ac 2019-08-03 stsp in_repo_path);
5408 9bead371 2019-07-28 stsp if (err) {
5409 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5410 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5411 1a36436d 2019-06-10 stsp goto done;
5412 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5413 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5414 1a36436d 2019-06-10 stsp } else {
5415 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5416 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5417 735ef5ac 2019-08-03 stsp in_repo_path);
5418 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5419 1a36436d 2019-06-10 stsp goto done;
5420 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5421 1a36436d 2019-06-10 stsp }
5422 1a36436d 2019-06-10 stsp done:
5423 1a36436d 2019-06-10 stsp free(id);
5424 1a36436d 2019-06-10 stsp return err;
5425 ebf99748 2019-05-09 stsp }
5426 ebf99748 2019-05-09 stsp
5427 c4296144 2019-05-09 stsp const struct got_error *
5428 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5429 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5430 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5431 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5432 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5433 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5434 afa376bf 2019-05-09 stsp struct got_repository *repo)
5435 c4296144 2019-05-09 stsp {
5436 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5437 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5438 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5439 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5440 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5441 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5442 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5443 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5444 ba580f68 2020-03-22 stsp int nentries;
5445 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5446 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5447 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5448 c4296144 2019-05-09 stsp
5449 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5450 c4296144 2019-05-09 stsp
5451 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5452 675c7539 2019-05-09 stsp
5453 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5454 588edf97 2019-05-10 stsp if (err)
5455 588edf97 2019-05-10 stsp goto done;
5456 de18fc63 2019-05-09 stsp
5457 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5458 588edf97 2019-05-10 stsp if (err)
5459 588edf97 2019-05-10 stsp goto done;
5460 588edf97 2019-05-10 stsp
5461 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5462 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5463 33ad4cbe 2019-05-12 jcs if (err)
5464 33ad4cbe 2019-05-12 jcs goto done;
5465 33ad4cbe 2019-05-12 jcs }
5466 c4296144 2019-05-09 stsp
5467 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5468 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5469 33ad4cbe 2019-05-12 jcs goto done;
5470 33ad4cbe 2019-05-12 jcs }
5471 33ad4cbe 2019-05-12 jcs
5472 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5473 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5474 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5475 cf066bf8 2019-05-09 stsp char *ondisk_path;
5476 cf066bf8 2019-05-09 stsp
5477 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5478 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5479 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5480 5f8a88c6 2019-08-03 stsp continue;
5481 5f8a88c6 2019-08-03 stsp
5482 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5483 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5484 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5485 cf066bf8 2019-05-09 stsp continue;
5486 cf066bf8 2019-05-09 stsp
5487 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5488 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5489 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5490 cf066bf8 2019-05-09 stsp goto done;
5491 cf066bf8 2019-05-09 stsp }
5492 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5493 2e1fa222 2020-07-23 stsp free(ondisk_path);
5494 2e1fa222 2020-07-23 stsp if (err)
5495 cf066bf8 2019-05-09 stsp goto done;
5496 cf066bf8 2019-05-09 stsp }
5497 036813ee 2019-05-09 stsp
5498 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5499 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5500 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5501 036813ee 2019-05-09 stsp if (err)
5502 036813ee 2019-05-09 stsp goto done;
5503 036813ee 2019-05-09 stsp
5504 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5505 de18fc63 2019-05-09 stsp if (err)
5506 de18fc63 2019-05-09 stsp goto done;
5507 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5508 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5509 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5510 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5511 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5512 33ad4cbe 2019-05-12 jcs free(logmsg);
5513 9d40349a 2019-05-09 stsp if (err)
5514 9d40349a 2019-05-09 stsp goto done;
5515 9d40349a 2019-05-09 stsp
5516 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5517 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5518 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5519 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5520 09f5bd90 2019-05-09 stsp goto done;
5521 09f5bd90 2019-05-09 stsp }
5522 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5523 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5524 09f5bd90 2019-05-09 stsp if (err)
5525 09f5bd90 2019-05-09 stsp goto done;
5526 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5527 ebf99748 2019-05-09 stsp if (err)
5528 ebf99748 2019-05-09 stsp goto done;
5529 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5530 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5531 09f5bd90 2019-05-09 stsp goto done;
5532 09f5bd90 2019-05-09 stsp }
5533 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5534 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5535 f2c16586 2019-05-09 stsp if (err)
5536 f2c16586 2019-05-09 stsp goto done;
5537 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5538 f2c16586 2019-05-09 stsp if (err)
5539 f2c16586 2019-05-09 stsp goto done;
5540 f2c16586 2019-05-09 stsp
5541 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5542 09f5bd90 2019-05-09 stsp if (err)
5543 09f5bd90 2019-05-09 stsp goto done;
5544 09f5bd90 2019-05-09 stsp
5545 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5546 ebf99748 2019-05-09 stsp if (err)
5547 ebf99748 2019-05-09 stsp goto done;
5548 c4296144 2019-05-09 stsp done:
5549 588edf97 2019-05-10 stsp if (head_tree)
5550 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5551 588edf97 2019-05-10 stsp if (head_commit)
5552 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5553 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5554 2f17228e 2019-05-12 stsp if (head_ref2) {
5555 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5556 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5557 2f17228e 2019-05-12 stsp err = unlockerr;
5558 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5559 39cd0ff6 2019-07-12 stsp }
5560 39cd0ff6 2019-07-12 stsp return err;
5561 39cd0ff6 2019-07-12 stsp }
5562 5c1e53bc 2019-07-28 stsp
5563 5c1e53bc 2019-07-28 stsp static const struct got_error *
5564 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5565 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5566 5c1e53bc 2019-07-28 stsp {
5567 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5568 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5569 39cd0ff6 2019-07-12 stsp
5570 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5571 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5572 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5573 5c1e53bc 2019-07-28 stsp
5574 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5575 5c1e53bc 2019-07-28 stsp ct_path++;
5576 5c1e53bc 2019-07-28 stsp
5577 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5578 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5579 5c1e53bc 2019-07-28 stsp break;
5580 5c1e53bc 2019-07-28 stsp }
5581 5c1e53bc 2019-07-28 stsp
5582 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5583 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5584 5c1e53bc 2019-07-28 stsp
5585 5c1e53bc 2019-07-28 stsp return NULL;
5586 5c1e53bc 2019-07-28 stsp }
5587 5c1e53bc 2019-07-28 stsp
5588 f0b75401 2019-08-03 stsp static const struct got_error *
5589 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5590 f0b75401 2019-08-03 stsp {
5591 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5592 f0b75401 2019-08-03 stsp
5593 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5594 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5595 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5596 f0b75401 2019-08-03 stsp }
5597 f0b75401 2019-08-03 stsp
5598 f0b75401 2019-08-03 stsp return NULL;
5599 f0b75401 2019-08-03 stsp }
5600 f0b75401 2019-08-03 stsp
5601 5f8a88c6 2019-08-03 stsp static const struct got_error *
5602 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5603 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5604 5f8a88c6 2019-08-03 stsp {
5605 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5606 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5607 5f8a88c6 2019-08-03 stsp
5608 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5609 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5610 5f8a88c6 2019-08-03 stsp continue;
5611 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5612 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5613 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5614 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5615 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5616 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5617 5f8a88c6 2019-08-03 stsp }
5618 5f8a88c6 2019-08-03 stsp
5619 5f8a88c6 2019-08-03 stsp return NULL;
5620 5f8a88c6 2019-08-03 stsp }
5621 5f8a88c6 2019-08-03 stsp
5622 39cd0ff6 2019-07-12 stsp const struct got_error *
5623 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5624 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5625 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5626 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5627 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5628 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5629 39cd0ff6 2019-07-12 stsp {
5630 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5631 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5632 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5633 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5634 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5635 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5636 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5637 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5638 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5639 39cd0ff6 2019-07-12 stsp
5640 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5641 39cd0ff6 2019-07-12 stsp
5642 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5643 39cd0ff6 2019-07-12 stsp
5644 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5645 39cd0ff6 2019-07-12 stsp if (err)
5646 39cd0ff6 2019-07-12 stsp goto done;
5647 39cd0ff6 2019-07-12 stsp
5648 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5649 39cd0ff6 2019-07-12 stsp if (err)
5650 39cd0ff6 2019-07-12 stsp goto done;
5651 39cd0ff6 2019-07-12 stsp
5652 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5653 39cd0ff6 2019-07-12 stsp if (err)
5654 39cd0ff6 2019-07-12 stsp goto done;
5655 39cd0ff6 2019-07-12 stsp
5656 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5657 39cd0ff6 2019-07-12 stsp if (err)
5658 39cd0ff6 2019-07-12 stsp goto done;
5659 39cd0ff6 2019-07-12 stsp
5660 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5661 5f8a88c6 2019-08-03 stsp &have_staged_files);
5662 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5663 5f8a88c6 2019-08-03 stsp goto done;
5664 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5665 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5666 5f8a88c6 2019-08-03 stsp if (err)
5667 5f8a88c6 2019-08-03 stsp goto done;
5668 5f8a88c6 2019-08-03 stsp }
5669 5f8a88c6 2019-08-03 stsp
5670 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5671 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5672 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5673 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5674 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5675 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5676 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5677 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5678 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5679 5c1e53bc 2019-07-28 stsp if (err)
5680 5c1e53bc 2019-07-28 stsp goto done;
5681 5c1e53bc 2019-07-28 stsp }
5682 39cd0ff6 2019-07-12 stsp
5683 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5684 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5685 39cd0ff6 2019-07-12 stsp goto done;
5686 5c1e53bc 2019-07-28 stsp }
5687 5c1e53bc 2019-07-28 stsp
5688 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5689 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5690 5c1e53bc 2019-07-28 stsp if (err)
5691 5c1e53bc 2019-07-28 stsp goto done;
5692 39cd0ff6 2019-07-12 stsp }
5693 f0b75401 2019-08-03 stsp
5694 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5695 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5696 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5697 f0b75401 2019-08-03 stsp
5698 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5699 f0b75401 2019-08-03 stsp ct_path++;
5700 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5701 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5702 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5703 b50cabdf 2019-07-12 stsp if (err)
5704 b50cabdf 2019-07-12 stsp goto done;
5705 f0b75401 2019-08-03 stsp
5706 b50cabdf 2019-07-12 stsp }
5707 b50cabdf 2019-07-12 stsp
5708 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5709 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5710 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5711 39cd0ff6 2019-07-12 stsp if (err)
5712 39cd0ff6 2019-07-12 stsp goto done;
5713 39cd0ff6 2019-07-12 stsp
5714 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5715 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5716 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5717 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5718 39cd0ff6 2019-07-12 stsp err = sync_err;
5719 39cd0ff6 2019-07-12 stsp done:
5720 39cd0ff6 2019-07-12 stsp if (fileindex)
5721 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5722 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5723 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5724 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5725 39cd0ff6 2019-07-12 stsp err = unlockerr;
5726 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5727 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5728 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5729 39cd0ff6 2019-07-12 stsp }
5730 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5731 c4296144 2019-05-09 stsp return err;
5732 8656d6c4 2019-05-20 stsp }
5733 8656d6c4 2019-05-20 stsp
5734 8656d6c4 2019-05-20 stsp const char *
5735 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5736 8656d6c4 2019-05-20 stsp {
5737 8656d6c4 2019-05-20 stsp return ct->path;
5738 8656d6c4 2019-05-20 stsp }
5739 8656d6c4 2019-05-20 stsp
5740 8656d6c4 2019-05-20 stsp unsigned int
5741 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5742 8656d6c4 2019-05-20 stsp {
5743 8656d6c4 2019-05-20 stsp return ct->status;
5744 818c7501 2019-07-11 stsp }
5745 818c7501 2019-07-11 stsp
5746 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5747 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5748 818c7501 2019-07-11 stsp struct got_repository *repo;
5749 818c7501 2019-07-11 stsp };
5750 818c7501 2019-07-11 stsp
5751 818c7501 2019-07-11 stsp static const struct got_error *
5752 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5753 818c7501 2019-07-11 stsp {
5754 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5755 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5756 818c7501 2019-07-11 stsp unsigned char status;
5757 818c7501 2019-07-11 stsp struct stat sb;
5758 818c7501 2019-07-11 stsp char *ondisk_path;
5759 818c7501 2019-07-11 stsp
5760 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5761 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5762 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5763 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5764 818c7501 2019-07-11 stsp
5765 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5766 818c7501 2019-07-11 stsp == -1)
5767 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5768 818c7501 2019-07-11 stsp
5769 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5770 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5771 818c7501 2019-07-11 stsp free(ondisk_path);
5772 818c7501 2019-07-11 stsp if (err)
5773 818c7501 2019-07-11 stsp return err;
5774 818c7501 2019-07-11 stsp
5775 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5776 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5777 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5778 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5779 818c7501 2019-07-11 stsp
5780 818c7501 2019-07-11 stsp return NULL;
5781 818c7501 2019-07-11 stsp }
5782 818c7501 2019-07-11 stsp
5783 818c7501 2019-07-11 stsp const struct got_error *
5784 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5785 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5786 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5787 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5788 818c7501 2019-07-11 stsp {
5789 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5790 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5791 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5792 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5793 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5794 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5795 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5796 818c7501 2019-07-11 stsp
5797 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5798 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5799 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5800 818c7501 2019-07-11 stsp
5801 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5802 818c7501 2019-07-11 stsp if (err)
5803 818c7501 2019-07-11 stsp return err;
5804 818c7501 2019-07-11 stsp
5805 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5806 818c7501 2019-07-11 stsp if (err)
5807 818c7501 2019-07-11 stsp goto done;
5808 818c7501 2019-07-11 stsp
5809 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5810 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5811 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5812 818c7501 2019-07-11 stsp &ok_arg);
5813 818c7501 2019-07-11 stsp if (err)
5814 818c7501 2019-07-11 stsp goto done;
5815 818c7501 2019-07-11 stsp
5816 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5817 818c7501 2019-07-11 stsp if (err)
5818 818c7501 2019-07-11 stsp goto done;
5819 818c7501 2019-07-11 stsp
5820 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5821 818c7501 2019-07-11 stsp if (err)
5822 818c7501 2019-07-11 stsp goto done;
5823 818c7501 2019-07-11 stsp
5824 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5825 818c7501 2019-07-11 stsp if (err)
5826 818c7501 2019-07-11 stsp goto done;
5827 818c7501 2019-07-11 stsp
5828 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5829 818c7501 2019-07-11 stsp 0);
5830 e51d7b55 2020-01-04 stsp if (err)
5831 e51d7b55 2020-01-04 stsp goto done;
5832 e51d7b55 2020-01-04 stsp
5833 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5834 818c7501 2019-07-11 stsp if (err)
5835 818c7501 2019-07-11 stsp goto done;
5836 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5837 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5838 e51d7b55 2020-01-04 stsp goto done;
5839 e51d7b55 2020-01-04 stsp }
5840 818c7501 2019-07-11 stsp
5841 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5842 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5843 818c7501 2019-07-11 stsp if (err)
5844 818c7501 2019-07-11 stsp goto done;
5845 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5846 818c7501 2019-07-11 stsp if (err)
5847 818c7501 2019-07-11 stsp goto done;
5848 818c7501 2019-07-11 stsp
5849 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5850 818c7501 2019-07-11 stsp
5851 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5852 818c7501 2019-07-11 stsp if (err)
5853 818c7501 2019-07-11 stsp goto done;
5854 818c7501 2019-07-11 stsp
5855 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5856 818c7501 2019-07-11 stsp if (err)
5857 818c7501 2019-07-11 stsp goto done;
5858 818c7501 2019-07-11 stsp
5859 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5860 818c7501 2019-07-11 stsp worktree->base_commit_id);
5861 818c7501 2019-07-11 stsp if (err)
5862 818c7501 2019-07-11 stsp goto done;
5863 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5864 818c7501 2019-07-11 stsp if (err)
5865 818c7501 2019-07-11 stsp goto done;
5866 818c7501 2019-07-11 stsp
5867 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5868 818c7501 2019-07-11 stsp if (err)
5869 818c7501 2019-07-11 stsp goto done;
5870 818c7501 2019-07-11 stsp done:
5871 818c7501 2019-07-11 stsp free(fileindex_path);
5872 818c7501 2019-07-11 stsp free(tmp_branch_name);
5873 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5874 818c7501 2019-07-11 stsp free(branch_ref_name);
5875 818c7501 2019-07-11 stsp if (branch_ref)
5876 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5877 818c7501 2019-07-11 stsp if (wt_branch)
5878 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5879 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5880 818c7501 2019-07-11 stsp if (err) {
5881 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5882 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5883 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5884 818c7501 2019-07-11 stsp }
5885 818c7501 2019-07-11 stsp if (*tmp_branch) {
5886 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5887 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5888 818c7501 2019-07-11 stsp }
5889 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5890 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5891 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5892 3e3a69f1 2019-07-25 stsp }
5893 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5894 818c7501 2019-07-11 stsp }
5895 818c7501 2019-07-11 stsp return err;
5896 818c7501 2019-07-11 stsp }
5897 818c7501 2019-07-11 stsp
5898 818c7501 2019-07-11 stsp const struct got_error *
5899 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5900 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5901 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5902 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5903 818c7501 2019-07-11 stsp {
5904 818c7501 2019-07-11 stsp const struct got_error *err;
5905 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5906 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5907 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5908 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5909 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5910 818c7501 2019-07-11 stsp
5911 818c7501 2019-07-11 stsp *commit_id = NULL;
5912 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5913 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5914 3e3a69f1 2019-07-25 stsp *branch = NULL;
5915 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5916 3e3a69f1 2019-07-25 stsp
5917 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5918 3e3a69f1 2019-07-25 stsp if (err)
5919 3e3a69f1 2019-07-25 stsp return err;
5920 818c7501 2019-07-11 stsp
5921 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5922 3e3a69f1 2019-07-25 stsp if (err)
5923 f032f1f7 2019-08-04 stsp goto done;
5924 f032f1f7 2019-08-04 stsp
5925 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5926 f032f1f7 2019-08-04 stsp &have_staged_files);
5927 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5928 f032f1f7 2019-08-04 stsp goto done;
5929 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5930 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5931 3e3a69f1 2019-07-25 stsp goto done;
5932 f032f1f7 2019-08-04 stsp }
5933 3e3a69f1 2019-07-25 stsp
5934 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5935 818c7501 2019-07-11 stsp if (err)
5936 f032f1f7 2019-08-04 stsp goto done;
5937 818c7501 2019-07-11 stsp
5938 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5939 818c7501 2019-07-11 stsp if (err)
5940 818c7501 2019-07-11 stsp goto done;
5941 818c7501 2019-07-11 stsp
5942 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5943 818c7501 2019-07-11 stsp if (err)
5944 818c7501 2019-07-11 stsp goto done;
5945 818c7501 2019-07-11 stsp
5946 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5947 818c7501 2019-07-11 stsp if (err)
5948 818c7501 2019-07-11 stsp goto done;
5949 818c7501 2019-07-11 stsp
5950 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5951 818c7501 2019-07-11 stsp if (err)
5952 818c7501 2019-07-11 stsp goto done;
5953 818c7501 2019-07-11 stsp
5954 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5955 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5956 818c7501 2019-07-11 stsp if (err)
5957 818c7501 2019-07-11 stsp goto done;
5958 818c7501 2019-07-11 stsp
5959 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5960 818c7501 2019-07-11 stsp if (err)
5961 818c7501 2019-07-11 stsp goto done;
5962 818c7501 2019-07-11 stsp
5963 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5964 818c7501 2019-07-11 stsp if (err)
5965 818c7501 2019-07-11 stsp goto done;
5966 818c7501 2019-07-11 stsp
5967 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5968 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5969 818c7501 2019-07-11 stsp if (err)
5970 818c7501 2019-07-11 stsp goto done;
5971 818c7501 2019-07-11 stsp
5972 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5973 818c7501 2019-07-11 stsp if (err)
5974 818c7501 2019-07-11 stsp goto done;
5975 818c7501 2019-07-11 stsp done:
5976 818c7501 2019-07-11 stsp free(commit_ref_name);
5977 818c7501 2019-07-11 stsp free(branch_ref_name);
5978 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5979 818c7501 2019-07-11 stsp if (commit_ref)
5980 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5981 818c7501 2019-07-11 stsp if (branch_ref)
5982 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5983 818c7501 2019-07-11 stsp if (err) {
5984 818c7501 2019-07-11 stsp free(*commit_id);
5985 818c7501 2019-07-11 stsp *commit_id = NULL;
5986 818c7501 2019-07-11 stsp if (*tmp_branch) {
5987 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5988 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5989 818c7501 2019-07-11 stsp }
5990 818c7501 2019-07-11 stsp if (*new_base_branch) {
5991 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5992 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5993 818c7501 2019-07-11 stsp }
5994 818c7501 2019-07-11 stsp if (*branch) {
5995 818c7501 2019-07-11 stsp got_ref_close(*branch);
5996 818c7501 2019-07-11 stsp *branch = NULL;
5997 818c7501 2019-07-11 stsp }
5998 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5999 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6000 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6001 3e3a69f1 2019-07-25 stsp }
6002 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6003 818c7501 2019-07-11 stsp }
6004 818c7501 2019-07-11 stsp return err;
6005 c4296144 2019-05-09 stsp }
6006 818c7501 2019-07-11 stsp
6007 818c7501 2019-07-11 stsp const struct got_error *
6008 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6009 818c7501 2019-07-11 stsp {
6010 818c7501 2019-07-11 stsp const struct got_error *err;
6011 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6012 818c7501 2019-07-11 stsp
6013 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6014 818c7501 2019-07-11 stsp if (err)
6015 818c7501 2019-07-11 stsp return err;
6016 818c7501 2019-07-11 stsp
6017 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6018 818c7501 2019-07-11 stsp free(tmp_branch_name);
6019 818c7501 2019-07-11 stsp return NULL;
6020 818c7501 2019-07-11 stsp }
6021 818c7501 2019-07-11 stsp
6022 818c7501 2019-07-11 stsp static const struct got_error *
6023 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6024 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6025 818c7501 2019-07-11 stsp {
6026 0ebf8283 2019-07-24 stsp *logmsg = arg;
6027 818c7501 2019-07-11 stsp return NULL;
6028 818c7501 2019-07-11 stsp }
6029 818c7501 2019-07-11 stsp
6030 818c7501 2019-07-11 stsp static const struct got_error *
6031 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6032 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6033 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6034 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6035 818c7501 2019-07-11 stsp {
6036 818c7501 2019-07-11 stsp return NULL;
6037 01757395 2019-07-12 stsp }
6038 01757395 2019-07-12 stsp
6039 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6040 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6041 01757395 2019-07-12 stsp void *progress_arg;
6042 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6043 01757395 2019-07-12 stsp };
6044 01757395 2019-07-12 stsp
6045 01757395 2019-07-12 stsp static const struct got_error *
6046 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6047 01757395 2019-07-12 stsp {
6048 01757395 2019-07-12 stsp const struct got_error *err;
6049 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6050 01757395 2019-07-12 stsp char *p;
6051 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6052 01757395 2019-07-12 stsp
6053 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6054 01757395 2019-07-12 stsp if (err)
6055 01757395 2019-07-12 stsp return err;
6056 01757395 2019-07-12 stsp
6057 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6058 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6059 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6060 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6061 01757395 2019-07-12 stsp return NULL;
6062 01757395 2019-07-12 stsp
6063 01757395 2019-07-12 stsp p = strdup(path);
6064 01757395 2019-07-12 stsp if (p == NULL)
6065 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6066 01757395 2019-07-12 stsp
6067 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6068 01757395 2019-07-12 stsp if (err || new == NULL)
6069 01757395 2019-07-12 stsp free(p);
6070 01757395 2019-07-12 stsp return err;
6071 01757395 2019-07-12 stsp }
6072 01757395 2019-07-12 stsp
6073 01757395 2019-07-12 stsp void
6074 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6075 01757395 2019-07-12 stsp {
6076 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6077 01757395 2019-07-12 stsp
6078 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6079 01757395 2019-07-12 stsp free((char *)pe->path);
6080 01757395 2019-07-12 stsp
6081 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6082 818c7501 2019-07-11 stsp }
6083 818c7501 2019-07-11 stsp
6084 0ebf8283 2019-07-24 stsp static const struct got_error *
6085 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6086 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6087 818c7501 2019-07-11 stsp {
6088 818c7501 2019-07-11 stsp const struct got_error *err;
6089 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6090 818c7501 2019-07-11 stsp
6091 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6092 818c7501 2019-07-11 stsp if (err) {
6093 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6094 818c7501 2019-07-11 stsp goto done;
6095 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6096 818c7501 2019-07-11 stsp if (err)
6097 818c7501 2019-07-11 stsp goto done;
6098 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6099 818c7501 2019-07-11 stsp if (err)
6100 818c7501 2019-07-11 stsp goto done;
6101 de05890f 2020-03-05 stsp } else if (is_rebase) {
6102 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6103 818c7501 2019-07-11 stsp int cmp;
6104 818c7501 2019-07-11 stsp
6105 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6106 818c7501 2019-07-11 stsp if (err)
6107 818c7501 2019-07-11 stsp goto done;
6108 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6109 818c7501 2019-07-11 stsp free(stored_id);
6110 818c7501 2019-07-11 stsp if (cmp != 0) {
6111 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6112 818c7501 2019-07-11 stsp goto done;
6113 818c7501 2019-07-11 stsp }
6114 818c7501 2019-07-11 stsp }
6115 0ebf8283 2019-07-24 stsp done:
6116 0ebf8283 2019-07-24 stsp if (commit_ref)
6117 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6118 0ebf8283 2019-07-24 stsp return err;
6119 0ebf8283 2019-07-24 stsp }
6120 0ebf8283 2019-07-24 stsp
6121 0ebf8283 2019-07-24 stsp static const struct got_error *
6122 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6123 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6124 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6125 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6126 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6127 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6128 0ebf8283 2019-07-24 stsp {
6129 0ebf8283 2019-07-24 stsp const struct got_error *err;
6130 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6131 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6132 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6133 818c7501 2019-07-11 stsp
6134 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6135 0ebf8283 2019-07-24 stsp
6136 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6137 0ebf8283 2019-07-24 stsp if (err)
6138 0ebf8283 2019-07-24 stsp return err;
6139 0ebf8283 2019-07-24 stsp
6140 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6141 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6142 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6143 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6144 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6145 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6146 818c7501 2019-07-11 stsp if (commit_ref)
6147 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6148 818c7501 2019-07-11 stsp return err;
6149 818c7501 2019-07-11 stsp }
6150 818c7501 2019-07-11 stsp
6151 818c7501 2019-07-11 stsp const struct got_error *
6152 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6153 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6154 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6155 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6156 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6157 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6158 818c7501 2019-07-11 stsp {
6159 0ebf8283 2019-07-24 stsp const struct got_error *err;
6160 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6161 0ebf8283 2019-07-24 stsp
6162 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6163 0ebf8283 2019-07-24 stsp if (err)
6164 0ebf8283 2019-07-24 stsp return err;
6165 0ebf8283 2019-07-24 stsp
6166 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6167 0ebf8283 2019-07-24 stsp if (err)
6168 0ebf8283 2019-07-24 stsp goto done;
6169 0ebf8283 2019-07-24 stsp
6170 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6171 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6172 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6173 0ebf8283 2019-07-24 stsp done:
6174 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6175 0ebf8283 2019-07-24 stsp return err;
6176 0ebf8283 2019-07-24 stsp }
6177 0ebf8283 2019-07-24 stsp
6178 0ebf8283 2019-07-24 stsp const struct got_error *
6179 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6180 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6181 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6182 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6183 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6184 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6185 0ebf8283 2019-07-24 stsp {
6186 0ebf8283 2019-07-24 stsp const struct got_error *err;
6187 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6188 0ebf8283 2019-07-24 stsp
6189 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6190 0ebf8283 2019-07-24 stsp if (err)
6191 0ebf8283 2019-07-24 stsp return err;
6192 0ebf8283 2019-07-24 stsp
6193 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6194 0ebf8283 2019-07-24 stsp if (err)
6195 0ebf8283 2019-07-24 stsp goto done;
6196 0ebf8283 2019-07-24 stsp
6197 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6198 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6199 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6200 0ebf8283 2019-07-24 stsp done:
6201 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6202 0ebf8283 2019-07-24 stsp return err;
6203 0ebf8283 2019-07-24 stsp }
6204 0ebf8283 2019-07-24 stsp
6205 0ebf8283 2019-07-24 stsp static const struct got_error *
6206 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6207 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6208 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6209 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6210 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6211 0ebf8283 2019-07-24 stsp {
6212 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6213 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6214 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6215 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6216 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6217 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6218 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6219 818c7501 2019-07-11 stsp
6220 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6221 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6222 a0e95631 2019-07-12 stsp
6223 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6224 818c7501 2019-07-11 stsp
6225 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6226 a0e95631 2019-07-12 stsp if (err)
6227 3e3a69f1 2019-07-25 stsp return err;
6228 a0e95631 2019-07-12 stsp
6229 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6230 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6231 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6232 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6233 01757395 2019-07-12 stsp /*
6234 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6235 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6236 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6237 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6238 01757395 2019-07-12 stsp */
6239 01757395 2019-07-12 stsp if (merged_paths) {
6240 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6241 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6242 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6243 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6244 f2a9dc41 2019-12-13 tracey 0);
6245 01757395 2019-07-12 stsp if (err)
6246 01757395 2019-07-12 stsp goto done;
6247 01757395 2019-07-12 stsp }
6248 01757395 2019-07-12 stsp } else {
6249 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6250 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6251 01757395 2019-07-12 stsp if (err)
6252 01757395 2019-07-12 stsp goto done;
6253 01757395 2019-07-12 stsp }
6254 a0e95631 2019-07-12 stsp
6255 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6256 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6257 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6258 a0e95631 2019-07-12 stsp if (err)
6259 a0e95631 2019-07-12 stsp goto done;
6260 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6261 a0e95631 2019-07-12 stsp goto done;
6262 ff0d2220 2019-07-11 stsp }
6263 818c7501 2019-07-11 stsp
6264 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6265 edd02c5e 2019-07-11 stsp if (err)
6266 edd02c5e 2019-07-11 stsp goto done;
6267 edd02c5e 2019-07-11 stsp
6268 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6269 818c7501 2019-07-11 stsp if (err)
6270 818c7501 2019-07-11 stsp goto done;
6271 818c7501 2019-07-11 stsp
6272 5943eee2 2019-08-13 stsp if (new_logmsg) {
6273 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6274 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6275 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6276 5943eee2 2019-08-13 stsp goto done;
6277 5943eee2 2019-08-13 stsp }
6278 5943eee2 2019-08-13 stsp } else {
6279 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6280 5943eee2 2019-08-13 stsp if (err)
6281 5943eee2 2019-08-13 stsp goto done;
6282 5943eee2 2019-08-13 stsp }
6283 0ebf8283 2019-07-24 stsp
6284 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6285 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6286 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6287 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6288 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6289 a0e95631 2019-07-12 stsp if (err)
6290 a0e95631 2019-07-12 stsp goto done;
6291 a0e95631 2019-07-12 stsp
6292 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6293 a0e95631 2019-07-12 stsp if (err)
6294 a0e95631 2019-07-12 stsp goto done;
6295 a0e95631 2019-07-12 stsp
6296 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6297 a0e95631 2019-07-12 stsp if (err)
6298 a0e95631 2019-07-12 stsp goto done;
6299 a0e95631 2019-07-12 stsp
6300 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6301 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6302 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6303 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6304 a0e95631 2019-07-12 stsp err = sync_err;
6305 818c7501 2019-07-11 stsp done:
6306 a0e95631 2019-07-12 stsp free(fileindex_path);
6307 a0e95631 2019-07-12 stsp free(head_commit_id);
6308 a0e95631 2019-07-12 stsp if (head_ref)
6309 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6310 818c7501 2019-07-11 stsp if (err) {
6311 818c7501 2019-07-11 stsp free(*new_commit_id);
6312 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6313 818c7501 2019-07-11 stsp }
6314 818c7501 2019-07-11 stsp return err;
6315 818c7501 2019-07-11 stsp }
6316 818c7501 2019-07-11 stsp
6317 818c7501 2019-07-11 stsp const struct got_error *
6318 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6319 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6320 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6321 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6322 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6323 0ebf8283 2019-07-24 stsp {
6324 0ebf8283 2019-07-24 stsp const struct got_error *err;
6325 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6326 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6327 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6328 0ebf8283 2019-07-24 stsp
6329 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6330 0ebf8283 2019-07-24 stsp if (err)
6331 0ebf8283 2019-07-24 stsp return err;
6332 0ebf8283 2019-07-24 stsp
6333 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6334 0ebf8283 2019-07-24 stsp if (err)
6335 0ebf8283 2019-07-24 stsp goto done;
6336 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6337 0ebf8283 2019-07-24 stsp if (err)
6338 0ebf8283 2019-07-24 stsp goto done;
6339 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6340 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6341 0ebf8283 2019-07-24 stsp goto done;
6342 0ebf8283 2019-07-24 stsp }
6343 0ebf8283 2019-07-24 stsp
6344 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6345 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6346 0ebf8283 2019-07-24 stsp done:
6347 0ebf8283 2019-07-24 stsp if (commit_ref)
6348 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6349 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6350 0ebf8283 2019-07-24 stsp free(commit_id);
6351 0ebf8283 2019-07-24 stsp return err;
6352 0ebf8283 2019-07-24 stsp }
6353 0ebf8283 2019-07-24 stsp
6354 0ebf8283 2019-07-24 stsp const struct got_error *
6355 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6356 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6357 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6358 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6359 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6360 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6361 0ebf8283 2019-07-24 stsp {
6362 0ebf8283 2019-07-24 stsp const struct got_error *err;
6363 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6364 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6365 0ebf8283 2019-07-24 stsp
6366 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6367 0ebf8283 2019-07-24 stsp if (err)
6368 0ebf8283 2019-07-24 stsp return err;
6369 0ebf8283 2019-07-24 stsp
6370 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6371 0ebf8283 2019-07-24 stsp if (err)
6372 0ebf8283 2019-07-24 stsp goto done;
6373 0ebf8283 2019-07-24 stsp
6374 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6375 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6376 0ebf8283 2019-07-24 stsp done:
6377 0ebf8283 2019-07-24 stsp if (commit_ref)
6378 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6379 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6380 0ebf8283 2019-07-24 stsp return err;
6381 0ebf8283 2019-07-24 stsp }
6382 0ebf8283 2019-07-24 stsp
6383 0ebf8283 2019-07-24 stsp const struct got_error *
6384 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6385 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6386 818c7501 2019-07-11 stsp {
6387 3e3a69f1 2019-07-25 stsp if (fileindex)
6388 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6389 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6390 69844fba 2019-07-11 stsp }
6391 69844fba 2019-07-11 stsp
6392 69844fba 2019-07-11 stsp static const struct got_error *
6393 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6394 69844fba 2019-07-11 stsp {
6395 69844fba 2019-07-11 stsp const struct got_error *err;
6396 69844fba 2019-07-11 stsp struct got_reference *ref;
6397 69844fba 2019-07-11 stsp
6398 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6399 69844fba 2019-07-11 stsp if (err) {
6400 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6401 69844fba 2019-07-11 stsp return NULL;
6402 69844fba 2019-07-11 stsp return err;
6403 69844fba 2019-07-11 stsp }
6404 69844fba 2019-07-11 stsp
6405 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6406 69844fba 2019-07-11 stsp got_ref_close(ref);
6407 69844fba 2019-07-11 stsp return err;
6408 818c7501 2019-07-11 stsp }
6409 818c7501 2019-07-11 stsp
6410 69844fba 2019-07-11 stsp static const struct got_error *
6411 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6412 69844fba 2019-07-11 stsp {
6413 69844fba 2019-07-11 stsp const struct got_error *err;
6414 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6415 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6416 69844fba 2019-07-11 stsp
6417 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6418 69844fba 2019-07-11 stsp if (err)
6419 69844fba 2019-07-11 stsp goto done;
6420 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6421 69844fba 2019-07-11 stsp if (err)
6422 69844fba 2019-07-11 stsp goto done;
6423 69844fba 2019-07-11 stsp
6424 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6425 69844fba 2019-07-11 stsp if (err)
6426 69844fba 2019-07-11 stsp goto done;
6427 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6428 69844fba 2019-07-11 stsp if (err)
6429 69844fba 2019-07-11 stsp goto done;
6430 69844fba 2019-07-11 stsp
6431 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6432 69844fba 2019-07-11 stsp if (err)
6433 69844fba 2019-07-11 stsp goto done;
6434 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6435 69844fba 2019-07-11 stsp if (err)
6436 69844fba 2019-07-11 stsp goto done;
6437 69844fba 2019-07-11 stsp
6438 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6439 69844fba 2019-07-11 stsp if (err)
6440 69844fba 2019-07-11 stsp goto done;
6441 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6442 69844fba 2019-07-11 stsp if (err)
6443 69844fba 2019-07-11 stsp goto done;
6444 69844fba 2019-07-11 stsp
6445 69844fba 2019-07-11 stsp done:
6446 69844fba 2019-07-11 stsp free(tmp_branch_name);
6447 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6448 69844fba 2019-07-11 stsp free(branch_ref_name);
6449 69844fba 2019-07-11 stsp free(commit_ref_name);
6450 69844fba 2019-07-11 stsp return err;
6451 69844fba 2019-07-11 stsp }
6452 69844fba 2019-07-11 stsp
6453 818c7501 2019-07-11 stsp const struct got_error *
6454 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6455 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6456 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6457 818c7501 2019-07-11 stsp struct got_repository *repo)
6458 818c7501 2019-07-11 stsp {
6459 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6460 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6461 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6462 818c7501 2019-07-11 stsp
6463 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6464 818c7501 2019-07-11 stsp if (err)
6465 818c7501 2019-07-11 stsp return err;
6466 818c7501 2019-07-11 stsp
6467 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6468 818c7501 2019-07-11 stsp if (err)
6469 818c7501 2019-07-11 stsp goto done;
6470 818c7501 2019-07-11 stsp
6471 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6472 818c7501 2019-07-11 stsp if (err)
6473 818c7501 2019-07-11 stsp goto done;
6474 818c7501 2019-07-11 stsp
6475 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6476 818c7501 2019-07-11 stsp if (err)
6477 818c7501 2019-07-11 stsp goto done;
6478 818c7501 2019-07-11 stsp
6479 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6480 a615e0e7 2020-12-16 stsp if (err)
6481 a615e0e7 2020-12-16 stsp goto done;
6482 a615e0e7 2020-12-16 stsp
6483 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6484 a615e0e7 2020-12-16 stsp if (err)
6485 a615e0e7 2020-12-16 stsp goto done;
6486 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6487 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6488 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6489 a615e0e7 2020-12-16 stsp err = sync_err;
6490 818c7501 2019-07-11 stsp done:
6491 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6492 a615e0e7 2020-12-16 stsp free(fileindex_path);
6493 818c7501 2019-07-11 stsp free(new_head_commit_id);
6494 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6495 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6496 818c7501 2019-07-11 stsp err = unlockerr;
6497 818c7501 2019-07-11 stsp return err;
6498 818c7501 2019-07-11 stsp }
6499 818c7501 2019-07-11 stsp
6500 818c7501 2019-07-11 stsp const struct got_error *
6501 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6502 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6503 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6504 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6505 818c7501 2019-07-11 stsp {
6506 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6507 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6508 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6509 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6510 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6511 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6512 818c7501 2019-07-11 stsp
6513 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6514 818c7501 2019-07-11 stsp if (err)
6515 818c7501 2019-07-11 stsp return err;
6516 818c7501 2019-07-11 stsp
6517 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6518 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6519 818c7501 2019-07-11 stsp if (err)
6520 818c7501 2019-07-11 stsp goto done;
6521 818c7501 2019-07-11 stsp
6522 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6523 818c7501 2019-07-11 stsp if (err)
6524 818c7501 2019-07-11 stsp goto done;
6525 818c7501 2019-07-11 stsp
6526 818c7501 2019-07-11 stsp /*
6527 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6528 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6529 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6530 818c7501 2019-07-11 stsp */
6531 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6532 818c7501 2019-07-11 stsp if (err)
6533 818c7501 2019-07-11 stsp goto done;
6534 818c7501 2019-07-11 stsp
6535 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6536 818c7501 2019-07-11 stsp if (err)
6537 818c7501 2019-07-11 stsp goto done;
6538 818c7501 2019-07-11 stsp
6539 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6540 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6541 ca355955 2019-07-12 stsp if (err)
6542 ca355955 2019-07-12 stsp goto done;
6543 ca355955 2019-07-12 stsp
6544 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6545 ca355955 2019-07-12 stsp if (err)
6546 ca355955 2019-07-12 stsp goto done;
6547 ca355955 2019-07-12 stsp
6548 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6549 818c7501 2019-07-11 stsp if (err)
6550 818c7501 2019-07-11 stsp goto done;
6551 818c7501 2019-07-11 stsp
6552 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6553 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6554 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6555 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6556 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6557 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6558 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6559 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6560 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6561 55bd499d 2019-07-12 stsp if (err)
6562 1f1abb7e 2019-08-08 stsp goto sync;
6563 55bd499d 2019-07-12 stsp
6564 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6565 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6566 ca355955 2019-07-12 stsp sync:
6567 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6568 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6569 ca355955 2019-07-12 stsp err = sync_err;
6570 818c7501 2019-07-11 stsp done:
6571 818c7501 2019-07-11 stsp got_ref_close(resolved);
6572 a3a2faf2 2019-07-12 stsp free(tree_id);
6573 818c7501 2019-07-11 stsp free(commit_id);
6574 0ebf8283 2019-07-24 stsp if (fileindex)
6575 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6576 0ebf8283 2019-07-24 stsp free(fileindex_path);
6577 0ebf8283 2019-07-24 stsp
6578 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6579 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6580 0ebf8283 2019-07-24 stsp err = unlockerr;
6581 0ebf8283 2019-07-24 stsp return err;
6582 0ebf8283 2019-07-24 stsp }
6583 0ebf8283 2019-07-24 stsp
6584 0ebf8283 2019-07-24 stsp const struct got_error *
6585 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6586 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6587 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6588 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6589 0ebf8283 2019-07-24 stsp {
6590 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6591 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6592 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6593 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6594 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6595 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6596 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6597 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6598 0ebf8283 2019-07-24 stsp
6599 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6600 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6601 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6602 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6603 0ebf8283 2019-07-24 stsp
6604 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6605 0ebf8283 2019-07-24 stsp if (err)
6606 0ebf8283 2019-07-24 stsp return err;
6607 0ebf8283 2019-07-24 stsp
6608 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6609 0ebf8283 2019-07-24 stsp if (err)
6610 0ebf8283 2019-07-24 stsp goto done;
6611 0ebf8283 2019-07-24 stsp
6612 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6613 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6614 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6615 0ebf8283 2019-07-24 stsp &ok_arg);
6616 0ebf8283 2019-07-24 stsp if (err)
6617 0ebf8283 2019-07-24 stsp goto done;
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6620 0ebf8283 2019-07-24 stsp if (err)
6621 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp
6623 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6624 0ebf8283 2019-07-24 stsp if (err)
6625 0ebf8283 2019-07-24 stsp goto done;
6626 0ebf8283 2019-07-24 stsp
6627 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6628 0ebf8283 2019-07-24 stsp worktree);
6629 0ebf8283 2019-07-24 stsp if (err)
6630 0ebf8283 2019-07-24 stsp goto done;
6631 0ebf8283 2019-07-24 stsp
6632 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6633 0ebf8283 2019-07-24 stsp 0);
6634 0ebf8283 2019-07-24 stsp if (err)
6635 0ebf8283 2019-07-24 stsp goto done;
6636 0ebf8283 2019-07-24 stsp
6637 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6638 0ebf8283 2019-07-24 stsp if (err)
6639 0ebf8283 2019-07-24 stsp goto done;
6640 0ebf8283 2019-07-24 stsp
6641 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6642 0ebf8283 2019-07-24 stsp if (err)
6643 0ebf8283 2019-07-24 stsp goto done;
6644 0ebf8283 2019-07-24 stsp
6645 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6646 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6647 0ebf8283 2019-07-24 stsp if (err)
6648 0ebf8283 2019-07-24 stsp goto done;
6649 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6650 0ebf8283 2019-07-24 stsp if (err)
6651 0ebf8283 2019-07-24 stsp goto done;
6652 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6653 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6654 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6655 0ebf8283 2019-07-24 stsp goto done;
6656 0ebf8283 2019-07-24 stsp }
6657 0ebf8283 2019-07-24 stsp
6658 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6659 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6660 0ebf8283 2019-07-24 stsp if (err)
6661 0ebf8283 2019-07-24 stsp goto done;
6662 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6663 0ebf8283 2019-07-24 stsp if (err)
6664 0ebf8283 2019-07-24 stsp goto done;
6665 0ebf8283 2019-07-24 stsp
6666 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6667 0ebf8283 2019-07-24 stsp if (err)
6668 0ebf8283 2019-07-24 stsp goto done;
6669 0ebf8283 2019-07-24 stsp done:
6670 0ebf8283 2019-07-24 stsp free(fileindex_path);
6671 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6672 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6673 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6674 0ebf8283 2019-07-24 stsp if (wt_branch)
6675 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6676 0ebf8283 2019-07-24 stsp if (err) {
6677 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6678 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6679 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6680 0ebf8283 2019-07-24 stsp }
6681 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6682 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6683 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6684 0ebf8283 2019-07-24 stsp }
6685 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6686 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6687 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6688 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6689 3e3a69f1 2019-07-25 stsp }
6690 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6691 0ebf8283 2019-07-24 stsp }
6692 0ebf8283 2019-07-24 stsp return err;
6693 0ebf8283 2019-07-24 stsp }
6694 0ebf8283 2019-07-24 stsp
6695 0ebf8283 2019-07-24 stsp const struct got_error *
6696 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6697 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6698 0ebf8283 2019-07-24 stsp {
6699 3e3a69f1 2019-07-25 stsp if (fileindex)
6700 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6701 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6702 0ebf8283 2019-07-24 stsp }
6703 0ebf8283 2019-07-24 stsp
6704 0ebf8283 2019-07-24 stsp const struct got_error *
6705 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6706 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6707 0ebf8283 2019-07-24 stsp {
6708 0ebf8283 2019-07-24 stsp const struct got_error *err;
6709 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6710 0ebf8283 2019-07-24 stsp
6711 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6712 0ebf8283 2019-07-24 stsp if (err)
6713 0ebf8283 2019-07-24 stsp return err;
6714 0ebf8283 2019-07-24 stsp
6715 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6716 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6717 0ebf8283 2019-07-24 stsp return NULL;
6718 0ebf8283 2019-07-24 stsp }
6719 0ebf8283 2019-07-24 stsp
6720 0ebf8283 2019-07-24 stsp const struct got_error *
6721 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6722 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6723 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6724 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6725 0ebf8283 2019-07-24 stsp {
6726 0ebf8283 2019-07-24 stsp const struct got_error *err;
6727 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6728 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6730 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6731 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6732 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6733 0ebf8283 2019-07-24 stsp
6734 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6735 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6736 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6737 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6738 0ebf8283 2019-07-24 stsp
6739 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6740 3e3a69f1 2019-07-25 stsp if (err)
6741 3e3a69f1 2019-07-25 stsp return err;
6742 3e3a69f1 2019-07-25 stsp
6743 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6744 3e3a69f1 2019-07-25 stsp if (err)
6745 f032f1f7 2019-08-04 stsp goto done;
6746 f032f1f7 2019-08-04 stsp
6747 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 f032f1f7 2019-08-04 stsp &have_staged_files);
6749 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6750 f032f1f7 2019-08-04 stsp goto done;
6751 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6752 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6753 3e3a69f1 2019-07-25 stsp goto done;
6754 f032f1f7 2019-08-04 stsp }
6755 3e3a69f1 2019-07-25 stsp
6756 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6757 0ebf8283 2019-07-24 stsp if (err)
6758 f032f1f7 2019-08-04 stsp goto done;
6759 0ebf8283 2019-07-24 stsp
6760 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6761 0ebf8283 2019-07-24 stsp if (err)
6762 0ebf8283 2019-07-24 stsp goto done;
6763 0ebf8283 2019-07-24 stsp
6764 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6765 0ebf8283 2019-07-24 stsp if (err)
6766 0ebf8283 2019-07-24 stsp goto done;
6767 0ebf8283 2019-07-24 stsp
6768 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6769 0ebf8283 2019-07-24 stsp worktree);
6770 0ebf8283 2019-07-24 stsp if (err)
6771 0ebf8283 2019-07-24 stsp goto done;
6772 0ebf8283 2019-07-24 stsp
6773 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6774 0ebf8283 2019-07-24 stsp if (err)
6775 0ebf8283 2019-07-24 stsp goto done;
6776 0ebf8283 2019-07-24 stsp
6777 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6778 0ebf8283 2019-07-24 stsp if (err)
6779 0ebf8283 2019-07-24 stsp goto done;
6780 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6781 0ebf8283 2019-07-24 stsp if (err)
6782 0ebf8283 2019-07-24 stsp goto done;
6783 0ebf8283 2019-07-24 stsp
6784 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6785 0ebf8283 2019-07-24 stsp if (err)
6786 0ebf8283 2019-07-24 stsp goto done;
6787 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6788 0ebf8283 2019-07-24 stsp if (err)
6789 0ebf8283 2019-07-24 stsp goto done;
6790 0ebf8283 2019-07-24 stsp
6791 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6792 0ebf8283 2019-07-24 stsp if (err)
6793 0ebf8283 2019-07-24 stsp goto done;
6794 0ebf8283 2019-07-24 stsp done:
6795 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6796 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6797 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6798 0ebf8283 2019-07-24 stsp if (commit_ref)
6799 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6800 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6801 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6802 0ebf8283 2019-07-24 stsp if (err) {
6803 0ebf8283 2019-07-24 stsp free(*commit_id);
6804 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6805 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6806 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6807 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6808 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6809 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6810 0ebf8283 2019-07-24 stsp }
6811 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6812 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6813 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6814 3e3a69f1 2019-07-25 stsp }
6815 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6816 0ebf8283 2019-07-24 stsp }
6817 0ebf8283 2019-07-24 stsp return err;
6818 0ebf8283 2019-07-24 stsp }
6819 0ebf8283 2019-07-24 stsp
6820 0ebf8283 2019-07-24 stsp static const struct got_error *
6821 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6822 0ebf8283 2019-07-24 stsp {
6823 0ebf8283 2019-07-24 stsp const struct got_error *err;
6824 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6825 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6826 0ebf8283 2019-07-24 stsp
6827 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6828 0ebf8283 2019-07-24 stsp if (err)
6829 0ebf8283 2019-07-24 stsp goto done;
6830 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6831 0ebf8283 2019-07-24 stsp if (err)
6832 0ebf8283 2019-07-24 stsp goto done;
6833 0ebf8283 2019-07-24 stsp
6834 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6835 0ebf8283 2019-07-24 stsp worktree);
6836 0ebf8283 2019-07-24 stsp if (err)
6837 0ebf8283 2019-07-24 stsp goto done;
6838 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6839 0ebf8283 2019-07-24 stsp if (err)
6840 0ebf8283 2019-07-24 stsp goto done;
6841 0ebf8283 2019-07-24 stsp
6842 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6843 0ebf8283 2019-07-24 stsp if (err)
6844 0ebf8283 2019-07-24 stsp goto done;
6845 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6846 0ebf8283 2019-07-24 stsp if (err)
6847 0ebf8283 2019-07-24 stsp goto done;
6848 0ebf8283 2019-07-24 stsp
6849 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6850 0ebf8283 2019-07-24 stsp if (err)
6851 0ebf8283 2019-07-24 stsp goto done;
6852 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6853 0ebf8283 2019-07-24 stsp if (err)
6854 0ebf8283 2019-07-24 stsp goto done;
6855 0ebf8283 2019-07-24 stsp done:
6856 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6857 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6858 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6859 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6860 0ebf8283 2019-07-24 stsp return err;
6861 0ebf8283 2019-07-24 stsp }
6862 0ebf8283 2019-07-24 stsp
6863 0ebf8283 2019-07-24 stsp const struct got_error *
6864 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6865 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6866 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6867 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6868 0ebf8283 2019-07-24 stsp {
6869 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6870 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6871 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6872 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6873 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6874 0ebf8283 2019-07-24 stsp
6875 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6876 0ebf8283 2019-07-24 stsp if (err)
6877 0ebf8283 2019-07-24 stsp return err;
6878 0ebf8283 2019-07-24 stsp
6879 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6880 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6881 0ebf8283 2019-07-24 stsp if (err)
6882 0ebf8283 2019-07-24 stsp goto done;
6883 0ebf8283 2019-07-24 stsp
6884 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6885 0ebf8283 2019-07-24 stsp if (err)
6886 0ebf8283 2019-07-24 stsp goto done;
6887 0ebf8283 2019-07-24 stsp
6888 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6889 0ebf8283 2019-07-24 stsp if (err)
6890 0ebf8283 2019-07-24 stsp goto done;
6891 0ebf8283 2019-07-24 stsp
6892 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6893 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6894 0ebf8283 2019-07-24 stsp if (err)
6895 0ebf8283 2019-07-24 stsp goto done;
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6898 0ebf8283 2019-07-24 stsp if (err)
6899 0ebf8283 2019-07-24 stsp goto done;
6900 0ebf8283 2019-07-24 stsp
6901 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6902 0ebf8283 2019-07-24 stsp if (err)
6903 0ebf8283 2019-07-24 stsp goto done;
6904 0ebf8283 2019-07-24 stsp
6905 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6906 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6907 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6908 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6909 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6910 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6911 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6912 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6913 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6914 0ebf8283 2019-07-24 stsp if (err)
6915 1f1abb7e 2019-08-08 stsp goto sync;
6916 0ebf8283 2019-07-24 stsp
6917 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6918 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6919 0ebf8283 2019-07-24 stsp sync:
6920 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6921 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6922 0ebf8283 2019-07-24 stsp err = sync_err;
6923 0ebf8283 2019-07-24 stsp done:
6924 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6925 0ebf8283 2019-07-24 stsp free(tree_id);
6926 818c7501 2019-07-11 stsp free(fileindex_path);
6927 818c7501 2019-07-11 stsp
6928 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6929 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6930 818c7501 2019-07-11 stsp err = unlockerr;
6931 818c7501 2019-07-11 stsp return err;
6932 818c7501 2019-07-11 stsp }
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp const struct got_error *
6935 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6936 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6937 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6938 0ebf8283 2019-07-24 stsp {
6939 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6940 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6941 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6942 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6945 0ebf8283 2019-07-24 stsp if (err)
6946 0ebf8283 2019-07-24 stsp return err;
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6949 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6950 0ebf8283 2019-07-24 stsp if (err)
6951 0ebf8283 2019-07-24 stsp goto done;
6952 0ebf8283 2019-07-24 stsp
6953 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6954 0ebf8283 2019-07-24 stsp if (err)
6955 0ebf8283 2019-07-24 stsp goto done;
6956 0ebf8283 2019-07-24 stsp
6957 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6958 0ebf8283 2019-07-24 stsp if (err)
6959 0ebf8283 2019-07-24 stsp goto done;
6960 0ebf8283 2019-07-24 stsp
6961 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6962 0ebf8283 2019-07-24 stsp if (err)
6963 0ebf8283 2019-07-24 stsp goto done;
6964 0ebf8283 2019-07-24 stsp
6965 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6966 a615e0e7 2020-12-16 stsp if (err)
6967 a615e0e7 2020-12-16 stsp goto done;
6968 a615e0e7 2020-12-16 stsp
6969 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6970 a615e0e7 2020-12-16 stsp if (err)
6971 a615e0e7 2020-12-16 stsp goto done;
6972 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6973 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6974 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6975 a615e0e7 2020-12-16 stsp err = sync_err;
6976 0ebf8283 2019-07-24 stsp done:
6977 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6978 a615e0e7 2020-12-16 stsp free(fileindex_path);
6979 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6980 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6981 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6982 0ebf8283 2019-07-24 stsp err = unlockerr;
6983 0ebf8283 2019-07-24 stsp return err;
6984 0ebf8283 2019-07-24 stsp }
6985 0ebf8283 2019-07-24 stsp
6986 0ebf8283 2019-07-24 stsp const struct got_error *
6987 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6988 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6989 0ebf8283 2019-07-24 stsp {
6990 0ebf8283 2019-07-24 stsp const struct got_error *err;
6991 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6992 0ebf8283 2019-07-24 stsp
6993 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6994 0ebf8283 2019-07-24 stsp if (err)
6995 0ebf8283 2019-07-24 stsp return err;
6996 0ebf8283 2019-07-24 stsp
6997 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6998 0ebf8283 2019-07-24 stsp if (err)
6999 0ebf8283 2019-07-24 stsp goto done;
7000 0ebf8283 2019-07-24 stsp
7001 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7002 0ebf8283 2019-07-24 stsp done:
7003 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7004 2822a352 2019-10-15 stsp return err;
7005 2822a352 2019-10-15 stsp }
7006 2822a352 2019-10-15 stsp
7007 2822a352 2019-10-15 stsp const struct got_error *
7008 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7009 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7010 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7011 2822a352 2019-10-15 stsp struct got_repository *repo)
7012 2822a352 2019-10-15 stsp {
7013 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7014 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7015 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7016 2822a352 2019-10-15 stsp
7017 2822a352 2019-10-15 stsp *fileindex = NULL;
7018 2822a352 2019-10-15 stsp *branch_ref = NULL;
7019 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7020 2822a352 2019-10-15 stsp
7021 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7022 2822a352 2019-10-15 stsp if (err)
7023 2822a352 2019-10-15 stsp return err;
7024 2822a352 2019-10-15 stsp
7025 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7026 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7027 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7028 2822a352 2019-10-15 stsp "update -b or different branch name required");
7029 2822a352 2019-10-15 stsp goto done;
7030 2822a352 2019-10-15 stsp }
7031 2822a352 2019-10-15 stsp
7032 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7033 2822a352 2019-10-15 stsp if (err)
7034 2822a352 2019-10-15 stsp goto done;
7035 2822a352 2019-10-15 stsp
7036 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7037 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7038 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7039 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7040 2822a352 2019-10-15 stsp &ok_arg);
7041 2822a352 2019-10-15 stsp if (err)
7042 2822a352 2019-10-15 stsp goto done;
7043 2822a352 2019-10-15 stsp
7044 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7045 2822a352 2019-10-15 stsp if (err)
7046 2822a352 2019-10-15 stsp goto done;
7047 2822a352 2019-10-15 stsp
7048 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7049 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7050 2822a352 2019-10-15 stsp done:
7051 2822a352 2019-10-15 stsp if (err) {
7052 2822a352 2019-10-15 stsp if (*branch_ref) {
7053 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7054 2822a352 2019-10-15 stsp *branch_ref = NULL;
7055 2822a352 2019-10-15 stsp }
7056 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7057 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7058 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7059 2822a352 2019-10-15 stsp }
7060 2822a352 2019-10-15 stsp if (*fileindex) {
7061 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7062 2822a352 2019-10-15 stsp *fileindex = NULL;
7063 2822a352 2019-10-15 stsp }
7064 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7065 2822a352 2019-10-15 stsp }
7066 0cb83759 2019-08-03 stsp return err;
7067 0cb83759 2019-08-03 stsp }
7068 0cb83759 2019-08-03 stsp
7069 2822a352 2019-10-15 stsp const struct got_error *
7070 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7071 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7072 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7073 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7074 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7075 2822a352 2019-10-15 stsp {
7076 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7077 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7078 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7079 2822a352 2019-10-15 stsp
7080 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7081 2822a352 2019-10-15 stsp if (err)
7082 2822a352 2019-10-15 stsp goto done;
7083 2822a352 2019-10-15 stsp
7084 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7085 2822a352 2019-10-15 stsp if (err)
7086 2822a352 2019-10-15 stsp goto done;
7087 2822a352 2019-10-15 stsp
7088 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7089 2822a352 2019-10-15 stsp worktree->path_prefix);
7090 2822a352 2019-10-15 stsp if (err)
7091 2822a352 2019-10-15 stsp goto done;
7092 2822a352 2019-10-15 stsp
7093 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7094 2822a352 2019-10-15 stsp if (err)
7095 2822a352 2019-10-15 stsp goto done;
7096 2822a352 2019-10-15 stsp
7097 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7098 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7099 2822a352 2019-10-15 stsp if (err)
7100 2822a352 2019-10-15 stsp goto sync;
7101 2822a352 2019-10-15 stsp
7102 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7103 2822a352 2019-10-15 stsp if (err)
7104 2822a352 2019-10-15 stsp goto sync;
7105 2822a352 2019-10-15 stsp
7106 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7107 2822a352 2019-10-15 stsp sync:
7108 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7109 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7110 2822a352 2019-10-15 stsp err = sync_err;
7111 2822a352 2019-10-15 stsp
7112 2822a352 2019-10-15 stsp done:
7113 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7114 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7115 2822a352 2019-10-15 stsp err = unlockerr;
7116 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7117 2822a352 2019-10-15 stsp
7118 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7119 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7120 2822a352 2019-10-15 stsp err = unlockerr;
7121 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7122 2822a352 2019-10-15 stsp
7123 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7124 2822a352 2019-10-15 stsp free(fileindex_path);
7125 2822a352 2019-10-15 stsp free(tree_id);
7126 2822a352 2019-10-15 stsp
7127 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7128 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7129 2822a352 2019-10-15 stsp err = unlockerr;
7130 2822a352 2019-10-15 stsp return err;
7131 2822a352 2019-10-15 stsp }
7132 2822a352 2019-10-15 stsp
7133 2822a352 2019-10-15 stsp const struct got_error *
7134 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7135 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7136 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7137 2822a352 2019-10-15 stsp {
7138 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7139 8b692cd0 2019-10-21 stsp
7140 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7141 8b692cd0 2019-10-21 stsp
7142 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7143 8b692cd0 2019-10-21 stsp
7144 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7145 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7146 8b692cd0 2019-10-21 stsp err = unlockerr;
7147 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7148 8b692cd0 2019-10-21 stsp
7149 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7150 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7151 8b692cd0 2019-10-21 stsp err = unlockerr;
7152 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7153 8b692cd0 2019-10-21 stsp
7154 8b692cd0 2019-10-21 stsp return err;
7155 2822a352 2019-10-15 stsp }
7156 2822a352 2019-10-15 stsp
7157 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7158 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7159 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7160 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7161 2db2652d 2019-08-07 stsp struct got_repository *repo;
7162 2db2652d 2019-08-07 stsp int have_changes;
7163 2db2652d 2019-08-07 stsp };
7164 2db2652d 2019-08-07 stsp
7165 2db2652d 2019-08-07 stsp const struct got_error *
7166 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7167 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7168 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7169 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7170 735ef5ac 2019-08-03 stsp {
7171 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7172 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7173 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7174 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7175 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7176 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7177 8b13ce36 2019-08-08 stsp
7178 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7179 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7180 8b13ce36 2019-08-08 stsp return NULL;
7181 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7182 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7183 735ef5ac 2019-08-03 stsp
7184 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7185 735ef5ac 2019-08-03 stsp if (ie == NULL)
7186 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7187 735ef5ac 2019-08-03 stsp
7188 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7189 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7190 735ef5ac 2019-08-03 stsp relpath) == -1)
7191 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7192 735ef5ac 2019-08-03 stsp
7193 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7194 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7195 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7196 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7197 735ef5ac 2019-08-03 stsp }
7198 735ef5ac 2019-08-03 stsp
7199 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7200 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7201 3aa5969e 2019-08-06 stsp goto done;
7202 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7203 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7204 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7205 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7206 735ef5ac 2019-08-03 stsp goto done;
7207 3aa5969e 2019-08-06 stsp }
7208 735ef5ac 2019-08-03 stsp
7209 2db2652d 2019-08-07 stsp a->have_changes = 1;
7210 2db2652d 2019-08-07 stsp
7211 735ef5ac 2019-08-03 stsp p = in_repo_path;
7212 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7213 735ef5ac 2019-08-03 stsp p++;
7214 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7215 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7216 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7217 735ef5ac 2019-08-03 stsp done:
7218 735ef5ac 2019-08-03 stsp free(in_repo_path);
7219 dc424a06 2019-08-07 stsp return err;
7220 dc424a06 2019-08-07 stsp }
7221 dc424a06 2019-08-07 stsp
7222 2db2652d 2019-08-07 stsp struct stage_path_arg {
7223 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7224 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7225 2db2652d 2019-08-07 stsp struct got_repository *repo;
7226 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7227 2db2652d 2019-08-07 stsp void *status_arg;
7228 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7229 2db2652d 2019-08-07 stsp void *patch_arg;
7230 7b5dc508 2019-10-28 stsp int staged_something;
7231 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7232 2db2652d 2019-08-07 stsp };
7233 2db2652d 2019-08-07 stsp
7234 2db2652d 2019-08-07 stsp static const struct got_error *
7235 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7236 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7237 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7238 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7239 0cb83759 2019-08-03 stsp {
7240 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7241 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7242 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7243 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7244 0cb83759 2019-08-03 stsp uint32_t stage;
7245 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7246 0aeb8099 2020-07-23 stsp struct stat sb;
7247 8b13ce36 2019-08-08 stsp
7248 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7249 8b13ce36 2019-08-08 stsp return NULL;
7250 0cb83759 2019-08-03 stsp
7251 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7252 d3e7c587 2019-08-03 stsp if (ie == NULL)
7253 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7254 0cb83759 2019-08-03 stsp
7255 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7256 2db2652d 2019-08-07 stsp relpath)== -1)
7257 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7258 0cb83759 2019-08-03 stsp
7259 0cb83759 2019-08-03 stsp switch (status) {
7260 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7261 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7262 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7263 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7264 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7265 0aeb8099 2020-07-23 stsp break;
7266 0aeb8099 2020-07-23 stsp }
7267 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7268 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7269 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7270 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7271 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7272 dc424a06 2019-08-07 stsp if (err)
7273 dc424a06 2019-08-07 stsp break;
7274 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7275 dc424a06 2019-08-07 stsp break;
7276 dc424a06 2019-08-07 stsp } else {
7277 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7278 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7279 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7280 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7281 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7282 dc424a06 2019-08-07 stsp break;
7283 dc424a06 2019-08-07 stsp }
7284 dc424a06 2019-08-07 stsp }
7285 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7286 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7287 0cb83759 2019-08-03 stsp if (err)
7288 dc424a06 2019-08-07 stsp break;
7289 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7290 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7291 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7292 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7293 0cb83759 2019-08-03 stsp else
7294 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7295 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7296 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7297 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7298 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7299 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7300 35213c7c 2020-07-23 stsp ssize_t target_len;
7301 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7302 35213c7c 2020-07-23 stsp sizeof(target_path));
7303 35213c7c 2020-07-23 stsp if (target_len == -1) {
7304 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7305 35213c7c 2020-07-23 stsp ondisk_path);
7306 35213c7c 2020-07-23 stsp break;
7307 35213c7c 2020-07-23 stsp }
7308 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7309 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7310 35213c7c 2020-07-23 stsp a->worktree->root_path);
7311 35213c7c 2020-07-23 stsp if (err)
7312 35213c7c 2020-07-23 stsp break;
7313 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7314 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7315 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7316 35213c7c 2020-07-23 stsp break;
7317 35213c7c 2020-07-23 stsp }
7318 35213c7c 2020-07-23 stsp }
7319 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7320 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7321 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7322 35213c7c 2020-07-23 stsp else
7323 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7324 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7325 0aeb8099 2020-07-23 stsp } else {
7326 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7327 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7328 0aeb8099 2020-07-23 stsp }
7329 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7330 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7331 dc424a06 2019-08-07 stsp break;
7332 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7333 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7334 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7335 0cb83759 2019-08-03 stsp break;
7336 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7337 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7338 d3e7c587 2019-08-03 stsp break;
7339 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7340 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7341 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7342 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7343 dc424a06 2019-08-07 stsp if (err)
7344 dc424a06 2019-08-07 stsp break;
7345 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7346 88f33a19 2019-08-08 stsp break;
7347 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7348 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7349 dc424a06 2019-08-07 stsp break;
7350 88f33a19 2019-08-08 stsp }
7351 dc424a06 2019-08-07 stsp }
7352 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7353 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7354 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7355 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7356 dc424a06 2019-08-07 stsp break;
7357 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7358 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7359 12463d8b 2019-12-13 stsp de_name);
7360 0cb83759 2019-08-03 stsp break;
7361 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7362 d3e7c587 2019-08-03 stsp break;
7363 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7364 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7365 ebf48fd5 2019-08-03 stsp break;
7366 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7367 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7368 2a06fe5f 2019-08-24 stsp break;
7369 0cb83759 2019-08-03 stsp default:
7370 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7371 537ac44b 2019-08-03 stsp break;
7372 0cb83759 2019-08-03 stsp }
7373 dc424a06 2019-08-07 stsp
7374 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7375 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7376 dc424a06 2019-08-07 stsp free(path_content);
7377 2db2652d 2019-08-07 stsp free(ondisk_path);
7378 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7379 0ebf8283 2019-07-24 stsp return err;
7380 0ebf8283 2019-07-24 stsp }
7381 0cb83759 2019-08-03 stsp
7382 0cb83759 2019-08-03 stsp const struct got_error *
7383 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7384 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7385 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7386 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7387 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7388 0cb83759 2019-08-03 stsp {
7389 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7390 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7391 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7392 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7393 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7394 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7395 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7396 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7397 0cb83759 2019-08-03 stsp
7398 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7399 0cb83759 2019-08-03 stsp if (err)
7400 0cb83759 2019-08-03 stsp return err;
7401 0cb83759 2019-08-03 stsp
7402 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7403 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7404 735ef5ac 2019-08-03 stsp if (err)
7405 735ef5ac 2019-08-03 stsp goto done;
7406 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7407 735ef5ac 2019-08-03 stsp if (err)
7408 735ef5ac 2019-08-03 stsp goto done;
7409 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7410 0cb83759 2019-08-03 stsp if (err)
7411 0cb83759 2019-08-03 stsp goto done;
7412 0cb83759 2019-08-03 stsp
7413 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7414 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7415 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7416 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7417 2db2652d 2019-08-07 stsp oka.repo = repo;
7418 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7419 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7420 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7421 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7422 735ef5ac 2019-08-03 stsp if (err)
7423 735ef5ac 2019-08-03 stsp goto done;
7424 735ef5ac 2019-08-03 stsp }
7425 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7426 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7427 2db2652d 2019-08-07 stsp goto done;
7428 2db2652d 2019-08-07 stsp }
7429 735ef5ac 2019-08-03 stsp
7430 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7431 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7432 2db2652d 2019-08-07 stsp spa.repo = repo;
7433 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7434 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7435 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7436 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7437 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7438 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7439 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7440 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7441 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7442 42005733 2019-08-03 stsp if (err)
7443 2db2652d 2019-08-07 stsp goto done;
7444 7b5dc508 2019-10-28 stsp }
7445 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7446 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7447 7b5dc508 2019-10-28 stsp goto done;
7448 0cb83759 2019-08-03 stsp }
7449 0cb83759 2019-08-03 stsp
7450 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7451 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7452 0cb83759 2019-08-03 stsp err = sync_err;
7453 0cb83759 2019-08-03 stsp done:
7454 735ef5ac 2019-08-03 stsp if (head_ref)
7455 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7456 735ef5ac 2019-08-03 stsp free(head_commit_id);
7457 0cb83759 2019-08-03 stsp free(fileindex_path);
7458 0cb83759 2019-08-03 stsp if (fileindex)
7459 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7460 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7461 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7462 0cb83759 2019-08-03 stsp err = unlockerr;
7463 0cb83759 2019-08-03 stsp return err;
7464 0cb83759 2019-08-03 stsp }
7465 ad493afc 2019-08-03 stsp
7466 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7467 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7468 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7469 ad493afc 2019-08-03 stsp struct got_repository *repo;
7470 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7471 ad493afc 2019-08-03 stsp void *progress_arg;
7472 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7473 2e1f37b0 2019-08-08 stsp void *patch_arg;
7474 ad493afc 2019-08-03 stsp };
7475 2e1f37b0 2019-08-08 stsp
7476 2e1f37b0 2019-08-08 stsp static const struct got_error *
7477 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7478 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7479 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7480 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7481 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7482 2e1f37b0 2019-08-08 stsp {
7483 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7484 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7485 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7486 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7487 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7488 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7489 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7490 2e1f37b0 2019-08-08 stsp
7491 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7492 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7493 2e1f37b0 2019-08-08 stsp
7494 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7495 2e1f37b0 2019-08-08 stsp if (err)
7496 2e1f37b0 2019-08-08 stsp return err;
7497 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7498 2e1f37b0 2019-08-08 stsp if (err)
7499 2e1f37b0 2019-08-08 stsp goto done;
7500 2e1f37b0 2019-08-08 stsp
7501 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7502 2e1f37b0 2019-08-08 stsp if (err)
7503 2e1f37b0 2019-08-08 stsp goto done;
7504 2e1f37b0 2019-08-08 stsp
7505 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7506 2e1f37b0 2019-08-08 stsp if (err)
7507 2e1f37b0 2019-08-08 stsp goto done;
7508 2e1f37b0 2019-08-08 stsp
7509 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7510 2e1f37b0 2019-08-08 stsp if (err)
7511 2e1f37b0 2019-08-08 stsp goto done;
7512 2e1f37b0 2019-08-08 stsp
7513 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7514 2e1f37b0 2019-08-08 stsp if (err)
7515 2e1f37b0 2019-08-08 stsp goto done;
7516 ad493afc 2019-08-03 stsp
7517 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7518 2e1f37b0 2019-08-08 stsp if (err)
7519 2e1f37b0 2019-08-08 stsp goto done;
7520 2e1f37b0 2019-08-08 stsp
7521 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7522 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7523 2e1f37b0 2019-08-08 stsp if (err)
7524 2e1f37b0 2019-08-08 stsp goto done;
7525 2e1f37b0 2019-08-08 stsp
7526 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7527 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7528 2e1f37b0 2019-08-08 stsp if (err)
7529 2e1f37b0 2019-08-08 stsp goto done;
7530 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7531 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7532 2e1f37b0 2019-08-08 stsp if (err)
7533 2e1f37b0 2019-08-08 stsp goto done;
7534 2e1f37b0 2019-08-08 stsp
7535 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7536 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7537 2e1f37b0 2019-08-08 stsp goto done;
7538 2e1f37b0 2019-08-08 stsp }
7539 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7540 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7541 2e1f37b0 2019-08-08 stsp goto done;
7542 2e1f37b0 2019-08-08 stsp }
7543 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7544 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7545 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7546 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7547 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7548 fe621944 2020-11-10 stsp nchanges++;
7549 fe621944 2020-11-10 stsp }
7550 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7551 2e1f37b0 2019-08-08 stsp int choice;
7552 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7553 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7554 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7555 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7556 2e1f37b0 2019-08-08 stsp if (err)
7557 2e1f37b0 2019-08-08 stsp goto done;
7558 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7559 2e1f37b0 2019-08-08 stsp have_content = 1;
7560 19e4b907 2019-08-08 stsp else
7561 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7562 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7563 2e1f37b0 2019-08-08 stsp break;
7564 2e1f37b0 2019-08-08 stsp }
7565 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7566 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7567 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7568 2e1f37b0 2019-08-08 stsp done:
7569 2e1f37b0 2019-08-08 stsp free(label1);
7570 2e1f37b0 2019-08-08 stsp if (blob)
7571 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7572 2e1f37b0 2019-08-08 stsp if (staged_blob)
7573 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7574 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7575 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7576 fe621944 2020-11-10 stsp err = free_err;
7577 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7578 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7579 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7580 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7581 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7582 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7583 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7584 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7585 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7586 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7587 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7588 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7589 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7590 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7591 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7592 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7593 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7594 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7595 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7596 2e1f37b0 2019-08-08 stsp }
7597 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7598 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7599 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7600 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7601 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7602 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7603 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7604 2e1f37b0 2019-08-08 stsp }
7605 2e1f37b0 2019-08-08 stsp free(path1);
7606 2e1f37b0 2019-08-08 stsp free(path2);
7607 fda8017d 2020-07-23 stsp return err;
7608 fda8017d 2020-07-23 stsp }
7609 fda8017d 2020-07-23 stsp
7610 fda8017d 2020-07-23 stsp static const struct got_error *
7611 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7612 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7613 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7614 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7615 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7616 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7617 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7618 fda8017d 2020-07-23 stsp {
7619 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7620 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7621 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7622 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7623 fda8017d 2020-07-23 stsp FILE *f = NULL;
7624 fda8017d 2020-07-23 stsp struct stat sb;
7625 fda8017d 2020-07-23 stsp
7626 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7627 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7628 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7629 fda8017d 2020-07-23 stsp if (err)
7630 fda8017d 2020-07-23 stsp return err;
7631 fda8017d 2020-07-23 stsp
7632 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7633 fda8017d 2020-07-23 stsp return NULL;
7634 fda8017d 2020-07-23 stsp
7635 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7636 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7637 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7638 fda8017d 2020-07-23 stsp if (err)
7639 fda8017d 2020-07-23 stsp goto done;
7640 fda8017d 2020-07-23 stsp }
7641 fda8017d 2020-07-23 stsp
7642 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7643 fda8017d 2020-07-23 stsp if (f == NULL) {
7644 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7645 fda8017d 2020-07-23 stsp path_unstaged_content);
7646 fda8017d 2020-07-23 stsp goto done;
7647 fda8017d 2020-07-23 stsp }
7648 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7649 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7650 fda8017d 2020-07-23 stsp goto done;
7651 fda8017d 2020-07-23 stsp }
7652 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7653 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7654 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7655 fda8017d 2020-07-23 stsp size_t r;
7656 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7657 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7658 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7659 fda8017d 2020-07-23 stsp goto done;
7660 fda8017d 2020-07-23 stsp }
7661 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7662 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7663 fda8017d 2020-07-23 stsp goto done;
7664 fda8017d 2020-07-23 stsp }
7665 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7666 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7667 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7668 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7669 fda8017d 2020-07-23 stsp progress_arg);
7670 fda8017d 2020-07-23 stsp } else {
7671 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7672 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7673 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7674 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7675 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7676 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7677 fda8017d 2020-07-23 stsp }
7678 fda8017d 2020-07-23 stsp if (err)
7679 fda8017d 2020-07-23 stsp goto done;
7680 fda8017d 2020-07-23 stsp
7681 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7682 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7683 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7684 2ac8aa02 2020-11-09 stsp } else {
7685 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7686 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7687 2ac8aa02 2020-11-09 stsp }
7688 fda8017d 2020-07-23 stsp done:
7689 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7690 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7691 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7692 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7693 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7694 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7695 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7696 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7697 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7698 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7699 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7700 2e1f37b0 2019-08-08 stsp return err;
7701 2e1f37b0 2019-08-08 stsp }
7702 2e1f37b0 2019-08-08 stsp
7703 ad493afc 2019-08-03 stsp static const struct got_error *
7704 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7705 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7706 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7707 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7708 ad493afc 2019-08-03 stsp {
7709 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7710 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7711 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7712 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7713 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7714 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7715 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7716 9bc94a15 2019-08-03 stsp struct stat sb;
7717 ad493afc 2019-08-03 stsp
7718 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7719 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7720 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7721 2e1f37b0 2019-08-08 stsp return NULL;
7722 2e1f37b0 2019-08-08 stsp
7723 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7724 ad493afc 2019-08-03 stsp if (ie == NULL)
7725 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7726 9bc94a15 2019-08-03 stsp
7727 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7728 9bc94a15 2019-08-03 stsp == -1)
7729 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7730 ad493afc 2019-08-03 stsp
7731 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7732 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7733 f69721c3 2019-10-21 stsp if (err)
7734 f69721c3 2019-10-21 stsp goto done;
7735 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7736 f69721c3 2019-10-21 stsp id_str) == -1) {
7737 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7738 f69721c3 2019-10-21 stsp goto done;
7739 f69721c3 2019-10-21 stsp }
7740 f69721c3 2019-10-21 stsp
7741 ad493afc 2019-08-03 stsp switch (staged_status) {
7742 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7743 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7744 ad493afc 2019-08-03 stsp blob_id, 8192);
7745 ad493afc 2019-08-03 stsp if (err)
7746 ad493afc 2019-08-03 stsp break;
7747 ad493afc 2019-08-03 stsp /* fall through */
7748 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7749 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7750 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7751 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7752 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7753 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7754 2e1f37b0 2019-08-08 stsp if (err)
7755 2e1f37b0 2019-08-08 stsp break;
7756 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7757 2e1f37b0 2019-08-08 stsp break;
7758 2e1f37b0 2019-08-08 stsp } else {
7759 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7760 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7761 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7762 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7763 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7764 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7765 2e1f37b0 2019-08-08 stsp }
7766 2e1f37b0 2019-08-08 stsp }
7767 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7768 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7769 ad493afc 2019-08-03 stsp if (err)
7770 ad493afc 2019-08-03 stsp break;
7771 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7772 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7773 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7774 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7775 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7776 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7777 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7778 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7779 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7780 ea7786be 2020-07-23 stsp break;
7781 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7782 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7783 36bf999c 2020-07-23 stsp char *staged_target;
7784 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7785 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7786 36bf999c 2020-07-23 stsp if (err)
7787 36bf999c 2020-07-23 stsp goto done;
7788 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7789 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7790 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7791 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7792 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7793 36bf999c 2020-07-23 stsp free(staged_target);
7794 dfe9fba0 2020-07-23 stsp } else {
7795 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7796 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7797 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7798 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7799 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7800 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7801 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7802 dfe9fba0 2020-07-23 stsp }
7803 ea7786be 2020-07-23 stsp break;
7804 ea7786be 2020-07-23 stsp default:
7805 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7806 ea7786be 2020-07-23 stsp break;
7807 ea7786be 2020-07-23 stsp }
7808 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7809 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7810 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7811 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7812 2ac8aa02 2020-11-09 stsp }
7813 ad493afc 2019-08-03 stsp break;
7814 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7815 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7816 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7817 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7818 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7819 2e1f37b0 2019-08-08 stsp if (err)
7820 2e1f37b0 2019-08-08 stsp break;
7821 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7822 2e1f37b0 2019-08-08 stsp break;
7823 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7824 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7825 2e1f37b0 2019-08-08 stsp break;
7826 2e1f37b0 2019-08-08 stsp }
7827 2e1f37b0 2019-08-08 stsp }
7828 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7829 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7830 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7831 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7832 9bc94a15 2019-08-03 stsp if (err)
7833 9bc94a15 2019-08-03 stsp break;
7834 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7835 ad493afc 2019-08-03 stsp break;
7836 ad493afc 2019-08-03 stsp }
7837 f69721c3 2019-10-21 stsp done:
7838 ad493afc 2019-08-03 stsp free(ondisk_path);
7839 ad493afc 2019-08-03 stsp if (blob_base)
7840 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7841 ad493afc 2019-08-03 stsp if (blob_staged)
7842 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7843 f69721c3 2019-10-21 stsp free(id_str);
7844 f69721c3 2019-10-21 stsp free(label_orig);
7845 ad493afc 2019-08-03 stsp return err;
7846 ad493afc 2019-08-03 stsp }
7847 ad493afc 2019-08-03 stsp
7848 ad493afc 2019-08-03 stsp const struct got_error *
7849 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7850 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7851 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7852 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7853 ad493afc 2019-08-03 stsp struct got_repository *repo)
7854 ad493afc 2019-08-03 stsp {
7855 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7856 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7857 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7858 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7859 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7860 ad493afc 2019-08-03 stsp
7861 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7862 ad493afc 2019-08-03 stsp if (err)
7863 ad493afc 2019-08-03 stsp return err;
7864 ad493afc 2019-08-03 stsp
7865 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7866 ad493afc 2019-08-03 stsp if (err)
7867 ad493afc 2019-08-03 stsp goto done;
7868 ad493afc 2019-08-03 stsp
7869 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7870 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7871 ad493afc 2019-08-03 stsp upa.repo = repo;
7872 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7873 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7874 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7875 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7876 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7877 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7878 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7879 ad493afc 2019-08-03 stsp if (err)
7880 ad493afc 2019-08-03 stsp goto done;
7881 ad493afc 2019-08-03 stsp }
7882 ad493afc 2019-08-03 stsp
7883 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7884 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7885 ad493afc 2019-08-03 stsp err = sync_err;
7886 ad493afc 2019-08-03 stsp done:
7887 ad493afc 2019-08-03 stsp free(fileindex_path);
7888 ad493afc 2019-08-03 stsp if (fileindex)
7889 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7890 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7891 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7892 ad493afc 2019-08-03 stsp err = unlockerr;
7893 ad493afc 2019-08-03 stsp return err;
7894 ad493afc 2019-08-03 stsp }
7895 b2118c49 2020-07-28 stsp
7896 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7897 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7898 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7899 b2118c49 2020-07-28 stsp void *info_arg;
7900 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7901 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7902 b2118c49 2020-07-28 stsp void *cancel_arg;
7903 b2118c49 2020-07-28 stsp };
7904 b2118c49 2020-07-28 stsp
7905 b2118c49 2020-07-28 stsp static const struct got_error *
7906 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7907 b2118c49 2020-07-28 stsp {
7908 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7909 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7910 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7911 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7912 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7913 b2118c49 2020-07-28 stsp int stage;
7914 b2118c49 2020-07-28 stsp
7915 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7916 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7917 b2118c49 2020-07-28 stsp
7918 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7919 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7920 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7921 b2118c49 2020-07-28 stsp break;
7922 b2118c49 2020-07-28 stsp }
7923 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7924 b2118c49 2020-07-28 stsp return NULL;
7925 b2118c49 2020-07-28 stsp
7926 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7927 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7928 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7929 b2118c49 2020-07-28 stsp }
7930 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7931 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7932 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7933 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7934 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7935 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7936 b2118c49 2020-07-28 stsp }
7937 b2118c49 2020-07-28 stsp
7938 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7939 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7940 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7941 b2118c49 2020-07-28 stsp }
7942 b2118c49 2020-07-28 stsp
7943 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7944 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7945 b2118c49 2020-07-28 stsp }
7946 b2118c49 2020-07-28 stsp
7947 b2118c49 2020-07-28 stsp const struct got_error *
7948 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7949 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7950 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7951 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7952 b2118c49 2020-07-28 stsp
7953 b2118c49 2020-07-28 stsp {
7954 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7955 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7956 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7957 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7958 b2118c49 2020-07-28 stsp
7959 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7960 b2118c49 2020-07-28 stsp if (err)
7961 b2118c49 2020-07-28 stsp return err;
7962 b2118c49 2020-07-28 stsp
7963 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7964 b2118c49 2020-07-28 stsp if (err)
7965 b2118c49 2020-07-28 stsp goto done;
7966 b2118c49 2020-07-28 stsp
7967 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7968 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7969 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7970 b2118c49 2020-07-28 stsp arg.paths = paths;
7971 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7972 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7973 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7974 b2118c49 2020-07-28 stsp &arg);
7975 b2118c49 2020-07-28 stsp done:
7976 b2118c49 2020-07-28 stsp free(fileindex_path);
7977 b2118c49 2020-07-28 stsp if (fileindex)
7978 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7979 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7980 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7981 b2118c49 2020-07-28 stsp err = unlockerr;
7982 b2118c49 2020-07-28 stsp return err;
7983 b2118c49 2020-07-28 stsp }