Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 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 60e40e95 2021-01-22 stsp
503 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
504 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
505 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
506 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
507 a6b21eef 2021-01-21 stsp }
508 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
509 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
510 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
511 60e40e95 2021-01-22 stsp free(worktree->repo_path);
512 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
513 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
514 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
515 60e40e95 2021-01-22 stsp free(worktree->root_path);
516 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
517 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
518 a06ff56f 2021-01-21 naddy free(worktree);
519 3a6ce05a 2019-02-11 stsp return err;
520 86c3caaf 2018-03-09 stsp }
521 86c3caaf 2018-03-09 stsp
522 2fbdb5ae 2018-12-29 stsp const char *
523 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
524 c7f4312f 2019-02-05 stsp {
525 c7f4312f 2019-02-05 stsp return worktree->root_path;
526 c7f4312f 2019-02-05 stsp }
527 c7f4312f 2019-02-05 stsp
528 c7f4312f 2019-02-05 stsp const char *
529 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
530 86c3caaf 2018-03-09 stsp {
531 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
532 49520a32 2018-12-29 stsp }
533 49520a32 2018-12-29 stsp const char *
534 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
535 49520a32 2018-12-29 stsp {
536 f609be2e 2018-12-29 stsp return worktree->path_prefix;
537 e5dc7198 2018-12-29 stsp }
538 e5dc7198 2018-12-29 stsp
539 e5dc7198 2018-12-29 stsp const struct got_error *
540 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 e5dc7198 2018-12-29 stsp const char *path_prefix)
542 e5dc7198 2018-12-29 stsp {
543 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
544 e5dc7198 2018-12-29 stsp
545 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
546 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
548 e5dc7198 2018-12-29 stsp }
549 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
550 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
551 e5dc7198 2018-12-29 stsp free(absprefix);
552 e5dc7198 2018-12-29 stsp return NULL;
553 86c3caaf 2018-03-09 stsp }
554 86c3caaf 2018-03-09 stsp
555 bc70eb79 2019-05-09 stsp const char *
556 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
557 86c3caaf 2018-03-09 stsp {
558 36a38700 2019-05-10 stsp return worktree->head_ref_name;
559 024e9686 2019-05-14 stsp }
560 024e9686 2019-05-14 stsp
561 024e9686 2019-05-14 stsp const struct got_error *
562 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
563 024e9686 2019-05-14 stsp struct got_reference *head_ref)
564 024e9686 2019-05-14 stsp {
565 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
566 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
567 024e9686 2019-05-14 stsp
568 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
570 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
571 024e9686 2019-05-14 stsp path_got = NULL;
572 024e9686 2019-05-14 stsp goto done;
573 024e9686 2019-05-14 stsp }
574 024e9686 2019-05-14 stsp
575 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
576 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
577 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
578 024e9686 2019-05-14 stsp goto done;
579 024e9686 2019-05-14 stsp }
580 024e9686 2019-05-14 stsp
581 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
582 024e9686 2019-05-14 stsp if (err)
583 024e9686 2019-05-14 stsp goto done;
584 024e9686 2019-05-14 stsp
585 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
586 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
587 024e9686 2019-05-14 stsp done:
588 024e9686 2019-05-14 stsp free(path_got);
589 024e9686 2019-05-14 stsp if (err)
590 024e9686 2019-05-14 stsp free(head_ref_name);
591 024e9686 2019-05-14 stsp return err;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp
594 b72f483a 2019-02-05 stsp struct got_object_id *
595 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
596 507dc3bb 2018-12-29 stsp {
597 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
598 86c3caaf 2018-03-09 stsp }
599 86c3caaf 2018-03-09 stsp
600 507dc3bb 2018-12-29 stsp const struct got_error *
601 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
603 507dc3bb 2018-12-29 stsp {
604 507dc3bb 2018-12-29 stsp const struct got_error *err;
605 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
606 507dc3bb 2018-12-29 stsp char *id_str = NULL;
607 507dc3bb 2018-12-29 stsp char *path_got = NULL;
608 507dc3bb 2018-12-29 stsp
609 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
611 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
612 507dc3bb 2018-12-29 stsp path_got = NULL;
613 507dc3bb 2018-12-29 stsp goto done;
614 507dc3bb 2018-12-29 stsp }
615 507dc3bb 2018-12-29 stsp
616 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
617 507dc3bb 2018-12-29 stsp if (err)
618 507dc3bb 2018-12-29 stsp return err;
619 507dc3bb 2018-12-29 stsp
620 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
622 507dc3bb 2018-12-29 stsp goto done;
623 507dc3bb 2018-12-29 stsp }
624 507dc3bb 2018-12-29 stsp
625 507dc3bb 2018-12-29 stsp /* Record our base commit. */
626 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
627 507dc3bb 2018-12-29 stsp if (err)
628 507dc3bb 2018-12-29 stsp goto done;
629 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 507dc3bb 2018-12-29 stsp if (err)
631 507dc3bb 2018-12-29 stsp goto done;
632 507dc3bb 2018-12-29 stsp
633 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
634 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
635 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
637 507dc3bb 2018-12-29 stsp goto done;
638 507dc3bb 2018-12-29 stsp }
639 507dc3bb 2018-12-29 stsp done:
640 507dc3bb 2018-12-29 stsp if (obj)
641 507dc3bb 2018-12-29 stsp got_object_close(obj);
642 507dc3bb 2018-12-29 stsp free(id_str);
643 507dc3bb 2018-12-29 stsp free(path_got);
644 507dc3bb 2018-12-29 stsp return err;
645 50b0790e 2020-09-11 stsp }
646 50b0790e 2020-09-11 stsp
647 50b0790e 2020-09-11 stsp const struct got_gotconfig *
648 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
649 50b0790e 2020-09-11 stsp {
650 50b0790e 2020-09-11 stsp return worktree->gotconfig;
651 507dc3bb 2018-12-29 stsp }
652 507dc3bb 2018-12-29 stsp
653 9d31a1d8 2018-03-11 stsp static const struct got_error *
654 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
655 86c3caaf 2018-03-09 stsp {
656 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
659 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
660 86c3caaf 2018-03-09 stsp return NULL;
661 21908da4 2019-01-13 stsp }
662 21908da4 2019-01-13 stsp
663 21908da4 2019-01-13 stsp static const struct got_error *
664 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
665 4a1ddfc2 2019-01-12 stsp {
666 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
667 4a1ddfc2 2019-01-12 stsp char *abspath;
668 4a1ddfc2 2019-01-12 stsp
669 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
671 4a1ddfc2 2019-01-12 stsp
672 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
673 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 ddcd8544 2019-03-11 stsp struct stat sb;
675 ddcd8544 2019-03-11 stsp err = NULL;
676 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
677 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
678 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
679 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
680 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
681 ddcd8544 2019-03-11 stsp }
682 ddcd8544 2019-03-11 stsp }
683 4a1ddfc2 2019-01-12 stsp free(abspath);
684 68c76935 2019-02-19 stsp return err;
685 68c76935 2019-02-19 stsp }
686 68c76935 2019-02-19 stsp
687 68c76935 2019-02-19 stsp static const struct got_error *
688 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
689 68c76935 2019-02-19 stsp {
690 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
691 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
692 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
693 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp *same = 1;
696 68c76935 2019-02-19 stsp
697 230a42bd 2019-05-11 jcs for (;;) {
698 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
700 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
701 68c76935 2019-02-19 stsp break;
702 68c76935 2019-02-19 stsp }
703 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
706 68c76935 2019-02-19 stsp break;
707 68c76935 2019-02-19 stsp }
708 68c76935 2019-02-19 stsp if (flen1 == 0) {
709 68c76935 2019-02-19 stsp if (flen2 != 0)
710 68c76935 2019-02-19 stsp *same = 0;
711 68c76935 2019-02-19 stsp break;
712 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
713 68c76935 2019-02-19 stsp if (flen1 != 0)
714 68c76935 2019-02-19 stsp *same = 0;
715 68c76935 2019-02-19 stsp break;
716 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
717 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
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 } else {
722 68c76935 2019-02-19 stsp *same = 0;
723 68c76935 2019-02-19 stsp break;
724 68c76935 2019-02-19 stsp }
725 68c76935 2019-02-19 stsp }
726 68c76935 2019-02-19 stsp
727 68c76935 2019-02-19 stsp return err;
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp static const struct got_error *
731 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
732 68c76935 2019-02-19 stsp {
733 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
734 68c76935 2019-02-19 stsp struct stat sb;
735 68c76935 2019-02-19 stsp size_t size1, size2;
736 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
737 68c76935 2019-02-19 stsp
738 68c76935 2019-02-19 stsp *same = 1;
739 68c76935 2019-02-19 stsp
740 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
741 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
742 68c76935 2019-02-19 stsp goto done;
743 68c76935 2019-02-19 stsp }
744 68c76935 2019-02-19 stsp size1 = sb.st_size;
745 68c76935 2019-02-19 stsp
746 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
747 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
748 68c76935 2019-02-19 stsp goto done;
749 68c76935 2019-02-19 stsp }
750 68c76935 2019-02-19 stsp size2 = sb.st_size;
751 68c76935 2019-02-19 stsp
752 68c76935 2019-02-19 stsp if (size1 != size2) {
753 68c76935 2019-02-19 stsp *same = 0;
754 68c76935 2019-02-19 stsp return NULL;
755 68c76935 2019-02-19 stsp }
756 68c76935 2019-02-19 stsp
757 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
758 68c76935 2019-02-19 stsp if (f1 == NULL)
759 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
760 68c76935 2019-02-19 stsp
761 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
762 68c76935 2019-02-19 stsp if (f2 == NULL) {
763 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
764 68c76935 2019-02-19 stsp goto done;
765 68c76935 2019-02-19 stsp }
766 68c76935 2019-02-19 stsp
767 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
768 68c76935 2019-02-19 stsp done:
769 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
770 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
771 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
772 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
773 68c76935 2019-02-19 stsp
774 6353ad76 2019-02-08 stsp return err;
775 6353ad76 2019-02-08 stsp }
776 6353ad76 2019-02-08 stsp
777 6353ad76 2019-02-08 stsp /*
778 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
779 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
780 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
781 6353ad76 2019-02-08 stsp */
782 6353ad76 2019-02-08 stsp static const struct got_error *
783 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
785 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
786 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
787 f69721c3 2019-10-21 stsp struct got_repository *repo,
788 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
789 6353ad76 2019-02-08 stsp {
790 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
791 6353ad76 2019-02-08 stsp int merged_fd = -1;
792 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
793 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
794 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
795 234035bc 2019-06-01 stsp int overlapcnt = 0;
796 3524bbf9 2020-10-20 stsp char *parent = NULL;
797 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
798 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
799 6353ad76 2019-02-08 stsp
800 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
801 234035bc 2019-06-01 stsp
802 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
803 3524bbf9 2020-10-20 stsp if (err)
804 3524bbf9 2020-10-20 stsp return err;
805 af54ae4a 2019-02-19 stsp
806 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
807 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
808 3524bbf9 2020-10-20 stsp goto done;
809 3524bbf9 2020-10-20 stsp }
810 af54ae4a 2019-02-19 stsp
811 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 6353ad76 2019-02-08 stsp if (err)
813 6353ad76 2019-02-08 stsp goto done;
814 6353ad76 2019-02-08 stsp
815 af54ae4a 2019-02-19 stsp free(base_path);
816 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
817 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
818 af54ae4a 2019-02-19 stsp base_path = NULL;
819 af54ae4a 2019-02-19 stsp goto done;
820 af54ae4a 2019-02-19 stsp }
821 af54ae4a 2019-02-19 stsp
822 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
823 6353ad76 2019-02-08 stsp if (err)
824 6353ad76 2019-02-08 stsp goto done;
825 46b6ee73 2019-06-04 stsp if (blob_orig) {
826 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
827 46b6ee73 2019-06-04 stsp blob_orig);
828 1430b4e0 2019-03-27 stsp if (err)
829 1430b4e0 2019-03-27 stsp goto done;
830 1430b4e0 2019-03-27 stsp } else {
831 1430b4e0 2019-03-27 stsp /*
832 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
833 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
834 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
835 1430b4e0 2019-03-27 stsp */
836 1430b4e0 2019-03-27 stsp }
837 6353ad76 2019-02-08 stsp
838 3b9f0f87 2020-07-23 stsp /*
839 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
840 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
841 3b9f0f87 2020-07-23 stsp */
842 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
843 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
844 3b9f0f87 2020-07-23 stsp ssize_t target_len;
845 3b9f0f87 2020-07-23 stsp size_t n;
846 3b9f0f87 2020-07-23 stsp
847 3b9f0f87 2020-07-23 stsp free(base_path);
848 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
849 3b9f0f87 2020-07-23 stsp parent) == -1) {
850 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
851 3b9f0f87 2020-07-23 stsp base_path = NULL;
852 3b9f0f87 2020-07-23 stsp goto done;
853 3b9f0f87 2020-07-23 stsp }
854 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
855 3b9f0f87 2020-07-23 stsp if (err)
856 3b9f0f87 2020-07-23 stsp goto done;
857 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
858 3b9f0f87 2020-07-23 stsp sizeof(target_path));
859 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
860 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
861 3b9f0f87 2020-07-23 stsp goto done;
862 3b9f0f87 2020-07-23 stsp }
863 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
864 3b9f0f87 2020-07-23 stsp if (n != target_len) {
865 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
866 3b9f0f87 2020-07-23 stsp goto done;
867 3b9f0f87 2020-07-23 stsp }
868 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
869 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
870 3b9f0f87 2020-07-23 stsp goto done;
871 3b9f0f87 2020-07-23 stsp }
872 3b9f0f87 2020-07-23 stsp }
873 3b9f0f87 2020-07-23 stsp
874 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
875 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
876 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
877 6353ad76 2019-02-08 stsp if (err)
878 6353ad76 2019-02-08 stsp goto done;
879 6353ad76 2019-02-08 stsp
880 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
881 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
882 1ee397ad 2019-07-12 stsp if (err)
883 1ee397ad 2019-07-12 stsp goto done;
884 6353ad76 2019-02-08 stsp
885 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
886 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
887 816dc654 2019-02-16 stsp goto done;
888 68c76935 2019-02-19 stsp }
889 68c76935 2019-02-19 stsp
890 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
891 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
892 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
893 68c76935 2019-02-19 stsp merged_path);
894 68c76935 2019-02-19 stsp if (err)
895 68c76935 2019-02-19 stsp goto done;
896 816dc654 2019-02-16 stsp }
897 6353ad76 2019-02-08 stsp
898 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
899 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
900 70a0c8ec 2019-02-20 stsp goto done;
901 70a0c8ec 2019-02-20 stsp }
902 70a0c8ec 2019-02-20 stsp
903 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
904 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
905 230a42bd 2019-05-11 jcs ondisk_path);
906 6353ad76 2019-02-08 stsp goto done;
907 6353ad76 2019-02-08 stsp }
908 6353ad76 2019-02-08 stsp done:
909 fdcb7daf 2019-12-15 stsp if (err) {
910 fdcb7daf 2019-12-15 stsp if (merged_path)
911 fdcb7daf 2019-12-15 stsp unlink(merged_path);
912 3b9f0f87 2020-07-23 stsp }
913 3b9f0f87 2020-07-23 stsp if (symlink_path) {
914 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
915 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
916 fdcb7daf 2019-12-15 stsp }
917 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
918 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
919 3b9f0f87 2020-07-23 stsp free(symlink_path);
920 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
921 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
922 56b63ca4 2021-01-22 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
923 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
924 6353ad76 2019-02-08 stsp free(merged_path);
925 af54ae4a 2019-02-19 stsp free(base_path);
926 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
927 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
928 46b6ee73 2019-06-04 stsp free(blob_orig_path);
929 af57b12a 2020-07-23 stsp }
930 3524bbf9 2020-10-20 stsp free(parent);
931 af57b12a 2020-07-23 stsp return err;
932 af57b12a 2020-07-23 stsp }
933 af57b12a 2020-07-23 stsp
934 af57b12a 2020-07-23 stsp static const struct got_error *
935 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
936 af57b12a 2020-07-23 stsp size_t target_len)
937 af57b12a 2020-07-23 stsp {
938 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
939 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
940 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
941 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
942 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
943 af57b12a 2020-07-23 stsp ondisk_path);
944 af57b12a 2020-07-23 stsp return NULL;
945 af57b12a 2020-07-23 stsp }
946 af57b12a 2020-07-23 stsp
947 af57b12a 2020-07-23 stsp /*
948 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
949 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
950 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
951 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
952 993e2a1b 2020-07-23 stsp *
953 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
954 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
955 993e2a1b 2020-07-23 stsp *
956 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
957 993e2a1b 2020-07-23 stsp * has deleted this symlink.
958 11cc08c1 2020-07-23 stsp */
959 11cc08c1 2020-07-23 stsp static const struct got_error *
960 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
961 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
962 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
963 11cc08c1 2020-07-23 stsp {
964 11cc08c1 2020-07-23 stsp const struct got_error *err;
965 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
966 11cc08c1 2020-07-23 stsp FILE *f = NULL;
967 11cc08c1 2020-07-23 stsp
968 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
969 11cc08c1 2020-07-23 stsp if (err)
970 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
971 11cc08c1 2020-07-23 stsp
972 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
973 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
974 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
975 11cc08c1 2020-07-23 stsp goto done;
976 11cc08c1 2020-07-23 stsp }
977 11cc08c1 2020-07-23 stsp
978 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
979 11cc08c1 2020-07-23 stsp if (err)
980 3818e3c4 2020-11-01 naddy goto done;
981 3818e3c4 2020-11-01 naddy
982 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
983 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
984 11cc08c1 2020-07-23 stsp goto done;
985 3818e3c4 2020-11-01 naddy }
986 11cc08c1 2020-07-23 stsp
987 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
988 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
989 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
990 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
991 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
992 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
993 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
994 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
995 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
996 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
997 11cc08c1 2020-07-23 stsp goto done;
998 11cc08c1 2020-07-23 stsp }
999 11cc08c1 2020-07-23 stsp
1000 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
1001 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
1002 11cc08c1 2020-07-23 stsp goto done;
1003 11cc08c1 2020-07-23 stsp }
1004 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
1005 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
1006 11cc08c1 2020-07-23 stsp goto done;
1007 11cc08c1 2020-07-23 stsp }
1008 11cc08c1 2020-07-23 stsp done:
1009 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1010 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1011 11cc08c1 2020-07-23 stsp free(path);
1012 11cc08c1 2020-07-23 stsp free(id_str);
1013 11cc08c1 2020-07-23 stsp free(label_deriv);
1014 11cc08c1 2020-07-23 stsp return err;
1015 11cc08c1 2020-07-23 stsp }
1016 d219f183 2020-07-23 stsp
1017 d219f183 2020-07-23 stsp /* forward declaration */
1018 d219f183 2020-07-23 stsp static const struct got_error *
1019 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1020 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1021 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1022 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1023 11cc08c1 2020-07-23 stsp
1024 11cc08c1 2020-07-23 stsp /*
1025 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1026 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1027 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1028 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1029 af57b12a 2020-07-23 stsp */
1030 af57b12a 2020-07-23 stsp static const struct got_error *
1031 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1032 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1033 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1034 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1035 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1036 af57b12a 2020-07-23 stsp {
1037 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1038 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1039 af57b12a 2020-07-23 stsp struct stat sb;
1040 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1041 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1042 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1043 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1044 af57b12a 2020-07-23 stsp
1045 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1046 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1047 af57b12a 2020-07-23 stsp
1048 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1049 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1050 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1051 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1052 af57b12a 2020-07-23 stsp ondisk_path);
1053 af57b12a 2020-07-23 stsp goto done;
1054 af57b12a 2020-07-23 stsp }
1055 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1056 af57b12a 2020-07-23 stsp
1057 526a746f 2020-07-23 stsp if (blob_orig) {
1058 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1059 526a746f 2020-07-23 stsp if (err)
1060 526a746f 2020-07-23 stsp goto done;
1061 526a746f 2020-07-23 stsp }
1062 af57b12a 2020-07-23 stsp
1063 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1064 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1065 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1066 11cc08c1 2020-07-23 stsp have_local_change = 1;
1067 11cc08c1 2020-07-23 stsp
1068 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1069 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1070 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1071 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1072 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1073 11cc08c1 2020-07-23 stsp
1074 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1075 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1076 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1077 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 11cc08c1 2020-07-23 stsp path);
1079 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1080 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1081 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1082 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1083 11cc08c1 2020-07-23 stsp path);
1084 11cc08c1 2020-07-23 stsp } else {
1085 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1086 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1087 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1088 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1089 11cc08c1 2020-07-23 stsp if (err)
1090 11cc08c1 2020-07-23 stsp goto done;
1091 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1092 11cc08c1 2020-07-23 stsp path);
1093 11cc08c1 2020-07-23 stsp }
1094 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1095 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1096 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1097 af57b12a 2020-07-23 stsp strlen(deriv_target));
1098 af57b12a 2020-07-23 stsp if (err)
1099 af57b12a 2020-07-23 stsp goto done;
1100 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1101 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1102 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1103 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1104 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1105 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1106 ea7786be 2020-07-23 stsp path);
1107 ea7786be 2020-07-23 stsp } else {
1108 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1109 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1110 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1111 ea7786be 2020-07-23 stsp if (err)
1112 ea7786be 2020-07-23 stsp goto done;
1113 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1114 ea7786be 2020-07-23 stsp path);
1115 ea7786be 2020-07-23 stsp }
1116 6353ad76 2019-02-08 stsp }
1117 11cc08c1 2020-07-23 stsp
1118 af57b12a 2020-07-23 stsp done:
1119 af57b12a 2020-07-23 stsp free(ancestor_target);
1120 14c901f1 2019-08-08 stsp return err;
1121 14c901f1 2019-08-08 stsp }
1122 14c901f1 2019-08-08 stsp
1123 14c901f1 2019-08-08 stsp /*
1124 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1125 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1126 14c901f1 2019-08-08 stsp * acts as the second derived version.
1127 14c901f1 2019-08-08 stsp */
1128 14c901f1 2019-08-08 stsp static const struct got_error *
1129 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1130 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1131 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1132 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1133 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1134 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1135 14c901f1 2019-08-08 stsp {
1136 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1137 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1138 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1139 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1140 14c901f1 2019-08-08 stsp
1141 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1142 14c901f1 2019-08-08 stsp
1143 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1144 ed6b5030 2020-10-20 stsp if (err)
1145 ed6b5030 2020-10-20 stsp return err;
1146 14c901f1 2019-08-08 stsp
1147 14c901f1 2019-08-08 stsp free(base_path);
1148 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1149 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1150 14c901f1 2019-08-08 stsp base_path = NULL;
1151 14c901f1 2019-08-08 stsp goto done;
1152 14c901f1 2019-08-08 stsp }
1153 14c901f1 2019-08-08 stsp
1154 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1155 14c901f1 2019-08-08 stsp if (err)
1156 14c901f1 2019-08-08 stsp goto done;
1157 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1158 14c901f1 2019-08-08 stsp blob_deriv);
1159 14c901f1 2019-08-08 stsp if (err)
1160 14c901f1 2019-08-08 stsp goto done;
1161 14c901f1 2019-08-08 stsp
1162 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1163 14c901f1 2019-08-08 stsp if (err)
1164 14c901f1 2019-08-08 stsp goto done;
1165 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1166 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1167 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1168 14c901f1 2019-08-08 stsp goto done;
1169 14c901f1 2019-08-08 stsp }
1170 14c901f1 2019-08-08 stsp
1171 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1172 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1173 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1174 14c901f1 2019-08-08 stsp done:
1175 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1176 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1177 14c901f1 2019-08-08 stsp free(base_path);
1178 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1179 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1180 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1181 14c901f1 2019-08-08 stsp }
1182 6353ad76 2019-02-08 stsp free(id_str);
1183 818c7501 2019-07-11 stsp free(label_deriv);
1184 ed6b5030 2020-10-20 stsp free(parent);
1185 4a1ddfc2 2019-01-12 stsp return err;
1186 4a1ddfc2 2019-01-12 stsp }
1187 4a1ddfc2 2019-01-12 stsp
1188 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1189 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1190 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1191 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1192 13d9040b 2019-03-27 stsp {
1193 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1194 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1195 13d9040b 2019-03-27 stsp
1196 65b05cec 2020-07-23 stsp *new_iep = NULL;
1197 65b05cec 2020-07-23 stsp
1198 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1199 054041d0 2020-06-24 stsp if (err)
1200 054041d0 2020-06-24 stsp return err;
1201 054041d0 2020-06-24 stsp
1202 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1203 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1204 054041d0 2020-06-24 stsp if (err)
1205 054041d0 2020-06-24 stsp goto done;
1206 054041d0 2020-06-24 stsp
1207 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1208 054041d0 2020-06-24 stsp done:
1209 054041d0 2020-06-24 stsp if (err)
1210 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1211 65b05cec 2020-07-23 stsp else
1212 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1213 13d9040b 2019-03-27 stsp return err;
1214 1ebedb77 2019-10-19 stsp }
1215 1ebedb77 2019-10-19 stsp
1216 1ebedb77 2019-10-19 stsp static mode_t
1217 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1218 1ebedb77 2019-10-19 stsp {
1219 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1220 1ebedb77 2019-10-19 stsp
1221 1ebedb77 2019-10-19 stsp if (executable) {
1222 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1223 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1224 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1225 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1226 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1227 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1228 1ebedb77 2019-10-19 stsp }
1229 1ebedb77 2019-10-19 stsp
1230 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1231 13d9040b 2019-03-27 stsp }
1232 13d9040b 2019-03-27 stsp
1233 8ba819a3 2020-07-23 stsp /* forward declaration */
1234 13d9040b 2019-03-27 stsp static const struct got_error *
1235 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1236 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1237 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1238 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1239 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1240 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1241 8ba819a3 2020-07-23 stsp
1242 5a1fbc73 2020-07-23 stsp /*
1243 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1244 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1245 5a1fbc73 2020-07-23 stsp */
1246 8ba819a3 2020-07-23 stsp static const struct got_error *
1247 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1248 5a1fbc73 2020-07-23 stsp size_t target_len)
1249 5a1fbc73 2020-07-23 stsp {
1250 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1251 5a1fbc73 2020-07-23 stsp ssize_t elen;
1252 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1253 5a1fbc73 2020-07-23 stsp int fd;
1254 5a1fbc73 2020-07-23 stsp
1255 5a1fbc73 2020-07-23 stsp /*
1256 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1257 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1258 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1259 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1260 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1261 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1262 5a1fbc73 2020-07-23 stsp */
1263 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1264 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1265 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1266 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1267 5a1fbc73 2020-07-23 stsp
1268 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1269 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1270 5a1fbc73 2020-07-23 stsp if (elen == -1)
1271 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1272 5a1fbc73 2020-07-23 stsp
1273 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1274 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1275 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1276 5a1fbc73 2020-07-23 stsp }
1277 5a1fbc73 2020-07-23 stsp
1278 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1279 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1280 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1281 5a1fbc73 2020-07-23 stsp return err;
1282 3c1ec782 2020-07-23 stsp }
1283 3c1ec782 2020-07-23 stsp
1284 3c1ec782 2020-07-23 stsp static const struct got_error *
1285 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1286 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1287 3c1ec782 2020-07-23 stsp {
1288 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1289 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1290 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1291 3c1ec782 2020-07-23 stsp
1292 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1293 3c1ec782 2020-07-23 stsp
1294 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1295 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1296 3c1ec782 2020-07-23 stsp return NULL;
1297 3c1ec782 2020-07-23 stsp }
1298 3c1ec782 2020-07-23 stsp
1299 3c1ec782 2020-07-23 stsp /*
1300 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1301 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1302 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1303 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1304 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1305 3c1ec782 2020-07-23 stsp */
1306 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1307 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1308 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1309 ce031e9e 2020-10-20 stsp if (err)
1310 ce031e9e 2020-10-20 stsp return err;
1311 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1312 ce031e9e 2020-10-20 stsp free(parent);
1313 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1314 ce031e9e 2020-10-20 stsp }
1315 ce031e9e 2020-10-20 stsp free(parent);
1316 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1317 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1318 3c1ec782 2020-07-23 stsp free(abspath);
1319 3c1ec782 2020-07-23 stsp return err;
1320 3c1ec782 2020-07-23 stsp }
1321 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1322 3c1ec782 2020-07-23 stsp free(abspath);
1323 3c1ec782 2020-07-23 stsp if (err)
1324 3c1ec782 2020-07-23 stsp return err;
1325 3c1ec782 2020-07-23 stsp } else {
1326 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1327 3c1ec782 2020-07-23 stsp if (err)
1328 3c1ec782 2020-07-23 stsp return err;
1329 3c1ec782 2020-07-23 stsp }
1330 3c1ec782 2020-07-23 stsp
1331 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1332 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1333 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1334 3c1ec782 2020-07-23 stsp return NULL;
1335 3c1ec782 2020-07-23 stsp }
1336 3c1ec782 2020-07-23 stsp
1337 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1338 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1339 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1340 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1341 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1342 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1343 3c1ec782 2020-07-23 stsp
1344 3c1ec782 2020-07-23 stsp free(path_got);
1345 3c1ec782 2020-07-23 stsp return NULL;
1346 5a1fbc73 2020-07-23 stsp }
1347 5a1fbc73 2020-07-23 stsp
1348 5a1fbc73 2020-07-23 stsp static const struct got_error *
1349 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1350 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1351 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1352 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1353 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1354 9d31a1d8 2018-03-11 stsp {
1355 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1356 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1357 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1358 906c123b 2020-07-23 stsp char *path_got = NULL;
1359 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1360 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1361 8ba819a3 2020-07-23 stsp
1362 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1363 2e1fa222 2020-07-23 stsp
1364 8ba819a3 2020-07-23 stsp /*
1365 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1366 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1367 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1368 8ba819a3 2020-07-23 stsp * in the blob object.
1369 8ba819a3 2020-07-23 stsp */
1370 8ba819a3 2020-07-23 stsp do {
1371 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1372 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1373 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1374 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1375 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1376 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1377 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1379 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1380 3b9f0f87 2020-07-23 stsp progress_arg);
1381 8ba819a3 2020-07-23 stsp }
1382 8ba819a3 2020-07-23 stsp if (len > 0) {
1383 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1384 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1385 8ba819a3 2020-07-23 stsp len - hdrlen);
1386 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1387 8ba819a3 2020-07-23 stsp hdrlen = 0;
1388 8ba819a3 2020-07-23 stsp }
1389 8ba819a3 2020-07-23 stsp } while (len != 0);
1390 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1391 8ba819a3 2020-07-23 stsp
1392 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1393 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1394 3c1ec782 2020-07-23 stsp if (err)
1395 3c1ec782 2020-07-23 stsp return err;
1396 8ba819a3 2020-07-23 stsp
1397 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1398 906c123b 2020-07-23 stsp /* install as a regular file */
1399 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1400 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1401 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1402 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1403 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1404 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1405 906c123b 2020-07-23 stsp goto done;
1406 906c123b 2020-07-23 stsp }
1407 906c123b 2020-07-23 stsp
1408 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1409 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1410 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1411 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1412 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1413 c90c8ce3 2020-07-23 stsp goto done;
1414 c90c8ce3 2020-07-23 stsp }
1415 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1416 5a1fbc73 2020-07-23 stsp target_path, target_len);
1417 5a1fbc73 2020-07-23 stsp if (err)
1418 f35fa46a 2020-07-23 stsp goto done;
1419 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1420 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1421 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1422 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1423 6e1eade5 2020-07-23 stsp path);
1424 f35fa46a 2020-07-23 stsp }
1425 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1426 f35fa46a 2020-07-23 stsp }
1427 f35fa46a 2020-07-23 stsp
1428 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1429 f4994adc 2020-10-20 stsp char *parent;
1430 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1431 f4994adc 2020-10-20 stsp if (err)
1432 f4994adc 2020-10-20 stsp goto done;
1433 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1434 f4994adc 2020-10-20 stsp free(parent);
1435 f35fa46a 2020-07-23 stsp if (err)
1436 f35fa46a 2020-07-23 stsp goto done;
1437 f35fa46a 2020-07-23 stsp /*
1438 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1439 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1440 f35fa46a 2020-07-23 stsp */
1441 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1442 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1443 f35fa46a 2020-07-23 stsp goto done;
1444 f35fa46a 2020-07-23 stsp }
1445 f35fa46a 2020-07-23 stsp }
1446 f35fa46a 2020-07-23 stsp
1447 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1448 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1449 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1450 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1451 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1452 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1453 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1454 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1455 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1456 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1457 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1458 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1459 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1460 8ba819a3 2020-07-23 stsp } else {
1461 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1462 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1463 8ba819a3 2020-07-23 stsp }
1464 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1465 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1466 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1467 8ba819a3 2020-07-23 stsp done:
1468 906c123b 2020-07-23 stsp free(path_got);
1469 8ba819a3 2020-07-23 stsp return err;
1470 8ba819a3 2020-07-23 stsp }
1471 8ba819a3 2020-07-23 stsp
1472 8ba819a3 2020-07-23 stsp static const struct got_error *
1473 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1474 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1475 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1476 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1477 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1478 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1479 8ba819a3 2020-07-23 stsp {
1480 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1481 507dc3bb 2018-12-29 stsp int fd = -1;
1482 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1483 507dc3bb 2018-12-29 stsp int update = 0;
1484 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1485 8ba819a3 2020-07-23 stsp
1486 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1487 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1488 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1489 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1490 f5375317 2020-10-20 stsp char *parent;
1491 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1492 f5375317 2020-10-20 stsp if (err)
1493 f5375317 2020-10-20 stsp return err;
1494 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1495 f5375317 2020-10-20 stsp free(parent);
1496 21908da4 2019-01-13 stsp if (err)
1497 21908da4 2019-01-13 stsp return err;
1498 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1499 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1500 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1501 21908da4 2019-01-13 stsp if (fd == -1)
1502 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1503 230a42bd 2019-05-11 jcs ondisk_path);
1504 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1505 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1506 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1507 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1508 3b9f0f87 2020-07-23 stsp goto done;
1509 3b9f0f87 2020-07-23 stsp }
1510 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1511 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1512 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1513 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1514 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1515 507dc3bb 2018-12-29 stsp goto done;
1516 d70b8e30 2018-12-27 stsp } else {
1517 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1518 507dc3bb 2018-12-29 stsp ondisk_path);
1519 507dc3bb 2018-12-29 stsp if (err)
1520 507dc3bb 2018-12-29 stsp goto done;
1521 507dc3bb 2018-12-29 stsp update = 1;
1522 9d31a1d8 2018-03-11 stsp }
1523 507dc3bb 2018-12-29 stsp } else
1524 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1525 9d31a1d8 2018-03-11 stsp }
1526 9d31a1d8 2018-03-11 stsp
1527 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1528 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1529 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1530 3818e3c4 2020-11-01 naddy goto done;
1531 3818e3c4 2020-11-01 naddy }
1532 3818e3c4 2020-11-01 naddy
1533 bd6aa359 2020-07-23 stsp if (progress_cb) {
1534 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1535 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1536 bd6aa359 2020-07-23 stsp path);
1537 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1538 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1539 bd6aa359 2020-07-23 stsp path);
1540 bd6aa359 2020-07-23 stsp else
1541 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1542 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1543 bd6aa359 2020-07-23 stsp if (err)
1544 bd6aa359 2020-07-23 stsp goto done;
1545 bd6aa359 2020-07-23 stsp }
1546 d7b62c98 2018-12-27 stsp
1547 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1548 9d31a1d8 2018-03-11 stsp do {
1549 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1550 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1551 9d31a1d8 2018-03-11 stsp if (err)
1552 9d31a1d8 2018-03-11 stsp break;
1553 9d31a1d8 2018-03-11 stsp if (len > 0) {
1554 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1555 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1556 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1557 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1558 b87c6f83 2018-12-24 stsp goto done;
1559 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1560 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1561 b87c6f83 2018-12-24 stsp goto done;
1562 9d31a1d8 2018-03-11 stsp }
1563 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1564 9d31a1d8 2018-03-11 stsp }
1565 9d31a1d8 2018-03-11 stsp } while (len != 0);
1566 9d31a1d8 2018-03-11 stsp
1567 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1568 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1569 816dc654 2019-02-16 stsp goto done;
1570 816dc654 2019-02-16 stsp }
1571 9d31a1d8 2018-03-11 stsp
1572 507dc3bb 2018-12-29 stsp if (update) {
1573 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1574 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1575 f6d8c0ac 2020-11-09 stsp goto done;
1576 f6d8c0ac 2020-11-09 stsp }
1577 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1578 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1579 230a42bd 2019-05-11 jcs ondisk_path);
1580 68ed9ba5 2019-02-10 stsp goto done;
1581 68ed9ba5 2019-02-10 stsp }
1582 63df146d 2020-11-09 stsp free(tmppath);
1583 63df146d 2020-11-09 stsp tmppath = NULL;
1584 507dc3bb 2018-12-29 stsp }
1585 507dc3bb 2018-12-29 stsp
1586 9d31a1d8 2018-03-11 stsp done:
1587 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1588 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1589 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1590 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1591 507dc3bb 2018-12-29 stsp free(tmppath);
1592 6353ad76 2019-02-08 stsp return err;
1593 6353ad76 2019-02-08 stsp }
1594 6353ad76 2019-02-08 stsp
1595 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1596 6353ad76 2019-02-08 stsp static const struct got_error *
1597 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1598 7154f6ce 2019-03-27 stsp {
1599 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1600 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1601 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1602 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1603 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1604 7154f6ce 2019-03-27 stsp };
1605 7154f6ce 2019-03-27 stsp int i = 0;
1606 9bdd68dd 2020-01-02 naddy char *line = NULL;
1607 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1608 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1609 7154f6ce 2019-03-27 stsp
1610 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1611 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1612 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1613 7154f6ce 2019-03-27 stsp if (feof(f))
1614 7154f6ce 2019-03-27 stsp break;
1615 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1616 7154f6ce 2019-03-27 stsp break;
1617 7154f6ce 2019-03-27 stsp }
1618 7154f6ce 2019-03-27 stsp
1619 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1620 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1621 19332e6d 2019-05-13 stsp == 0)
1622 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1623 7154f6ce 2019-03-27 stsp else
1624 7154f6ce 2019-03-27 stsp i++;
1625 7154f6ce 2019-03-27 stsp }
1626 7154f6ce 2019-03-27 stsp }
1627 9bdd68dd 2020-01-02 naddy free(line);
1628 7154f6ce 2019-03-27 stsp
1629 7154f6ce 2019-03-27 stsp return err;
1630 e2b1e152 2019-07-13 stsp }
1631 e2b1e152 2019-07-13 stsp
1632 e2b1e152 2019-07-13 stsp static int
1633 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1634 1ebedb77 2019-10-19 stsp {
1635 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1637 1ebedb77 2019-10-19 stsp }
1638 1ebedb77 2019-10-19 stsp
1639 1ebedb77 2019-10-19 stsp static int
1640 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1641 e2b1e152 2019-07-13 stsp {
1642 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1647 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1648 c363b2c1 2019-08-03 stsp }
1649 c363b2c1 2019-08-03 stsp
1650 c363b2c1 2019-08-03 stsp static unsigned char
1651 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1652 c363b2c1 2019-08-03 stsp {
1653 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1654 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1655 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1656 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1657 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1658 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1659 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1660 c363b2c1 2019-08-03 stsp default:
1661 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1662 a919d5c4 2020-07-23 stsp }
1663 a919d5c4 2020-07-23 stsp }
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp static const struct got_error *
1666 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1667 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1668 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1669 a919d5c4 2020-07-23 stsp {
1670 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1671 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1672 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1673 a919d5c4 2020-07-23 stsp ssize_t elen;
1674 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1675 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1677 a919d5c4 2020-07-23 stsp
1678 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1679 a919d5c4 2020-07-23 stsp
1680 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1681 a919d5c4 2020-07-23 stsp do {
1682 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1683 a919d5c4 2020-07-23 stsp if (err)
1684 a919d5c4 2020-07-23 stsp return err;
1685 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1686 a919d5c4 2020-07-23 stsp /*
1687 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1688 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1689 a919d5c4 2020-07-23 stsp */
1690 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1691 a919d5c4 2020-07-23 stsp }
1692 a919d5c4 2020-07-23 stsp if (len > 0) {
1693 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1694 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1695 a919d5c4 2020-07-23 stsp len - hdrlen);
1696 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1697 a919d5c4 2020-07-23 stsp hdrlen = 0;
1698 a919d5c4 2020-07-23 stsp }
1699 a919d5c4 2020-07-23 stsp } while (len != 0);
1700 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1701 a919d5c4 2020-07-23 stsp
1702 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1703 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 a919d5c4 2020-07-23 stsp if (elen == -1)
1705 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1706 a919d5c4 2020-07-23 stsp } else {
1707 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1708 a919d5c4 2020-07-23 stsp if (elen == -1)
1709 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1710 c363b2c1 2019-08-03 stsp }
1711 a919d5c4 2020-07-23 stsp
1712 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1714 a919d5c4 2020-07-23 stsp
1715 a919d5c4 2020-07-23 stsp return NULL;
1716 7154f6ce 2019-03-27 stsp }
1717 7154f6ce 2019-03-27 stsp
1718 7154f6ce 2019-03-27 stsp static const struct got_error *
1719 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1720 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1721 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1722 6353ad76 2019-02-08 stsp {
1723 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1724 6353ad76 2019-02-08 stsp struct got_object_id id;
1725 6353ad76 2019-02-08 stsp size_t hdrlen;
1726 1338848f 2019-12-13 stsp int fd = -1;
1727 6353ad76 2019-02-08 stsp FILE *f = NULL;
1728 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1729 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1730 6353ad76 2019-02-08 stsp size_t flen, blen;
1731 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1732 6353ad76 2019-02-08 stsp
1733 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1734 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1735 6353ad76 2019-02-08 stsp
1736 7f91a133 2019-12-13 stsp /*
1737 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1738 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1739 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1740 7f91a133 2019-12-13 stsp */
1741 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1742 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1743 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1744 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1745 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1746 882ef1b9 2019-12-13 stsp else
1747 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1748 882ef1b9 2019-12-13 stsp goto done;
1749 882ef1b9 2019-12-13 stsp }
1750 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1751 3d35a492 2019-12-13 stsp goto done;
1752 3d35a492 2019-12-13 stsp }
1753 7f91a133 2019-12-13 stsp } else {
1754 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1755 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1756 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1757 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1758 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1759 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1760 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1761 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1762 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1763 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1764 3d35a492 2019-12-13 stsp else
1765 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1766 3d35a492 2019-12-13 stsp goto done;
1767 3d35a492 2019-12-13 stsp }
1768 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1769 1338848f 2019-12-13 stsp goto done;
1770 a378724f 2019-02-10 stsp }
1771 a378724f 2019-02-10 stsp }
1772 6353ad76 2019-02-08 stsp
1773 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1774 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1775 1338848f 2019-12-13 stsp goto done;
1776 3f148bc6 2019-07-27 stsp }
1777 efdd40df 2019-07-27 stsp
1778 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1779 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1780 1338848f 2019-12-13 stsp goto done;
1781 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1782 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1783 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1784 1338848f 2019-12-13 stsp goto done;
1785 d00136be 2019-03-26 stsp }
1786 b8f41171 2019-02-10 stsp
1787 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1788 f179e66d 2020-07-23 stsp goto done;
1789 f179e66d 2020-07-23 stsp
1790 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1791 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1792 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1793 1338848f 2019-12-13 stsp goto done;
1794 f179e66d 2020-07-23 stsp }
1795 6353ad76 2019-02-08 stsp
1796 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1797 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1798 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1799 c363b2c1 2019-08-03 stsp else
1800 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1801 c363b2c1 2019-08-03 stsp
1802 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1803 6353ad76 2019-02-08 stsp if (err)
1804 1338848f 2019-12-13 stsp goto done;
1805 6353ad76 2019-02-08 stsp
1806 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1807 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1808 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1809 a919d5c4 2020-07-23 stsp goto done;
1810 a919d5c4 2020-07-23 stsp }
1811 a919d5c4 2020-07-23 stsp
1812 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1813 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1814 ab0d4361 2019-12-13 stsp if (fd == -1) {
1815 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1816 ab0d4361 2019-12-13 stsp goto done;
1817 ab0d4361 2019-12-13 stsp }
1818 3d35a492 2019-12-13 stsp }
1819 3d35a492 2019-12-13 stsp
1820 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1821 6353ad76 2019-02-08 stsp if (f == NULL) {
1822 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1823 6353ad76 2019-02-08 stsp goto done;
1824 6353ad76 2019-02-08 stsp }
1825 1338848f 2019-12-13 stsp fd = -1;
1826 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1827 656b1f76 2019-05-11 jcs for (;;) {
1828 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1829 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1830 6353ad76 2019-02-08 stsp if (err)
1831 7154f6ce 2019-03-27 stsp goto done;
1832 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1833 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1834 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1835 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1836 7154f6ce 2019-03-27 stsp goto done;
1837 80c5c120 2019-02-19 stsp }
1838 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1839 6353ad76 2019-02-08 stsp if (flen != 0)
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1841 6353ad76 2019-02-08 stsp break;
1842 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1843 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1847 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1848 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1849 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1850 6353ad76 2019-02-08 stsp break;
1851 6353ad76 2019-02-08 stsp }
1852 6353ad76 2019-02-08 stsp } else {
1853 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1854 6353ad76 2019-02-08 stsp break;
1855 6353ad76 2019-02-08 stsp }
1856 6353ad76 2019-02-08 stsp hdrlen = 0;
1857 6353ad76 2019-02-08 stsp }
1858 7154f6ce 2019-03-27 stsp
1859 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1860 7154f6ce 2019-03-27 stsp rewind(f);
1861 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1862 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1863 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1864 6353ad76 2019-02-08 stsp done:
1865 6353ad76 2019-02-08 stsp if (blob)
1866 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1867 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1868 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1869 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1870 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1871 9d31a1d8 2018-03-11 stsp return err;
1872 e2b1e152 2019-07-13 stsp }
1873 e2b1e152 2019-07-13 stsp
1874 e2b1e152 2019-07-13 stsp /*
1875 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1876 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1877 e2b1e152 2019-07-13 stsp */
1878 e2b1e152 2019-07-13 stsp static const struct got_error *
1879 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1880 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1881 e2b1e152 2019-07-13 stsp {
1882 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1883 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1884 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1885 e2b1e152 2019-07-13 stsp
1886 e2b1e152 2019-07-13 stsp return NULL;
1887 9d31a1d8 2018-03-11 stsp }
1888 9d31a1d8 2018-03-11 stsp
1889 9d31a1d8 2018-03-11 stsp static const struct got_error *
1890 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1891 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1892 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1893 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1894 0584f854 2019-04-06 stsp void *progress_arg)
1895 9d31a1d8 2018-03-11 stsp {
1896 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1897 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1898 6353ad76 2019-02-08 stsp char *ondisk_path;
1899 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1900 b8f41171 2019-02-10 stsp struct stat sb;
1901 9d31a1d8 2018-03-11 stsp
1902 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1904 6353ad76 2019-02-08 stsp
1905 abb4604f 2019-07-27 stsp if (ie) {
1906 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 a76c42e6 2019-08-03 stsp goto done;
1909 a76c42e6 2019-08-03 stsp }
1910 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 7f91a133 2019-12-13 stsp repo);
1912 abb4604f 2019-07-27 stsp if (err)
1913 abb4604f 2019-07-27 stsp goto done;
1914 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1916 c90c8ce3 2020-07-23 stsp } else {
1917 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1918 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1919 c90c8ce3 2020-07-23 stsp }
1920 6353ad76 2019-02-08 stsp
1921 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1922 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1923 b8f41171 2019-02-10 stsp goto done;
1924 b8f41171 2019-02-10 stsp }
1925 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1926 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1927 5036ab18 2020-04-18 stsp path);
1928 5036ab18 2020-04-18 stsp goto done;
1929 5036ab18 2020-04-18 stsp }
1930 b8f41171 2019-02-10 stsp
1931 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1932 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1933 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1934 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1935 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1936 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1937 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1938 e2b1e152 2019-07-13 stsp if (err)
1939 e2b1e152 2019-07-13 stsp goto done;
1940 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1941 b8f41171 2019-02-10 stsp path);
1942 6353ad76 2019-02-08 stsp goto done;
1943 a378724f 2019-02-10 stsp }
1944 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1945 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1946 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1947 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1948 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1949 b8f41171 2019-02-10 stsp goto done;
1950 e2b1e152 2019-07-13 stsp }
1951 9d31a1d8 2018-03-11 stsp }
1952 9d31a1d8 2018-03-11 stsp
1953 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1954 8da9e5f4 2019-01-12 stsp if (err)
1955 6353ad76 2019-02-08 stsp goto done;
1956 512f0d0e 2019-01-02 stsp
1957 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1958 234035bc 2019-06-01 stsp int update_timestamps;
1959 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1960 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1961 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1962 234035bc 2019-06-01 stsp struct got_object_id id2;
1963 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1964 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1965 234035bc 2019-06-01 stsp if (err)
1966 f69721c3 2019-10-21 stsp goto done;
1967 f69721c3 2019-10-21 stsp }
1968 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1969 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1970 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1971 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1972 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1973 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1974 f69721c3 2019-10-21 stsp goto done;
1975 f69721c3 2019-10-21 stsp }
1976 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1977 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1978 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1979 234035bc 2019-06-01 stsp goto done;
1980 f69721c3 2019-10-21 stsp }
1981 234035bc 2019-06-01 stsp }
1982 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1983 36bf999c 2020-07-23 stsp char *link_target;
1984 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1985 36bf999c 2020-07-23 stsp if (err)
1986 36bf999c 2020-07-23 stsp goto done;
1987 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1988 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1989 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1990 36bf999c 2020-07-23 stsp free(link_target);
1991 993e2a1b 2020-07-23 stsp } else {
1992 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1993 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1994 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1995 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1996 993e2a1b 2020-07-23 stsp }
1997 f69721c3 2019-10-21 stsp free(label_orig);
1998 234035bc 2019-06-01 stsp if (blob2)
1999 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2000 909d120e 2019-09-22 stsp if (err)
2001 909d120e 2019-09-22 stsp goto done;
2002 234035bc 2019-06-01 stsp /*
2003 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2004 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2005 234035bc 2019-06-01 stsp * unmodified files again.
2006 234035bc 2019-06-01 stsp */
2007 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2008 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2009 234035bc 2019-06-01 stsp update_timestamps);
2010 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2011 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2012 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2013 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2014 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2015 1ee397ad 2019-07-12 stsp if (err)
2016 1ee397ad 2019-07-12 stsp goto done;
2017 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2018 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2019 13d9040b 2019-03-27 stsp if (err)
2020 13d9040b 2019-03-27 stsp goto done;
2021 13d9040b 2019-03-27 stsp } else {
2022 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2023 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2024 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2025 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2026 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2027 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2028 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2029 2e1fa222 2020-07-23 stsp } else {
2030 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2031 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2032 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2033 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2034 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2035 2e1fa222 2020-07-23 stsp }
2036 13d9040b 2019-03-27 stsp if (err)
2037 13d9040b 2019-03-27 stsp goto done;
2038 65b05cec 2020-07-23 stsp
2039 054041d0 2020-06-24 stsp if (ie) {
2040 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2041 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2042 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2043 054041d0 2020-06-24 stsp } else {
2044 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2045 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2046 054041d0 2020-06-24 stsp &blob->id);
2047 054041d0 2020-06-24 stsp }
2048 13d9040b 2019-03-27 stsp if (err)
2049 13d9040b 2019-03-27 stsp goto done;
2050 65b05cec 2020-07-23 stsp
2051 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2052 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2053 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2054 2e1fa222 2020-07-23 stsp }
2055 13d9040b 2019-03-27 stsp }
2056 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2057 6353ad76 2019-02-08 stsp done:
2058 6353ad76 2019-02-08 stsp free(ondisk_path);
2059 8da9e5f4 2019-01-12 stsp return err;
2060 90285c3b 2019-01-08 stsp }
2061 90285c3b 2019-01-08 stsp
2062 90285c3b 2019-01-08 stsp static const struct got_error *
2063 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2064 90285c3b 2019-01-08 stsp {
2065 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2066 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2067 90285c3b 2019-01-08 stsp
2068 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2069 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2070 90285c3b 2019-01-08 stsp
2071 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2072 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2073 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2074 c97665ae 2019-01-13 stsp } else {
2075 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2076 fddefe3b 2020-10-20 stsp do {
2077 fddefe3b 2020-10-20 stsp char *parent;
2078 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2079 fddefe3b 2020-10-20 stsp if (err)
2080 2513f20a 2020-10-20 stsp break;
2081 fddefe3b 2020-10-20 stsp free(ondisk_path);
2082 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2083 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2084 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2085 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2086 fddefe3b 2020-10-20 stsp ondisk_path);
2087 90285c3b 2019-01-08 stsp break;
2088 90285c3b 2019-01-08 stsp }
2089 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2090 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2091 90285c3b 2019-01-08 stsp }
2092 90285c3b 2019-01-08 stsp free(ondisk_path);
2093 90285c3b 2019-01-08 stsp return err;
2094 512f0d0e 2019-01-02 stsp }
2095 512f0d0e 2019-01-02 stsp
2096 708d8e67 2019-03-27 stsp static const struct got_error *
2097 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2098 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2099 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2100 708d8e67 2019-03-27 stsp {
2101 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2102 708d8e67 2019-03-27 stsp unsigned char status;
2103 708d8e67 2019-03-27 stsp struct stat sb;
2104 708d8e67 2019-03-27 stsp char *ondisk_path;
2105 708d8e67 2019-03-27 stsp
2106 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2107 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2108 a76c42e6 2019-08-03 stsp
2109 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2110 708d8e67 2019-03-27 stsp == -1)
2111 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2112 708d8e67 2019-03-27 stsp
2113 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2114 708d8e67 2019-03-27 stsp if (err)
2115 5a58a424 2020-06-23 stsp goto done;
2116 708d8e67 2019-03-27 stsp
2117 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2118 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2119 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2120 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2121 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2122 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2123 993e2a1b 2020-07-23 stsp goto done;
2124 993e2a1b 2020-07-23 stsp }
2125 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2126 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2127 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2128 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2129 993e2a1b 2020-07-23 stsp if (err)
2130 993e2a1b 2020-07-23 stsp goto done;
2131 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2132 993e2a1b 2020-07-23 stsp ie->path);
2133 993e2a1b 2020-07-23 stsp goto done;
2134 993e2a1b 2020-07-23 stsp }
2135 993e2a1b 2020-07-23 stsp
2136 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2137 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2138 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2139 1ee397ad 2019-07-12 stsp if (err)
2140 5a58a424 2020-06-23 stsp goto done;
2141 fc6346c4 2019-03-27 stsp /*
2142 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2143 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2144 fc6346c4 2019-03-27 stsp */
2145 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2146 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2147 fc6346c4 2019-03-27 stsp } else {
2148 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2149 1ee397ad 2019-07-12 stsp if (err)
2150 5a58a424 2020-06-23 stsp goto done;
2151 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2152 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2153 fc6346c4 2019-03-27 stsp if (err)
2154 5a58a424 2020-06-23 stsp goto done;
2155 fc6346c4 2019-03-27 stsp }
2156 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2157 708d8e67 2019-03-27 stsp }
2158 5a58a424 2020-06-23 stsp done:
2159 5a58a424 2020-06-23 stsp free(ondisk_path);
2160 fc6346c4 2019-03-27 stsp return err;
2161 708d8e67 2019-03-27 stsp }
2162 708d8e67 2019-03-27 stsp
2163 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2164 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2165 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2166 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2167 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2168 8da9e5f4 2019-01-12 stsp void *progress_arg;
2169 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2170 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2171 8da9e5f4 2019-01-12 stsp };
2172 8da9e5f4 2019-01-12 stsp
2173 512f0d0e 2019-01-02 stsp static const struct got_error *
2174 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2175 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2176 512f0d0e 2019-01-02 stsp {
2177 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2178 0584f854 2019-04-06 stsp
2179 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2180 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2181 512f0d0e 2019-01-02 stsp
2182 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2183 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2184 8da9e5f4 2019-01-12 stsp }
2185 512f0d0e 2019-01-02 stsp
2186 8da9e5f4 2019-01-12 stsp static const struct got_error *
2187 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2188 8da9e5f4 2019-01-12 stsp {
2189 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2190 7a9df742 2019-01-08 stsp
2191 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2192 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2193 0584f854 2019-04-06 stsp
2194 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2195 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2196 9d31a1d8 2018-03-11 stsp }
2197 9d31a1d8 2018-03-11 stsp
2198 9d31a1d8 2018-03-11 stsp static const struct got_error *
2199 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2200 9d31a1d8 2018-03-11 stsp {
2201 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2202 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2203 8da9e5f4 2019-01-12 stsp char *path;
2204 9d31a1d8 2018-03-11 stsp
2205 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2206 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2207 0584f854 2019-04-06 stsp
2208 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2209 63c5ca5d 2019-08-24 stsp return NULL;
2210 63c5ca5d 2019-08-24 stsp
2211 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2212 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2213 8da9e5f4 2019-01-12 stsp == -1)
2214 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2215 9d31a1d8 2018-03-11 stsp
2216 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2217 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2218 8da9e5f4 2019-01-12 stsp else
2219 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2220 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2221 9d31a1d8 2018-03-11 stsp
2222 8da9e5f4 2019-01-12 stsp free(path);
2223 0cd1c46a 2019-03-11 stsp return err;
2224 0cd1c46a 2019-03-11 stsp }
2225 0cd1c46a 2019-03-11 stsp
2226 b2118c49 2020-07-28 stsp const struct got_error *
2227 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2228 b2118c49 2020-07-28 stsp {
2229 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2230 b2118c49 2020-07-28 stsp
2231 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2232 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2233 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2234 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2235 b2118c49 2020-07-28 stsp }
2236 b2118c49 2020-07-28 stsp
2237 b2118c49 2020-07-28 stsp return NULL;
2238 b2118c49 2020-07-28 stsp }
2239 b2118c49 2020-07-28 stsp
2240 818c7501 2019-07-11 stsp static const struct got_error *
2241 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2242 0cd1c46a 2019-03-11 stsp {
2243 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2244 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2245 0cd1c46a 2019-03-11 stsp
2246 517bab73 2019-03-11 stsp *refname = NULL;
2247 517bab73 2019-03-11 stsp
2248 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2249 b2118c49 2020-07-28 stsp if (err)
2250 b2118c49 2020-07-28 stsp return err;
2251 0cd1c46a 2019-03-11 stsp
2252 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2253 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2254 0647c563 2019-03-11 stsp *refname = NULL;
2255 0cd1c46a 2019-03-11 stsp }
2256 517bab73 2019-03-11 stsp free(uuidstr);
2257 517bab73 2019-03-11 stsp return err;
2258 517bab73 2019-03-11 stsp }
2259 517bab73 2019-03-11 stsp
2260 818c7501 2019-07-11 stsp const struct got_error *
2261 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2262 818c7501 2019-07-11 stsp {
2263 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2264 818c7501 2019-07-11 stsp }
2265 818c7501 2019-07-11 stsp
2266 818c7501 2019-07-11 stsp static const struct got_error *
2267 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2268 818c7501 2019-07-11 stsp {
2269 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2270 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2271 818c7501 2019-07-11 stsp }
2272 818c7501 2019-07-11 stsp
2273 818c7501 2019-07-11 stsp static const struct got_error *
2274 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2275 818c7501 2019-07-11 stsp {
2276 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2277 818c7501 2019-07-11 stsp }
2278 818c7501 2019-07-11 stsp
2279 818c7501 2019-07-11 stsp static const struct got_error *
2280 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2281 818c7501 2019-07-11 stsp {
2282 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2283 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2284 818c7501 2019-07-11 stsp }
2285 818c7501 2019-07-11 stsp
2286 818c7501 2019-07-11 stsp static const struct got_error *
2287 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2288 818c7501 2019-07-11 stsp {
2289 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2290 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2291 0ebf8283 2019-07-24 stsp }
2292 0ebf8283 2019-07-24 stsp
2293 0ebf8283 2019-07-24 stsp static const struct got_error *
2294 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2295 0ebf8283 2019-07-24 stsp {
2296 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2297 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2298 0ebf8283 2019-07-24 stsp }
2299 0ebf8283 2019-07-24 stsp
2300 0ebf8283 2019-07-24 stsp static const struct got_error *
2301 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2302 0ebf8283 2019-07-24 stsp {
2303 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2304 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2305 0ebf8283 2019-07-24 stsp }
2306 0ebf8283 2019-07-24 stsp
2307 0ebf8283 2019-07-24 stsp static const struct got_error *
2308 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 0ebf8283 2019-07-24 stsp {
2310 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2311 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2312 818c7501 2019-07-11 stsp }
2313 818c7501 2019-07-11 stsp
2314 0ebf8283 2019-07-24 stsp static const struct got_error *
2315 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2316 0ebf8283 2019-07-24 stsp {
2317 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2318 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2319 0ebf8283 2019-07-24 stsp }
2320 818c7501 2019-07-11 stsp
2321 0ebf8283 2019-07-24 stsp const struct got_error *
2322 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2323 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2324 0ebf8283 2019-07-24 stsp {
2325 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2326 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2327 0ebf8283 2019-07-24 stsp *path = NULL;
2328 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2329 0ebf8283 2019-07-24 stsp }
2330 0ebf8283 2019-07-24 stsp return NULL;
2331 0ebf8283 2019-07-24 stsp }
2332 0ebf8283 2019-07-24 stsp
2333 517bab73 2019-03-11 stsp /*
2334 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2335 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2336 517bab73 2019-03-11 stsp */
2337 517bab73 2019-03-11 stsp static const struct got_error *
2338 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2339 517bab73 2019-03-11 stsp {
2340 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2341 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2342 517bab73 2019-03-11 stsp char *refname;
2343 517bab73 2019-03-11 stsp
2344 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2345 517bab73 2019-03-11 stsp if (err)
2346 517bab73 2019-03-11 stsp return err;
2347 0cd1c46a 2019-03-11 stsp
2348 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2349 0cd1c46a 2019-03-11 stsp if (err)
2350 0cd1c46a 2019-03-11 stsp goto done;
2351 0cd1c46a 2019-03-11 stsp
2352 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2353 0cd1c46a 2019-03-11 stsp done:
2354 0cd1c46a 2019-03-11 stsp free(refname);
2355 0cd1c46a 2019-03-11 stsp if (ref)
2356 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2357 3e3a69f1 2019-07-25 stsp return err;
2358 3e3a69f1 2019-07-25 stsp }
2359 3e3a69f1 2019-07-25 stsp
2360 3e3a69f1 2019-07-25 stsp static const struct got_error *
2361 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2362 3e3a69f1 2019-07-25 stsp {
2363 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2364 3e3a69f1 2019-07-25 stsp
2365 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2366 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2367 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2368 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2369 3e3a69f1 2019-07-25 stsp }
2370 9d31a1d8 2018-03-11 stsp return err;
2371 9d31a1d8 2018-03-11 stsp }
2372 9d31a1d8 2018-03-11 stsp
2373 3e3a69f1 2019-07-25 stsp
2374 ebf99748 2019-05-09 stsp static const struct got_error *
2375 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2376 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2377 ebf99748 2019-05-09 stsp {
2378 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2379 ebf99748 2019-05-09 stsp FILE *index = NULL;
2380 0cd1c46a 2019-03-11 stsp
2381 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2382 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2383 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2384 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2385 ebf99748 2019-05-09 stsp
2386 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2387 3e3a69f1 2019-07-25 stsp if (err)
2388 ebf99748 2019-05-09 stsp goto done;
2389 ebf99748 2019-05-09 stsp
2390 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2391 ebf99748 2019-05-09 stsp if (index == NULL) {
2392 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2393 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2394 ebf99748 2019-05-09 stsp } else {
2395 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2396 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2397 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2398 ebf99748 2019-05-09 stsp }
2399 ebf99748 2019-05-09 stsp done:
2400 ebf99748 2019-05-09 stsp if (err) {
2401 ebf99748 2019-05-09 stsp free(*fileindex_path);
2402 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2403 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2404 ebf99748 2019-05-09 stsp *fileindex = NULL;
2405 ebf99748 2019-05-09 stsp }
2406 ebf99748 2019-05-09 stsp return err;
2407 ebf99748 2019-05-09 stsp }
2408 c932eeeb 2019-05-22 stsp
2409 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2410 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2411 c932eeeb 2019-05-22 stsp const char *path;
2412 c932eeeb 2019-05-22 stsp size_t path_len;
2413 c932eeeb 2019-05-22 stsp const char *entry_name;
2414 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2415 a484d721 2019-06-10 stsp void *progress_arg;
2416 c932eeeb 2019-05-22 stsp };
2417 ebf99748 2019-05-09 stsp
2418 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2419 c932eeeb 2019-05-22 stsp static const struct got_error *
2420 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2421 c932eeeb 2019-05-22 stsp {
2422 1ee397ad 2019-07-12 stsp const struct got_error *err;
2423 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2424 c932eeeb 2019-05-22 stsp
2425 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2426 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2427 c932eeeb 2019-05-22 stsp return NULL;
2428 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2429 102ff934 2019-06-11 stsp return NULL;
2430 102ff934 2019-06-11 stsp
2431 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2433 c932eeeb 2019-05-22 stsp return NULL;
2434 c932eeeb 2019-05-22 stsp
2435 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2436 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2437 edd02c5e 2019-07-11 stsp ie->path);
2438 1ee397ad 2019-07-12 stsp if (err)
2439 1ee397ad 2019-07-12 stsp return err;
2440 1ee397ad 2019-07-12 stsp }
2441 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2442 c932eeeb 2019-05-22 stsp return NULL;
2443 a615e0e7 2020-12-16 stsp }
2444 a615e0e7 2020-12-16 stsp
2445 a615e0e7 2020-12-16 stsp static const struct got_error *
2446 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2448 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2449 a615e0e7 2020-12-16 stsp {
2450 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2451 a615e0e7 2020-12-16 stsp
2452 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2453 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2454 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2455 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2456 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2457 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2458 a615e0e7 2020-12-16 stsp
2459 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2460 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2461 9c6338c4 2019-06-02 stsp }
2462 9c6338c4 2019-06-02 stsp
2463 9c6338c4 2019-06-02 stsp static const struct got_error *
2464 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2465 9c6338c4 2019-06-02 stsp {
2466 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2467 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2468 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2469 867630bb 2020-01-17 stsp struct timespec timeout;
2470 9c6338c4 2019-06-02 stsp
2471 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2472 9c6338c4 2019-06-02 stsp fileindex_path);
2473 9c6338c4 2019-06-02 stsp if (err)
2474 9c6338c4 2019-06-02 stsp goto done;
2475 9c6338c4 2019-06-02 stsp
2476 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2477 9c6338c4 2019-06-02 stsp if (err)
2478 9c6338c4 2019-06-02 stsp goto done;
2479 9c6338c4 2019-06-02 stsp
2480 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2482 9c6338c4 2019-06-02 stsp fileindex_path);
2483 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2484 9c6338c4 2019-06-02 stsp }
2485 867630bb 2020-01-17 stsp
2486 867630bb 2020-01-17 stsp /*
2487 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2488 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2489 867630bb 2020-01-17 stsp * was recorded in the file index.
2490 867630bb 2020-01-17 stsp */
2491 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2492 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2493 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2494 9c6338c4 2019-06-02 stsp done:
2495 9c6338c4 2019-06-02 stsp if (new_index)
2496 9c6338c4 2019-06-02 stsp fclose(new_index);
2497 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2498 9c6338c4 2019-06-02 stsp return err;
2499 c932eeeb 2019-05-22 stsp }
2500 6ced4176 2019-07-12 stsp
2501 6ced4176 2019-07-12 stsp static const struct got_error *
2502 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2504 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2505 6ced4176 2019-07-12 stsp {
2506 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2507 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2508 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2509 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2510 6ced4176 2019-07-12 stsp
2511 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2512 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2513 6ced4176 2019-07-12 stsp *tree_id = NULL;
2514 6ced4176 2019-07-12 stsp
2515 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2516 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2517 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2518 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2519 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2520 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2521 6ced4176 2019-07-12 stsp goto done;
2522 6ced4176 2019-07-12 stsp }
2523 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2524 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2525 6ced4176 2019-07-12 stsp if (err)
2526 6ced4176 2019-07-12 stsp goto done;
2527 6ced4176 2019-07-12 stsp return NULL;
2528 6ced4176 2019-07-12 stsp }
2529 6ced4176 2019-07-12 stsp
2530 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2531 6ced4176 2019-07-12 stsp
2532 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2533 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2534 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2535 6ced4176 2019-07-12 stsp goto done;
2536 6ced4176 2019-07-12 stsp }
2537 6ced4176 2019-07-12 stsp
2538 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2539 6ced4176 2019-07-12 stsp in_repo_path);
2540 6ced4176 2019-07-12 stsp if (err)
2541 6ced4176 2019-07-12 stsp goto done;
2542 6ced4176 2019-07-12 stsp
2543 6ced4176 2019-07-12 stsp free(in_repo_path);
2544 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2545 6ced4176 2019-07-12 stsp
2546 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2547 6ced4176 2019-07-12 stsp if (err)
2548 6ced4176 2019-07-12 stsp goto done;
2549 c932eeeb 2019-05-22 stsp
2550 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 6ced4176 2019-07-12 stsp /* Check out a single file. */
2552 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2553 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2554 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2555 6ced4176 2019-07-12 stsp if (in_repo_path == 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 *tree_relpath = strdup("");
2560 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2561 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2562 6ced4176 2019-07-12 stsp goto done;
2563 6ced4176 2019-07-12 stsp }
2564 6ced4176 2019-07-12 stsp } else {
2565 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2566 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2567 6ced4176 2019-07-12 stsp if (err)
2568 6ced4176 2019-07-12 stsp return err;
2569 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2570 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2571 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2572 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2573 6ced4176 2019-07-12 stsp goto done;
2574 6ced4176 2019-07-12 stsp }
2575 6ced4176 2019-07-12 stsp }
2576 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2577 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2578 6ced4176 2019-07-12 stsp } else {
2579 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2580 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2581 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2582 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2583 6ced4176 2019-07-12 stsp goto done;
2584 6ced4176 2019-07-12 stsp }
2585 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2586 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2587 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2588 6ced4176 2019-07-12 stsp goto done;
2589 6ced4176 2019-07-12 stsp }
2590 6ced4176 2019-07-12 stsp }
2591 6ced4176 2019-07-12 stsp done:
2592 6ced4176 2019-07-12 stsp free(id);
2593 6ced4176 2019-07-12 stsp free(in_repo_path);
2594 6ced4176 2019-07-12 stsp if (err) {
2595 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2596 6ced4176 2019-07-12 stsp free(*tree_relpath);
2597 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2598 6ced4176 2019-07-12 stsp free(*tree_id);
2599 6ced4176 2019-07-12 stsp *tree_id = NULL;
2600 6ced4176 2019-07-12 stsp }
2601 07ed472d 2019-07-12 stsp return err;
2602 07ed472d 2019-07-12 stsp }
2603 07ed472d 2019-07-12 stsp
2604 07ed472d 2019-07-12 stsp static const struct got_error *
2605 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2609 07ed472d 2019-07-12 stsp {
2610 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2611 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2612 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2613 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2614 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2615 07ed472d 2019-07-12 stsp
2616 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2617 7f47418f 2019-12-20 stsp if (err) {
2618 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2619 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2620 7f47418f 2019-12-20 stsp goto done;
2621 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2622 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2623 7f47418f 2019-12-20 stsp if (err)
2624 7f47418f 2019-12-20 stsp return err;
2625 7f47418f 2019-12-20 stsp }
2626 07ed472d 2019-07-12 stsp
2627 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2628 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2629 07ed472d 2019-07-12 stsp if (err)
2630 07ed472d 2019-07-12 stsp goto done;
2631 07ed472d 2019-07-12 stsp
2632 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2633 07ed472d 2019-07-12 stsp if (err)
2634 07ed472d 2019-07-12 stsp goto done;
2635 07ed472d 2019-07-12 stsp
2636 07ed472d 2019-07-12 stsp if (entry_name &&
2637 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2639 07ed472d 2019-07-12 stsp goto done;
2640 07ed472d 2019-07-12 stsp }
2641 07ed472d 2019-07-12 stsp
2642 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2643 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2644 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2645 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2646 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2647 07ed472d 2019-07-12 stsp arg.repo = repo;
2648 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2649 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2650 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2651 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2652 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2654 07ed472d 2019-07-12 stsp done:
2655 07ed472d 2019-07-12 stsp if (tree)
2656 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2657 07ed472d 2019-07-12 stsp if (commit)
2658 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2659 6ced4176 2019-07-12 stsp return err;
2660 6ced4176 2019-07-12 stsp }
2661 6ced4176 2019-07-12 stsp
2662 9d31a1d8 2018-03-11 stsp const struct got_error *
2663 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2664 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2665 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2667 9d31a1d8 2018-03-11 stsp {
2668 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2670 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2671 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2672 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2673 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2674 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2675 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2676 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2677 f2ea84fa 2019-07-27 stsp int entry_type;
2678 f2ea84fa 2019-07-27 stsp char *relpath;
2679 f2ea84fa 2019-07-27 stsp char *entry_name;
2680 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2681 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2682 9d31a1d8 2018-03-11 stsp
2683 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2684 f2ea84fa 2019-07-27 stsp
2685 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2686 9d31a1d8 2018-03-11 stsp if (err)
2687 9d31a1d8 2018-03-11 stsp return err;
2688 93a30277 2018-12-24 stsp
2689 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2690 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2691 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2692 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2693 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2694 f2ea84fa 2019-07-27 stsp goto done;
2695 f2ea84fa 2019-07-27 stsp }
2696 6ced4176 2019-07-12 stsp
2697 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2698 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2699 f2ea84fa 2019-07-27 stsp if (err) {
2700 f2ea84fa 2019-07-27 stsp free(tpd);
2701 6ced4176 2019-07-12 stsp goto done;
2702 6ced4176 2019-07-12 stsp }
2703 f2ea84fa 2019-07-27 stsp
2704 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2705 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2706 f2ea84fa 2019-07-27 stsp if (err) {
2707 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2708 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2709 f2ea84fa 2019-07-27 stsp free(tpd);
2710 f2ea84fa 2019-07-27 stsp goto done;
2711 f2ea84fa 2019-07-27 stsp }
2712 f2ea84fa 2019-07-27 stsp } else
2713 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2714 f2ea84fa 2019-07-27 stsp
2715 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2716 6ced4176 2019-07-12 stsp }
2717 6ced4176 2019-07-12 stsp
2718 51514078 2018-12-25 stsp /*
2719 51514078 2018-12-25 stsp * Read the file index.
2720 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2721 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2722 51514078 2018-12-25 stsp */
2723 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2724 8da9e5f4 2019-01-12 stsp if (err)
2725 c4cdcb68 2019-04-03 stsp goto done;
2726 c4cdcb68 2019-04-03 stsp
2727 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2728 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2729 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2730 f2ea84fa 2019-07-27 stsp
2731 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2732 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2733 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2734 f2ea84fa 2019-07-27 stsp if (err)
2735 f2ea84fa 2019-07-27 stsp break;
2736 f2ea84fa 2019-07-27 stsp
2737 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2738 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2739 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2740 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2741 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2742 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2743 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2744 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2745 f2ea84fa 2019-07-27 stsp if (err)
2746 f2ea84fa 2019-07-27 stsp break;
2747 f2ea84fa 2019-07-27 stsp
2748 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2749 711d9cd9 2019-07-12 stsp }
2750 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2751 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2752 af12c6b9 2019-06-04 stsp err = sync_err;
2753 9d31a1d8 2018-03-11 stsp done:
2754 9c6338c4 2019-06-02 stsp free(fileindex_path);
2755 edfa7d7f 2018-09-11 stsp if (tree)
2756 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2757 9d31a1d8 2018-03-11 stsp if (commit)
2758 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2759 5ade8233 2019-07-12 stsp if (fileindex)
2760 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2761 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2762 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2763 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2764 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2765 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2766 f2ea84fa 2019-07-27 stsp free(tpd);
2767 f2ea84fa 2019-07-27 stsp }
2768 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2769 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2770 9d31a1d8 2018-03-11 stsp err = unlockerr;
2771 f8d1f275 2019-02-04 stsp return err;
2772 f8d1f275 2019-02-04 stsp }
2773 f8d1f275 2019-02-04 stsp
2774 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2775 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2776 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2777 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2778 234035bc 2019-06-01 stsp void *progress_arg;
2779 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2780 234035bc 2019-06-01 stsp void *cancel_arg;
2781 f69721c3 2019-10-21 stsp const char *label_orig;
2782 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2783 234035bc 2019-06-01 stsp };
2784 234035bc 2019-06-01 stsp
2785 234035bc 2019-06-01 stsp static const struct got_error *
2786 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2787 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2788 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2789 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2790 234035bc 2019-06-01 stsp {
2791 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2792 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2793 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2794 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2795 234035bc 2019-06-01 stsp struct stat sb;
2796 234035bc 2019-06-01 stsp unsigned char status;
2797 234035bc 2019-06-01 stsp int local_changes_subsumed;
2798 234035bc 2019-06-01 stsp
2799 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2800 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2801 d6c87207 2019-08-02 stsp strlen(path2));
2802 1ee397ad 2019-07-12 stsp if (ie == NULL)
2803 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2804 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2805 234035bc 2019-06-01 stsp
2806 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2807 234035bc 2019-06-01 stsp path2) == -1)
2808 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2809 234035bc 2019-06-01 stsp
2810 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2811 7f91a133 2019-12-13 stsp repo);
2812 234035bc 2019-06-01 stsp if (err)
2813 234035bc 2019-06-01 stsp goto done;
2814 234035bc 2019-06-01 stsp
2815 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2816 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2817 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2818 234035bc 2019-06-01 stsp goto done;
2819 234035bc 2019-06-01 stsp }
2820 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2821 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2822 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2823 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2824 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2825 234035bc 2019-06-01 stsp goto done;
2826 234035bc 2019-06-01 stsp }
2827 234035bc 2019-06-01 stsp
2828 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2829 36bf999c 2020-07-23 stsp char *link_target2;
2830 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2831 36bf999c 2020-07-23 stsp if (err)
2832 36bf999c 2020-07-23 stsp goto done;
2833 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2834 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2835 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2836 36bf999c 2020-07-23 stsp free(link_target2);
2837 af57b12a 2020-07-23 stsp } else {
2838 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2839 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2840 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2841 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2842 af57b12a 2020-07-23 stsp }
2843 234035bc 2019-06-01 stsp } else if (blob1) {
2844 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2845 d6c87207 2019-08-02 stsp strlen(path1));
2846 1ee397ad 2019-07-12 stsp if (ie == NULL)
2847 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2848 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2849 2b92fad7 2019-06-02 stsp
2850 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2851 2b92fad7 2019-06-02 stsp path1) == -1)
2852 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2853 2b92fad7 2019-06-02 stsp
2854 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2855 7f91a133 2019-12-13 stsp repo);
2856 2b92fad7 2019-06-02 stsp if (err)
2857 2b92fad7 2019-06-02 stsp goto done;
2858 2b92fad7 2019-06-02 stsp
2859 2b92fad7 2019-06-02 stsp switch (status) {
2860 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2861 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2862 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2863 1ee397ad 2019-07-12 stsp if (err)
2864 1ee397ad 2019-07-12 stsp goto done;
2865 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2866 2b92fad7 2019-06-02 stsp if (err)
2867 2b92fad7 2019-06-02 stsp goto done;
2868 2b92fad7 2019-06-02 stsp if (ie)
2869 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2870 2b92fad7 2019-06-02 stsp break;
2871 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2872 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2873 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2874 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2875 1ee397ad 2019-07-12 stsp if (err)
2876 1ee397ad 2019-07-12 stsp goto done;
2877 2b92fad7 2019-06-02 stsp if (ie)
2878 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2879 2b92fad7 2019-06-02 stsp break;
2880 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2881 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2882 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2883 0a22ca1a 2020-09-23 stsp /*
2884 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2885 0a22ca1a 2020-09-23 stsp * exists in the repository.
2886 0a22ca1a 2020-09-23 stsp */
2887 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2888 0a22ca1a 2020-09-23 stsp if (err)
2889 0a22ca1a 2020-09-23 stsp goto done;
2890 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2891 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2892 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2893 0a22ca1a 2020-09-23 stsp if (err)
2894 0a22ca1a 2020-09-23 stsp goto done;
2895 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2896 0a22ca1a 2020-09-23 stsp path1);
2897 0a22ca1a 2020-09-23 stsp if (err)
2898 0a22ca1a 2020-09-23 stsp goto done;
2899 0a22ca1a 2020-09-23 stsp if (ie)
2900 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2901 0a22ca1a 2020-09-23 stsp ie);
2902 0a22ca1a 2020-09-23 stsp } else {
2903 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2904 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2905 0a22ca1a 2020-09-23 stsp }
2906 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2907 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2908 0a22ca1a 2020-09-23 stsp free(id);
2909 0a22ca1a 2020-09-23 stsp if (err)
2910 0a22ca1a 2020-09-23 stsp goto done;
2911 0a22ca1a 2020-09-23 stsp break;
2912 0a22ca1a 2020-09-23 stsp }
2913 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2914 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2915 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2916 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, 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 case GOT_STATUS_OBSTRUCTED:
2921 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2922 1ee397ad 2019-07-12 stsp if (err)
2923 1ee397ad 2019-07-12 stsp goto done;
2924 2b92fad7 2019-06-02 stsp break;
2925 2b92fad7 2019-06-02 stsp default:
2926 2b92fad7 2019-06-02 stsp break;
2927 2b92fad7 2019-06-02 stsp }
2928 234035bc 2019-06-01 stsp } else if (blob2) {
2929 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2930 234035bc 2019-06-01 stsp path2) == -1)
2931 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2932 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2933 d6c87207 2019-08-02 stsp strlen(path2));
2934 234035bc 2019-06-01 stsp if (ie) {
2935 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2936 7f91a133 2019-12-13 stsp -1, NULL, repo);
2937 234035bc 2019-06-01 stsp if (err)
2938 234035bc 2019-06-01 stsp goto done;
2939 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2940 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2941 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2942 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2943 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2944 1ee397ad 2019-07-12 stsp status, path2);
2945 234035bc 2019-06-01 stsp goto done;
2946 234035bc 2019-06-01 stsp }
2947 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2948 36bf999c 2020-07-23 stsp char *link_target2;
2949 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2950 36bf999c 2020-07-23 stsp blob2);
2951 36bf999c 2020-07-23 stsp if (err)
2952 36bf999c 2020-07-23 stsp goto done;
2953 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2954 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2955 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2956 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2957 36bf999c 2020-07-23 stsp free(link_target2);
2958 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2959 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2960 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2961 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2962 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2963 526a746f 2020-07-23 stsp a->progress_arg);
2964 dfe9fba0 2020-07-23 stsp } else {
2965 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2966 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2967 526a746f 2020-07-23 stsp }
2968 dfe9fba0 2020-07-23 stsp if (err)
2969 dfe9fba0 2020-07-23 stsp goto done;
2970 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2971 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2972 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2973 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2974 234035bc 2019-06-01 stsp if (err)
2975 234035bc 2019-06-01 stsp goto done;
2976 234035bc 2019-06-01 stsp }
2977 234035bc 2019-06-01 stsp } else {
2978 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2979 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2980 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2981 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2982 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2983 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2984 2e1fa222 2020-07-23 stsp } else {
2985 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2986 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2987 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2988 2e1fa222 2020-07-23 stsp }
2989 234035bc 2019-06-01 stsp if (err)
2990 234035bc 2019-06-01 stsp goto done;
2991 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2992 234035bc 2019-06-01 stsp if (err)
2993 234035bc 2019-06-01 stsp goto done;
2994 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2995 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
2996 3969253a 2020-03-07 stsp if (err) {
2997 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2998 3969253a 2020-03-07 stsp goto done;
2999 3969253a 2020-03-07 stsp }
3000 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3001 2b92fad7 2019-06-02 stsp if (err) {
3002 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3003 2b92fad7 2019-06-02 stsp goto done;
3004 2b92fad7 2019-06-02 stsp }
3005 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3006 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3007 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3008 2e1fa222 2020-07-23 stsp }
3009 234035bc 2019-06-01 stsp }
3010 234035bc 2019-06-01 stsp }
3011 234035bc 2019-06-01 stsp done:
3012 234035bc 2019-06-01 stsp free(ondisk_path);
3013 234035bc 2019-06-01 stsp return err;
3014 234035bc 2019-06-01 stsp }
3015 234035bc 2019-06-01 stsp
3016 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3017 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3018 234035bc 2019-06-01 stsp struct got_repository *repo;
3019 234035bc 2019-06-01 stsp };
3020 234035bc 2019-06-01 stsp
3021 234035bc 2019-06-01 stsp static const struct got_error *
3022 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3023 234035bc 2019-06-01 stsp {
3024 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3025 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3026 234035bc 2019-06-01 stsp unsigned char status;
3027 234035bc 2019-06-01 stsp struct stat sb;
3028 234035bc 2019-06-01 stsp char *ondisk_path;
3029 234035bc 2019-06-01 stsp
3030 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3031 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3032 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3033 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3034 234035bc 2019-06-01 stsp
3035 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3036 234035bc 2019-06-01 stsp == -1)
3037 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3038 234035bc 2019-06-01 stsp
3039 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3040 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3041 234035bc 2019-06-01 stsp if (err)
3042 234035bc 2019-06-01 stsp return err;
3043 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3044 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3045 234035bc 2019-06-01 stsp
3046 234035bc 2019-06-01 stsp return NULL;
3047 234035bc 2019-06-01 stsp }
3048 234035bc 2019-06-01 stsp
3049 818c7501 2019-07-11 stsp static const struct got_error *
3050 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3051 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3052 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3053 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3054 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3055 234035bc 2019-06-01 stsp {
3056 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3057 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3058 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3059 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3060 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3061 234035bc 2019-06-01 stsp
3062 03415a1a 2019-06-02 stsp if (commit_id1) {
3063 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3064 03415a1a 2019-06-02 stsp worktree->path_prefix);
3065 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3066 03415a1a 2019-06-02 stsp goto done;
3067 69d57f3d 2020-07-31 stsp }
3068 69d57f3d 2020-07-31 stsp if (tree_id1) {
3069 69d57f3d 2020-07-31 stsp char *id_str;
3070 03415a1a 2019-06-02 stsp
3071 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3072 03415a1a 2019-06-02 stsp if (err)
3073 03415a1a 2019-06-02 stsp goto done;
3074 f69721c3 2019-10-21 stsp
3075 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3076 f69721c3 2019-10-21 stsp if (err)
3077 f69721c3 2019-10-21 stsp goto done;
3078 f69721c3 2019-10-21 stsp
3079 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3080 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3081 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3082 f69721c3 2019-10-21 stsp free(id_str);
3083 f69721c3 2019-10-21 stsp goto done;
3084 f69721c3 2019-10-21 stsp }
3085 f69721c3 2019-10-21 stsp free(id_str);
3086 03415a1a 2019-06-02 stsp }
3087 234035bc 2019-06-01 stsp
3088 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3089 234035bc 2019-06-01 stsp worktree->path_prefix);
3090 234035bc 2019-06-01 stsp if (err)
3091 234035bc 2019-06-01 stsp goto done;
3092 234035bc 2019-06-01 stsp
3093 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3094 234035bc 2019-06-01 stsp if (err)
3095 234035bc 2019-06-01 stsp goto done;
3096 234035bc 2019-06-01 stsp
3097 234035bc 2019-06-01 stsp arg.worktree = worktree;
3098 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3099 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3100 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3101 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3102 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3103 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3104 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3105 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3106 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3107 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3108 af12c6b9 2019-06-04 stsp err = sync_err;
3109 234035bc 2019-06-01 stsp done:
3110 234035bc 2019-06-01 stsp if (tree1)
3111 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3112 234035bc 2019-06-01 stsp if (tree2)
3113 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3114 f69721c3 2019-10-21 stsp free(label_orig);
3115 818c7501 2019-07-11 stsp return err;
3116 818c7501 2019-07-11 stsp }
3117 234035bc 2019-06-01 stsp
3118 818c7501 2019-07-11 stsp const struct got_error *
3119 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3120 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3121 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3122 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3123 818c7501 2019-07-11 stsp {
3124 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3125 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3126 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3127 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3128 818c7501 2019-07-11 stsp
3129 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3130 818c7501 2019-07-11 stsp if (err)
3131 818c7501 2019-07-11 stsp return err;
3132 818c7501 2019-07-11 stsp
3133 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3134 818c7501 2019-07-11 stsp if (err)
3135 818c7501 2019-07-11 stsp goto done;
3136 818c7501 2019-07-11 stsp
3137 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3138 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3139 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3140 818c7501 2019-07-11 stsp &mok_arg);
3141 818c7501 2019-07-11 stsp if (err)
3142 818c7501 2019-07-11 stsp goto done;
3143 818c7501 2019-07-11 stsp
3144 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3145 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3146 818c7501 2019-07-11 stsp done:
3147 818c7501 2019-07-11 stsp if (fileindex)
3148 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3149 818c7501 2019-07-11 stsp free(fileindex_path);
3150 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3151 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3152 234035bc 2019-06-01 stsp err = unlockerr;
3153 234035bc 2019-06-01 stsp return err;
3154 234035bc 2019-06-01 stsp }
3155 234035bc 2019-06-01 stsp
3156 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3157 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3158 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3159 927df6b7 2019-02-10 stsp const char *status_path;
3160 927df6b7 2019-02-10 stsp size_t status_path_len;
3161 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3162 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3163 f8d1f275 2019-02-04 stsp void *status_arg;
3164 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3165 f8d1f275 2019-02-04 stsp void *cancel_arg;
3166 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3167 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3168 f2a9dc41 2019-12-13 tracey int report_unchanged;
3169 3143d852 2020-06-25 stsp int no_ignores;
3170 f8d1f275 2019-02-04 stsp };
3171 88d0e355 2019-08-03 stsp
3172 f8d1f275 2019-02-04 stsp static const struct got_error *
3173 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3174 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3175 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3176 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3177 927df6b7 2019-02-10 stsp {
3178 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3179 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3180 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3181 927df6b7 2019-02-10 stsp struct stat sb;
3182 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3183 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3184 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3185 927df6b7 2019-02-10 stsp
3186 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3187 98eaaa12 2019-08-03 stsp if (err)
3188 98eaaa12 2019-08-03 stsp return err;
3189 98eaaa12 2019-08-03 stsp
3190 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3191 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3192 98eaaa12 2019-08-03 stsp return NULL;
3193 98eaaa12 2019-08-03 stsp
3194 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3195 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3196 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3197 98eaaa12 2019-08-03 stsp }
3198 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3199 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3200 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3201 927df6b7 2019-02-10 stsp }
3202 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3203 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3204 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3205 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3206 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3207 98eaaa12 2019-08-03 stsp }
3208 98eaaa12 2019-08-03 stsp
3209 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3210 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3211 927df6b7 2019-02-10 stsp }
3212 927df6b7 2019-02-10 stsp
3213 927df6b7 2019-02-10 stsp static const struct got_error *
3214 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3215 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3216 f8d1f275 2019-02-04 stsp {
3217 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3218 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3219 f8d1f275 2019-02-04 stsp char *abspath;
3220 f8d1f275 2019-02-04 stsp
3221 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3222 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3223 0584f854 2019-04-06 stsp
3224 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3225 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3226 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3227 927df6b7 2019-02-10 stsp return NULL;
3228 927df6b7 2019-02-10 stsp
3229 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3230 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3231 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3232 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3233 f8d1f275 2019-02-04 stsp } else {
3234 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3235 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3236 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3237 f8d1f275 2019-02-04 stsp }
3238 f8d1f275 2019-02-04 stsp
3239 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3240 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3241 f8d1f275 2019-02-04 stsp free(abspath);
3242 9d31a1d8 2018-03-11 stsp return err;
3243 9d31a1d8 2018-03-11 stsp }
3244 f8d1f275 2019-02-04 stsp
3245 f8d1f275 2019-02-04 stsp static const struct got_error *
3246 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3247 f8d1f275 2019-02-04 stsp {
3248 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3249 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3250 2ec1f75b 2019-03-26 stsp unsigned char status;
3251 927df6b7 2019-02-10 stsp
3252 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3253 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3254 0584f854 2019-04-06 stsp
3255 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3256 927df6b7 2019-02-10 stsp return NULL;
3257 927df6b7 2019-02-10 stsp
3258 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3259 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3260 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3261 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3262 2ec1f75b 2019-03-26 stsp else
3263 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3264 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3265 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3266 6841da00 2019-08-08 stsp }
3267 6841da00 2019-08-08 stsp
3268 6841da00 2019-08-08 stsp void
3269 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3270 6841da00 2019-08-08 stsp {
3271 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3272 6841da00 2019-08-08 stsp
3273 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3274 6841da00 2019-08-08 stsp free((char *)pe->path);
3275 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3276 6841da00 2019-08-08 stsp }
3277 6841da00 2019-08-08 stsp
3278 6841da00 2019-08-08 stsp void
3279 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3280 6841da00 2019-08-08 stsp {
3281 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3282 6841da00 2019-08-08 stsp
3283 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3284 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3285 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3286 6841da00 2019-08-08 stsp free((char *)pe->path);
3287 6841da00 2019-08-08 stsp }
3288 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3289 6841da00 2019-08-08 stsp }
3290 6841da00 2019-08-08 stsp
3291 6841da00 2019-08-08 stsp static const struct got_error *
3292 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3293 6841da00 2019-08-08 stsp {
3294 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3295 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3296 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3297 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3298 6841da00 2019-08-08 stsp size_t linesize = 0;
3299 6841da00 2019-08-08 stsp ssize_t linelen;
3300 6841da00 2019-08-08 stsp
3301 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3302 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3303 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3304 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3305 6841da00 2019-08-08 stsp
3306 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3307 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3308 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3309 bd8de430 2019-10-04 stsp
3310 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3311 bd8de430 2019-10-04 stsp if (line[0] == '#')
3312 bd8de430 2019-10-04 stsp continue;
3313 bd8de430 2019-10-04 stsp
3314 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3315 bd8de430 2019-10-04 stsp if (line[0] == '!')
3316 bd8de430 2019-10-04 stsp continue;
3317 bd8de430 2019-10-04 stsp
3318 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3319 6841da00 2019-08-08 stsp line) == -1) {
3320 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3321 6841da00 2019-08-08 stsp goto done;
3322 6841da00 2019-08-08 stsp }
3323 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3324 6841da00 2019-08-08 stsp if (err)
3325 6841da00 2019-08-08 stsp goto done;
3326 6841da00 2019-08-08 stsp }
3327 6841da00 2019-08-08 stsp if (ferror(f)) {
3328 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3329 6841da00 2019-08-08 stsp goto done;
3330 6841da00 2019-08-08 stsp }
3331 6841da00 2019-08-08 stsp
3332 6841da00 2019-08-08 stsp dirpath = strdup(path);
3333 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3334 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3335 6841da00 2019-08-08 stsp goto done;
3336 6841da00 2019-08-08 stsp }
3337 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3338 6841da00 2019-08-08 stsp done:
3339 6841da00 2019-08-08 stsp free(line);
3340 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3341 6841da00 2019-08-08 stsp free(dirpath);
3342 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3343 6841da00 2019-08-08 stsp }
3344 6841da00 2019-08-08 stsp return err;
3345 6841da00 2019-08-08 stsp }
3346 6841da00 2019-08-08 stsp
3347 6841da00 2019-08-08 stsp int
3348 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3349 6841da00 2019-08-08 stsp {
3350 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3351 bd8de430 2019-10-04 stsp
3352 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3353 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3354 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3355 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3356 bd8de430 2019-10-04 stsp
3357 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3358 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3359 6841da00 2019-08-08 stsp
3360 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3361 bd8de430 2019-10-04 stsp continue;
3362 bd8de430 2019-10-04 stsp pattern += 3;
3363 bd8de430 2019-10-04 stsp p = path;
3364 bd8de430 2019-10-04 stsp while (*p) {
3365 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3366 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3367 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3368 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3369 bd8de430 2019-10-04 stsp p++;
3370 bd8de430 2019-10-04 stsp while (*p == '/')
3371 bd8de430 2019-10-04 stsp p++;
3372 bd8de430 2019-10-04 stsp continue;
3373 bd8de430 2019-10-04 stsp }
3374 bd8de430 2019-10-04 stsp return 1;
3375 bd8de430 2019-10-04 stsp }
3376 bd8de430 2019-10-04 stsp }
3377 bd8de430 2019-10-04 stsp }
3378 bd8de430 2019-10-04 stsp
3379 6841da00 2019-08-08 stsp /*
3380 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3381 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3382 6841da00 2019-08-08 stsp * ignores backwards.
3383 6841da00 2019-08-08 stsp */
3384 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3385 6841da00 2019-08-08 stsp while (pe) {
3386 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3387 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3388 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3389 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3390 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3391 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3392 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3393 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3394 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3395 6841da00 2019-08-08 stsp continue;
3396 6841da00 2019-08-08 stsp return 1;
3397 6841da00 2019-08-08 stsp }
3398 6841da00 2019-08-08 stsp }
3399 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3400 6841da00 2019-08-08 stsp }
3401 6841da00 2019-08-08 stsp
3402 6841da00 2019-08-08 stsp return 0;
3403 6841da00 2019-08-08 stsp }
3404 6841da00 2019-08-08 stsp
3405 6841da00 2019-08-08 stsp static const struct got_error *
3406 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3407 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3408 6841da00 2019-08-08 stsp {
3409 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3410 6841da00 2019-08-08 stsp char *ignorespath;
3411 886cec17 2019-12-15 stsp int fd = -1;
3412 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3413 6841da00 2019-08-08 stsp
3414 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3415 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3416 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3417 6841da00 2019-08-08 stsp
3418 886cec17 2019-12-15 stsp if (dirfd != -1) {
3419 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3420 886cec17 2019-12-15 stsp if (fd == -1) {
3421 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3422 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3423 886cec17 2019-12-15 stsp ignorespath);
3424 886cec17 2019-12-15 stsp } else {
3425 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3426 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3427 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3428 886cec17 2019-12-15 stsp ignorespath);
3429 886cec17 2019-12-15 stsp else {
3430 886cec17 2019-12-15 stsp fd = -1;
3431 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3432 886cec17 2019-12-15 stsp }
3433 886cec17 2019-12-15 stsp }
3434 886cec17 2019-12-15 stsp } else {
3435 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3436 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3437 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3438 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3439 886cec17 2019-12-15 stsp ignorespath);
3440 886cec17 2019-12-15 stsp } else
3441 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3442 886cec17 2019-12-15 stsp }
3443 6841da00 2019-08-08 stsp
3444 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3445 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3446 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3447 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3448 6841da00 2019-08-08 stsp free(ignorespath);
3449 6841da00 2019-08-08 stsp return err;
3450 f8d1f275 2019-02-04 stsp }
3451 f8d1f275 2019-02-04 stsp
3452 f8d1f275 2019-02-04 stsp static const struct got_error *
3453 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3454 f8d1f275 2019-02-04 stsp {
3455 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3456 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3457 f8d1f275 2019-02-04 stsp char *path = NULL;
3458 f8d1f275 2019-02-04 stsp
3459 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3460 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3461 0584f854 2019-04-06 stsp
3462 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3463 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3464 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3465 f8d1f275 2019-02-04 stsp } else {
3466 f8d1f275 2019-02-04 stsp path = de->d_name;
3467 f8d1f275 2019-02-04 stsp }
3468 f8d1f275 2019-02-04 stsp
3469 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3470 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3471 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3472 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3473 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3474 f8d1f275 2019-02-04 stsp if (parent_path[0])
3475 f8d1f275 2019-02-04 stsp free(path);
3476 b72f483a 2019-02-05 stsp return err;
3477 f8d1f275 2019-02-04 stsp }
3478 f8d1f275 2019-02-04 stsp
3479 347d1d3e 2019-07-12 stsp static const struct got_error *
3480 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3481 3143d852 2020-06-25 stsp {
3482 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3483 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3484 3143d852 2020-06-25 stsp
3485 3143d852 2020-06-25 stsp if (a->no_ignores)
3486 3143d852 2020-06-25 stsp return NULL;
3487 3143d852 2020-06-25 stsp
3488 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3489 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3490 3143d852 2020-06-25 stsp if (err)
3491 3143d852 2020-06-25 stsp return err;
3492 3143d852 2020-06-25 stsp
3493 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3494 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3495 3143d852 2020-06-25 stsp
3496 3143d852 2020-06-25 stsp return err;
3497 3143d852 2020-06-25 stsp }
3498 3143d852 2020-06-25 stsp
3499 3143d852 2020-06-25 stsp static const struct got_error *
3500 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3501 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3502 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3503 abb4604f 2019-07-27 stsp {
3504 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3505 abb4604f 2019-07-27 stsp struct stat sb;
3506 abb4604f 2019-07-27 stsp
3507 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3508 abb4604f 2019-07-27 stsp if (ie)
3509 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3510 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3511 abb4604f 2019-07-27 stsp
3512 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3513 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3514 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3515 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3516 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3517 abb4604f 2019-07-27 stsp return NULL;
3518 abb4604f 2019-07-27 stsp }
3519 abb4604f 2019-07-27 stsp
3520 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3521 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3522 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3523 abb4604f 2019-07-27 stsp
3524 abb4604f 2019-07-27 stsp return NULL;
3525 3143d852 2020-06-25 stsp }
3526 3143d852 2020-06-25 stsp
3527 3143d852 2020-06-25 stsp static const struct got_error *
3528 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3529 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3530 3143d852 2020-06-25 stsp {
3531 3143d852 2020-06-25 stsp const struct got_error *err;
3532 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
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 ".cvsignore");
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 = add_ignores(ignores, root_path, "", -1,
3540 3143d852 2020-06-25 stsp ".gitignore");
3541 3143d852 2020-06-25 stsp if (err)
3542 3143d852 2020-06-25 stsp return err;
3543 3143d852 2020-06-25 stsp
3544 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3545 3143d852 2020-06-25 stsp if (err) {
3546 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3547 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3548 3143d852 2020-06-25 stsp return err;
3549 3143d852 2020-06-25 stsp }
3550 3143d852 2020-06-25 stsp for (;;) {
3551 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3552 3143d852 2020-06-25 stsp ".cvsignore");
3553 3143d852 2020-06-25 stsp if (err)
3554 3143d852 2020-06-25 stsp break;
3555 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3556 3143d852 2020-06-25 stsp ".gitignore");
3557 3143d852 2020-06-25 stsp if (err)
3558 3143d852 2020-06-25 stsp break;
3559 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3560 3143d852 2020-06-25 stsp if (err) {
3561 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3562 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3563 3143d852 2020-06-25 stsp break;
3564 3143d852 2020-06-25 stsp }
3565 b737c85a 2020-06-26 stsp free(parent_path);
3566 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3567 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3568 3143d852 2020-06-25 stsp }
3569 3143d852 2020-06-25 stsp
3570 b737c85a 2020-06-26 stsp free(parent_path);
3571 b737c85a 2020-06-26 stsp free(next_parent_path);
3572 3143d852 2020-06-25 stsp return err;
3573 abb4604f 2019-07-27 stsp }
3574 abb4604f 2019-07-27 stsp
3575 abb4604f 2019-07-27 stsp static const struct got_error *
3576 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3577 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3578 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3579 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3580 f2a9dc41 2019-12-13 tracey int report_unchanged)
3581 f8d1f275 2019-02-04 stsp {
3582 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3583 6fc93f37 2019-12-13 stsp int fd = -1;
3584 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3585 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3586 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3587 3143d852 2020-06-25 stsp
3588 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3589 f8d1f275 2019-02-04 stsp
3590 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3591 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3592 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3593 8dc303cc 2019-07-27 stsp
3594 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3595 6fc93f37 2019-12-13 stsp if (fd == -1) {
3596 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3597 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3598 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3599 abb4604f 2019-07-27 stsp else
3600 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3601 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3602 f2a9dc41 2019-12-13 tracey report_unchanged);
3603 abb4604f 2019-07-27 stsp } else {
3604 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3605 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3606 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3607 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3608 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3609 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3610 abb4604f 2019-07-27 stsp arg.status_path = path;
3611 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3612 abb4604f 2019-07-27 stsp arg.repo = repo;
3613 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3614 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3615 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3616 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3617 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3618 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3619 022fae89 2019-12-06 tracey if (!no_ignores) {
3620 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3621 3143d852 2020-06-25 stsp worktree->root_path, path);
3622 3143d852 2020-06-25 stsp if (err)
3623 3143d852 2020-06-25 stsp goto done;
3624 022fae89 2019-12-06 tracey }
3625 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3626 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3627 927df6b7 2019-02-10 stsp }
3628 3143d852 2020-06-25 stsp done:
3629 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3630 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3631 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3632 927df6b7 2019-02-10 stsp free(ondisk_path);
3633 347d1d3e 2019-07-12 stsp return err;
3634 347d1d3e 2019-07-12 stsp }
3635 347d1d3e 2019-07-12 stsp
3636 347d1d3e 2019-07-12 stsp const struct got_error *
3637 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3638 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3639 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3640 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3641 347d1d3e 2019-07-12 stsp {
3642 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3643 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3644 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3645 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3646 347d1d3e 2019-07-12 stsp
3647 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3648 347d1d3e 2019-07-12 stsp if (err)
3649 347d1d3e 2019-07-12 stsp return err;
3650 347d1d3e 2019-07-12 stsp
3651 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3652 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3653 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3654 72ea6654 2019-07-27 stsp if (err)
3655 72ea6654 2019-07-27 stsp break;
3656 72ea6654 2019-07-27 stsp }
3657 f8d1f275 2019-02-04 stsp free(fileindex_path);
3658 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3659 f8d1f275 2019-02-04 stsp return err;
3660 f8d1f275 2019-02-04 stsp }
3661 6c7ab921 2019-03-18 stsp
3662 6c7ab921 2019-03-18 stsp const struct got_error *
3663 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3664 6c7ab921 2019-03-18 stsp const char *arg)
3665 6c7ab921 2019-03-18 stsp {
3666 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3667 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3668 6c7ab921 2019-03-18 stsp size_t len;
3669 00bb5ea0 2020-07-23 stsp struct stat sb;
3670 01740607 2020-11-04 stsp char *abspath = NULL;
3671 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3672 6c7ab921 2019-03-18 stsp
3673 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3674 6c7ab921 2019-03-18 stsp
3675 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3676 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3677 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3678 00bb5ea0 2020-07-23 stsp
3679 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3680 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3681 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3682 00bb5ea0 2020-07-23 stsp goto done;
3683 00bb5ea0 2020-07-23 stsp }
3684 727173c3 2020-11-06 stsp sb.st_mode = 0;
3685 00bb5ea0 2020-07-23 stsp }
3686 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3687 00bb5ea0 2020-07-23 stsp /*
3688 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3689 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3690 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3691 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3692 00bb5ea0 2020-07-23 stsp */
3693 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3694 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3695 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3696 00bb5ea0 2020-07-23 stsp goto done;
3697 00bb5ea0 2020-07-23 stsp }
3698 00bb5ea0 2020-07-23 stsp
3699 00bb5ea0 2020-07-23 stsp }
3700 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3701 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3702 00bb5ea0 2020-07-23 stsp if (err)
3703 d0710d08 2019-07-22 stsp goto done;
3704 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3705 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3706 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3707 00bb5ea0 2020-07-23 stsp goto done;
3708 00bb5ea0 2020-07-23 stsp }
3709 00bb5ea0 2020-07-23 stsp } else {
3710 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3711 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3712 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3713 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3714 00bb5ea0 2020-07-23 stsp goto done;
3715 00bb5ea0 2020-07-23 stsp }
3716 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3717 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3718 01740607 2020-11-04 stsp goto done;
3719 01740607 2020-11-04 stsp }
3720 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3721 01740607 2020-11-04 stsp sizeof(canonpath));
3722 01740607 2020-11-04 stsp if (err)
3723 00bb5ea0 2020-07-23 stsp goto done;
3724 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3725 01740607 2020-11-04 stsp if (resolved == NULL) {
3726 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3727 01740607 2020-11-04 stsp goto done;
3728 00bb5ea0 2020-07-23 stsp }
3729 d0710d08 2019-07-22 stsp }
3730 d0710d08 2019-07-22 stsp }
3731 6c7ab921 2019-03-18 stsp
3732 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3733 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3734 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3735 6c7ab921 2019-03-18 stsp goto done;
3736 6c7ab921 2019-03-18 stsp }
3737 6c7ab921 2019-03-18 stsp
3738 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3739 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3740 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3741 f6d88e1a 2019-05-29 stsp if (err)
3742 f6d88e1a 2019-05-29 stsp goto done;
3743 f6d88e1a 2019-05-29 stsp } else {
3744 f6d88e1a 2019-05-29 stsp path = strdup("");
3745 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3746 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3747 f6d88e1a 2019-05-29 stsp goto done;
3748 f6d88e1a 2019-05-29 stsp }
3749 6c7ab921 2019-03-18 stsp }
3750 6c7ab921 2019-03-18 stsp
3751 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3752 6c7ab921 2019-03-18 stsp len = strlen(path);
3753 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3754 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3755 6c7ab921 2019-03-18 stsp len--;
3756 6c7ab921 2019-03-18 stsp }
3757 6c7ab921 2019-03-18 stsp done:
3758 01740607 2020-11-04 stsp free(abspath);
3759 6c7ab921 2019-03-18 stsp free(resolved);
3760 d0710d08 2019-07-22 stsp free(cwd);
3761 6c7ab921 2019-03-18 stsp if (err == NULL)
3762 6c7ab921 2019-03-18 stsp *wt_path = path;
3763 6c7ab921 2019-03-18 stsp else
3764 6c7ab921 2019-03-18 stsp free(path);
3765 d00136be 2019-03-26 stsp return err;
3766 d00136be 2019-03-26 stsp }
3767 d00136be 2019-03-26 stsp
3768 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3769 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3770 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3771 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3772 4e68cba3 2019-11-23 stsp void *progress_arg;
3773 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3774 4e68cba3 2019-11-23 stsp };
3775 4e68cba3 2019-11-23 stsp
3776 4e68cba3 2019-11-23 stsp static const struct got_error *
3777 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3778 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3779 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3780 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3781 07f5b47a 2019-06-02 stsp {
3782 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3783 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3784 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3785 6d022e97 2019-08-04 stsp struct stat sb;
3786 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3787 07f5b47a 2019-06-02 stsp
3788 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3789 dbb83fbd 2019-12-12 stsp relpath) == -1)
3790 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3791 dbb83fbd 2019-12-12 stsp
3792 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3793 6d022e97 2019-08-04 stsp if (ie) {
3794 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3795 12463d8b 2019-12-13 stsp de_name, a->repo);
3796 6d022e97 2019-08-04 stsp if (err)
3797 dbb83fbd 2019-12-12 stsp goto done;
3798 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3799 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3800 dbb83fbd 2019-12-12 stsp goto done;
3801 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3802 dbb83fbd 2019-12-12 stsp if (err)
3803 dbb83fbd 2019-12-12 stsp goto done;
3804 6d022e97 2019-08-04 stsp }
3805 07f5b47a 2019-06-02 stsp
3806 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3807 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3808 dbb83fbd 2019-12-12 stsp goto done;
3809 4e68cba3 2019-11-23 stsp }
3810 07f5b47a 2019-06-02 stsp
3811 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3812 4e68cba3 2019-11-23 stsp if (err)
3813 4e68cba3 2019-11-23 stsp goto done;
3814 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3815 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3816 3969253a 2020-03-07 stsp if (err) {
3817 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3818 3969253a 2020-03-07 stsp goto done;
3819 3969253a 2020-03-07 stsp }
3820 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3821 07f5b47a 2019-06-02 stsp if (err) {
3822 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3823 4e68cba3 2019-11-23 stsp goto done;
3824 07f5b47a 2019-06-02 stsp }
3825 4e68cba3 2019-11-23 stsp done:
3826 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3827 dbb83fbd 2019-12-12 stsp if (err)
3828 dbb83fbd 2019-12-12 stsp return err;
3829 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3830 dbb83fbd 2019-12-12 stsp return NULL;
3831 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3832 07f5b47a 2019-06-02 stsp }
3833 07f5b47a 2019-06-02 stsp
3834 d00136be 2019-03-26 stsp const struct got_error *
3835 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3836 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3837 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3838 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3839 d00136be 2019-03-26 stsp {
3840 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3841 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3842 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3843 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3844 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3845 d00136be 2019-03-26 stsp
3846 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3847 d00136be 2019-03-26 stsp if (err)
3848 d00136be 2019-03-26 stsp return err;
3849 d00136be 2019-03-26 stsp
3850 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3851 d00136be 2019-03-26 stsp if (err)
3852 d00136be 2019-03-26 stsp goto done;
3853 d00136be 2019-03-26 stsp
3854 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3855 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3856 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3857 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3858 4e68cba3 2019-11-23 stsp saa.repo = repo;
3859 4e68cba3 2019-11-23 stsp
3860 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3861 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3862 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3863 1dd54920 2019-05-11 stsp if (err)
3864 af12c6b9 2019-06-04 stsp break;
3865 1dd54920 2019-05-11 stsp }
3866 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3867 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3868 af12c6b9 2019-06-04 stsp err = sync_err;
3869 d00136be 2019-03-26 stsp done:
3870 fb399478 2019-07-12 stsp free(fileindex_path);
3871 d00136be 2019-03-26 stsp if (fileindex)
3872 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3873 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3874 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3875 d00136be 2019-03-26 stsp err = unlockerr;
3876 6c7ab921 2019-03-18 stsp return err;
3877 6c7ab921 2019-03-18 stsp }
3878 17ed4618 2019-06-02 stsp
3879 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3880 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3881 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3882 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3883 f2a9dc41 2019-12-13 tracey void *progress_arg;
3884 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3885 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3886 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3887 766841c2 2020-08-13 stsp const char *status_codes;
3888 f2a9dc41 2019-12-13 tracey };
3889 f2a9dc41 2019-12-13 tracey
3890 f2a9dc41 2019-12-13 tracey static const struct got_error *
3891 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3892 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3893 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3894 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3895 17ed4618 2019-06-02 stsp {
3896 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3897 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3898 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3899 17ed4618 2019-06-02 stsp struct stat sb;
3900 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3901 17ed4618 2019-06-02 stsp
3902 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3903 17ed4618 2019-06-02 stsp if (ie == NULL)
3904 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3905 2ec1f75b 2019-03-26 stsp
3906 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3907 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3908 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3909 9acbc4fa 2019-08-03 stsp return NULL;
3910 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3911 9acbc4fa 2019-08-03 stsp }
3912 9acbc4fa 2019-08-03 stsp
3913 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3914 f2a9dc41 2019-12-13 tracey relpath) == -1)
3915 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3916 f2a9dc41 2019-12-13 tracey
3917 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3918 12463d8b 2019-12-13 stsp a->repo);
3919 17ed4618 2019-06-02 stsp if (err)
3920 f2a9dc41 2019-12-13 tracey goto done;
3921 17ed4618 2019-06-02 stsp
3922 766841c2 2020-08-13 stsp if (a->status_codes) {
3923 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3924 766841c2 2020-08-13 stsp int i;
3925 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3926 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3927 766841c2 2020-08-13 stsp break;
3928 766841c2 2020-08-13 stsp }
3929 766841c2 2020-08-13 stsp if (i == ncodes) {
3930 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3931 766841c2 2020-08-13 stsp free(ondisk_path);
3932 766841c2 2020-08-13 stsp return NULL;
3933 766841c2 2020-08-13 stsp }
3934 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3935 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3936 766841c2 2020-08-13 stsp static char msg[64];
3937 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3938 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3939 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3940 766841c2 2020-08-13 stsp goto done;
3941 766841c2 2020-08-13 stsp }
3942 766841c2 2020-08-13 stsp }
3943 766841c2 2020-08-13 stsp
3944 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3945 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3946 f2a9dc41 2019-12-13 tracey goto done;
3947 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3948 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3949 f2a9dc41 2019-12-13 tracey goto done;
3950 f2a9dc41 2019-12-13 tracey }
3951 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3952 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3953 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3954 f2a9dc41 2019-12-13 tracey goto done;
3955 f2a9dc41 2019-12-13 tracey }
3956 17ed4618 2019-06-02 stsp }
3957 17ed4618 2019-06-02 stsp
3958 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3959 20a2ad1c 2020-10-20 stsp size_t root_len;
3960 20a2ad1c 2020-10-20 stsp
3961 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3962 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3963 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3964 12463d8b 2019-12-13 stsp ondisk_path);
3965 12463d8b 2019-12-13 stsp goto done;
3966 12463d8b 2019-12-13 stsp }
3967 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3968 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3969 12463d8b 2019-12-13 stsp goto done;
3970 15341bfd 2020-03-05 tracey }
3971 15341bfd 2020-03-05 tracey
3972 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3973 20a2ad1c 2020-10-20 stsp do {
3974 20a2ad1c 2020-10-20 stsp char *parent;
3975 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3976 20a2ad1c 2020-10-20 stsp if (err)
3977 2513f20a 2020-10-20 stsp goto done;
3978 20a2ad1c 2020-10-20 stsp free(ondisk_path);
3979 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
3980 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
3981 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3982 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3983 20a2ad1c 2020-10-20 stsp ondisk_path);
3984 15341bfd 2020-03-05 tracey break;
3985 15341bfd 2020-03-05 tracey }
3986 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3987 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
3988 f2a9dc41 2019-12-13 tracey }
3989 17ed4618 2019-06-02 stsp
3990 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3991 f2a9dc41 2019-12-13 tracey done:
3992 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3993 f2a9dc41 2019-12-13 tracey if (err)
3994 f2a9dc41 2019-12-13 tracey return err;
3995 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3996 f2a9dc41 2019-12-13 tracey return NULL;
3997 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3998 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3999 17ed4618 2019-06-02 stsp }
4000 17ed4618 2019-06-02 stsp
4001 2ec1f75b 2019-03-26 stsp const struct got_error *
4002 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4003 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4004 766841c2 2020-08-13 stsp const char *status_codes,
4005 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4006 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4007 2ec1f75b 2019-03-26 stsp {
4008 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4009 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4010 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4011 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4012 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4013 2ec1f75b 2019-03-26 stsp
4014 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4015 2ec1f75b 2019-03-26 stsp if (err)
4016 2ec1f75b 2019-03-26 stsp return err;
4017 2ec1f75b 2019-03-26 stsp
4018 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4019 2ec1f75b 2019-03-26 stsp if (err)
4020 2ec1f75b 2019-03-26 stsp goto done;
4021 f2a9dc41 2019-12-13 tracey
4022 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4023 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4024 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4025 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4026 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4027 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4028 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4029 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4030 2ec1f75b 2019-03-26 stsp
4031 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4032 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4033 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4034 17ed4618 2019-06-02 stsp if (err)
4035 af12c6b9 2019-06-04 stsp break;
4036 2ec1f75b 2019-03-26 stsp }
4037 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4038 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4039 af12c6b9 2019-06-04 stsp err = sync_err;
4040 2ec1f75b 2019-03-26 stsp done:
4041 fb399478 2019-07-12 stsp free(fileindex_path);
4042 2ec1f75b 2019-03-26 stsp if (fileindex)
4043 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4044 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4045 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4046 2ec1f75b 2019-03-26 stsp err = unlockerr;
4047 33aa809d 2019-08-08 stsp return err;
4048 33aa809d 2019-08-08 stsp }
4049 33aa809d 2019-08-08 stsp
4050 33aa809d 2019-08-08 stsp static const struct got_error *
4051 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4052 33aa809d 2019-08-08 stsp {
4053 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4054 33aa809d 2019-08-08 stsp char *line = NULL;
4055 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4056 33aa809d 2019-08-08 stsp ssize_t linelen;
4057 33aa809d 2019-08-08 stsp
4058 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4059 33aa809d 2019-08-08 stsp if (linelen == -1) {
4060 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4061 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4062 33aa809d 2019-08-08 stsp goto done;
4063 33aa809d 2019-08-08 stsp }
4064 33aa809d 2019-08-08 stsp return NULL;
4065 33aa809d 2019-08-08 stsp }
4066 33aa809d 2019-08-08 stsp if (outfile) {
4067 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4068 33aa809d 2019-08-08 stsp if (n != linelen) {
4069 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4070 33aa809d 2019-08-08 stsp goto done;
4071 33aa809d 2019-08-08 stsp }
4072 33aa809d 2019-08-08 stsp }
4073 33aa809d 2019-08-08 stsp if (rejectfile) {
4074 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4075 33aa809d 2019-08-08 stsp if (n != linelen)
4076 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4077 33aa809d 2019-08-08 stsp }
4078 33aa809d 2019-08-08 stsp done:
4079 33aa809d 2019-08-08 stsp free(line);
4080 2ec1f75b 2019-03-26 stsp return err;
4081 2ec1f75b 2019-03-26 stsp }
4082 1f1abb7e 2019-08-08 stsp
4083 33aa809d 2019-08-08 stsp static const struct got_error *
4084 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4085 33aa809d 2019-08-08 stsp {
4086 33aa809d 2019-08-08 stsp char *line = NULL;
4087 33aa809d 2019-08-08 stsp size_t linesize = 0;
4088 33aa809d 2019-08-08 stsp ssize_t linelen;
4089 33aa809d 2019-08-08 stsp
4090 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4091 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4092 500467ff 2019-09-25 hiltjo if (ferror(f))
4093 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4094 500467ff 2019-09-25 hiltjo return NULL;
4095 500467ff 2019-09-25 hiltjo }
4096 33aa809d 2019-08-08 stsp free(line);
4097 33aa809d 2019-08-08 stsp return NULL;
4098 33aa809d 2019-08-08 stsp }
4099 33aa809d 2019-08-08 stsp
4100 33aa809d 2019-08-08 stsp static const struct got_error *
4101 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4102 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4103 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4104 33aa809d 2019-08-08 stsp {
4105 33aa809d 2019-08-08 stsp const struct got_error *err;
4106 33aa809d 2019-08-08 stsp
4107 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4108 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4109 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4110 33aa809d 2019-08-08 stsp if (err)
4111 33aa809d 2019-08-08 stsp return err;
4112 33aa809d 2019-08-08 stsp (*line_cur1)++;
4113 33aa809d 2019-08-08 stsp }
4114 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4115 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4116 33aa809d 2019-08-08 stsp if (rejectfile)
4117 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4118 33aa809d 2019-08-08 stsp else
4119 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4120 33aa809d 2019-08-08 stsp if (err)
4121 33aa809d 2019-08-08 stsp return err;
4122 33aa809d 2019-08-08 stsp (*line_cur2)++;
4123 33aa809d 2019-08-08 stsp }
4124 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4125 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4126 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4127 33aa809d 2019-08-08 stsp if (err)
4128 33aa809d 2019-08-08 stsp return err;
4129 33aa809d 2019-08-08 stsp (*line_cur2)++;
4130 33aa809d 2019-08-08 stsp }
4131 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4132 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4133 33aa809d 2019-08-08 stsp if (rejectfile)
4134 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4135 33aa809d 2019-08-08 stsp else
4136 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4137 33aa809d 2019-08-08 stsp if (err)
4138 33aa809d 2019-08-08 stsp return err;
4139 33aa809d 2019-08-08 stsp (*line_cur1)++;
4140 33aa809d 2019-08-08 stsp }
4141 f1e81a05 2019-08-10 stsp
4142 f1e81a05 2019-08-10 stsp return NULL;
4143 f1e81a05 2019-08-10 stsp }
4144 f1e81a05 2019-08-10 stsp
4145 f1e81a05 2019-08-10 stsp static const struct got_error *
4146 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4147 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4148 f1e81a05 2019-08-10 stsp {
4149 f1e81a05 2019-08-10 stsp const struct got_error *err;
4150 f1e81a05 2019-08-10 stsp
4151 f1e81a05 2019-08-10 stsp if (outfile) {
4152 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4153 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4154 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4155 f1e81a05 2019-08-10 stsp if (err)
4156 f1e81a05 2019-08-10 stsp return err;
4157 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4158 f1e81a05 2019-08-10 stsp }
4159 f1e81a05 2019-08-10 stsp }
4160 f1e81a05 2019-08-10 stsp if (rejectfile) {
4161 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4162 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4163 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4164 f1e81a05 2019-08-10 stsp if (err)
4165 f1e81a05 2019-08-10 stsp return err;
4166 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4167 f1e81a05 2019-08-10 stsp }
4168 33aa809d 2019-08-08 stsp }
4169 33aa809d 2019-08-08 stsp
4170 33aa809d 2019-08-08 stsp return NULL;
4171 33aa809d 2019-08-08 stsp }
4172 33aa809d 2019-08-08 stsp
4173 33aa809d 2019-08-08 stsp static const struct got_error *
4174 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4175 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4176 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4177 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4178 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4179 33aa809d 2019-08-08 stsp {
4180 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4181 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4182 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4183 33aa809d 2019-08-08 stsp FILE *hunkfile;
4184 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4185 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4186 fe621944 2020-11-10 stsp int rc;
4187 33aa809d 2019-08-08 stsp
4188 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4189 33aa809d 2019-08-08 stsp
4190 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4191 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4192 fe621944 2020-11-10 stsp start_old = cc.left.start;
4193 fe621944 2020-11-10 stsp end_old = cc.left.end;
4194 fe621944 2020-11-10 stsp start_new = cc.right.start;
4195 fe621944 2020-11-10 stsp end_new = cc.right.end;
4196 33aa809d 2019-08-08 stsp
4197 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4198 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4199 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4200 33aa809d 2019-08-08 stsp
4201 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4202 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4203 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4204 33aa809d 2019-08-08 stsp
4205 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4206 fe621944 2020-11-10 stsp if (diff_state == NULL)
4207 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4208 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4209 fe621944 2020-11-10 stsp
4210 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4211 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4212 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4213 33aa809d 2019-08-08 stsp goto done;
4214 33aa809d 2019-08-08 stsp }
4215 fe621944 2020-11-10 stsp
4216 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4217 fe621944 2020-11-10 stsp diff_result, &cc);
4218 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4219 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4220 33aa809d 2019-08-08 stsp goto done;
4221 33aa809d 2019-08-08 stsp }
4222 fe621944 2020-11-10 stsp
4223 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4224 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4225 33aa809d 2019-08-08 stsp goto done;
4226 33aa809d 2019-08-08 stsp }
4227 33aa809d 2019-08-08 stsp
4228 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4229 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4230 33aa809d 2019-08-08 stsp if (err)
4231 33aa809d 2019-08-08 stsp goto done;
4232 33aa809d 2019-08-08 stsp
4233 33aa809d 2019-08-08 stsp switch (*choice) {
4234 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4235 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4236 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4237 33aa809d 2019-08-08 stsp break;
4238 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4239 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4240 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4241 33aa809d 2019-08-08 stsp break;
4242 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4243 33aa809d 2019-08-08 stsp break;
4244 33aa809d 2019-08-08 stsp default:
4245 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4246 33aa809d 2019-08-08 stsp break;
4247 33aa809d 2019-08-08 stsp }
4248 33aa809d 2019-08-08 stsp done:
4249 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4250 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4251 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4252 33aa809d 2019-08-08 stsp return err;
4253 33aa809d 2019-08-08 stsp }
4254 33aa809d 2019-08-08 stsp
4255 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4256 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4257 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4258 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4259 1f1abb7e 2019-08-08 stsp void *progress_arg;
4260 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4261 33aa809d 2019-08-08 stsp void *patch_arg;
4262 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4263 1f1abb7e 2019-08-08 stsp };
4264 a129376b 2019-03-28 stsp
4265 e20a8b6f 2019-06-04 stsp static const struct got_error *
4266 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4267 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4268 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4269 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4270 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4271 33aa809d 2019-08-08 stsp {
4272 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4273 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4274 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4275 1ebedb77 2019-10-19 stsp int fd2 = -1;
4276 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4277 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4278 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4279 fe621944 2020-11-10 stsp struct stat sb2;
4280 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4281 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4282 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4283 33aa809d 2019-08-08 stsp
4284 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4285 33aa809d 2019-08-08 stsp
4286 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4287 33aa809d 2019-08-08 stsp if (err)
4288 33aa809d 2019-08-08 stsp return err;
4289 33aa809d 2019-08-08 stsp
4290 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4291 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4292 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4293 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4294 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4295 fa3cef63 2020-07-23 stsp goto done;
4296 fa3cef63 2020-07-23 stsp }
4297 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4298 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4299 fa3cef63 2020-07-23 stsp if (link_len == -1)
4300 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4301 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4302 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4303 12463d8b 2019-12-13 stsp }
4304 12463d8b 2019-12-13 stsp } else {
4305 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4306 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4307 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4308 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4309 fa3cef63 2020-07-23 stsp goto done;
4310 fa3cef63 2020-07-23 stsp }
4311 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4312 fa3cef63 2020-07-23 stsp sizeof(link_target));
4313 fa3cef63 2020-07-23 stsp if (link_len == -1)
4314 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4315 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4316 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4317 12463d8b 2019-12-13 stsp }
4318 1ebedb77 2019-10-19 stsp }
4319 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4320 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4321 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4322 fa3cef63 2020-07-23 stsp goto done;
4323 fa3cef63 2020-07-23 stsp }
4324 1ebedb77 2019-10-19 stsp
4325 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4326 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4327 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4328 fa3cef63 2020-07-23 stsp goto done;
4329 fa3cef63 2020-07-23 stsp }
4330 fa3cef63 2020-07-23 stsp fd2 = -1;
4331 fa3cef63 2020-07-23 stsp } else {
4332 fa3cef63 2020-07-23 stsp size_t n;
4333 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4334 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4335 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4336 fa3cef63 2020-07-23 stsp goto done;
4337 fa3cef63 2020-07-23 stsp }
4338 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4339 fa3cef63 2020-07-23 stsp if (n != link_len) {
4340 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4341 fa3cef63 2020-07-23 stsp goto done;
4342 fa3cef63 2020-07-23 stsp }
4343 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4344 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4345 fa3cef63 2020-07-23 stsp goto done;
4346 fa3cef63 2020-07-23 stsp }
4347 fa3cef63 2020-07-23 stsp rewind(f2);
4348 33aa809d 2019-08-08 stsp }
4349 33aa809d 2019-08-08 stsp
4350 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4351 33aa809d 2019-08-08 stsp if (err)
4352 33aa809d 2019-08-08 stsp goto done;
4353 33aa809d 2019-08-08 stsp
4354 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4355 33aa809d 2019-08-08 stsp if (err)
4356 33aa809d 2019-08-08 stsp goto done;
4357 33aa809d 2019-08-08 stsp
4358 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4359 33aa809d 2019-08-08 stsp if (err)
4360 33aa809d 2019-08-08 stsp goto done;
4361 33aa809d 2019-08-08 stsp
4362 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4363 fe621944 2020-11-10 stsp NULL);
4364 33aa809d 2019-08-08 stsp if (err)
4365 33aa809d 2019-08-08 stsp goto done;
4366 33aa809d 2019-08-08 stsp
4367 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4368 33aa809d 2019-08-08 stsp if (err)
4369 33aa809d 2019-08-08 stsp goto done;
4370 33aa809d 2019-08-08 stsp
4371 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4372 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4373 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4374 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4375 fe621944 2020-11-10 stsp
4376 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4377 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4378 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4379 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4380 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4381 fe621944 2020-11-10 stsp nchanges++;
4382 fe621944 2020-11-10 stsp }
4383 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4384 33aa809d 2019-08-08 stsp int choice;
4385 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4386 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4387 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4388 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4389 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4390 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4391 33aa809d 2019-08-08 stsp if (err)
4392 33aa809d 2019-08-08 stsp goto done;
4393 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4394 33aa809d 2019-08-08 stsp have_content = 1;
4395 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4396 33aa809d 2019-08-08 stsp break;
4397 33aa809d 2019-08-08 stsp }
4398 1ebedb77 2019-10-19 stsp if (have_content) {
4399 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4400 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4401 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4402 1ebedb77 2019-10-19 stsp if (err)
4403 1ebedb77 2019-10-19 stsp goto done;
4404 1ebedb77 2019-10-19 stsp
4405 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4406 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4407 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4408 fa3cef63 2020-07-23 stsp goto done;
4409 fa3cef63 2020-07-23 stsp }
4410 1ebedb77 2019-10-19 stsp }
4411 1ebedb77 2019-10-19 stsp }
4412 33aa809d 2019-08-08 stsp done:
4413 33aa809d 2019-08-08 stsp free(id_str);
4414 33aa809d 2019-08-08 stsp if (blob)
4415 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4416 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4417 fe621944 2020-11-10 stsp if (err == NULL)
4418 fe621944 2020-11-10 stsp err = free_err;
4419 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4420 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4421 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4422 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4423 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4424 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4425 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4426 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4427 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4428 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4429 33aa809d 2019-08-08 stsp if (err || !have_content) {
4430 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4431 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4432 33aa809d 2019-08-08 stsp free(*path_outfile);
4433 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4434 33aa809d 2019-08-08 stsp }
4435 33aa809d 2019-08-08 stsp free(path1);
4436 33aa809d 2019-08-08 stsp return err;
4437 33aa809d 2019-08-08 stsp }
4438 33aa809d 2019-08-08 stsp
4439 33aa809d 2019-08-08 stsp static const struct got_error *
4440 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4441 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4442 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4443 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4444 a129376b 2019-03-28 stsp {
4445 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4446 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4447 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4448 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4449 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4450 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4451 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4452 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4453 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4454 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4455 a129376b 2019-03-28 stsp
4456 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4457 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4458 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4459 d3bcc3d1 2019-08-08 stsp return NULL;
4460 3d69ad8d 2019-08-17 semarie
4461 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4462 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4463 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4464 d3bcc3d1 2019-08-08 stsp
4465 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4466 65084dad 2019-08-08 stsp if (ie == NULL)
4467 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4468 a129376b 2019-03-28 stsp
4469 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4470 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4471 a129376b 2019-03-28 stsp if (err) {
4472 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4473 a129376b 2019-03-28 stsp goto done;
4474 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4475 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4476 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4477 e20a8b6f 2019-06-04 stsp goto done;
4478 e20a8b6f 2019-06-04 stsp }
4479 a129376b 2019-03-28 stsp }
4480 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4481 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4482 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4483 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4484 a129376b 2019-03-28 stsp goto done;
4485 a129376b 2019-03-28 stsp }
4486 a129376b 2019-03-28 stsp } else {
4487 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4488 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4489 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4490 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4491 a129376b 2019-03-28 stsp goto done;
4492 a129376b 2019-03-28 stsp }
4493 a129376b 2019-03-28 stsp } else {
4494 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4495 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4496 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4497 a129376b 2019-03-28 stsp goto done;
4498 a129376b 2019-03-28 stsp }
4499 a129376b 2019-03-28 stsp }
4500 a129376b 2019-03-28 stsp }
4501 a129376b 2019-03-28 stsp
4502 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4503 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4504 a9fa2909 2019-07-27 stsp if (err) {
4505 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4506 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4507 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4508 a9fa2909 2019-07-27 stsp goto done;
4509 a9fa2909 2019-07-27 stsp } else {
4510 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4511 a9fa2909 2019-07-27 stsp if (err)
4512 a9fa2909 2019-07-27 stsp goto done;
4513 a9fa2909 2019-07-27 stsp
4514 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4515 1233e6b6 2020-10-19 stsp if (err)
4516 a9fa2909 2019-07-27 stsp goto done;
4517 a9fa2909 2019-07-27 stsp
4518 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4519 1233e6b6 2020-10-19 stsp free(te_name);
4520 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4521 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4522 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4523 a9fa2909 2019-07-27 stsp goto done;
4524 a9fa2909 2019-07-27 stsp }
4525 a129376b 2019-03-28 stsp }
4526 a129376b 2019-03-28 stsp
4527 a129376b 2019-03-28 stsp switch (status) {
4528 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4529 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4530 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4531 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4532 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4533 33aa809d 2019-08-08 stsp if (err)
4534 33aa809d 2019-08-08 stsp goto done;
4535 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4536 33aa809d 2019-08-08 stsp break;
4537 33aa809d 2019-08-08 stsp }
4538 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4539 1f1abb7e 2019-08-08 stsp ie->path);
4540 1ee397ad 2019-07-12 stsp if (err)
4541 1ee397ad 2019-07-12 stsp goto done;
4542 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4543 a129376b 2019-03-28 stsp break;
4544 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4545 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4546 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4547 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4548 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4549 33aa809d 2019-08-08 stsp if (err)
4550 33aa809d 2019-08-08 stsp goto done;
4551 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4552 33aa809d 2019-08-08 stsp break;
4553 33aa809d 2019-08-08 stsp }
4554 33aa809d 2019-08-08 stsp /* fall through */
4555 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4556 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4557 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4558 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4559 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4560 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4561 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4562 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4563 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4564 24278f30 2019-08-03 stsp } else
4565 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4566 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4567 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4568 a129376b 2019-03-28 stsp if (err)
4569 65084dad 2019-08-08 stsp goto done;
4570 65084dad 2019-08-08 stsp
4571 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4572 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4573 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4574 a129376b 2019-03-28 stsp goto done;
4575 65084dad 2019-08-08 stsp }
4576 65084dad 2019-08-08 stsp
4577 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4578 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4579 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4580 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4581 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4582 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4583 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4584 33aa809d 2019-08-08 stsp break;
4585 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4586 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4587 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4588 369fd7e5 2020-07-23 stsp path_content);
4589 369fd7e5 2020-07-23 stsp break;
4590 369fd7e5 2020-07-23 stsp }
4591 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4592 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4593 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4594 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4595 369fd7e5 2020-07-23 stsp } else {
4596 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4597 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4598 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4599 369fd7e5 2020-07-23 stsp goto done;
4600 369fd7e5 2020-07-23 stsp }
4601 33aa809d 2019-08-08 stsp }
4602 33aa809d 2019-08-08 stsp } else {
4603 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4604 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4605 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4606 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4607 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4608 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4609 2e1fa222 2020-07-23 stsp } else {
4610 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4611 2e1fa222 2020-07-23 stsp ie->path,
4612 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4613 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4614 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4615 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4616 2e1fa222 2020-07-23 stsp }
4617 a129376b 2019-03-28 stsp if (err)
4618 a129376b 2019-03-28 stsp goto done;
4619 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4620 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4621 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4622 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4623 437adc9d 2020-12-10 yzhong blob->id.sha1,
4624 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4625 2e1fa222 2020-07-23 stsp if (err)
4626 2e1fa222 2020-07-23 stsp goto done;
4627 2e1fa222 2020-07-23 stsp }
4628 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4629 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4630 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4631 33aa809d 2019-08-08 stsp }
4632 a129376b 2019-03-28 stsp }
4633 a129376b 2019-03-28 stsp break;
4634 e20a8b6f 2019-06-04 stsp }
4635 a129376b 2019-03-28 stsp default:
4636 1f1abb7e 2019-08-08 stsp break;
4637 a129376b 2019-03-28 stsp }
4638 a129376b 2019-03-28 stsp done:
4639 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4640 33aa809d 2019-08-08 stsp free(path_content);
4641 e20a8b6f 2019-06-04 stsp free(parent_path);
4642 a129376b 2019-03-28 stsp free(tree_path);
4643 a129376b 2019-03-28 stsp if (blob)
4644 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4645 a129376b 2019-03-28 stsp if (tree)
4646 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4647 a129376b 2019-03-28 stsp free(tree_id);
4648 e20a8b6f 2019-06-04 stsp return err;
4649 e20a8b6f 2019-06-04 stsp }
4650 e20a8b6f 2019-06-04 stsp
4651 e20a8b6f 2019-06-04 stsp const struct got_error *
4652 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4653 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4654 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4655 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4656 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4657 e20a8b6f 2019-06-04 stsp {
4658 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4659 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4660 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4661 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4662 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4663 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4664 e20a8b6f 2019-06-04 stsp
4665 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4666 e20a8b6f 2019-06-04 stsp if (err)
4667 e20a8b6f 2019-06-04 stsp return err;
4668 e20a8b6f 2019-06-04 stsp
4669 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4670 e20a8b6f 2019-06-04 stsp if (err)
4671 e20a8b6f 2019-06-04 stsp goto done;
4672 e20a8b6f 2019-06-04 stsp
4673 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4674 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4675 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4676 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4677 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4678 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4679 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4680 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4681 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4682 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4683 e20a8b6f 2019-06-04 stsp if (err)
4684 af12c6b9 2019-06-04 stsp break;
4685 e20a8b6f 2019-06-04 stsp }
4686 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4687 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4688 e20a8b6f 2019-06-04 stsp err = sync_err;
4689 af12c6b9 2019-06-04 stsp done:
4690 fb399478 2019-07-12 stsp free(fileindex_path);
4691 a129376b 2019-03-28 stsp if (fileindex)
4692 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4693 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4694 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4695 a129376b 2019-03-28 stsp err = unlockerr;
4696 c4296144 2019-05-09 stsp return err;
4697 c4296144 2019-05-09 stsp }
4698 c4296144 2019-05-09 stsp
4699 cf066bf8 2019-05-09 stsp static void
4700 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4701 cf066bf8 2019-05-09 stsp {
4702 24519714 2019-05-09 stsp free(ct->path);
4703 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4704 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4705 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4706 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4707 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4708 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4709 cf066bf8 2019-05-09 stsp free(ct);
4710 cf066bf8 2019-05-09 stsp }
4711 24519714 2019-05-09 stsp
4712 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4713 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4714 24519714 2019-05-09 stsp struct got_repository *repo;
4715 24519714 2019-05-09 stsp struct got_worktree *worktree;
4716 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4717 5f8a88c6 2019-08-03 stsp int have_staged_files;
4718 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4719 24519714 2019-05-09 stsp };
4720 cf066bf8 2019-05-09 stsp
4721 c4296144 2019-05-09 stsp static const struct got_error *
4722 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4723 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4724 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4725 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4726 c4296144 2019-05-09 stsp {
4727 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4728 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4729 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4730 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4731 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4732 768aea60 2019-05-09 stsp struct stat sb;
4733 c4296144 2019-05-09 stsp
4734 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4735 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4736 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4737 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4738 5f8a88c6 2019-08-03 stsp return NULL;
4739 5f8a88c6 2019-08-03 stsp } else {
4740 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4741 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4742 c4296144 2019-05-09 stsp
4743 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4744 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4745 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4746 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4747 5f8a88c6 2019-08-03 stsp return NULL;
4748 5f8a88c6 2019-08-03 stsp }
4749 0b5cc0d6 2019-05-09 stsp
4750 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4751 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4752 036813ee 2019-05-09 stsp goto done;
4753 036813ee 2019-05-09 stsp }
4754 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4755 036813ee 2019-05-09 stsp parent_path = strdup("");
4756 69960a46 2019-05-09 stsp if (parent_path == NULL)
4757 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4758 69960a46 2019-05-09 stsp } else {
4759 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4760 69960a46 2019-05-09 stsp if (err)
4761 69960a46 2019-05-09 stsp return err;
4762 69960a46 2019-05-09 stsp }
4763 c4296144 2019-05-09 stsp
4764 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4765 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4766 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4767 c4296144 2019-05-09 stsp goto done;
4768 768aea60 2019-05-09 stsp }
4769 768aea60 2019-05-09 stsp
4770 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4771 768aea60 2019-05-09 stsp relpath) == -1) {
4772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4773 768aea60 2019-05-09 stsp goto done;
4774 768aea60 2019-05-09 stsp }
4775 0aeb8099 2020-07-23 stsp
4776 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4777 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4778 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4779 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4780 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4781 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4782 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4783 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4784 0aeb8099 2020-07-23 stsp break;
4785 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4786 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4787 0aeb8099 2020-07-23 stsp break;
4788 0aeb8099 2020-07-23 stsp default:
4789 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4790 0aeb8099 2020-07-23 stsp goto done;
4791 0aeb8099 2020-07-23 stsp }
4792 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4793 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4794 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4795 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4796 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4797 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4798 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4799 12463d8b 2019-12-13 stsp ct->ondisk_path);
4800 12463d8b 2019-12-13 stsp goto done;
4801 12463d8b 2019-12-13 stsp }
4802 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4803 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4804 768aea60 2019-05-09 stsp goto done;
4805 768aea60 2019-05-09 stsp }
4806 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4807 c4296144 2019-05-09 stsp }
4808 c4296144 2019-05-09 stsp
4809 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4810 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4811 44d03001 2019-05-09 stsp relpath) == -1) {
4812 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4813 44d03001 2019-05-09 stsp goto done;
4814 44d03001 2019-05-09 stsp }
4815 44d03001 2019-05-09 stsp
4816 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4817 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4818 35213c7c 2020-07-23 stsp int is_bad_symlink;
4819 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4820 35213c7c 2020-07-23 stsp ssize_t target_len;
4821 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4822 35213c7c 2020-07-23 stsp sizeof(target_path));
4823 35213c7c 2020-07-23 stsp if (target_len == -1) {
4824 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4825 35213c7c 2020-07-23 stsp ct->ondisk_path);
4826 35213c7c 2020-07-23 stsp goto done;
4827 35213c7c 2020-07-23 stsp }
4828 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4829 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4830 35213c7c 2020-07-23 stsp if (err)
4831 35213c7c 2020-07-23 stsp goto done;
4832 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4833 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4834 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4835 35213c7c 2020-07-23 stsp goto done;
4836 35213c7c 2020-07-23 stsp }
4837 35213c7c 2020-07-23 stsp }
4838 35213c7c 2020-07-23 stsp
4839 35213c7c 2020-07-23 stsp
4840 cf066bf8 2019-05-09 stsp ct->status = status;
4841 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4842 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4843 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4844 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4845 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4846 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4847 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4848 b416585c 2019-05-13 stsp goto done;
4849 b416585c 2019-05-13 stsp }
4850 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4851 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4852 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4853 f0b75401 2019-08-03 stsp goto done;
4854 f0b75401 2019-08-03 stsp }
4855 f0b75401 2019-08-03 stsp }
4856 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4857 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4858 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4859 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4860 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4861 036813ee 2019-05-09 stsp goto done;
4862 036813ee 2019-05-09 stsp }
4863 ca2503ea 2019-05-09 stsp }
4864 24519714 2019-05-09 stsp ct->path = strdup(path);
4865 24519714 2019-05-09 stsp if (ct->path == NULL) {
4866 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4867 24519714 2019-05-09 stsp goto done;
4868 24519714 2019-05-09 stsp }
4869 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4870 c4296144 2019-05-09 stsp done:
4871 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4872 ed175427 2019-05-09 stsp free_commitable(ct);
4873 24519714 2019-05-09 stsp free(parent_path);
4874 036813ee 2019-05-09 stsp free(path);
4875 a129376b 2019-03-28 stsp return err;
4876 a129376b 2019-03-28 stsp }
4877 c4296144 2019-05-09 stsp
4878 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4879 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4880 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4881 036813ee 2019-05-09 stsp struct got_repository *);
4882 ed175427 2019-05-09 stsp
4883 ed175427 2019-05-09 stsp static const struct got_error *
4884 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4885 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4886 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4887 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4888 afa376bf 2019-05-09 stsp struct got_repository *repo)
4889 ed175427 2019-05-09 stsp {
4890 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4891 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4892 036813ee 2019-05-09 stsp char *subpath;
4893 ed175427 2019-05-09 stsp
4894 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4895 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4896 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4897 ed175427 2019-05-09 stsp
4898 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4899 ed175427 2019-05-09 stsp if (err)
4900 ed175427 2019-05-09 stsp return err;
4901 ed175427 2019-05-09 stsp
4902 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4903 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4904 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4905 036813ee 2019-05-09 stsp free(subpath);
4906 036813ee 2019-05-09 stsp return err;
4907 036813ee 2019-05-09 stsp }
4908 ed175427 2019-05-09 stsp
4909 036813ee 2019-05-09 stsp static const struct got_error *
4910 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4911 036813ee 2019-05-09 stsp {
4912 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4913 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4914 ed175427 2019-05-09 stsp
4915 036813ee 2019-05-09 stsp *match = 0;
4916 ed175427 2019-05-09 stsp
4917 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4918 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4919 0f63689d 2019-05-10 stsp return NULL;
4920 036813ee 2019-05-09 stsp }
4921 0b5cc0d6 2019-05-09 stsp
4922 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4923 0f63689d 2019-05-10 stsp if (err)
4924 0f63689d 2019-05-10 stsp return err;
4925 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4926 036813ee 2019-05-09 stsp free(ct_parent_path);
4927 036813ee 2019-05-09 stsp return err;
4928 036813ee 2019-05-09 stsp }
4929 0b5cc0d6 2019-05-09 stsp
4930 768aea60 2019-05-09 stsp static mode_t
4931 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4932 768aea60 2019-05-09 stsp {
4933 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4934 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4935 3d9a4ec4 2020-07-23 stsp
4936 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4937 768aea60 2019-05-09 stsp }
4938 768aea60 2019-05-09 stsp
4939 036813ee 2019-05-09 stsp static const struct got_error *
4940 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4941 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4942 036813ee 2019-05-09 stsp {
4943 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4944 ca2503ea 2019-05-09 stsp
4945 036813ee 2019-05-09 stsp *new_te = NULL;
4946 0b5cc0d6 2019-05-09 stsp
4947 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4948 036813ee 2019-05-09 stsp if (err)
4949 036813ee 2019-05-09 stsp goto done;
4950 0b5cc0d6 2019-05-09 stsp
4951 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4952 036813ee 2019-05-09 stsp
4953 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4954 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4955 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4956 5f8a88c6 2019-08-03 stsp else
4957 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4958 036813ee 2019-05-09 stsp done:
4959 036813ee 2019-05-09 stsp if (err && *new_te) {
4960 56e0773d 2019-11-28 stsp free(*new_te);
4961 036813ee 2019-05-09 stsp *new_te = NULL;
4962 036813ee 2019-05-09 stsp }
4963 036813ee 2019-05-09 stsp return err;
4964 036813ee 2019-05-09 stsp }
4965 036813ee 2019-05-09 stsp
4966 036813ee 2019-05-09 stsp static const struct got_error *
4967 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4968 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4969 036813ee 2019-05-09 stsp {
4970 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4971 102b254e 2020-10-19 stsp char *ct_name = NULL;
4972 036813ee 2019-05-09 stsp
4973 036813ee 2019-05-09 stsp *new_te = NULL;
4974 036813ee 2019-05-09 stsp
4975 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4976 036813ee 2019-05-09 stsp if (*new_te == NULL)
4977 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4978 036813ee 2019-05-09 stsp
4979 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4980 102b254e 2020-10-19 stsp if (err)
4981 036813ee 2019-05-09 stsp goto done;
4982 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4983 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4984 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4985 036813ee 2019-05-09 stsp goto done;
4986 036813ee 2019-05-09 stsp }
4987 036813ee 2019-05-09 stsp
4988 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4989 036813ee 2019-05-09 stsp
4990 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4991 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4992 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4993 5f8a88c6 2019-08-03 stsp else
4994 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4995 036813ee 2019-05-09 stsp done:
4996 102b254e 2020-10-19 stsp free(ct_name);
4997 036813ee 2019-05-09 stsp if (err && *new_te) {
4998 56e0773d 2019-11-28 stsp free(*new_te);
4999 036813ee 2019-05-09 stsp *new_te = NULL;
5000 036813ee 2019-05-09 stsp }
5001 036813ee 2019-05-09 stsp return err;
5002 036813ee 2019-05-09 stsp }
5003 036813ee 2019-05-09 stsp
5004 036813ee 2019-05-09 stsp static const struct got_error *
5005 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5006 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5007 036813ee 2019-05-09 stsp {
5008 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5009 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5010 036813ee 2019-05-09 stsp
5011 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5012 036813ee 2019-05-09 stsp if (err)
5013 036813ee 2019-05-09 stsp return err;
5014 036813ee 2019-05-09 stsp if (new_pe == NULL)
5015 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5016 036813ee 2019-05-09 stsp return NULL;
5017 afa376bf 2019-05-09 stsp }
5018 afa376bf 2019-05-09 stsp
5019 afa376bf 2019-05-09 stsp static const struct got_error *
5020 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5021 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5022 afa376bf 2019-05-09 stsp {
5023 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5024 5f8a88c6 2019-08-03 stsp unsigned char status;
5025 5f8a88c6 2019-08-03 stsp
5026 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5027 afa376bf 2019-05-09 stsp ct_path++;
5028 5f8a88c6 2019-08-03 stsp
5029 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5030 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5031 5f8a88c6 2019-08-03 stsp else
5032 5f8a88c6 2019-08-03 stsp status = ct->status;
5033 5f8a88c6 2019-08-03 stsp
5034 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5035 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5036 036813ee 2019-05-09 stsp }
5037 036813ee 2019-05-09 stsp
5038 036813ee 2019-05-09 stsp static const struct got_error *
5039 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5040 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5041 44d03001 2019-05-09 stsp {
5042 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5043 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5044 44d03001 2019-05-09 stsp char *te_path;
5045 44d03001 2019-05-09 stsp
5046 44d03001 2019-05-09 stsp *modified = 0;
5047 44d03001 2019-05-09 stsp
5048 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5049 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5050 44d03001 2019-05-09 stsp te->name) == -1)
5051 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5052 44d03001 2019-05-09 stsp
5053 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5054 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5055 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5056 44d03001 2019-05-09 stsp strlen(te_path));
5057 62d463ca 2020-10-20 naddy if (*modified)
5058 44d03001 2019-05-09 stsp break;
5059 44d03001 2019-05-09 stsp }
5060 44d03001 2019-05-09 stsp
5061 44d03001 2019-05-09 stsp free(te_path);
5062 44d03001 2019-05-09 stsp return err;
5063 44d03001 2019-05-09 stsp }
5064 44d03001 2019-05-09 stsp
5065 44d03001 2019-05-09 stsp static const struct got_error *
5066 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5067 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5068 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5069 036813ee 2019-05-09 stsp {
5070 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5071 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5072 036813ee 2019-05-09 stsp
5073 036813ee 2019-05-09 stsp *ctp = NULL;
5074 036813ee 2019-05-09 stsp
5075 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5076 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5077 036813ee 2019-05-09 stsp char *ct_name = NULL;
5078 036813ee 2019-05-09 stsp int path_matches;
5079 036813ee 2019-05-09 stsp
5080 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5081 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5082 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5083 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5084 5f8a88c6 2019-08-03 stsp continue;
5085 5f8a88c6 2019-08-03 stsp } else {
5086 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5087 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5088 5f8a88c6 2019-08-03 stsp continue;
5089 5f8a88c6 2019-08-03 stsp }
5090 036813ee 2019-05-09 stsp
5091 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5092 036813ee 2019-05-09 stsp continue;
5093 036813ee 2019-05-09 stsp
5094 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5095 62d463ca 2020-10-20 naddy if (err)
5096 036813ee 2019-05-09 stsp return err;
5097 036813ee 2019-05-09 stsp if (!path_matches)
5098 036813ee 2019-05-09 stsp continue;
5099 036813ee 2019-05-09 stsp
5100 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5101 d34b633e 2020-10-19 stsp if (err)
5102 d34b633e 2020-10-19 stsp return err;
5103 d34b633e 2020-10-19 stsp
5104 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5105 d34b633e 2020-10-19 stsp free(ct_name);
5106 036813ee 2019-05-09 stsp continue;
5107 d34b633e 2020-10-19 stsp }
5108 d34b633e 2020-10-19 stsp free(ct_name);
5109 036813ee 2019-05-09 stsp
5110 036813ee 2019-05-09 stsp *ctp = ct;
5111 036813ee 2019-05-09 stsp break;
5112 036813ee 2019-05-09 stsp }
5113 2b496619 2019-07-10 stsp
5114 2b496619 2019-07-10 stsp return err;
5115 2b496619 2019-07-10 stsp }
5116 2b496619 2019-07-10 stsp
5117 2b496619 2019-07-10 stsp static const struct got_error *
5118 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5119 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5120 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5121 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5122 2b496619 2019-07-10 stsp struct got_repository *repo)
5123 2b496619 2019-07-10 stsp {
5124 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5125 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5126 2b496619 2019-07-10 stsp char *subtree_path;
5127 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5128 ba580f68 2020-03-22 stsp int nentries;
5129 2b496619 2019-07-10 stsp
5130 2b496619 2019-07-10 stsp *new_tep = NULL;
5131 2b496619 2019-07-10 stsp
5132 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5133 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5134 2b496619 2019-07-10 stsp child_path) == -1)
5135 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5136 036813ee 2019-05-09 stsp
5137 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5138 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5139 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5140 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5141 56e0773d 2019-11-28 stsp
5142 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5143 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5144 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5145 2b496619 2019-07-10 stsp goto done;
5146 2b496619 2019-07-10 stsp }
5147 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5148 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5149 2b496619 2019-07-10 stsp if (err) {
5150 56e0773d 2019-11-28 stsp free(new_te);
5151 2b496619 2019-07-10 stsp goto done;
5152 2b496619 2019-07-10 stsp }
5153 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5154 2b496619 2019-07-10 stsp done:
5155 56e0773d 2019-11-28 stsp free(id);
5156 2b496619 2019-07-10 stsp free(subtree_path);
5157 2b496619 2019-07-10 stsp if (err == NULL)
5158 2b496619 2019-07-10 stsp *new_tep = new_te;
5159 036813ee 2019-05-09 stsp return err;
5160 036813ee 2019-05-09 stsp }
5161 036813ee 2019-05-09 stsp
5162 036813ee 2019-05-09 stsp static const struct got_error *
5163 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5164 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5165 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5166 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5167 036813ee 2019-05-09 stsp struct got_repository *repo)
5168 036813ee 2019-05-09 stsp {
5169 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5170 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5171 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5172 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5173 036813ee 2019-05-09 stsp
5174 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5175 ba580f68 2020-03-22 stsp *nentries = 0;
5176 036813ee 2019-05-09 stsp
5177 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5178 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5179 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5180 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5181 036813ee 2019-05-09 stsp
5182 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5183 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5184 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5185 036813ee 2019-05-09 stsp continue;
5186 036813ee 2019-05-09 stsp
5187 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5188 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5189 036813ee 2019-05-09 stsp continue;
5190 036813ee 2019-05-09 stsp
5191 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5192 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5193 036813ee 2019-05-09 stsp if (err)
5194 036813ee 2019-05-09 stsp goto done;
5195 036813ee 2019-05-09 stsp
5196 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5197 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5198 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5199 036813ee 2019-05-09 stsp if (err)
5200 036813ee 2019-05-09 stsp goto done;
5201 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5202 afa376bf 2019-05-09 stsp if (err)
5203 afa376bf 2019-05-09 stsp goto done;
5204 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5205 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5206 2b496619 2019-07-10 stsp if (err)
5207 2f51b5b3 2019-05-09 stsp goto done;
5208 ba580f68 2020-03-22 stsp (*nentries)++;
5209 2b496619 2019-07-10 stsp } else {
5210 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5211 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5212 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5213 2b496619 2019-07-10 stsp == NULL) {
5214 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5215 2b496619 2019-07-10 stsp child_path, path_base_tree,
5216 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5217 2b496619 2019-07-10 stsp repo);
5218 2b496619 2019-07-10 stsp if (err)
5219 2b496619 2019-07-10 stsp goto done;
5220 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5221 2b496619 2019-07-10 stsp if (err)
5222 2b496619 2019-07-10 stsp goto done;
5223 ba580f68 2020-03-22 stsp (*nentries)++;
5224 9ba0479c 2019-05-10 stsp }
5225 ed175427 2019-05-09 stsp }
5226 2f51b5b3 2019-05-09 stsp }
5227 2f51b5b3 2019-05-09 stsp
5228 2f51b5b3 2019-05-09 stsp if (base_tree) {
5229 56e0773d 2019-11-28 stsp int i, nbase_entries;
5230 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5231 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5232 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5233 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5234 63c5ca5d 2019-08-24 stsp
5235 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5236 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5237 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5238 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5239 63c5ca5d 2019-08-24 stsp if (err)
5240 63c5ca5d 2019-08-24 stsp goto done;
5241 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5242 63c5ca5d 2019-08-24 stsp if (err)
5243 63c5ca5d 2019-08-24 stsp goto done;
5244 ba580f68 2020-03-22 stsp (*nentries)++;
5245 63c5ca5d 2019-08-24 stsp continue;
5246 63c5ca5d 2019-08-24 stsp }
5247 2f51b5b3 2019-05-09 stsp
5248 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5249 44d03001 2019-05-09 stsp int modified;
5250 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5251 036813ee 2019-05-09 stsp if (err)
5252 036813ee 2019-05-09 stsp goto done;
5253 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5254 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5255 2f51b5b3 2019-05-09 stsp if (err)
5256 2f51b5b3 2019-05-09 stsp goto done;
5257 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5258 44d03001 2019-05-09 stsp if (modified) {
5259 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5260 ba580f68 2020-03-22 stsp int nsubentries;
5261 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5262 ba580f68 2020-03-22 stsp &nsubentries, te,
5263 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5264 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5265 44d03001 2019-05-09 stsp if (err)
5266 44d03001 2019-05-09 stsp goto done;
5267 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5268 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5269 ba580f68 2020-03-22 stsp free(new_id);
5270 ba580f68 2020-03-22 stsp continue;
5271 ba580f68 2020-03-22 stsp }
5272 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5273 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5274 56e0773d 2019-11-28 stsp free(new_id);
5275 44d03001 2019-05-09 stsp }
5276 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5277 036813ee 2019-05-09 stsp if (err)
5278 036813ee 2019-05-09 stsp goto done;
5279 ba580f68 2020-03-22 stsp (*nentries)++;
5280 2f51b5b3 2019-05-09 stsp continue;
5281 036813ee 2019-05-09 stsp }
5282 2f51b5b3 2019-05-09 stsp
5283 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5284 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5285 f66c734c 2019-09-22 stsp if (err)
5286 f66c734c 2019-09-22 stsp goto done;
5287 2f51b5b3 2019-05-09 stsp if (ct) {
5288 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5289 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5290 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5291 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5292 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5293 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5294 2f51b5b3 2019-05-09 stsp if (err)
5295 2f51b5b3 2019-05-09 stsp goto done;
5296 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5297 2f51b5b3 2019-05-09 stsp if (err)
5298 2f51b5b3 2019-05-09 stsp goto done;
5299 ba580f68 2020-03-22 stsp (*nentries)++;
5300 2f51b5b3 2019-05-09 stsp }
5301 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5302 b416585c 2019-05-13 stsp status_arg);
5303 afa376bf 2019-05-09 stsp if (err)
5304 afa376bf 2019-05-09 stsp goto done;
5305 2f51b5b3 2019-05-09 stsp } else {
5306 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5307 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5308 2f51b5b3 2019-05-09 stsp if (err)
5309 2f51b5b3 2019-05-09 stsp goto done;
5310 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5311 2f51b5b3 2019-05-09 stsp if (err)
5312 2f51b5b3 2019-05-09 stsp goto done;
5313 ba580f68 2020-03-22 stsp (*nentries)++;
5314 2f51b5b3 2019-05-09 stsp }
5315 ca2503ea 2019-05-09 stsp }
5316 ed175427 2019-05-09 stsp }
5317 0b5cc0d6 2019-05-09 stsp
5318 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5319 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5320 ed175427 2019-05-09 stsp done:
5321 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5322 2e1fa222 2020-07-23 stsp return err;
5323 2e1fa222 2020-07-23 stsp }
5324 2e1fa222 2020-07-23 stsp
5325 2e1fa222 2020-07-23 stsp static const struct got_error *
5326 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5327 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5328 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5329 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5330 ebf99748 2019-05-09 stsp {
5331 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5332 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5333 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5334 ebf99748 2019-05-09 stsp
5335 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5336 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5337 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5338 ebf99748 2019-05-09 stsp
5339 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5340 437adc9d 2020-12-10 yzhong
5341 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5342 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5343 437adc9d 2020-12-10 yzhong if (err)
5344 437adc9d 2020-12-10 yzhong goto done;
5345 437adc9d 2020-12-10 yzhong
5346 ebf99748 2019-05-09 stsp if (ie) {
5347 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5348 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5349 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5350 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5351 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5352 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5353 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5354 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5355 437adc9d 2020-12-10 yzhong
5356 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5357 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5358 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5359 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5360 72fd46fa 2019-09-06 stsp !have_staged_files);
5361 ebf99748 2019-05-09 stsp } else
5362 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5363 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5364 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5365 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5366 72fd46fa 2019-09-06 stsp !have_staged_files);
5367 ebf99748 2019-05-09 stsp } else {
5368 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5369 ebf99748 2019-05-09 stsp if (err)
5370 437adc9d 2020-12-10 yzhong goto done;
5371 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5372 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5373 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
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 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5379 3969253a 2020-03-07 stsp if (err) {
5380 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5381 437adc9d 2020-12-10 yzhong goto done;
5382 3969253a 2020-03-07 stsp }
5383 ebf99748 2019-05-09 stsp }
5384 437adc9d 2020-12-10 yzhong free(relpath);
5385 437adc9d 2020-12-10 yzhong relpath = NULL;
5386 ebf99748 2019-05-09 stsp }
5387 437adc9d 2020-12-10 yzhong done:
5388 437adc9d 2020-12-10 yzhong free(relpath);
5389 d56d26ce 2019-05-10 stsp return err;
5390 d56d26ce 2019-05-10 stsp }
5391 735ef5ac 2019-08-03 stsp
5392 d56d26ce 2019-05-10 stsp
5393 d56d26ce 2019-05-10 stsp static const struct got_error *
5394 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5395 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5396 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5397 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5398 735ef5ac 2019-08-03 stsp int ood_errcode)
5399 d56d26ce 2019-05-10 stsp {
5400 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5401 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5402 1a36436d 2019-06-10 stsp
5403 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5404 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5405 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5406 1a36436d 2019-06-10 stsp return NULL;
5407 9bead371 2019-07-28 stsp /*
5408 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5409 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5410 9bead371 2019-07-28 stsp */
5411 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5412 735ef5ac 2019-08-03 stsp in_repo_path);
5413 9bead371 2019-07-28 stsp if (err) {
5414 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5415 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5416 1a36436d 2019-06-10 stsp goto done;
5417 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5418 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5419 1a36436d 2019-06-10 stsp } else {
5420 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5421 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5422 735ef5ac 2019-08-03 stsp in_repo_path);
5423 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5424 1a36436d 2019-06-10 stsp goto done;
5425 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5426 1a36436d 2019-06-10 stsp }
5427 1a36436d 2019-06-10 stsp done:
5428 1a36436d 2019-06-10 stsp free(id);
5429 1a36436d 2019-06-10 stsp return err;
5430 ebf99748 2019-05-09 stsp }
5431 ebf99748 2019-05-09 stsp
5432 c4296144 2019-05-09 stsp const struct got_error *
5433 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5434 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5435 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5436 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5437 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5438 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5439 afa376bf 2019-05-09 stsp struct got_repository *repo)
5440 c4296144 2019-05-09 stsp {
5441 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5442 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5443 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5444 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5445 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5446 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5447 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5448 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5449 ba580f68 2020-03-22 stsp int nentries;
5450 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5451 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5452 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5453 c4296144 2019-05-09 stsp
5454 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5455 c4296144 2019-05-09 stsp
5456 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5457 675c7539 2019-05-09 stsp
5458 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5459 588edf97 2019-05-10 stsp if (err)
5460 588edf97 2019-05-10 stsp goto done;
5461 de18fc63 2019-05-09 stsp
5462 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5463 588edf97 2019-05-10 stsp if (err)
5464 588edf97 2019-05-10 stsp goto done;
5465 588edf97 2019-05-10 stsp
5466 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5467 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5468 33ad4cbe 2019-05-12 jcs if (err)
5469 33ad4cbe 2019-05-12 jcs goto done;
5470 33ad4cbe 2019-05-12 jcs }
5471 c4296144 2019-05-09 stsp
5472 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5473 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5474 33ad4cbe 2019-05-12 jcs goto done;
5475 33ad4cbe 2019-05-12 jcs }
5476 33ad4cbe 2019-05-12 jcs
5477 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5478 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5479 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5480 cf066bf8 2019-05-09 stsp char *ondisk_path;
5481 cf066bf8 2019-05-09 stsp
5482 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5483 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5484 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5485 5f8a88c6 2019-08-03 stsp continue;
5486 5f8a88c6 2019-08-03 stsp
5487 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5488 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5489 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5490 cf066bf8 2019-05-09 stsp continue;
5491 cf066bf8 2019-05-09 stsp
5492 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5493 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5494 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5495 cf066bf8 2019-05-09 stsp goto done;
5496 cf066bf8 2019-05-09 stsp }
5497 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5498 2e1fa222 2020-07-23 stsp free(ondisk_path);
5499 2e1fa222 2020-07-23 stsp if (err)
5500 cf066bf8 2019-05-09 stsp goto done;
5501 cf066bf8 2019-05-09 stsp }
5502 036813ee 2019-05-09 stsp
5503 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5504 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5505 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5506 036813ee 2019-05-09 stsp if (err)
5507 036813ee 2019-05-09 stsp goto done;
5508 036813ee 2019-05-09 stsp
5509 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5510 de18fc63 2019-05-09 stsp if (err)
5511 de18fc63 2019-05-09 stsp goto done;
5512 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5513 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5514 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5515 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5516 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5517 33ad4cbe 2019-05-12 jcs free(logmsg);
5518 9d40349a 2019-05-09 stsp if (err)
5519 9d40349a 2019-05-09 stsp goto done;
5520 9d40349a 2019-05-09 stsp
5521 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5522 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5523 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5524 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5525 09f5bd90 2019-05-09 stsp goto done;
5526 09f5bd90 2019-05-09 stsp }
5527 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5528 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5529 09f5bd90 2019-05-09 stsp if (err)
5530 09f5bd90 2019-05-09 stsp goto done;
5531 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5532 ebf99748 2019-05-09 stsp if (err)
5533 ebf99748 2019-05-09 stsp goto done;
5534 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5535 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5536 09f5bd90 2019-05-09 stsp goto done;
5537 09f5bd90 2019-05-09 stsp }
5538 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5539 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5540 f2c16586 2019-05-09 stsp if (err)
5541 f2c16586 2019-05-09 stsp goto done;
5542 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5543 f2c16586 2019-05-09 stsp if (err)
5544 f2c16586 2019-05-09 stsp goto done;
5545 f2c16586 2019-05-09 stsp
5546 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5547 09f5bd90 2019-05-09 stsp if (err)
5548 09f5bd90 2019-05-09 stsp goto done;
5549 09f5bd90 2019-05-09 stsp
5550 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5551 ebf99748 2019-05-09 stsp if (err)
5552 ebf99748 2019-05-09 stsp goto done;
5553 c4296144 2019-05-09 stsp done:
5554 588edf97 2019-05-10 stsp if (head_tree)
5555 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5556 588edf97 2019-05-10 stsp if (head_commit)
5557 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5558 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5559 2f17228e 2019-05-12 stsp if (head_ref2) {
5560 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5561 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5562 2f17228e 2019-05-12 stsp err = unlockerr;
5563 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5564 39cd0ff6 2019-07-12 stsp }
5565 39cd0ff6 2019-07-12 stsp return err;
5566 39cd0ff6 2019-07-12 stsp }
5567 5c1e53bc 2019-07-28 stsp
5568 5c1e53bc 2019-07-28 stsp static const struct got_error *
5569 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5570 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5571 5c1e53bc 2019-07-28 stsp {
5572 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5573 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5574 39cd0ff6 2019-07-12 stsp
5575 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5576 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5577 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5578 5c1e53bc 2019-07-28 stsp
5579 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5580 5c1e53bc 2019-07-28 stsp ct_path++;
5581 5c1e53bc 2019-07-28 stsp
5582 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5583 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5584 5c1e53bc 2019-07-28 stsp break;
5585 5c1e53bc 2019-07-28 stsp }
5586 5c1e53bc 2019-07-28 stsp
5587 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5588 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5589 5c1e53bc 2019-07-28 stsp
5590 5c1e53bc 2019-07-28 stsp return NULL;
5591 5c1e53bc 2019-07-28 stsp }
5592 5c1e53bc 2019-07-28 stsp
5593 f0b75401 2019-08-03 stsp static const struct got_error *
5594 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5595 f0b75401 2019-08-03 stsp {
5596 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5597 f0b75401 2019-08-03 stsp
5598 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5599 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5600 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5601 f0b75401 2019-08-03 stsp }
5602 f0b75401 2019-08-03 stsp
5603 f0b75401 2019-08-03 stsp return NULL;
5604 f0b75401 2019-08-03 stsp }
5605 f0b75401 2019-08-03 stsp
5606 5f8a88c6 2019-08-03 stsp static const struct got_error *
5607 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5608 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5609 5f8a88c6 2019-08-03 stsp {
5610 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5611 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5612 5f8a88c6 2019-08-03 stsp
5613 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5614 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5615 5f8a88c6 2019-08-03 stsp continue;
5616 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5617 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5618 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5619 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5620 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5621 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5622 5f8a88c6 2019-08-03 stsp }
5623 5f8a88c6 2019-08-03 stsp
5624 5f8a88c6 2019-08-03 stsp return NULL;
5625 5f8a88c6 2019-08-03 stsp }
5626 5f8a88c6 2019-08-03 stsp
5627 39cd0ff6 2019-07-12 stsp const struct got_error *
5628 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5629 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5630 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5631 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5632 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5633 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5634 39cd0ff6 2019-07-12 stsp {
5635 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5636 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5637 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5638 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5639 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5640 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5641 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5642 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5643 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5644 39cd0ff6 2019-07-12 stsp
5645 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5646 39cd0ff6 2019-07-12 stsp
5647 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5648 39cd0ff6 2019-07-12 stsp
5649 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5650 39cd0ff6 2019-07-12 stsp if (err)
5651 39cd0ff6 2019-07-12 stsp goto done;
5652 39cd0ff6 2019-07-12 stsp
5653 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5654 39cd0ff6 2019-07-12 stsp if (err)
5655 39cd0ff6 2019-07-12 stsp goto done;
5656 39cd0ff6 2019-07-12 stsp
5657 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5658 39cd0ff6 2019-07-12 stsp if (err)
5659 39cd0ff6 2019-07-12 stsp goto done;
5660 39cd0ff6 2019-07-12 stsp
5661 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5662 39cd0ff6 2019-07-12 stsp if (err)
5663 39cd0ff6 2019-07-12 stsp goto done;
5664 39cd0ff6 2019-07-12 stsp
5665 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5666 5f8a88c6 2019-08-03 stsp &have_staged_files);
5667 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5668 5f8a88c6 2019-08-03 stsp goto done;
5669 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5670 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5671 5f8a88c6 2019-08-03 stsp if (err)
5672 5f8a88c6 2019-08-03 stsp goto done;
5673 5f8a88c6 2019-08-03 stsp }
5674 5f8a88c6 2019-08-03 stsp
5675 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5676 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5677 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5678 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5679 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5680 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5681 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5682 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5683 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5684 5c1e53bc 2019-07-28 stsp if (err)
5685 5c1e53bc 2019-07-28 stsp goto done;
5686 5c1e53bc 2019-07-28 stsp }
5687 39cd0ff6 2019-07-12 stsp
5688 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5689 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5690 39cd0ff6 2019-07-12 stsp goto done;
5691 5c1e53bc 2019-07-28 stsp }
5692 5c1e53bc 2019-07-28 stsp
5693 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5694 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5695 5c1e53bc 2019-07-28 stsp if (err)
5696 5c1e53bc 2019-07-28 stsp goto done;
5697 39cd0ff6 2019-07-12 stsp }
5698 f0b75401 2019-08-03 stsp
5699 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5700 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5701 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5702 f0b75401 2019-08-03 stsp
5703 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5704 f0b75401 2019-08-03 stsp ct_path++;
5705 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5706 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5707 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5708 b50cabdf 2019-07-12 stsp if (err)
5709 b50cabdf 2019-07-12 stsp goto done;
5710 f0b75401 2019-08-03 stsp
5711 b50cabdf 2019-07-12 stsp }
5712 b50cabdf 2019-07-12 stsp
5713 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5714 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5715 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5716 39cd0ff6 2019-07-12 stsp if (err)
5717 39cd0ff6 2019-07-12 stsp goto done;
5718 39cd0ff6 2019-07-12 stsp
5719 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5720 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5721 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5722 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5723 39cd0ff6 2019-07-12 stsp err = sync_err;
5724 39cd0ff6 2019-07-12 stsp done:
5725 39cd0ff6 2019-07-12 stsp if (fileindex)
5726 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5727 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5728 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5729 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5730 39cd0ff6 2019-07-12 stsp err = unlockerr;
5731 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5732 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5733 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5734 39cd0ff6 2019-07-12 stsp }
5735 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5736 c4296144 2019-05-09 stsp return err;
5737 8656d6c4 2019-05-20 stsp }
5738 8656d6c4 2019-05-20 stsp
5739 8656d6c4 2019-05-20 stsp const char *
5740 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5741 8656d6c4 2019-05-20 stsp {
5742 8656d6c4 2019-05-20 stsp return ct->path;
5743 8656d6c4 2019-05-20 stsp }
5744 8656d6c4 2019-05-20 stsp
5745 8656d6c4 2019-05-20 stsp unsigned int
5746 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5747 8656d6c4 2019-05-20 stsp {
5748 8656d6c4 2019-05-20 stsp return ct->status;
5749 818c7501 2019-07-11 stsp }
5750 818c7501 2019-07-11 stsp
5751 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5752 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5753 818c7501 2019-07-11 stsp struct got_repository *repo;
5754 818c7501 2019-07-11 stsp };
5755 818c7501 2019-07-11 stsp
5756 818c7501 2019-07-11 stsp static const struct got_error *
5757 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5758 818c7501 2019-07-11 stsp {
5759 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5760 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5761 818c7501 2019-07-11 stsp unsigned char status;
5762 818c7501 2019-07-11 stsp struct stat sb;
5763 818c7501 2019-07-11 stsp char *ondisk_path;
5764 818c7501 2019-07-11 stsp
5765 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5766 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5767 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5768 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5769 818c7501 2019-07-11 stsp
5770 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5771 818c7501 2019-07-11 stsp == -1)
5772 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5773 818c7501 2019-07-11 stsp
5774 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5775 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5776 818c7501 2019-07-11 stsp free(ondisk_path);
5777 818c7501 2019-07-11 stsp if (err)
5778 818c7501 2019-07-11 stsp return err;
5779 818c7501 2019-07-11 stsp
5780 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5781 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5782 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5783 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5784 818c7501 2019-07-11 stsp
5785 818c7501 2019-07-11 stsp return NULL;
5786 818c7501 2019-07-11 stsp }
5787 818c7501 2019-07-11 stsp
5788 818c7501 2019-07-11 stsp const struct got_error *
5789 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5790 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5791 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5792 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5793 818c7501 2019-07-11 stsp {
5794 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5795 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5796 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5797 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5798 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5799 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5800 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5801 818c7501 2019-07-11 stsp
5802 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5803 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5804 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5805 818c7501 2019-07-11 stsp
5806 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5807 818c7501 2019-07-11 stsp if (err)
5808 818c7501 2019-07-11 stsp return err;
5809 818c7501 2019-07-11 stsp
5810 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5811 818c7501 2019-07-11 stsp if (err)
5812 818c7501 2019-07-11 stsp goto done;
5813 818c7501 2019-07-11 stsp
5814 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5815 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5816 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5817 818c7501 2019-07-11 stsp &ok_arg);
5818 818c7501 2019-07-11 stsp if (err)
5819 818c7501 2019-07-11 stsp goto done;
5820 818c7501 2019-07-11 stsp
5821 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5822 818c7501 2019-07-11 stsp if (err)
5823 818c7501 2019-07-11 stsp goto done;
5824 818c7501 2019-07-11 stsp
5825 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5826 818c7501 2019-07-11 stsp if (err)
5827 818c7501 2019-07-11 stsp goto done;
5828 818c7501 2019-07-11 stsp
5829 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5830 818c7501 2019-07-11 stsp if (err)
5831 818c7501 2019-07-11 stsp goto done;
5832 818c7501 2019-07-11 stsp
5833 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5834 818c7501 2019-07-11 stsp 0);
5835 e51d7b55 2020-01-04 stsp if (err)
5836 e51d7b55 2020-01-04 stsp goto done;
5837 e51d7b55 2020-01-04 stsp
5838 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5839 818c7501 2019-07-11 stsp if (err)
5840 818c7501 2019-07-11 stsp goto done;
5841 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5842 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5843 e51d7b55 2020-01-04 stsp goto done;
5844 e51d7b55 2020-01-04 stsp }
5845 818c7501 2019-07-11 stsp
5846 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5847 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5848 818c7501 2019-07-11 stsp if (err)
5849 818c7501 2019-07-11 stsp goto done;
5850 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5851 818c7501 2019-07-11 stsp if (err)
5852 818c7501 2019-07-11 stsp goto done;
5853 818c7501 2019-07-11 stsp
5854 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5855 818c7501 2019-07-11 stsp
5856 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5857 818c7501 2019-07-11 stsp if (err)
5858 818c7501 2019-07-11 stsp goto done;
5859 818c7501 2019-07-11 stsp
5860 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5861 818c7501 2019-07-11 stsp if (err)
5862 818c7501 2019-07-11 stsp goto done;
5863 818c7501 2019-07-11 stsp
5864 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5865 818c7501 2019-07-11 stsp worktree->base_commit_id);
5866 818c7501 2019-07-11 stsp if (err)
5867 818c7501 2019-07-11 stsp goto done;
5868 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5869 818c7501 2019-07-11 stsp if (err)
5870 818c7501 2019-07-11 stsp goto done;
5871 818c7501 2019-07-11 stsp
5872 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5873 818c7501 2019-07-11 stsp if (err)
5874 818c7501 2019-07-11 stsp goto done;
5875 818c7501 2019-07-11 stsp done:
5876 818c7501 2019-07-11 stsp free(fileindex_path);
5877 818c7501 2019-07-11 stsp free(tmp_branch_name);
5878 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5879 818c7501 2019-07-11 stsp free(branch_ref_name);
5880 818c7501 2019-07-11 stsp if (branch_ref)
5881 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5882 818c7501 2019-07-11 stsp if (wt_branch)
5883 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5884 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5885 818c7501 2019-07-11 stsp if (err) {
5886 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5887 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5888 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5889 818c7501 2019-07-11 stsp }
5890 818c7501 2019-07-11 stsp if (*tmp_branch) {
5891 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5892 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5893 818c7501 2019-07-11 stsp }
5894 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5895 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5896 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5897 3e3a69f1 2019-07-25 stsp }
5898 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5899 818c7501 2019-07-11 stsp }
5900 818c7501 2019-07-11 stsp return err;
5901 818c7501 2019-07-11 stsp }
5902 818c7501 2019-07-11 stsp
5903 818c7501 2019-07-11 stsp const struct got_error *
5904 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5905 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5906 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5907 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5908 818c7501 2019-07-11 stsp {
5909 818c7501 2019-07-11 stsp const struct got_error *err;
5910 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5911 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5912 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5913 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5914 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5915 818c7501 2019-07-11 stsp
5916 818c7501 2019-07-11 stsp *commit_id = NULL;
5917 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5918 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5919 3e3a69f1 2019-07-25 stsp *branch = NULL;
5920 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5921 3e3a69f1 2019-07-25 stsp
5922 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5923 3e3a69f1 2019-07-25 stsp if (err)
5924 3e3a69f1 2019-07-25 stsp return err;
5925 818c7501 2019-07-11 stsp
5926 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5927 3e3a69f1 2019-07-25 stsp if (err)
5928 f032f1f7 2019-08-04 stsp goto done;
5929 f032f1f7 2019-08-04 stsp
5930 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5931 f032f1f7 2019-08-04 stsp &have_staged_files);
5932 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5933 f032f1f7 2019-08-04 stsp goto done;
5934 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5935 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5936 3e3a69f1 2019-07-25 stsp goto done;
5937 f032f1f7 2019-08-04 stsp }
5938 3e3a69f1 2019-07-25 stsp
5939 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5940 818c7501 2019-07-11 stsp if (err)
5941 f032f1f7 2019-08-04 stsp goto done;
5942 818c7501 2019-07-11 stsp
5943 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5944 818c7501 2019-07-11 stsp if (err)
5945 818c7501 2019-07-11 stsp goto done;
5946 818c7501 2019-07-11 stsp
5947 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5948 818c7501 2019-07-11 stsp if (err)
5949 818c7501 2019-07-11 stsp goto done;
5950 818c7501 2019-07-11 stsp
5951 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5952 818c7501 2019-07-11 stsp if (err)
5953 818c7501 2019-07-11 stsp goto done;
5954 818c7501 2019-07-11 stsp
5955 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 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(branch, repo,
5960 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5961 818c7501 2019-07-11 stsp if (err)
5962 818c7501 2019-07-11 stsp goto done;
5963 818c7501 2019-07-11 stsp
5964 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5965 818c7501 2019-07-11 stsp if (err)
5966 818c7501 2019-07-11 stsp goto done;
5967 818c7501 2019-07-11 stsp
5968 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
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(new_base_branch, repo,
5973 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5974 818c7501 2019-07-11 stsp if (err)
5975 818c7501 2019-07-11 stsp goto done;
5976 818c7501 2019-07-11 stsp
5977 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5978 818c7501 2019-07-11 stsp if (err)
5979 818c7501 2019-07-11 stsp goto done;
5980 818c7501 2019-07-11 stsp done:
5981 818c7501 2019-07-11 stsp free(commit_ref_name);
5982 818c7501 2019-07-11 stsp free(branch_ref_name);
5983 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5984 818c7501 2019-07-11 stsp if (commit_ref)
5985 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5986 818c7501 2019-07-11 stsp if (branch_ref)
5987 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5988 818c7501 2019-07-11 stsp if (err) {
5989 818c7501 2019-07-11 stsp free(*commit_id);
5990 818c7501 2019-07-11 stsp *commit_id = NULL;
5991 818c7501 2019-07-11 stsp if (*tmp_branch) {
5992 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5993 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5994 818c7501 2019-07-11 stsp }
5995 818c7501 2019-07-11 stsp if (*new_base_branch) {
5996 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5997 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5998 818c7501 2019-07-11 stsp }
5999 818c7501 2019-07-11 stsp if (*branch) {
6000 818c7501 2019-07-11 stsp got_ref_close(*branch);
6001 818c7501 2019-07-11 stsp *branch = NULL;
6002 818c7501 2019-07-11 stsp }
6003 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6004 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6005 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6006 3e3a69f1 2019-07-25 stsp }
6007 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6008 818c7501 2019-07-11 stsp }
6009 818c7501 2019-07-11 stsp return err;
6010 c4296144 2019-05-09 stsp }
6011 818c7501 2019-07-11 stsp
6012 818c7501 2019-07-11 stsp const struct got_error *
6013 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6014 818c7501 2019-07-11 stsp {
6015 818c7501 2019-07-11 stsp const struct got_error *err;
6016 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6017 818c7501 2019-07-11 stsp
6018 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6019 818c7501 2019-07-11 stsp if (err)
6020 818c7501 2019-07-11 stsp return err;
6021 818c7501 2019-07-11 stsp
6022 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6023 818c7501 2019-07-11 stsp free(tmp_branch_name);
6024 818c7501 2019-07-11 stsp return NULL;
6025 818c7501 2019-07-11 stsp }
6026 818c7501 2019-07-11 stsp
6027 818c7501 2019-07-11 stsp static const struct got_error *
6028 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6029 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6030 818c7501 2019-07-11 stsp {
6031 0ebf8283 2019-07-24 stsp *logmsg = arg;
6032 818c7501 2019-07-11 stsp return NULL;
6033 818c7501 2019-07-11 stsp }
6034 818c7501 2019-07-11 stsp
6035 818c7501 2019-07-11 stsp static const struct got_error *
6036 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6037 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6038 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6039 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6040 818c7501 2019-07-11 stsp {
6041 818c7501 2019-07-11 stsp return NULL;
6042 01757395 2019-07-12 stsp }
6043 01757395 2019-07-12 stsp
6044 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6045 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6046 01757395 2019-07-12 stsp void *progress_arg;
6047 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6048 01757395 2019-07-12 stsp };
6049 01757395 2019-07-12 stsp
6050 01757395 2019-07-12 stsp static const struct got_error *
6051 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6052 01757395 2019-07-12 stsp {
6053 01757395 2019-07-12 stsp const struct got_error *err;
6054 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6055 01757395 2019-07-12 stsp char *p;
6056 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6057 01757395 2019-07-12 stsp
6058 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6059 01757395 2019-07-12 stsp if (err)
6060 01757395 2019-07-12 stsp return err;
6061 01757395 2019-07-12 stsp
6062 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6063 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6064 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6065 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6066 01757395 2019-07-12 stsp return NULL;
6067 01757395 2019-07-12 stsp
6068 01757395 2019-07-12 stsp p = strdup(path);
6069 01757395 2019-07-12 stsp if (p == NULL)
6070 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6071 01757395 2019-07-12 stsp
6072 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6073 01757395 2019-07-12 stsp if (err || new == NULL)
6074 01757395 2019-07-12 stsp free(p);
6075 01757395 2019-07-12 stsp return err;
6076 01757395 2019-07-12 stsp }
6077 01757395 2019-07-12 stsp
6078 01757395 2019-07-12 stsp void
6079 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6080 01757395 2019-07-12 stsp {
6081 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6082 01757395 2019-07-12 stsp
6083 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6084 01757395 2019-07-12 stsp free((char *)pe->path);
6085 01757395 2019-07-12 stsp
6086 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6087 818c7501 2019-07-11 stsp }
6088 818c7501 2019-07-11 stsp
6089 0ebf8283 2019-07-24 stsp static const struct got_error *
6090 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6091 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6092 818c7501 2019-07-11 stsp {
6093 818c7501 2019-07-11 stsp const struct got_error *err;
6094 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6095 818c7501 2019-07-11 stsp
6096 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6097 818c7501 2019-07-11 stsp if (err) {
6098 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6099 818c7501 2019-07-11 stsp goto done;
6100 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6101 818c7501 2019-07-11 stsp if (err)
6102 818c7501 2019-07-11 stsp goto done;
6103 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6104 818c7501 2019-07-11 stsp if (err)
6105 818c7501 2019-07-11 stsp goto done;
6106 de05890f 2020-03-05 stsp } else if (is_rebase) {
6107 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6108 818c7501 2019-07-11 stsp int cmp;
6109 818c7501 2019-07-11 stsp
6110 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6111 818c7501 2019-07-11 stsp if (err)
6112 818c7501 2019-07-11 stsp goto done;
6113 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6114 818c7501 2019-07-11 stsp free(stored_id);
6115 818c7501 2019-07-11 stsp if (cmp != 0) {
6116 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6117 818c7501 2019-07-11 stsp goto done;
6118 818c7501 2019-07-11 stsp }
6119 818c7501 2019-07-11 stsp }
6120 0ebf8283 2019-07-24 stsp done:
6121 0ebf8283 2019-07-24 stsp if (commit_ref)
6122 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6123 0ebf8283 2019-07-24 stsp return err;
6124 0ebf8283 2019-07-24 stsp }
6125 0ebf8283 2019-07-24 stsp
6126 0ebf8283 2019-07-24 stsp static const struct got_error *
6127 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6128 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6129 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6130 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6131 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6132 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6133 0ebf8283 2019-07-24 stsp {
6134 0ebf8283 2019-07-24 stsp const struct got_error *err;
6135 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6136 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6137 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6138 818c7501 2019-07-11 stsp
6139 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6140 0ebf8283 2019-07-24 stsp
6141 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6142 0ebf8283 2019-07-24 stsp if (err)
6143 0ebf8283 2019-07-24 stsp return err;
6144 0ebf8283 2019-07-24 stsp
6145 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6146 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6147 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6148 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6149 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6150 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6151 818c7501 2019-07-11 stsp if (commit_ref)
6152 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6153 818c7501 2019-07-11 stsp return err;
6154 818c7501 2019-07-11 stsp }
6155 818c7501 2019-07-11 stsp
6156 818c7501 2019-07-11 stsp const struct got_error *
6157 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6158 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6159 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6160 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6161 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6162 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6163 818c7501 2019-07-11 stsp {
6164 0ebf8283 2019-07-24 stsp const struct got_error *err;
6165 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6166 0ebf8283 2019-07-24 stsp
6167 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6168 0ebf8283 2019-07-24 stsp if (err)
6169 0ebf8283 2019-07-24 stsp return err;
6170 0ebf8283 2019-07-24 stsp
6171 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6172 0ebf8283 2019-07-24 stsp if (err)
6173 0ebf8283 2019-07-24 stsp goto done;
6174 0ebf8283 2019-07-24 stsp
6175 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6176 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6177 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6178 0ebf8283 2019-07-24 stsp done:
6179 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6180 0ebf8283 2019-07-24 stsp return err;
6181 0ebf8283 2019-07-24 stsp }
6182 0ebf8283 2019-07-24 stsp
6183 0ebf8283 2019-07-24 stsp const struct got_error *
6184 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6185 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6186 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6187 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6188 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6189 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6190 0ebf8283 2019-07-24 stsp {
6191 0ebf8283 2019-07-24 stsp const struct got_error *err;
6192 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6193 0ebf8283 2019-07-24 stsp
6194 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6195 0ebf8283 2019-07-24 stsp if (err)
6196 0ebf8283 2019-07-24 stsp return err;
6197 0ebf8283 2019-07-24 stsp
6198 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6199 0ebf8283 2019-07-24 stsp if (err)
6200 0ebf8283 2019-07-24 stsp goto done;
6201 0ebf8283 2019-07-24 stsp
6202 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6203 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6204 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6205 0ebf8283 2019-07-24 stsp done:
6206 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6207 0ebf8283 2019-07-24 stsp return err;
6208 0ebf8283 2019-07-24 stsp }
6209 0ebf8283 2019-07-24 stsp
6210 0ebf8283 2019-07-24 stsp static const struct got_error *
6211 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6212 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6213 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6214 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6215 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6216 0ebf8283 2019-07-24 stsp {
6217 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6218 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6219 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6220 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6221 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6222 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6223 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6224 818c7501 2019-07-11 stsp
6225 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6226 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6227 a0e95631 2019-07-12 stsp
6228 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6229 818c7501 2019-07-11 stsp
6230 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6231 a0e95631 2019-07-12 stsp if (err)
6232 3e3a69f1 2019-07-25 stsp return err;
6233 a0e95631 2019-07-12 stsp
6234 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6235 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6236 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6237 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6238 01757395 2019-07-12 stsp /*
6239 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6240 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6241 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6242 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6243 01757395 2019-07-12 stsp */
6244 01757395 2019-07-12 stsp if (merged_paths) {
6245 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6246 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6247 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6248 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6249 f2a9dc41 2019-12-13 tracey 0);
6250 01757395 2019-07-12 stsp if (err)
6251 01757395 2019-07-12 stsp goto done;
6252 01757395 2019-07-12 stsp }
6253 01757395 2019-07-12 stsp } else {
6254 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6255 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6256 01757395 2019-07-12 stsp if (err)
6257 01757395 2019-07-12 stsp goto done;
6258 01757395 2019-07-12 stsp }
6259 a0e95631 2019-07-12 stsp
6260 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6261 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6262 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6263 a0e95631 2019-07-12 stsp if (err)
6264 a0e95631 2019-07-12 stsp goto done;
6265 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6266 a0e95631 2019-07-12 stsp goto done;
6267 ff0d2220 2019-07-11 stsp }
6268 818c7501 2019-07-11 stsp
6269 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6270 edd02c5e 2019-07-11 stsp if (err)
6271 edd02c5e 2019-07-11 stsp goto done;
6272 edd02c5e 2019-07-11 stsp
6273 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6274 818c7501 2019-07-11 stsp if (err)
6275 818c7501 2019-07-11 stsp goto done;
6276 818c7501 2019-07-11 stsp
6277 5943eee2 2019-08-13 stsp if (new_logmsg) {
6278 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6279 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6280 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6281 5943eee2 2019-08-13 stsp goto done;
6282 5943eee2 2019-08-13 stsp }
6283 5943eee2 2019-08-13 stsp } else {
6284 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6285 5943eee2 2019-08-13 stsp if (err)
6286 5943eee2 2019-08-13 stsp goto done;
6287 5943eee2 2019-08-13 stsp }
6288 0ebf8283 2019-07-24 stsp
6289 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6290 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6291 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6292 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6293 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6294 a0e95631 2019-07-12 stsp if (err)
6295 a0e95631 2019-07-12 stsp goto done;
6296 a0e95631 2019-07-12 stsp
6297 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6298 a0e95631 2019-07-12 stsp if (err)
6299 a0e95631 2019-07-12 stsp goto done;
6300 a0e95631 2019-07-12 stsp
6301 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6302 a0e95631 2019-07-12 stsp if (err)
6303 a0e95631 2019-07-12 stsp goto done;
6304 a0e95631 2019-07-12 stsp
6305 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6306 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6307 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6308 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6309 a0e95631 2019-07-12 stsp err = sync_err;
6310 818c7501 2019-07-11 stsp done:
6311 a0e95631 2019-07-12 stsp free(fileindex_path);
6312 a0e95631 2019-07-12 stsp free(head_commit_id);
6313 a0e95631 2019-07-12 stsp if (head_ref)
6314 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6315 818c7501 2019-07-11 stsp if (err) {
6316 818c7501 2019-07-11 stsp free(*new_commit_id);
6317 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6318 818c7501 2019-07-11 stsp }
6319 818c7501 2019-07-11 stsp return err;
6320 818c7501 2019-07-11 stsp }
6321 818c7501 2019-07-11 stsp
6322 818c7501 2019-07-11 stsp const struct got_error *
6323 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6324 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6325 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6326 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6327 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6328 0ebf8283 2019-07-24 stsp {
6329 0ebf8283 2019-07-24 stsp const struct got_error *err;
6330 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6331 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6332 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6333 0ebf8283 2019-07-24 stsp
6334 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6335 0ebf8283 2019-07-24 stsp if (err)
6336 0ebf8283 2019-07-24 stsp return err;
6337 0ebf8283 2019-07-24 stsp
6338 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6339 0ebf8283 2019-07-24 stsp if (err)
6340 0ebf8283 2019-07-24 stsp goto done;
6341 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6342 0ebf8283 2019-07-24 stsp if (err)
6343 0ebf8283 2019-07-24 stsp goto done;
6344 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6345 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6346 0ebf8283 2019-07-24 stsp goto done;
6347 0ebf8283 2019-07-24 stsp }
6348 0ebf8283 2019-07-24 stsp
6349 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6350 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6351 0ebf8283 2019-07-24 stsp done:
6352 0ebf8283 2019-07-24 stsp if (commit_ref)
6353 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6354 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6355 0ebf8283 2019-07-24 stsp free(commit_id);
6356 0ebf8283 2019-07-24 stsp return err;
6357 0ebf8283 2019-07-24 stsp }
6358 0ebf8283 2019-07-24 stsp
6359 0ebf8283 2019-07-24 stsp const struct got_error *
6360 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6361 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6362 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6363 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6364 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6365 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6366 0ebf8283 2019-07-24 stsp {
6367 0ebf8283 2019-07-24 stsp const struct got_error *err;
6368 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6369 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6370 0ebf8283 2019-07-24 stsp
6371 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6372 0ebf8283 2019-07-24 stsp if (err)
6373 0ebf8283 2019-07-24 stsp return err;
6374 0ebf8283 2019-07-24 stsp
6375 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6376 0ebf8283 2019-07-24 stsp if (err)
6377 0ebf8283 2019-07-24 stsp goto done;
6378 0ebf8283 2019-07-24 stsp
6379 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6380 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6381 0ebf8283 2019-07-24 stsp done:
6382 0ebf8283 2019-07-24 stsp if (commit_ref)
6383 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6384 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6385 0ebf8283 2019-07-24 stsp return err;
6386 0ebf8283 2019-07-24 stsp }
6387 0ebf8283 2019-07-24 stsp
6388 0ebf8283 2019-07-24 stsp const struct got_error *
6389 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6390 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6391 818c7501 2019-07-11 stsp {
6392 3e3a69f1 2019-07-25 stsp if (fileindex)
6393 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6394 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6395 69844fba 2019-07-11 stsp }
6396 69844fba 2019-07-11 stsp
6397 69844fba 2019-07-11 stsp static const struct got_error *
6398 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6399 69844fba 2019-07-11 stsp {
6400 69844fba 2019-07-11 stsp const struct got_error *err;
6401 69844fba 2019-07-11 stsp struct got_reference *ref;
6402 69844fba 2019-07-11 stsp
6403 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6404 69844fba 2019-07-11 stsp if (err) {
6405 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6406 69844fba 2019-07-11 stsp return NULL;
6407 69844fba 2019-07-11 stsp return err;
6408 69844fba 2019-07-11 stsp }
6409 69844fba 2019-07-11 stsp
6410 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6411 69844fba 2019-07-11 stsp got_ref_close(ref);
6412 69844fba 2019-07-11 stsp return err;
6413 818c7501 2019-07-11 stsp }
6414 818c7501 2019-07-11 stsp
6415 69844fba 2019-07-11 stsp static const struct got_error *
6416 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6417 69844fba 2019-07-11 stsp {
6418 69844fba 2019-07-11 stsp const struct got_error *err;
6419 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6420 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6421 69844fba 2019-07-11 stsp
6422 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6423 69844fba 2019-07-11 stsp if (err)
6424 69844fba 2019-07-11 stsp goto done;
6425 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6426 69844fba 2019-07-11 stsp if (err)
6427 69844fba 2019-07-11 stsp goto done;
6428 69844fba 2019-07-11 stsp
6429 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6430 69844fba 2019-07-11 stsp if (err)
6431 69844fba 2019-07-11 stsp goto done;
6432 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6433 69844fba 2019-07-11 stsp if (err)
6434 69844fba 2019-07-11 stsp goto done;
6435 69844fba 2019-07-11 stsp
6436 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6437 69844fba 2019-07-11 stsp if (err)
6438 69844fba 2019-07-11 stsp goto done;
6439 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6440 69844fba 2019-07-11 stsp if (err)
6441 69844fba 2019-07-11 stsp goto done;
6442 69844fba 2019-07-11 stsp
6443 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6444 69844fba 2019-07-11 stsp if (err)
6445 69844fba 2019-07-11 stsp goto done;
6446 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6447 69844fba 2019-07-11 stsp if (err)
6448 69844fba 2019-07-11 stsp goto done;
6449 69844fba 2019-07-11 stsp
6450 69844fba 2019-07-11 stsp done:
6451 69844fba 2019-07-11 stsp free(tmp_branch_name);
6452 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6453 69844fba 2019-07-11 stsp free(branch_ref_name);
6454 69844fba 2019-07-11 stsp free(commit_ref_name);
6455 e600f124 2021-03-21 stsp return err;
6456 e600f124 2021-03-21 stsp }
6457 e600f124 2021-03-21 stsp
6458 e600f124 2021-03-21 stsp const struct got_error *
6459 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6460 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6461 e600f124 2021-03-21 stsp {
6462 e600f124 2021-03-21 stsp const struct got_error *err;
6463 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6464 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6465 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6466 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6467 e600f124 2021-03-21 stsp char *refname = NULL;
6468 e600f124 2021-03-21 stsp
6469 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6470 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6471 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6472 e600f124 2021-03-21 stsp branch_name += 11;
6473 e600f124 2021-03-21 stsp
6474 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6475 e600f124 2021-03-21 stsp if (err)
6476 e600f124 2021-03-21 stsp return err;
6477 e600f124 2021-03-21 stsp
6478 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6479 e600f124 2021-03-21 stsp new_id_str) == -1) {
6480 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6481 e600f124 2021-03-21 stsp goto done;
6482 e600f124 2021-03-21 stsp }
6483 e600f124 2021-03-21 stsp
6484 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6485 e600f124 2021-03-21 stsp if (err)
6486 e600f124 2021-03-21 stsp goto done;
6487 e600f124 2021-03-21 stsp
6488 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6489 e600f124 2021-03-21 stsp if (err)
6490 e600f124 2021-03-21 stsp goto done;
6491 e600f124 2021-03-21 stsp
6492 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6493 e600f124 2021-03-21 stsp done:
6494 e600f124 2021-03-21 stsp free(new_id_str);
6495 e600f124 2021-03-21 stsp free(refname);
6496 e600f124 2021-03-21 stsp free(old_commit_id);
6497 e600f124 2021-03-21 stsp if (ref)
6498 e600f124 2021-03-21 stsp got_ref_close(ref);
6499 69844fba 2019-07-11 stsp return err;
6500 69844fba 2019-07-11 stsp }
6501 69844fba 2019-07-11 stsp
6502 818c7501 2019-07-11 stsp const struct got_error *
6503 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6504 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6505 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6506 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6507 818c7501 2019-07-11 stsp {
6508 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6509 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6510 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6511 818c7501 2019-07-11 stsp
6512 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6513 818c7501 2019-07-11 stsp if (err)
6514 818c7501 2019-07-11 stsp return err;
6515 e600f124 2021-03-21 stsp
6516 e600f124 2021-03-21 stsp if (create_backup) {
6517 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6518 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6519 e600f124 2021-03-21 stsp if (err)
6520 e600f124 2021-03-21 stsp goto done;
6521 e600f124 2021-03-21 stsp }
6522 818c7501 2019-07-11 stsp
6523 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6524 818c7501 2019-07-11 stsp if (err)
6525 818c7501 2019-07-11 stsp goto done;
6526 818c7501 2019-07-11 stsp
6527 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6528 818c7501 2019-07-11 stsp if (err)
6529 818c7501 2019-07-11 stsp goto done;
6530 818c7501 2019-07-11 stsp
6531 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6532 818c7501 2019-07-11 stsp if (err)
6533 818c7501 2019-07-11 stsp goto done;
6534 818c7501 2019-07-11 stsp
6535 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6536 a615e0e7 2020-12-16 stsp if (err)
6537 a615e0e7 2020-12-16 stsp goto done;
6538 a615e0e7 2020-12-16 stsp
6539 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6540 a615e0e7 2020-12-16 stsp if (err)
6541 a615e0e7 2020-12-16 stsp goto done;
6542 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6543 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6544 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6545 a615e0e7 2020-12-16 stsp err = sync_err;
6546 818c7501 2019-07-11 stsp done:
6547 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6548 a615e0e7 2020-12-16 stsp free(fileindex_path);
6549 818c7501 2019-07-11 stsp free(new_head_commit_id);
6550 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6551 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6552 818c7501 2019-07-11 stsp err = unlockerr;
6553 818c7501 2019-07-11 stsp return err;
6554 818c7501 2019-07-11 stsp }
6555 818c7501 2019-07-11 stsp
6556 818c7501 2019-07-11 stsp const struct got_error *
6557 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6558 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6559 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6560 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6561 818c7501 2019-07-11 stsp {
6562 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6563 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6564 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6565 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6566 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6567 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6568 818c7501 2019-07-11 stsp
6569 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6570 818c7501 2019-07-11 stsp if (err)
6571 818c7501 2019-07-11 stsp return err;
6572 818c7501 2019-07-11 stsp
6573 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6574 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6575 818c7501 2019-07-11 stsp if (err)
6576 818c7501 2019-07-11 stsp goto done;
6577 818c7501 2019-07-11 stsp
6578 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6579 818c7501 2019-07-11 stsp if (err)
6580 818c7501 2019-07-11 stsp goto done;
6581 818c7501 2019-07-11 stsp
6582 818c7501 2019-07-11 stsp /*
6583 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6584 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6585 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6586 818c7501 2019-07-11 stsp */
6587 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6588 818c7501 2019-07-11 stsp if (err)
6589 818c7501 2019-07-11 stsp goto done;
6590 818c7501 2019-07-11 stsp
6591 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6592 818c7501 2019-07-11 stsp if (err)
6593 818c7501 2019-07-11 stsp goto done;
6594 818c7501 2019-07-11 stsp
6595 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6596 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6597 ca355955 2019-07-12 stsp if (err)
6598 ca355955 2019-07-12 stsp goto done;
6599 ca355955 2019-07-12 stsp
6600 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6601 ca355955 2019-07-12 stsp if (err)
6602 ca355955 2019-07-12 stsp goto done;
6603 ca355955 2019-07-12 stsp
6604 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6605 818c7501 2019-07-11 stsp if (err)
6606 818c7501 2019-07-11 stsp goto done;
6607 818c7501 2019-07-11 stsp
6608 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6609 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6610 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6611 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6612 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6613 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6614 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6615 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6616 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6617 55bd499d 2019-07-12 stsp if (err)
6618 1f1abb7e 2019-08-08 stsp goto sync;
6619 55bd499d 2019-07-12 stsp
6620 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6621 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6622 ca355955 2019-07-12 stsp sync:
6623 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6624 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6625 ca355955 2019-07-12 stsp err = sync_err;
6626 818c7501 2019-07-11 stsp done:
6627 818c7501 2019-07-11 stsp got_ref_close(resolved);
6628 a3a2faf2 2019-07-12 stsp free(tree_id);
6629 818c7501 2019-07-11 stsp free(commit_id);
6630 0ebf8283 2019-07-24 stsp if (fileindex)
6631 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6632 0ebf8283 2019-07-24 stsp free(fileindex_path);
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6635 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6636 0ebf8283 2019-07-24 stsp err = unlockerr;
6637 0ebf8283 2019-07-24 stsp return err;
6638 0ebf8283 2019-07-24 stsp }
6639 0ebf8283 2019-07-24 stsp
6640 0ebf8283 2019-07-24 stsp const struct got_error *
6641 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6642 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6643 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6644 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6645 0ebf8283 2019-07-24 stsp {
6646 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6647 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6648 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6649 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6650 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6651 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6652 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6653 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6654 0ebf8283 2019-07-24 stsp
6655 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6656 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6657 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6658 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6659 0ebf8283 2019-07-24 stsp
6660 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6661 0ebf8283 2019-07-24 stsp if (err)
6662 0ebf8283 2019-07-24 stsp return err;
6663 0ebf8283 2019-07-24 stsp
6664 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6665 0ebf8283 2019-07-24 stsp if (err)
6666 0ebf8283 2019-07-24 stsp goto done;
6667 0ebf8283 2019-07-24 stsp
6668 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6669 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6670 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6671 0ebf8283 2019-07-24 stsp &ok_arg);
6672 0ebf8283 2019-07-24 stsp if (err)
6673 0ebf8283 2019-07-24 stsp goto done;
6674 0ebf8283 2019-07-24 stsp
6675 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6676 0ebf8283 2019-07-24 stsp if (err)
6677 0ebf8283 2019-07-24 stsp goto done;
6678 0ebf8283 2019-07-24 stsp
6679 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6680 0ebf8283 2019-07-24 stsp if (err)
6681 0ebf8283 2019-07-24 stsp goto done;
6682 0ebf8283 2019-07-24 stsp
6683 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6684 0ebf8283 2019-07-24 stsp worktree);
6685 0ebf8283 2019-07-24 stsp if (err)
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp
6688 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6689 0ebf8283 2019-07-24 stsp 0);
6690 0ebf8283 2019-07-24 stsp if (err)
6691 0ebf8283 2019-07-24 stsp goto done;
6692 0ebf8283 2019-07-24 stsp
6693 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6694 0ebf8283 2019-07-24 stsp if (err)
6695 0ebf8283 2019-07-24 stsp goto done;
6696 0ebf8283 2019-07-24 stsp
6697 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6698 0ebf8283 2019-07-24 stsp if (err)
6699 0ebf8283 2019-07-24 stsp goto done;
6700 0ebf8283 2019-07-24 stsp
6701 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6702 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6703 0ebf8283 2019-07-24 stsp if (err)
6704 0ebf8283 2019-07-24 stsp goto done;
6705 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6706 0ebf8283 2019-07-24 stsp if (err)
6707 0ebf8283 2019-07-24 stsp goto done;
6708 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6709 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6710 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6711 0ebf8283 2019-07-24 stsp goto done;
6712 0ebf8283 2019-07-24 stsp }
6713 0ebf8283 2019-07-24 stsp
6714 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6715 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6716 0ebf8283 2019-07-24 stsp if (err)
6717 0ebf8283 2019-07-24 stsp goto done;
6718 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6719 0ebf8283 2019-07-24 stsp if (err)
6720 0ebf8283 2019-07-24 stsp goto done;
6721 0ebf8283 2019-07-24 stsp
6722 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6723 0ebf8283 2019-07-24 stsp if (err)
6724 0ebf8283 2019-07-24 stsp goto done;
6725 0ebf8283 2019-07-24 stsp done:
6726 0ebf8283 2019-07-24 stsp free(fileindex_path);
6727 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6728 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6729 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6730 0ebf8283 2019-07-24 stsp if (wt_branch)
6731 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6732 0ebf8283 2019-07-24 stsp if (err) {
6733 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6734 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6735 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6736 0ebf8283 2019-07-24 stsp }
6737 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6738 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6739 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6740 0ebf8283 2019-07-24 stsp }
6741 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6742 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6743 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6744 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6745 3e3a69f1 2019-07-25 stsp }
6746 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6747 0ebf8283 2019-07-24 stsp }
6748 0ebf8283 2019-07-24 stsp return err;
6749 0ebf8283 2019-07-24 stsp }
6750 0ebf8283 2019-07-24 stsp
6751 0ebf8283 2019-07-24 stsp const struct got_error *
6752 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6753 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6754 0ebf8283 2019-07-24 stsp {
6755 3e3a69f1 2019-07-25 stsp if (fileindex)
6756 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6757 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6758 0ebf8283 2019-07-24 stsp }
6759 0ebf8283 2019-07-24 stsp
6760 0ebf8283 2019-07-24 stsp const struct got_error *
6761 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6762 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6763 0ebf8283 2019-07-24 stsp {
6764 0ebf8283 2019-07-24 stsp const struct got_error *err;
6765 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6766 0ebf8283 2019-07-24 stsp
6767 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6768 0ebf8283 2019-07-24 stsp if (err)
6769 0ebf8283 2019-07-24 stsp return err;
6770 0ebf8283 2019-07-24 stsp
6771 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6772 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6773 0ebf8283 2019-07-24 stsp return NULL;
6774 0ebf8283 2019-07-24 stsp }
6775 0ebf8283 2019-07-24 stsp
6776 0ebf8283 2019-07-24 stsp const struct got_error *
6777 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6778 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6779 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6780 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6781 0ebf8283 2019-07-24 stsp {
6782 0ebf8283 2019-07-24 stsp const struct got_error *err;
6783 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6784 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6785 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6786 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6787 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6788 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6789 0ebf8283 2019-07-24 stsp
6790 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6791 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6792 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6793 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6794 0ebf8283 2019-07-24 stsp
6795 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6796 3e3a69f1 2019-07-25 stsp if (err)
6797 3e3a69f1 2019-07-25 stsp return err;
6798 3e3a69f1 2019-07-25 stsp
6799 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6800 3e3a69f1 2019-07-25 stsp if (err)
6801 f032f1f7 2019-08-04 stsp goto done;
6802 f032f1f7 2019-08-04 stsp
6803 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6804 f032f1f7 2019-08-04 stsp &have_staged_files);
6805 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6806 f032f1f7 2019-08-04 stsp goto done;
6807 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6808 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6809 3e3a69f1 2019-07-25 stsp goto done;
6810 f032f1f7 2019-08-04 stsp }
6811 3e3a69f1 2019-07-25 stsp
6812 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6813 0ebf8283 2019-07-24 stsp if (err)
6814 f032f1f7 2019-08-04 stsp goto done;
6815 0ebf8283 2019-07-24 stsp
6816 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6817 0ebf8283 2019-07-24 stsp if (err)
6818 0ebf8283 2019-07-24 stsp goto done;
6819 0ebf8283 2019-07-24 stsp
6820 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6821 0ebf8283 2019-07-24 stsp if (err)
6822 0ebf8283 2019-07-24 stsp goto done;
6823 0ebf8283 2019-07-24 stsp
6824 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6825 0ebf8283 2019-07-24 stsp worktree);
6826 0ebf8283 2019-07-24 stsp if (err)
6827 0ebf8283 2019-07-24 stsp goto done;
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6830 0ebf8283 2019-07-24 stsp if (err)
6831 0ebf8283 2019-07-24 stsp goto done;
6832 0ebf8283 2019-07-24 stsp
6833 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6834 0ebf8283 2019-07-24 stsp if (err)
6835 0ebf8283 2019-07-24 stsp goto done;
6836 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6837 0ebf8283 2019-07-24 stsp if (err)
6838 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp
6840 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6841 0ebf8283 2019-07-24 stsp if (err)
6842 0ebf8283 2019-07-24 stsp goto done;
6843 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6844 0ebf8283 2019-07-24 stsp if (err)
6845 0ebf8283 2019-07-24 stsp goto done;
6846 0ebf8283 2019-07-24 stsp
6847 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6848 0ebf8283 2019-07-24 stsp if (err)
6849 0ebf8283 2019-07-24 stsp goto done;
6850 0ebf8283 2019-07-24 stsp done:
6851 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6852 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6853 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6854 0ebf8283 2019-07-24 stsp if (commit_ref)
6855 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6856 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6857 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6858 0ebf8283 2019-07-24 stsp if (err) {
6859 0ebf8283 2019-07-24 stsp free(*commit_id);
6860 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6861 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6862 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6863 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6864 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6865 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6866 0ebf8283 2019-07-24 stsp }
6867 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6868 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6869 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6870 3e3a69f1 2019-07-25 stsp }
6871 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6872 0ebf8283 2019-07-24 stsp }
6873 0ebf8283 2019-07-24 stsp return err;
6874 0ebf8283 2019-07-24 stsp }
6875 0ebf8283 2019-07-24 stsp
6876 0ebf8283 2019-07-24 stsp static const struct got_error *
6877 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6878 0ebf8283 2019-07-24 stsp {
6879 0ebf8283 2019-07-24 stsp const struct got_error *err;
6880 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6881 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6882 0ebf8283 2019-07-24 stsp
6883 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6884 0ebf8283 2019-07-24 stsp if (err)
6885 0ebf8283 2019-07-24 stsp goto done;
6886 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6887 0ebf8283 2019-07-24 stsp if (err)
6888 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6891 0ebf8283 2019-07-24 stsp worktree);
6892 0ebf8283 2019-07-24 stsp if (err)
6893 0ebf8283 2019-07-24 stsp goto done;
6894 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6895 0ebf8283 2019-07-24 stsp if (err)
6896 0ebf8283 2019-07-24 stsp goto done;
6897 0ebf8283 2019-07-24 stsp
6898 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6899 0ebf8283 2019-07-24 stsp if (err)
6900 0ebf8283 2019-07-24 stsp goto done;
6901 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6902 0ebf8283 2019-07-24 stsp if (err)
6903 0ebf8283 2019-07-24 stsp goto done;
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6906 0ebf8283 2019-07-24 stsp if (err)
6907 0ebf8283 2019-07-24 stsp goto done;
6908 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6909 0ebf8283 2019-07-24 stsp if (err)
6910 0ebf8283 2019-07-24 stsp goto done;
6911 0ebf8283 2019-07-24 stsp done:
6912 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6913 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6914 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6915 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6916 0ebf8283 2019-07-24 stsp return err;
6917 0ebf8283 2019-07-24 stsp }
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp const struct got_error *
6920 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6921 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6922 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6923 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6924 0ebf8283 2019-07-24 stsp {
6925 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6926 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6927 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6928 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6929 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6930 0ebf8283 2019-07-24 stsp
6931 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6932 0ebf8283 2019-07-24 stsp if (err)
6933 0ebf8283 2019-07-24 stsp return err;
6934 0ebf8283 2019-07-24 stsp
6935 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6936 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6937 0ebf8283 2019-07-24 stsp if (err)
6938 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp
6940 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6941 0ebf8283 2019-07-24 stsp if (err)
6942 0ebf8283 2019-07-24 stsp goto done;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6945 0ebf8283 2019-07-24 stsp if (err)
6946 0ebf8283 2019-07-24 stsp goto done;
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6949 0ebf8283 2019-07-24 stsp worktree->path_prefix);
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 = delete_histedit_refs(worktree, repo);
6954 0ebf8283 2019-07-24 stsp if (err)
6955 0ebf8283 2019-07-24 stsp goto done;
6956 0ebf8283 2019-07-24 stsp
6957 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6958 0ebf8283 2019-07-24 stsp if (err)
6959 0ebf8283 2019-07-24 stsp goto done;
6960 0ebf8283 2019-07-24 stsp
6961 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6962 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6963 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6964 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6965 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6966 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6967 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6968 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6969 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6970 0ebf8283 2019-07-24 stsp if (err)
6971 1f1abb7e 2019-08-08 stsp goto sync;
6972 0ebf8283 2019-07-24 stsp
6973 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6974 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6975 0ebf8283 2019-07-24 stsp sync:
6976 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6977 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6978 0ebf8283 2019-07-24 stsp err = sync_err;
6979 0ebf8283 2019-07-24 stsp done:
6980 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6981 0ebf8283 2019-07-24 stsp free(tree_id);
6982 818c7501 2019-07-11 stsp free(fileindex_path);
6983 818c7501 2019-07-11 stsp
6984 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6985 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6986 818c7501 2019-07-11 stsp err = unlockerr;
6987 818c7501 2019-07-11 stsp return err;
6988 818c7501 2019-07-11 stsp }
6989 0ebf8283 2019-07-24 stsp
6990 0ebf8283 2019-07-24 stsp const struct got_error *
6991 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6992 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6993 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6994 0ebf8283 2019-07-24 stsp {
6995 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6996 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6997 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6998 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6999 0ebf8283 2019-07-24 stsp
7000 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7001 0ebf8283 2019-07-24 stsp if (err)
7002 0ebf8283 2019-07-24 stsp return err;
7003 0ebf8283 2019-07-24 stsp
7004 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7005 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7006 e600f124 2021-03-21 stsp if (err)
7007 e600f124 2021-03-21 stsp goto done;
7008 e600f124 2021-03-21 stsp
7009 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7010 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7011 0ebf8283 2019-07-24 stsp if (err)
7012 0ebf8283 2019-07-24 stsp goto done;
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7015 0ebf8283 2019-07-24 stsp if (err)
7016 0ebf8283 2019-07-24 stsp goto done;
7017 0ebf8283 2019-07-24 stsp
7018 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7019 0ebf8283 2019-07-24 stsp if (err)
7020 0ebf8283 2019-07-24 stsp goto done;
7021 0ebf8283 2019-07-24 stsp
7022 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7023 0ebf8283 2019-07-24 stsp if (err)
7024 0ebf8283 2019-07-24 stsp goto done;
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7027 a615e0e7 2020-12-16 stsp if (err)
7028 a615e0e7 2020-12-16 stsp goto done;
7029 a615e0e7 2020-12-16 stsp
7030 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7031 a615e0e7 2020-12-16 stsp if (err)
7032 a615e0e7 2020-12-16 stsp goto done;
7033 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7034 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7035 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7036 a615e0e7 2020-12-16 stsp err = sync_err;
7037 0ebf8283 2019-07-24 stsp done:
7038 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7039 a615e0e7 2020-12-16 stsp free(fileindex_path);
7040 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7041 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7042 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7043 0ebf8283 2019-07-24 stsp err = unlockerr;
7044 0ebf8283 2019-07-24 stsp return err;
7045 0ebf8283 2019-07-24 stsp }
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp const struct got_error *
7048 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7049 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7050 0ebf8283 2019-07-24 stsp {
7051 0ebf8283 2019-07-24 stsp const struct got_error *err;
7052 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7053 0ebf8283 2019-07-24 stsp
7054 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7055 0ebf8283 2019-07-24 stsp if (err)
7056 0ebf8283 2019-07-24 stsp return err;
7057 0ebf8283 2019-07-24 stsp
7058 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7059 0ebf8283 2019-07-24 stsp if (err)
7060 0ebf8283 2019-07-24 stsp goto done;
7061 0ebf8283 2019-07-24 stsp
7062 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7063 0ebf8283 2019-07-24 stsp done:
7064 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7065 2822a352 2019-10-15 stsp return err;
7066 2822a352 2019-10-15 stsp }
7067 2822a352 2019-10-15 stsp
7068 2822a352 2019-10-15 stsp const struct got_error *
7069 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7070 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7071 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7072 2822a352 2019-10-15 stsp struct got_repository *repo)
7073 2822a352 2019-10-15 stsp {
7074 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7075 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7076 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7077 2822a352 2019-10-15 stsp
7078 2822a352 2019-10-15 stsp *fileindex = NULL;
7079 2822a352 2019-10-15 stsp *branch_ref = NULL;
7080 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7081 2822a352 2019-10-15 stsp
7082 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7083 2822a352 2019-10-15 stsp if (err)
7084 2822a352 2019-10-15 stsp return err;
7085 2822a352 2019-10-15 stsp
7086 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7087 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7088 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7089 2822a352 2019-10-15 stsp "update -b or different branch name required");
7090 2822a352 2019-10-15 stsp goto done;
7091 2822a352 2019-10-15 stsp }
7092 2822a352 2019-10-15 stsp
7093 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
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 /* Preconditions are the same as for rebase. */
7098 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7099 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7100 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7101 2822a352 2019-10-15 stsp &ok_arg);
7102 2822a352 2019-10-15 stsp if (err)
7103 2822a352 2019-10-15 stsp goto done;
7104 2822a352 2019-10-15 stsp
7105 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7106 2822a352 2019-10-15 stsp if (err)
7107 2822a352 2019-10-15 stsp goto done;
7108 2822a352 2019-10-15 stsp
7109 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7110 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7111 2822a352 2019-10-15 stsp done:
7112 2822a352 2019-10-15 stsp if (err) {
7113 2822a352 2019-10-15 stsp if (*branch_ref) {
7114 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7115 2822a352 2019-10-15 stsp *branch_ref = NULL;
7116 2822a352 2019-10-15 stsp }
7117 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7118 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7119 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7120 2822a352 2019-10-15 stsp }
7121 2822a352 2019-10-15 stsp if (*fileindex) {
7122 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7123 2822a352 2019-10-15 stsp *fileindex = NULL;
7124 2822a352 2019-10-15 stsp }
7125 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7126 2822a352 2019-10-15 stsp }
7127 0cb83759 2019-08-03 stsp return err;
7128 0cb83759 2019-08-03 stsp }
7129 0cb83759 2019-08-03 stsp
7130 2822a352 2019-10-15 stsp const struct got_error *
7131 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7132 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7133 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7134 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7136 2822a352 2019-10-15 stsp {
7137 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7138 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7139 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7140 2822a352 2019-10-15 stsp
7141 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7142 2822a352 2019-10-15 stsp if (err)
7143 2822a352 2019-10-15 stsp goto done;
7144 2822a352 2019-10-15 stsp
7145 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7146 2822a352 2019-10-15 stsp if (err)
7147 2822a352 2019-10-15 stsp goto done;
7148 2822a352 2019-10-15 stsp
7149 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7150 2822a352 2019-10-15 stsp worktree->path_prefix);
7151 2822a352 2019-10-15 stsp if (err)
7152 2822a352 2019-10-15 stsp goto done;
7153 2822a352 2019-10-15 stsp
7154 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7155 2822a352 2019-10-15 stsp if (err)
7156 2822a352 2019-10-15 stsp goto done;
7157 2822a352 2019-10-15 stsp
7158 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7159 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7160 2822a352 2019-10-15 stsp if (err)
7161 2822a352 2019-10-15 stsp goto sync;
7162 2822a352 2019-10-15 stsp
7163 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7164 2822a352 2019-10-15 stsp if (err)
7165 2822a352 2019-10-15 stsp goto sync;
7166 2822a352 2019-10-15 stsp
7167 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7168 6e210706 2021-01-22 stsp if (err)
7169 6e210706 2021-01-22 stsp goto sync;
7170 6e210706 2021-01-22 stsp
7171 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7172 2822a352 2019-10-15 stsp sync:
7173 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7174 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7175 2822a352 2019-10-15 stsp err = sync_err;
7176 2822a352 2019-10-15 stsp
7177 2822a352 2019-10-15 stsp done:
7178 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7179 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7180 2822a352 2019-10-15 stsp err = unlockerr;
7181 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7182 2822a352 2019-10-15 stsp
7183 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7184 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7185 2822a352 2019-10-15 stsp err = unlockerr;
7186 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7187 2822a352 2019-10-15 stsp
7188 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7189 2822a352 2019-10-15 stsp free(fileindex_path);
7190 2822a352 2019-10-15 stsp free(tree_id);
7191 2822a352 2019-10-15 stsp
7192 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7193 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7194 2822a352 2019-10-15 stsp err = unlockerr;
7195 2822a352 2019-10-15 stsp return err;
7196 2822a352 2019-10-15 stsp }
7197 2822a352 2019-10-15 stsp
7198 2822a352 2019-10-15 stsp const struct got_error *
7199 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7200 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7201 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7202 2822a352 2019-10-15 stsp {
7203 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7204 8b692cd0 2019-10-21 stsp
7205 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7206 8b692cd0 2019-10-21 stsp
7207 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7208 8b692cd0 2019-10-21 stsp
7209 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7210 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7211 8b692cd0 2019-10-21 stsp err = unlockerr;
7212 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7213 8b692cd0 2019-10-21 stsp
7214 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7215 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7216 8b692cd0 2019-10-21 stsp err = unlockerr;
7217 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7218 8b692cd0 2019-10-21 stsp
7219 8b692cd0 2019-10-21 stsp return err;
7220 2822a352 2019-10-15 stsp }
7221 2822a352 2019-10-15 stsp
7222 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7223 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7224 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7225 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7226 2db2652d 2019-08-07 stsp struct got_repository *repo;
7227 2db2652d 2019-08-07 stsp int have_changes;
7228 2db2652d 2019-08-07 stsp };
7229 2db2652d 2019-08-07 stsp
7230 2db2652d 2019-08-07 stsp const struct got_error *
7231 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7232 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7233 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7234 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7235 735ef5ac 2019-08-03 stsp {
7236 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7237 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7238 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7239 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7240 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7241 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7242 8b13ce36 2019-08-08 stsp
7243 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7244 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7245 8b13ce36 2019-08-08 stsp return NULL;
7246 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7247 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7248 735ef5ac 2019-08-03 stsp
7249 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7250 735ef5ac 2019-08-03 stsp if (ie == NULL)
7251 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7252 735ef5ac 2019-08-03 stsp
7253 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7254 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7255 735ef5ac 2019-08-03 stsp relpath) == -1)
7256 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7257 735ef5ac 2019-08-03 stsp
7258 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7259 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7260 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7261 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7262 735ef5ac 2019-08-03 stsp }
7263 735ef5ac 2019-08-03 stsp
7264 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7265 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7266 3aa5969e 2019-08-06 stsp goto done;
7267 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7268 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7269 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7270 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7271 735ef5ac 2019-08-03 stsp goto done;
7272 3aa5969e 2019-08-06 stsp }
7273 735ef5ac 2019-08-03 stsp
7274 2db2652d 2019-08-07 stsp a->have_changes = 1;
7275 2db2652d 2019-08-07 stsp
7276 735ef5ac 2019-08-03 stsp p = in_repo_path;
7277 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7278 735ef5ac 2019-08-03 stsp p++;
7279 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7280 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7281 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7282 735ef5ac 2019-08-03 stsp done:
7283 735ef5ac 2019-08-03 stsp free(in_repo_path);
7284 dc424a06 2019-08-07 stsp return err;
7285 dc424a06 2019-08-07 stsp }
7286 dc424a06 2019-08-07 stsp
7287 2db2652d 2019-08-07 stsp struct stage_path_arg {
7288 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7289 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7290 2db2652d 2019-08-07 stsp struct got_repository *repo;
7291 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7292 2db2652d 2019-08-07 stsp void *status_arg;
7293 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7294 2db2652d 2019-08-07 stsp void *patch_arg;
7295 7b5dc508 2019-10-28 stsp int staged_something;
7296 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7297 2db2652d 2019-08-07 stsp };
7298 2db2652d 2019-08-07 stsp
7299 2db2652d 2019-08-07 stsp static const struct got_error *
7300 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7301 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7302 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7303 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7304 0cb83759 2019-08-03 stsp {
7305 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7306 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7307 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7308 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7309 0cb83759 2019-08-03 stsp uint32_t stage;
7310 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7311 0aeb8099 2020-07-23 stsp struct stat sb;
7312 8b13ce36 2019-08-08 stsp
7313 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7314 8b13ce36 2019-08-08 stsp return NULL;
7315 0cb83759 2019-08-03 stsp
7316 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7317 d3e7c587 2019-08-03 stsp if (ie == NULL)
7318 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7319 0cb83759 2019-08-03 stsp
7320 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7321 2db2652d 2019-08-07 stsp relpath)== -1)
7322 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7323 0cb83759 2019-08-03 stsp
7324 0cb83759 2019-08-03 stsp switch (status) {
7325 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7326 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7327 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7328 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7329 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7330 0aeb8099 2020-07-23 stsp break;
7331 0aeb8099 2020-07-23 stsp }
7332 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7333 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7334 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7335 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7336 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7337 dc424a06 2019-08-07 stsp if (err)
7338 dc424a06 2019-08-07 stsp break;
7339 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7340 dc424a06 2019-08-07 stsp break;
7341 dc424a06 2019-08-07 stsp } else {
7342 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7343 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7344 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7345 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7346 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7347 dc424a06 2019-08-07 stsp break;
7348 dc424a06 2019-08-07 stsp }
7349 dc424a06 2019-08-07 stsp }
7350 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7351 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7352 0cb83759 2019-08-03 stsp if (err)
7353 dc424a06 2019-08-07 stsp break;
7354 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7355 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7356 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7357 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7358 0cb83759 2019-08-03 stsp else
7359 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7360 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7361 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7362 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7363 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7364 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7365 35213c7c 2020-07-23 stsp ssize_t target_len;
7366 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7367 35213c7c 2020-07-23 stsp sizeof(target_path));
7368 35213c7c 2020-07-23 stsp if (target_len == -1) {
7369 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7370 35213c7c 2020-07-23 stsp ondisk_path);
7371 35213c7c 2020-07-23 stsp break;
7372 35213c7c 2020-07-23 stsp }
7373 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7374 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7375 35213c7c 2020-07-23 stsp a->worktree->root_path);
7376 35213c7c 2020-07-23 stsp if (err)
7377 35213c7c 2020-07-23 stsp break;
7378 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7379 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7380 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7381 35213c7c 2020-07-23 stsp break;
7382 35213c7c 2020-07-23 stsp }
7383 35213c7c 2020-07-23 stsp }
7384 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7385 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7386 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7387 35213c7c 2020-07-23 stsp else
7388 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7389 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7390 0aeb8099 2020-07-23 stsp } else {
7391 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7392 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7393 0aeb8099 2020-07-23 stsp }
7394 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7395 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7396 dc424a06 2019-08-07 stsp break;
7397 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7398 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7399 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7400 0cb83759 2019-08-03 stsp break;
7401 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7402 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7403 d3e7c587 2019-08-03 stsp break;
7404 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7405 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7406 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7407 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7408 dc424a06 2019-08-07 stsp if (err)
7409 dc424a06 2019-08-07 stsp break;
7410 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7411 88f33a19 2019-08-08 stsp break;
7412 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7413 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7414 dc424a06 2019-08-07 stsp break;
7415 88f33a19 2019-08-08 stsp }
7416 dc424a06 2019-08-07 stsp }
7417 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7418 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7419 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7420 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7421 dc424a06 2019-08-07 stsp break;
7422 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7423 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7424 12463d8b 2019-12-13 stsp de_name);
7425 0cb83759 2019-08-03 stsp break;
7426 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7427 d3e7c587 2019-08-03 stsp break;
7428 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7429 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7430 ebf48fd5 2019-08-03 stsp break;
7431 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7432 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7433 2a06fe5f 2019-08-24 stsp break;
7434 0cb83759 2019-08-03 stsp default:
7435 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7436 537ac44b 2019-08-03 stsp break;
7437 0cb83759 2019-08-03 stsp }
7438 dc424a06 2019-08-07 stsp
7439 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7440 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7441 dc424a06 2019-08-07 stsp free(path_content);
7442 2db2652d 2019-08-07 stsp free(ondisk_path);
7443 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7444 0ebf8283 2019-07-24 stsp return err;
7445 0ebf8283 2019-07-24 stsp }
7446 0cb83759 2019-08-03 stsp
7447 0cb83759 2019-08-03 stsp const struct got_error *
7448 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7449 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7450 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7451 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7452 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7453 0cb83759 2019-08-03 stsp {
7454 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7455 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7456 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7457 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7458 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7459 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7460 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7461 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7462 0cb83759 2019-08-03 stsp
7463 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7464 0cb83759 2019-08-03 stsp if (err)
7465 0cb83759 2019-08-03 stsp return err;
7466 0cb83759 2019-08-03 stsp
7467 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7468 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7469 735ef5ac 2019-08-03 stsp if (err)
7470 735ef5ac 2019-08-03 stsp goto done;
7471 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7472 735ef5ac 2019-08-03 stsp if (err)
7473 735ef5ac 2019-08-03 stsp goto done;
7474 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7475 0cb83759 2019-08-03 stsp if (err)
7476 0cb83759 2019-08-03 stsp goto done;
7477 0cb83759 2019-08-03 stsp
7478 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7479 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7480 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7481 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7482 2db2652d 2019-08-07 stsp oka.repo = repo;
7483 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7484 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7485 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7486 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7487 735ef5ac 2019-08-03 stsp if (err)
7488 735ef5ac 2019-08-03 stsp goto done;
7489 735ef5ac 2019-08-03 stsp }
7490 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7491 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7492 2db2652d 2019-08-07 stsp goto done;
7493 2db2652d 2019-08-07 stsp }
7494 735ef5ac 2019-08-03 stsp
7495 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7496 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7497 2db2652d 2019-08-07 stsp spa.repo = repo;
7498 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7499 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7500 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7501 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7502 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7503 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7504 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7505 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7506 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7507 42005733 2019-08-03 stsp if (err)
7508 2db2652d 2019-08-07 stsp goto done;
7509 7b5dc508 2019-10-28 stsp }
7510 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7511 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7512 7b5dc508 2019-10-28 stsp goto done;
7513 0cb83759 2019-08-03 stsp }
7514 0cb83759 2019-08-03 stsp
7515 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7516 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7517 0cb83759 2019-08-03 stsp err = sync_err;
7518 0cb83759 2019-08-03 stsp done:
7519 735ef5ac 2019-08-03 stsp if (head_ref)
7520 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7521 735ef5ac 2019-08-03 stsp free(head_commit_id);
7522 0cb83759 2019-08-03 stsp free(fileindex_path);
7523 0cb83759 2019-08-03 stsp if (fileindex)
7524 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7525 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7526 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7527 0cb83759 2019-08-03 stsp err = unlockerr;
7528 0cb83759 2019-08-03 stsp return err;
7529 0cb83759 2019-08-03 stsp }
7530 ad493afc 2019-08-03 stsp
7531 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7532 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7533 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7534 ad493afc 2019-08-03 stsp struct got_repository *repo;
7535 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7536 ad493afc 2019-08-03 stsp void *progress_arg;
7537 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7538 2e1f37b0 2019-08-08 stsp void *patch_arg;
7539 ad493afc 2019-08-03 stsp };
7540 2e1f37b0 2019-08-08 stsp
7541 2e1f37b0 2019-08-08 stsp static const struct got_error *
7542 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7543 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7544 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7545 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7546 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7547 2e1f37b0 2019-08-08 stsp {
7548 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7549 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7550 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7551 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7552 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7553 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7554 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7555 2e1f37b0 2019-08-08 stsp
7556 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7557 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7558 2e1f37b0 2019-08-08 stsp
7559 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7560 2e1f37b0 2019-08-08 stsp if (err)
7561 2e1f37b0 2019-08-08 stsp return err;
7562 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7563 2e1f37b0 2019-08-08 stsp if (err)
7564 2e1f37b0 2019-08-08 stsp goto done;
7565 2e1f37b0 2019-08-08 stsp
7566 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7567 2e1f37b0 2019-08-08 stsp if (err)
7568 2e1f37b0 2019-08-08 stsp goto done;
7569 2e1f37b0 2019-08-08 stsp
7570 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7571 2e1f37b0 2019-08-08 stsp if (err)
7572 2e1f37b0 2019-08-08 stsp goto done;
7573 2e1f37b0 2019-08-08 stsp
7574 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7575 2e1f37b0 2019-08-08 stsp if (err)
7576 2e1f37b0 2019-08-08 stsp goto done;
7577 2e1f37b0 2019-08-08 stsp
7578 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7579 2e1f37b0 2019-08-08 stsp if (err)
7580 2e1f37b0 2019-08-08 stsp goto done;
7581 ad493afc 2019-08-03 stsp
7582 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7583 2e1f37b0 2019-08-08 stsp if (err)
7584 2e1f37b0 2019-08-08 stsp goto done;
7585 2e1f37b0 2019-08-08 stsp
7586 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7587 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7588 2e1f37b0 2019-08-08 stsp if (err)
7589 2e1f37b0 2019-08-08 stsp goto done;
7590 2e1f37b0 2019-08-08 stsp
7591 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7592 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7593 2e1f37b0 2019-08-08 stsp if (err)
7594 2e1f37b0 2019-08-08 stsp goto done;
7595 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7596 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7597 2e1f37b0 2019-08-08 stsp if (err)
7598 2e1f37b0 2019-08-08 stsp goto done;
7599 2e1f37b0 2019-08-08 stsp
7600 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7601 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7602 2e1f37b0 2019-08-08 stsp goto done;
7603 2e1f37b0 2019-08-08 stsp }
7604 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7605 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7606 2e1f37b0 2019-08-08 stsp goto done;
7607 2e1f37b0 2019-08-08 stsp }
7608 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7609 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7610 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7611 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7612 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7613 fe621944 2020-11-10 stsp nchanges++;
7614 fe621944 2020-11-10 stsp }
7615 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7616 2e1f37b0 2019-08-08 stsp int choice;
7617 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7618 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7619 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7620 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7621 2e1f37b0 2019-08-08 stsp if (err)
7622 2e1f37b0 2019-08-08 stsp goto done;
7623 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7624 2e1f37b0 2019-08-08 stsp have_content = 1;
7625 19e4b907 2019-08-08 stsp else
7626 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7627 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7628 2e1f37b0 2019-08-08 stsp break;
7629 2e1f37b0 2019-08-08 stsp }
7630 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7631 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7632 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7633 2e1f37b0 2019-08-08 stsp done:
7634 2e1f37b0 2019-08-08 stsp free(label1);
7635 2e1f37b0 2019-08-08 stsp if (blob)
7636 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7637 2e1f37b0 2019-08-08 stsp if (staged_blob)
7638 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7639 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7640 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7641 fe621944 2020-11-10 stsp err = free_err;
7642 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7643 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7644 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7645 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7646 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7647 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7648 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7649 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7650 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7651 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7652 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7653 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7654 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7655 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7656 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7657 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7658 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7659 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7660 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7661 2e1f37b0 2019-08-08 stsp }
7662 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7663 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7664 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7665 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7666 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7667 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7668 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7669 2e1f37b0 2019-08-08 stsp }
7670 2e1f37b0 2019-08-08 stsp free(path1);
7671 2e1f37b0 2019-08-08 stsp free(path2);
7672 fda8017d 2020-07-23 stsp return err;
7673 fda8017d 2020-07-23 stsp }
7674 fda8017d 2020-07-23 stsp
7675 fda8017d 2020-07-23 stsp static const struct got_error *
7676 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7677 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7678 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7679 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7680 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7681 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7682 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7683 fda8017d 2020-07-23 stsp {
7684 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7685 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7686 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7687 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7688 fda8017d 2020-07-23 stsp FILE *f = NULL;
7689 fda8017d 2020-07-23 stsp struct stat sb;
7690 fda8017d 2020-07-23 stsp
7691 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7692 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7693 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7694 fda8017d 2020-07-23 stsp if (err)
7695 fda8017d 2020-07-23 stsp return err;
7696 fda8017d 2020-07-23 stsp
7697 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7698 fda8017d 2020-07-23 stsp return NULL;
7699 fda8017d 2020-07-23 stsp
7700 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7701 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7702 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7703 fda8017d 2020-07-23 stsp if (err)
7704 fda8017d 2020-07-23 stsp goto done;
7705 fda8017d 2020-07-23 stsp }
7706 fda8017d 2020-07-23 stsp
7707 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7708 fda8017d 2020-07-23 stsp if (f == NULL) {
7709 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7710 fda8017d 2020-07-23 stsp path_unstaged_content);
7711 fda8017d 2020-07-23 stsp goto done;
7712 fda8017d 2020-07-23 stsp }
7713 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7714 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7715 fda8017d 2020-07-23 stsp goto done;
7716 fda8017d 2020-07-23 stsp }
7717 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7718 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7719 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7720 fda8017d 2020-07-23 stsp size_t r;
7721 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7722 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7723 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7724 fda8017d 2020-07-23 stsp goto done;
7725 fda8017d 2020-07-23 stsp }
7726 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7727 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7728 fda8017d 2020-07-23 stsp goto done;
7729 fda8017d 2020-07-23 stsp }
7730 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7731 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7732 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7733 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7734 fda8017d 2020-07-23 stsp progress_arg);
7735 fda8017d 2020-07-23 stsp } else {
7736 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7737 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7738 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7739 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7740 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7741 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7742 fda8017d 2020-07-23 stsp }
7743 fda8017d 2020-07-23 stsp if (err)
7744 fda8017d 2020-07-23 stsp goto done;
7745 fda8017d 2020-07-23 stsp
7746 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7747 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7748 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7749 2ac8aa02 2020-11-09 stsp } else {
7750 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7751 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7752 2ac8aa02 2020-11-09 stsp }
7753 fda8017d 2020-07-23 stsp done:
7754 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7755 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7756 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7757 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7758 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7759 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7760 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7761 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7762 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7763 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7764 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7765 2e1f37b0 2019-08-08 stsp return err;
7766 2e1f37b0 2019-08-08 stsp }
7767 2e1f37b0 2019-08-08 stsp
7768 ad493afc 2019-08-03 stsp static const struct got_error *
7769 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7770 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7771 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7772 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7773 ad493afc 2019-08-03 stsp {
7774 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7775 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7776 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7777 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7778 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7779 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7780 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7781 9bc94a15 2019-08-03 stsp struct stat sb;
7782 ad493afc 2019-08-03 stsp
7783 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7784 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7785 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7786 2e1f37b0 2019-08-08 stsp return NULL;
7787 2e1f37b0 2019-08-08 stsp
7788 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7789 ad493afc 2019-08-03 stsp if (ie == NULL)
7790 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7791 9bc94a15 2019-08-03 stsp
7792 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7793 9bc94a15 2019-08-03 stsp == -1)
7794 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7795 ad493afc 2019-08-03 stsp
7796 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7797 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7798 f69721c3 2019-10-21 stsp if (err)
7799 f69721c3 2019-10-21 stsp goto done;
7800 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7801 f69721c3 2019-10-21 stsp id_str) == -1) {
7802 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7803 f69721c3 2019-10-21 stsp goto done;
7804 f69721c3 2019-10-21 stsp }
7805 f69721c3 2019-10-21 stsp
7806 ad493afc 2019-08-03 stsp switch (staged_status) {
7807 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7808 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7809 ad493afc 2019-08-03 stsp blob_id, 8192);
7810 ad493afc 2019-08-03 stsp if (err)
7811 ad493afc 2019-08-03 stsp break;
7812 ad493afc 2019-08-03 stsp /* fall through */
7813 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7814 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7815 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
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_YES)
7822 2e1f37b0 2019-08-08 stsp break;
7823 2e1f37b0 2019-08-08 stsp } else {
7824 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7825 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7826 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7827 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7828 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7829 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7830 2e1f37b0 2019-08-08 stsp }
7831 2e1f37b0 2019-08-08 stsp }
7832 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7833 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7834 ad493afc 2019-08-03 stsp if (err)
7835 ad493afc 2019-08-03 stsp break;
7836 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7837 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7838 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7839 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7840 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7841 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7842 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7843 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7844 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7845 ea7786be 2020-07-23 stsp break;
7846 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7847 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7848 36bf999c 2020-07-23 stsp char *staged_target;
7849 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7850 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7851 36bf999c 2020-07-23 stsp if (err)
7852 36bf999c 2020-07-23 stsp goto done;
7853 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7854 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7855 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7856 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7857 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7858 36bf999c 2020-07-23 stsp free(staged_target);
7859 dfe9fba0 2020-07-23 stsp } else {
7860 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7861 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7862 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7863 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7864 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7865 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7866 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7867 dfe9fba0 2020-07-23 stsp }
7868 ea7786be 2020-07-23 stsp break;
7869 ea7786be 2020-07-23 stsp default:
7870 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7871 ea7786be 2020-07-23 stsp break;
7872 ea7786be 2020-07-23 stsp }
7873 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7874 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7875 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7876 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7877 2ac8aa02 2020-11-09 stsp }
7878 ad493afc 2019-08-03 stsp break;
7879 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7880 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7881 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7882 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7883 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7884 2e1f37b0 2019-08-08 stsp if (err)
7885 2e1f37b0 2019-08-08 stsp break;
7886 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7887 2e1f37b0 2019-08-08 stsp break;
7888 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7889 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7890 2e1f37b0 2019-08-08 stsp break;
7891 2e1f37b0 2019-08-08 stsp }
7892 2e1f37b0 2019-08-08 stsp }
7893 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7894 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7895 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7896 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7897 9bc94a15 2019-08-03 stsp if (err)
7898 9bc94a15 2019-08-03 stsp break;
7899 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7900 ad493afc 2019-08-03 stsp break;
7901 ad493afc 2019-08-03 stsp }
7902 f69721c3 2019-10-21 stsp done:
7903 ad493afc 2019-08-03 stsp free(ondisk_path);
7904 ad493afc 2019-08-03 stsp if (blob_base)
7905 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7906 ad493afc 2019-08-03 stsp if (blob_staged)
7907 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7908 f69721c3 2019-10-21 stsp free(id_str);
7909 f69721c3 2019-10-21 stsp free(label_orig);
7910 ad493afc 2019-08-03 stsp return err;
7911 ad493afc 2019-08-03 stsp }
7912 ad493afc 2019-08-03 stsp
7913 ad493afc 2019-08-03 stsp const struct got_error *
7914 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7915 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7916 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7917 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7918 ad493afc 2019-08-03 stsp struct got_repository *repo)
7919 ad493afc 2019-08-03 stsp {
7920 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7921 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7922 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7923 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7924 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7925 ad493afc 2019-08-03 stsp
7926 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7927 ad493afc 2019-08-03 stsp if (err)
7928 ad493afc 2019-08-03 stsp return err;
7929 ad493afc 2019-08-03 stsp
7930 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7931 ad493afc 2019-08-03 stsp if (err)
7932 ad493afc 2019-08-03 stsp goto done;
7933 ad493afc 2019-08-03 stsp
7934 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7935 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7936 ad493afc 2019-08-03 stsp upa.repo = repo;
7937 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7938 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7939 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7940 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7941 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7942 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7943 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7944 ad493afc 2019-08-03 stsp if (err)
7945 ad493afc 2019-08-03 stsp goto done;
7946 ad493afc 2019-08-03 stsp }
7947 ad493afc 2019-08-03 stsp
7948 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7949 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7950 ad493afc 2019-08-03 stsp err = sync_err;
7951 ad493afc 2019-08-03 stsp done:
7952 ad493afc 2019-08-03 stsp free(fileindex_path);
7953 ad493afc 2019-08-03 stsp if (fileindex)
7954 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7955 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7956 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7957 ad493afc 2019-08-03 stsp err = unlockerr;
7958 ad493afc 2019-08-03 stsp return err;
7959 ad493afc 2019-08-03 stsp }
7960 b2118c49 2020-07-28 stsp
7961 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7962 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7963 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7964 b2118c49 2020-07-28 stsp void *info_arg;
7965 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7966 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7967 b2118c49 2020-07-28 stsp void *cancel_arg;
7968 b2118c49 2020-07-28 stsp };
7969 b2118c49 2020-07-28 stsp
7970 b2118c49 2020-07-28 stsp static const struct got_error *
7971 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7972 b2118c49 2020-07-28 stsp {
7973 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7974 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7975 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7976 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7977 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7978 b2118c49 2020-07-28 stsp int stage;
7979 b2118c49 2020-07-28 stsp
7980 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7981 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7982 b2118c49 2020-07-28 stsp
7983 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7984 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7985 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7986 b2118c49 2020-07-28 stsp break;
7987 b2118c49 2020-07-28 stsp }
7988 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7989 b2118c49 2020-07-28 stsp return NULL;
7990 b2118c49 2020-07-28 stsp
7991 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7992 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7993 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7994 b2118c49 2020-07-28 stsp }
7995 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7996 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7997 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7998 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7999 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8000 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8001 b2118c49 2020-07-28 stsp }
8002 b2118c49 2020-07-28 stsp
8003 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8004 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8005 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8006 b2118c49 2020-07-28 stsp }
8007 b2118c49 2020-07-28 stsp
8008 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8009 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8010 b2118c49 2020-07-28 stsp }
8011 b2118c49 2020-07-28 stsp
8012 b2118c49 2020-07-28 stsp const struct got_error *
8013 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8014 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8015 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8016 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8017 b2118c49 2020-07-28 stsp
8018 b2118c49 2020-07-28 stsp {
8019 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8020 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8021 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8022 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8023 b2118c49 2020-07-28 stsp
8024 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8025 b2118c49 2020-07-28 stsp if (err)
8026 b2118c49 2020-07-28 stsp return err;
8027 b2118c49 2020-07-28 stsp
8028 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8029 b2118c49 2020-07-28 stsp if (err)
8030 b2118c49 2020-07-28 stsp goto done;
8031 b2118c49 2020-07-28 stsp
8032 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8033 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8034 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8035 b2118c49 2020-07-28 stsp arg.paths = paths;
8036 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8037 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8038 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8039 b2118c49 2020-07-28 stsp &arg);
8040 b2118c49 2020-07-28 stsp done:
8041 b2118c49 2020-07-28 stsp free(fileindex_path);
8042 b2118c49 2020-07-28 stsp if (fileindex)
8043 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8044 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8045 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8046 b2118c49 2020-07-28 stsp err = unlockerr;
8047 b2118c49 2020-07-28 stsp return err;
8048 b2118c49 2020-07-28 stsp }