Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp
59 9d31a1d8 2018-03-11 stsp #ifndef MIN
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 9d31a1d8 2018-03-11 stsp #endif
62 f69721c3 2019-10-21 stsp
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 86c3caaf 2018-03-09 stsp
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
68 99724ed4 2018-03-10 stsp {
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
70 99724ed4 2018-03-10 stsp char *path;
71 99724ed4 2018-03-10 stsp
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
76 99724ed4 2018-03-10 stsp free(path);
77 507dc3bb 2018-12-29 stsp return err;
78 507dc3bb 2018-12-29 stsp }
79 507dc3bb 2018-12-29 stsp
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
82 507dc3bb 2018-12-29 stsp {
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
87 507dc3bb 2018-12-29 stsp
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp }
93 507dc3bb 2018-12-29 stsp
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
95 507dc3bb 2018-12-29 stsp if (err)
96 507dc3bb 2018-12-29 stsp goto done;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
102 507dc3bb 2018-12-29 stsp goto done;
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
109 507dc3bb 2018-12-29 stsp goto done;
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp
112 507dc3bb 2018-12-29 stsp done:
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
117 99724ed4 2018-03-10 stsp }
118 99724ed4 2018-03-10 stsp
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
121 09fe317a 2018-03-11 stsp {
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
125 09fe317a 2018-03-11 stsp ssize_t n;
126 09fe317a 2018-03-11 stsp struct stat sb;
127 09fe317a 2018-03-11 stsp
128 09fe317a 2018-03-11 stsp *content = NULL;
129 09fe317a 2018-03-11 stsp
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
133 09fe317a 2018-03-11 stsp goto done;
134 09fe317a 2018-03-11 stsp }
135 09fe317a 2018-03-11 stsp
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 f02eaa22 2019-03-11 stsp else
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
142 ef99fdb1 2018-03-11 stsp goto done;
143 ef99fdb1 2018-03-11 stsp }
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
147 09fe317a 2018-03-11 stsp goto done;
148 09fe317a 2018-03-11 stsp }
149 09fe317a 2018-03-11 stsp
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
152 d10c9b58 2019-02-19 stsp goto done;
153 d10c9b58 2019-02-19 stsp }
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
157 09fe317a 2018-03-11 stsp goto done;
158 09fe317a 2018-03-11 stsp }
159 09fe317a 2018-03-11 stsp
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
164 09fe317a 2018-03-11 stsp goto done;
165 09fe317a 2018-03-11 stsp }
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 09fe317a 2018-03-11 stsp goto done;
169 09fe317a 2018-03-11 stsp }
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
171 09fe317a 2018-03-11 stsp
172 09fe317a 2018-03-11 stsp done:
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
176 09fe317a 2018-03-11 stsp if (err) {
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
179 09fe317a 2018-03-11 stsp }
180 09fe317a 2018-03-11 stsp return err;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
185 024e9686 2019-05-14 stsp {
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
188 024e9686 2019-05-14 stsp
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
193 024e9686 2019-05-14 stsp } else {
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
197 024e9686 2019-05-14 stsp }
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
201 024e9686 2019-05-14 stsp }
202 024e9686 2019-05-14 stsp
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
206 86c3caaf 2018-03-09 stsp {
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
217 65596e15 2018-12-24 stsp
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
220 0c48fee2 2019-03-11 stsp goto done;
221 0c48fee2 2019-03-11 stsp }
222 0c48fee2 2019-03-11 stsp
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
224 65596e15 2018-12-24 stsp if (err)
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
227 65596e15 2018-12-24 stsp if (err)
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
231 86c3caaf 2018-03-09 stsp
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
235 0bb8a95e 2018-03-12 stsp }
236 577ec78f 2018-03-11 stsp
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
240 86c3caaf 2018-03-09 stsp goto done;
241 86c3caaf 2018-03-09 stsp }
242 86c3caaf 2018-03-09 stsp
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 86c3caaf 2018-03-09 stsp goto done;
247 86c3caaf 2018-03-09 stsp }
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
250 86c3caaf 2018-03-09 stsp goto done;
251 86c3caaf 2018-03-09 stsp }
252 86c3caaf 2018-03-09 stsp
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 056e7441 2018-03-11 stsp if (err)
256 056e7441 2018-03-11 stsp goto done;
257 056e7441 2018-03-11 stsp
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 99724ed4 2018-03-10 stsp if (err)
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 65596e15 2018-12-24 stsp
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
270 65596e15 2018-12-24 stsp if (err)
271 65596e15 2018-12-24 stsp goto done;
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 99724ed4 2018-03-10 stsp if (err)
274 86c3caaf 2018-03-09 stsp goto done;
275 86c3caaf 2018-03-09 stsp
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
279 99724ed4 2018-03-10 stsp if (err)
280 86c3caaf 2018-03-09 stsp goto done;
281 86c3caaf 2018-03-09 stsp
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
285 ec22038e 2019-03-10 stsp if (err)
286 ec22038e 2019-03-10 stsp goto done;
287 ec22038e 2019-03-10 stsp
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
297 ec22038e 2019-03-10 stsp goto done;
298 ec22038e 2019-03-10 stsp }
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 577ec78f 2018-03-11 stsp if (err)
301 577ec78f 2018-03-11 stsp goto done;
302 577ec78f 2018-03-11 stsp
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
306 1451e70d 2018-03-10 stsp goto done;
307 1451e70d 2018-03-10 stsp }
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 99724ed4 2018-03-10 stsp if (err)
310 1451e70d 2018-03-10 stsp goto done;
311 1451e70d 2018-03-10 stsp
312 86c3caaf 2018-03-09 stsp done:
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
324 86c3caaf 2018-03-09 stsp {
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 6d9d28c3 2018-03-11 stsp
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
341 6d9d28c3 2018-03-11 stsp goto done;
342 6d9d28c3 2018-03-11 stsp }
343 6d9d28c3 2018-03-11 stsp
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
347 6d9d28c3 2018-03-11 stsp goto done;
348 6d9d28c3 2018-03-11 stsp }
349 6d9d28c3 2018-03-11 stsp
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp }
356 6d9d28c3 2018-03-11 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 6d9d28c3 2018-03-11 stsp if (err)
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
365 6d9d28c3 2018-03-11 stsp goto done;
366 6d9d28c3 2018-03-11 stsp }
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 6d9d28c3 2018-03-11 stsp
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
375 6d9d28c3 2018-03-11 stsp goto done;
376 6d9d28c3 2018-03-11 stsp }
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
378 6d9d28c3 2018-03-11 stsp
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
382 6d9d28c3 2018-03-11 stsp goto done;
383 6d9d28c3 2018-03-11 stsp }
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
386 6d9d28c3 2018-03-11 stsp if (err)
387 6d9d28c3 2018-03-11 stsp goto done;
388 eaccb85f 2018-12-25 stsp
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
391 93a30277 2018-12-24 stsp if (err)
392 93a30277 2018-12-24 stsp goto done;
393 93a30277 2018-12-24 stsp
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
396 c442a90d 2019-03-10 stsp if (err)
397 c442a90d 2019-03-10 stsp goto done;
398 c442a90d 2019-03-10 stsp
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 eaccb85f 2018-12-25 stsp if (err)
401 c442a90d 2019-03-10 stsp goto done;
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
405 eaccb85f 2018-12-25 stsp goto done;
406 c442a90d 2019-03-10 stsp }
407 eaccb85f 2018-12-25 stsp
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 eaccb85f 2018-12-25 stsp if (err)
410 eaccb85f 2018-12-25 stsp goto done;
411 eaccb85f 2018-12-25 stsp
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
414 f5baf295 2018-03-11 stsp if (err)
415 6d9d28c3 2018-03-11 stsp goto done;
416 6d9d28c3 2018-03-11 stsp
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
419 6d9d28c3 2018-03-11 stsp done:
420 eaccb85f 2018-12-25 stsp if (repo)
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
427 6d9d28c3 2018-03-11 stsp if (err) {
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
429 6d9d28c3 2018-03-11 stsp close(fd);
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
433 6d9d28c3 2018-03-11 stsp } else
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
435 6d9d28c3 2018-03-11 stsp
436 6d9d28c3 2018-03-11 stsp return err;
437 86c3caaf 2018-03-09 stsp }
438 247140b2 2019-02-05 stsp
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
441 247140b2 2019-02-05 stsp {
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
443 86c3caaf 2018-03-09 stsp
444 247140b2 2019-02-05 stsp do {
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
454 247140b2 2019-02-05 stsp
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
456 247140b2 2019-02-05 stsp }
457 247140b2 2019-02-05 stsp
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
460 86c3caaf 2018-03-09 stsp {
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
473 86c3caaf 2018-03-09 stsp }
474 86c3caaf 2018-03-09 stsp
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
477 c7f4312f 2019-02-05 stsp {
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
479 c7f4312f 2019-02-05 stsp }
480 c7f4312f 2019-02-05 stsp
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
483 86c3caaf 2018-03-09 stsp {
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
485 49520a32 2018-12-29 stsp }
486 49520a32 2018-12-29 stsp
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
489 49520a32 2018-12-29 stsp {
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
491 e5dc7198 2018-12-29 stsp }
492 e5dc7198 2018-12-29 stsp
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
496 e5dc7198 2018-12-29 stsp {
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
498 e5dc7198 2018-12-29 stsp
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
502 e5dc7198 2018-12-29 stsp }
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
507 86c3caaf 2018-03-09 stsp }
508 86c3caaf 2018-03-09 stsp
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
511 86c3caaf 2018-03-09 stsp {
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
513 024e9686 2019-05-14 stsp }
514 024e9686 2019-05-14 stsp
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
518 024e9686 2019-05-14 stsp {
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
521 024e9686 2019-05-14 stsp
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
526 024e9686 2019-05-14 stsp goto done;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp }
534 024e9686 2019-05-14 stsp
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
536 024e9686 2019-05-14 stsp if (err)
537 024e9686 2019-05-14 stsp goto done;
538 024e9686 2019-05-14 stsp
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
541 024e9686 2019-05-14 stsp done:
542 024e9686 2019-05-14 stsp free(path_got);
543 024e9686 2019-05-14 stsp if (err)
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
546 507dc3bb 2018-12-29 stsp }
547 507dc3bb 2018-12-29 stsp
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
550 507dc3bb 2018-12-29 stsp {
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
552 86c3caaf 2018-03-09 stsp }
553 86c3caaf 2018-03-09 stsp
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
557 507dc3bb 2018-12-29 stsp {
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
562 507dc3bb 2018-12-29 stsp
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
567 507dc3bb 2018-12-29 stsp goto done;
568 507dc3bb 2018-12-29 stsp }
569 507dc3bb 2018-12-29 stsp
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
571 507dc3bb 2018-12-29 stsp if (err)
572 507dc3bb 2018-12-29 stsp return err;
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
576 507dc3bb 2018-12-29 stsp goto done;
577 507dc3bb 2018-12-29 stsp }
578 507dc3bb 2018-12-29 stsp
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
581 507dc3bb 2018-12-29 stsp if (err)
582 507dc3bb 2018-12-29 stsp goto done;
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 507dc3bb 2018-12-29 stsp if (err)
585 507dc3bb 2018-12-29 stsp goto done;
586 507dc3bb 2018-12-29 stsp
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
591 507dc3bb 2018-12-29 stsp goto done;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp done:
594 507dc3bb 2018-12-29 stsp if (obj)
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
599 507dc3bb 2018-12-29 stsp }
600 507dc3bb 2018-12-29 stsp
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
603 86c3caaf 2018-03-09 stsp {
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
609 21908da4 2019-01-13 stsp }
610 21908da4 2019-01-13 stsp
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
613 4a1ddfc2 2019-01-12 stsp {
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
616 4a1ddfc2 2019-01-12 stsp
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
619 4a1ddfc2 2019-01-12 stsp
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
629 ddcd8544 2019-03-11 stsp }
630 ddcd8544 2019-03-11 stsp }
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
633 68c76935 2019-02-19 stsp }
634 68c76935 2019-02-19 stsp
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
637 68c76935 2019-02-19 stsp {
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
642 68c76935 2019-02-19 stsp
643 68c76935 2019-02-19 stsp *same = 1;
644 68c76935 2019-02-19 stsp
645 230a42bd 2019-05-11 jcs for (;;) {
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp }
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
658 68c76935 2019-02-19 stsp *same = 0;
659 68c76935 2019-02-19 stsp break;
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
662 68c76935 2019-02-19 stsp *same = 0;
663 68c76935 2019-02-19 stsp break;
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 68c76935 2019-02-19 stsp *same = 0;
667 68c76935 2019-02-19 stsp break;
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp } else {
670 68c76935 2019-02-19 stsp *same = 0;
671 68c76935 2019-02-19 stsp break;
672 68c76935 2019-02-19 stsp }
673 68c76935 2019-02-19 stsp }
674 68c76935 2019-02-19 stsp
675 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp }
677 68c76935 2019-02-19 stsp
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
680 68c76935 2019-02-19 stsp {
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
685 68c76935 2019-02-19 stsp
686 68c76935 2019-02-19 stsp *same = 1;
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
690 68c76935 2019-02-19 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
693 68c76935 2019-02-19 stsp
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
696 68c76935 2019-02-19 stsp goto done;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp if (size1 != size2) {
701 68c76935 2019-02-19 stsp *same = 0;
702 68c76935 2019-02-19 stsp return NULL;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
712 68c76935 2019-02-19 stsp goto done;
713 68c76935 2019-02-19 stsp }
714 68c76935 2019-02-19 stsp
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
716 68c76935 2019-02-19 stsp done:
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
721 68c76935 2019-02-19 stsp
722 6353ad76 2019-02-08 stsp return err;
723 6353ad76 2019-02-08 stsp }
724 6353ad76 2019-02-08 stsp
725 6353ad76 2019-02-08 stsp /*
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
729 6353ad76 2019-02-08 stsp */
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
737 6353ad76 2019-02-08 stsp {
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
745 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
746 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
747 6353ad76 2019-02-08 stsp
748 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
749 234035bc 2019-06-01 stsp
750 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
751 af54ae4a 2019-02-19 stsp if (parent == NULL)
752 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
753 af54ae4a 2019-02-19 stsp
754 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
756 af54ae4a 2019-02-19 stsp
757 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 6353ad76 2019-02-08 stsp if (err)
759 6353ad76 2019-02-08 stsp goto done;
760 6353ad76 2019-02-08 stsp
761 af54ae4a 2019-02-19 stsp free(base_path);
762 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
764 af54ae4a 2019-02-19 stsp base_path = NULL;
765 af54ae4a 2019-02-19 stsp goto done;
766 af54ae4a 2019-02-19 stsp }
767 af54ae4a 2019-02-19 stsp
768 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 6353ad76 2019-02-08 stsp if (err)
770 6353ad76 2019-02-08 stsp goto done;
771 46b6ee73 2019-06-04 stsp if (blob_orig) {
772 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 46b6ee73 2019-06-04 stsp blob_orig);
774 1430b4e0 2019-03-27 stsp if (err)
775 1430b4e0 2019-03-27 stsp goto done;
776 1430b4e0 2019-03-27 stsp } else {
777 1430b4e0 2019-03-27 stsp /*
778 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
779 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
780 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
781 1430b4e0 2019-03-27 stsp */
782 1430b4e0 2019-03-27 stsp }
783 6353ad76 2019-02-08 stsp
784 3b9f0f87 2020-07-23 stsp /*
785 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
786 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
787 3b9f0f87 2020-07-23 stsp */
788 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
789 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
790 3b9f0f87 2020-07-23 stsp ssize_t target_len;
791 3b9f0f87 2020-07-23 stsp size_t n;
792 3b9f0f87 2020-07-23 stsp
793 3b9f0f87 2020-07-23 stsp free(base_path);
794 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
795 3b9f0f87 2020-07-23 stsp parent) == -1) {
796 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
797 3b9f0f87 2020-07-23 stsp base_path = NULL;
798 3b9f0f87 2020-07-23 stsp goto done;
799 3b9f0f87 2020-07-23 stsp }
800 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 3b9f0f87 2020-07-23 stsp if (err)
802 3b9f0f87 2020-07-23 stsp goto done;
803 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
804 3b9f0f87 2020-07-23 stsp sizeof(target_path));
805 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
806 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
807 3b9f0f87 2020-07-23 stsp goto done;
808 3b9f0f87 2020-07-23 stsp }
809 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
810 3b9f0f87 2020-07-23 stsp if (n != target_len) {
811 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
812 3b9f0f87 2020-07-23 stsp goto done;
813 3b9f0f87 2020-07-23 stsp }
814 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
815 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
816 3b9f0f87 2020-07-23 stsp goto done;
817 3b9f0f87 2020-07-23 stsp }
818 3b9f0f87 2020-07-23 stsp }
819 3b9f0f87 2020-07-23 stsp
820 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
823 6353ad76 2019-02-08 stsp if (err)
824 6353ad76 2019-02-08 stsp goto done;
825 6353ad76 2019-02-08 stsp
826 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
827 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 1ee397ad 2019-07-12 stsp if (err)
829 1ee397ad 2019-07-12 stsp goto done;
830 6353ad76 2019-02-08 stsp
831 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
832 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
833 816dc654 2019-02-16 stsp goto done;
834 68c76935 2019-02-19 stsp }
835 68c76935 2019-02-19 stsp
836 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
837 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
838 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
839 68c76935 2019-02-19 stsp merged_path);
840 68c76935 2019-02-19 stsp if (err)
841 68c76935 2019-02-19 stsp goto done;
842 816dc654 2019-02-16 stsp }
843 6353ad76 2019-02-08 stsp
844 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
845 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
846 70a0c8ec 2019-02-20 stsp goto done;
847 70a0c8ec 2019-02-20 stsp }
848 70a0c8ec 2019-02-20 stsp
849 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
850 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
851 230a42bd 2019-05-11 jcs ondisk_path);
852 6353ad76 2019-02-08 stsp goto done;
853 6353ad76 2019-02-08 stsp }
854 6353ad76 2019-02-08 stsp done:
855 fdcb7daf 2019-12-15 stsp if (err) {
856 fdcb7daf 2019-12-15 stsp if (merged_path)
857 fdcb7daf 2019-12-15 stsp unlink(merged_path);
858 3b9f0f87 2020-07-23 stsp }
859 3b9f0f87 2020-07-23 stsp if (symlink_path) {
860 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
861 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
862 fdcb7daf 2019-12-15 stsp }
863 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
865 3b9f0f87 2020-07-23 stsp free(symlink_path);
866 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
868 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
870 6353ad76 2019-02-08 stsp free(merged_path);
871 af54ae4a 2019-02-19 stsp free(base_path);
872 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
873 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
874 46b6ee73 2019-06-04 stsp free(blob_orig_path);
875 af57b12a 2020-07-23 stsp }
876 af57b12a 2020-07-23 stsp return err;
877 af57b12a 2020-07-23 stsp }
878 af57b12a 2020-07-23 stsp
879 af57b12a 2020-07-23 stsp static const struct got_error *
880 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
881 af57b12a 2020-07-23 stsp size_t target_len)
882 af57b12a 2020-07-23 stsp {
883 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
884 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
885 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
886 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
887 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
888 af57b12a 2020-07-23 stsp ondisk_path);
889 af57b12a 2020-07-23 stsp return NULL;
890 af57b12a 2020-07-23 stsp }
891 af57b12a 2020-07-23 stsp
892 af57b12a 2020-07-23 stsp /*
893 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
895 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
896 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
897 993e2a1b 2020-07-23 stsp *
898 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
899 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
900 993e2a1b 2020-07-23 stsp *
901 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
902 993e2a1b 2020-07-23 stsp * has deleted this symlink.
903 11cc08c1 2020-07-23 stsp */
904 11cc08c1 2020-07-23 stsp static const struct got_error *
905 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
906 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
908 11cc08c1 2020-07-23 stsp {
909 11cc08c1 2020-07-23 stsp const struct got_error *err;
910 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 11cc08c1 2020-07-23 stsp FILE *f = NULL;
912 11cc08c1 2020-07-23 stsp
913 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
914 11cc08c1 2020-07-23 stsp if (err)
915 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
916 11cc08c1 2020-07-23 stsp
917 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
918 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
920 11cc08c1 2020-07-23 stsp goto done;
921 11cc08c1 2020-07-23 stsp }
922 11cc08c1 2020-07-23 stsp
923 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 11cc08c1 2020-07-23 stsp if (err)
925 11cc08c1 2020-07-23 stsp goto done;
926 11cc08c1 2020-07-23 stsp
927 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
929 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
930 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
931 11cc08c1 2020-07-23 stsp "target paths were found:\n"
932 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
935 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
936 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
937 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
938 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
939 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
940 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
942 11cc08c1 2020-07-23 stsp goto done;
943 11cc08c1 2020-07-23 stsp }
944 11cc08c1 2020-07-23 stsp
945 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
946 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
947 11cc08c1 2020-07-23 stsp goto done;
948 11cc08c1 2020-07-23 stsp }
949 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
950 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
951 11cc08c1 2020-07-23 stsp goto done;
952 11cc08c1 2020-07-23 stsp }
953 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
955 11cc08c1 2020-07-23 stsp goto done;
956 11cc08c1 2020-07-23 stsp }
957 11cc08c1 2020-07-23 stsp done:
958 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
959 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
960 11cc08c1 2020-07-23 stsp free(path);
961 11cc08c1 2020-07-23 stsp free(id_str);
962 11cc08c1 2020-07-23 stsp free(label_deriv);
963 11cc08c1 2020-07-23 stsp return err;
964 11cc08c1 2020-07-23 stsp }
965 d219f183 2020-07-23 stsp
966 d219f183 2020-07-23 stsp /* forward declaration */
967 d219f183 2020-07-23 stsp static const struct got_error *
968 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
970 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
971 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
972 11cc08c1 2020-07-23 stsp
973 11cc08c1 2020-07-23 stsp /*
974 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
975 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
976 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
977 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
978 af57b12a 2020-07-23 stsp */
979 af57b12a 2020-07-23 stsp static const struct got_error *
980 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
981 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
982 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
983 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
984 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
986 af57b12a 2020-07-23 stsp {
987 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
988 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
989 af57b12a 2020-07-23 stsp struct stat sb;
990 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
991 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
992 11cc08c1 2020-07-23 stsp int have_local_change = 0;
993 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
994 af57b12a 2020-07-23 stsp
995 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
996 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
997 af57b12a 2020-07-23 stsp
998 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
999 d219f183 2020-07-23 stsp /*
1000 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
1001 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
1002 d219f183 2020-07-23 stsp * a merge conflict.
1003 d219f183 2020-07-23 stsp */
1004 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
1005 d219f183 2020-07-23 stsp int local_changes_subsumed;
1006 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
1007 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
1009 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
1010 d219f183 2020-07-23 stsp }
1011 d219f183 2020-07-23 stsp
1012 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
1013 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1014 af57b12a 2020-07-23 stsp }
1015 af57b12a 2020-07-23 stsp
1016 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1017 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1018 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1019 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1020 af57b12a 2020-07-23 stsp ondisk_path);
1021 af57b12a 2020-07-23 stsp goto done;
1022 af57b12a 2020-07-23 stsp }
1023 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1024 af57b12a 2020-07-23 stsp
1025 526a746f 2020-07-23 stsp if (blob_orig) {
1026 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1027 526a746f 2020-07-23 stsp if (err)
1028 526a746f 2020-07-23 stsp goto done;
1029 526a746f 2020-07-23 stsp }
1030 af57b12a 2020-07-23 stsp
1031 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1032 af57b12a 2020-07-23 stsp if (err)
1033 af57b12a 2020-07-23 stsp goto done;
1034 af57b12a 2020-07-23 stsp
1035 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1036 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1037 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 11cc08c1 2020-07-23 stsp have_local_change = 1;
1039 11cc08c1 2020-07-23 stsp
1040 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1041 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1042 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1043 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1045 11cc08c1 2020-07-23 stsp
1046 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1047 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1048 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1049 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 11cc08c1 2020-07-23 stsp path);
1051 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1052 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1054 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1055 11cc08c1 2020-07-23 stsp path);
1056 11cc08c1 2020-07-23 stsp } else {
1057 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1058 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1059 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1060 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1061 11cc08c1 2020-07-23 stsp if (err)
1062 11cc08c1 2020-07-23 stsp goto done;
1063 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1064 11cc08c1 2020-07-23 stsp path);
1065 11cc08c1 2020-07-23 stsp }
1066 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1067 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1068 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1069 af57b12a 2020-07-23 stsp strlen(deriv_target));
1070 af57b12a 2020-07-23 stsp if (err)
1071 af57b12a 2020-07-23 stsp goto done;
1072 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1074 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1075 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1076 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1077 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 ea7786be 2020-07-23 stsp path);
1079 ea7786be 2020-07-23 stsp } else {
1080 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1081 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1082 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1083 ea7786be 2020-07-23 stsp if (err)
1084 ea7786be 2020-07-23 stsp goto done;
1085 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1086 ea7786be 2020-07-23 stsp path);
1087 ea7786be 2020-07-23 stsp }
1088 6353ad76 2019-02-08 stsp }
1089 11cc08c1 2020-07-23 stsp
1090 af57b12a 2020-07-23 stsp done:
1091 af57b12a 2020-07-23 stsp free(ancestor_target);
1092 af57b12a 2020-07-23 stsp free(deriv_target);
1093 14c901f1 2019-08-08 stsp return err;
1094 14c901f1 2019-08-08 stsp }
1095 14c901f1 2019-08-08 stsp
1096 14c901f1 2019-08-08 stsp /*
1097 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1098 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1099 14c901f1 2019-08-08 stsp * acts as the second derived version.
1100 14c901f1 2019-08-08 stsp */
1101 14c901f1 2019-08-08 stsp static const struct got_error *
1102 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1103 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1104 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1105 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1106 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1107 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1108 14c901f1 2019-08-08 stsp {
1109 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1110 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1111 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1112 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1113 14c901f1 2019-08-08 stsp
1114 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1115 14c901f1 2019-08-08 stsp
1116 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1117 14c901f1 2019-08-08 stsp if (parent == NULL)
1118 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1119 14c901f1 2019-08-08 stsp
1120 14c901f1 2019-08-08 stsp free(base_path);
1121 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1122 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1123 14c901f1 2019-08-08 stsp base_path = NULL;
1124 14c901f1 2019-08-08 stsp goto done;
1125 14c901f1 2019-08-08 stsp }
1126 14c901f1 2019-08-08 stsp
1127 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1128 14c901f1 2019-08-08 stsp if (err)
1129 14c901f1 2019-08-08 stsp goto done;
1130 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1131 14c901f1 2019-08-08 stsp blob_deriv);
1132 14c901f1 2019-08-08 stsp if (err)
1133 14c901f1 2019-08-08 stsp goto done;
1134 14c901f1 2019-08-08 stsp
1135 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1136 14c901f1 2019-08-08 stsp if (err)
1137 14c901f1 2019-08-08 stsp goto done;
1138 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1139 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1140 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1141 14c901f1 2019-08-08 stsp goto done;
1142 14c901f1 2019-08-08 stsp }
1143 14c901f1 2019-08-08 stsp
1144 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1145 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1146 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1147 14c901f1 2019-08-08 stsp done:
1148 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1149 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1150 14c901f1 2019-08-08 stsp free(base_path);
1151 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1152 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1153 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1154 14c901f1 2019-08-08 stsp }
1155 6353ad76 2019-02-08 stsp free(id_str);
1156 818c7501 2019-07-11 stsp free(label_deriv);
1157 4a1ddfc2 2019-01-12 stsp return err;
1158 4a1ddfc2 2019-01-12 stsp }
1159 4a1ddfc2 2019-01-12 stsp
1160 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1161 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1162 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1163 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1164 13d9040b 2019-03-27 stsp {
1165 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1166 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1167 13d9040b 2019-03-27 stsp
1168 65b05cec 2020-07-23 stsp *new_iep = NULL;
1169 65b05cec 2020-07-23 stsp
1170 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1171 054041d0 2020-06-24 stsp if (err)
1172 054041d0 2020-06-24 stsp return err;
1173 054041d0 2020-06-24 stsp
1174 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1175 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1176 054041d0 2020-06-24 stsp if (err)
1177 054041d0 2020-06-24 stsp goto done;
1178 054041d0 2020-06-24 stsp
1179 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1180 054041d0 2020-06-24 stsp done:
1181 054041d0 2020-06-24 stsp if (err)
1182 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1183 65b05cec 2020-07-23 stsp else
1184 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1185 13d9040b 2019-03-27 stsp return err;
1186 1ebedb77 2019-10-19 stsp }
1187 1ebedb77 2019-10-19 stsp
1188 1ebedb77 2019-10-19 stsp static mode_t
1189 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1190 1ebedb77 2019-10-19 stsp {
1191 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1192 1ebedb77 2019-10-19 stsp
1193 1ebedb77 2019-10-19 stsp if (executable) {
1194 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1195 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1196 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1197 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1198 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1199 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1200 1ebedb77 2019-10-19 stsp }
1201 1ebedb77 2019-10-19 stsp
1202 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1203 13d9040b 2019-03-27 stsp }
1204 13d9040b 2019-03-27 stsp
1205 8ba819a3 2020-07-23 stsp /* forward declaration */
1206 13d9040b 2019-03-27 stsp static const struct got_error *
1207 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1208 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1209 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1210 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1211 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1212 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1213 8ba819a3 2020-07-23 stsp
1214 5a1fbc73 2020-07-23 stsp /*
1215 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1216 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1217 5a1fbc73 2020-07-23 stsp */
1218 8ba819a3 2020-07-23 stsp static const struct got_error *
1219 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1220 5a1fbc73 2020-07-23 stsp size_t target_len)
1221 5a1fbc73 2020-07-23 stsp {
1222 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1223 5a1fbc73 2020-07-23 stsp ssize_t elen;
1224 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1225 5a1fbc73 2020-07-23 stsp int fd;
1226 5a1fbc73 2020-07-23 stsp
1227 5a1fbc73 2020-07-23 stsp /*
1228 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1229 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1230 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1231 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1232 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1233 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1234 5a1fbc73 2020-07-23 stsp */
1235 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1236 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1237 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1238 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1239 5a1fbc73 2020-07-23 stsp
1240 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1241 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1242 5a1fbc73 2020-07-23 stsp if (elen == -1)
1243 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1244 5a1fbc73 2020-07-23 stsp
1245 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1246 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1247 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1248 5a1fbc73 2020-07-23 stsp }
1249 5a1fbc73 2020-07-23 stsp
1250 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1251 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1252 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1253 5a1fbc73 2020-07-23 stsp return err;
1254 5a1fbc73 2020-07-23 stsp }
1255 5a1fbc73 2020-07-23 stsp
1256 5a1fbc73 2020-07-23 stsp static const struct got_error *
1257 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1258 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1259 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1260 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1261 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1262 9d31a1d8 2018-03-11 stsp {
1263 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1264 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1265 b7422a2f 2020-07-23 stsp char canonpath[PATH_MAX];
1266 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1267 906c123b 2020-07-23 stsp char *path_got = NULL;
1268 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1270 8ba819a3 2020-07-23 stsp
1271 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1272 2e1fa222 2020-07-23 stsp
1273 8ba819a3 2020-07-23 stsp /*
1274 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1275 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1276 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1277 8ba819a3 2020-07-23 stsp * in the blob object.
1278 8ba819a3 2020-07-23 stsp */
1279 8ba819a3 2020-07-23 stsp do {
1280 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1281 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1282 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1283 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1284 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1285 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1286 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1287 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1288 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1289 3b9f0f87 2020-07-23 stsp progress_arg);
1290 8ba819a3 2020-07-23 stsp }
1291 8ba819a3 2020-07-23 stsp if (len > 0) {
1292 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1293 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1294 8ba819a3 2020-07-23 stsp len - hdrlen);
1295 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1296 8ba819a3 2020-07-23 stsp hdrlen = 0;
1297 8ba819a3 2020-07-23 stsp }
1298 8ba819a3 2020-07-23 stsp } while (len != 0);
1299 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1300 8ba819a3 2020-07-23 stsp
1301 8ba819a3 2020-07-23 stsp /*
1302 b7422a2f 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target path
1303 b7422a2f 2020-07-23 stsp * because we don't want to resolve symlinks recursively. Instead
1304 b7422a2f 2020-07-23 stsp * we make the path absolute and then canonicalize it.
1305 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1306 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1307 8ba819a3 2020-07-23 stsp */
1308 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1309 b7422a2f 2020-07-23 stsp char *abspath;
1310 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1311 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1312 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1313 ef68ca6f 2020-07-23 stsp goto done;
1314 ef68ca6f 2020-07-23 stsp }
1315 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1316 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1317 8ba819a3 2020-07-23 stsp goto done;
1318 8ba819a3 2020-07-23 stsp }
1319 b7422a2f 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1320 b7422a2f 2020-07-23 stsp free(abspath);
1321 b7422a2f 2020-07-23 stsp if (err)
1322 b15bc87b 2020-07-23 stsp goto done;
1323 b7422a2f 2020-07-23 stsp } else {
1324 b7422a2f 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1325 b7422a2f 2020-07-23 stsp if (err)
1326 b7422a2f 2020-07-23 stsp goto done;
1327 8ba819a3 2020-07-23 stsp }
1328 8ba819a3 2020-07-23 stsp
1329 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1330 b7422a2f 2020-07-23 stsp if (!got_path_is_child(canonpath, worktree->root_path,
1331 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1332 8ba819a3 2020-07-23 stsp /* install as a regular file */
1333 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1334 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1335 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1336 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1337 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1338 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1339 8ba819a3 2020-07-23 stsp goto done;
1340 8ba819a3 2020-07-23 stsp }
1341 8ba819a3 2020-07-23 stsp
1342 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1343 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1344 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1345 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1346 906c123b 2020-07-23 stsp goto done;
1347 906c123b 2020-07-23 stsp }
1348 b7422a2f 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got))) {
1349 906c123b 2020-07-23 stsp /* install as a regular file */
1350 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1351 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1352 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1353 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1354 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1355 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1356 906c123b 2020-07-23 stsp goto done;
1357 906c123b 2020-07-23 stsp }
1358 906c123b 2020-07-23 stsp
1359 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1360 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1361 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1362 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1363 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1364 c90c8ce3 2020-07-23 stsp goto done;
1365 c90c8ce3 2020-07-23 stsp }
1366 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1367 5a1fbc73 2020-07-23 stsp target_path, target_len);
1368 5a1fbc73 2020-07-23 stsp if (err)
1369 f35fa46a 2020-07-23 stsp goto done;
1370 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1371 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1372 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1373 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1374 6e1eade5 2020-07-23 stsp path);
1375 f35fa46a 2020-07-23 stsp }
1376 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1377 f35fa46a 2020-07-23 stsp }
1378 f35fa46a 2020-07-23 stsp
1379 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1380 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1381 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1382 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1383 f35fa46a 2020-07-23 stsp ondisk_path);
1384 f35fa46a 2020-07-23 stsp goto done;
1385 f35fa46a 2020-07-23 stsp }
1386 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1387 f35fa46a 2020-07-23 stsp if (err)
1388 f35fa46a 2020-07-23 stsp goto done;
1389 f35fa46a 2020-07-23 stsp /*
1390 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1391 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1392 f35fa46a 2020-07-23 stsp */
1393 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1394 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1395 f35fa46a 2020-07-23 stsp goto done;
1396 f35fa46a 2020-07-23 stsp }
1397 f35fa46a 2020-07-23 stsp }
1398 f35fa46a 2020-07-23 stsp
1399 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1400 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1401 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1402 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1403 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1404 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1405 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1406 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1407 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1408 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1409 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1410 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1411 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1412 8ba819a3 2020-07-23 stsp } else {
1413 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1414 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1415 8ba819a3 2020-07-23 stsp }
1416 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1417 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1418 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1419 8ba819a3 2020-07-23 stsp done:
1420 906c123b 2020-07-23 stsp free(path_got);
1421 8ba819a3 2020-07-23 stsp return err;
1422 8ba819a3 2020-07-23 stsp }
1423 8ba819a3 2020-07-23 stsp
1424 8ba819a3 2020-07-23 stsp static const struct got_error *
1425 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1426 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1427 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1428 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1429 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1430 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1431 8ba819a3 2020-07-23 stsp {
1432 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1433 507dc3bb 2018-12-29 stsp int fd = -1;
1434 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1435 507dc3bb 2018-12-29 stsp int update = 0;
1436 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1437 8ba819a3 2020-07-23 stsp
1438 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1440 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1441 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1442 21908da4 2019-01-13 stsp char *parent = dirname(path);
1443 21908da4 2019-01-13 stsp if (parent == NULL)
1444 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1445 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1446 21908da4 2019-01-13 stsp if (err)
1447 21908da4 2019-01-13 stsp return err;
1448 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1449 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1450 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1451 21908da4 2019-01-13 stsp if (fd == -1)
1452 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1453 230a42bd 2019-05-11 jcs ondisk_path);
1454 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1455 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1456 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1457 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1458 3b9f0f87 2020-07-23 stsp goto done;
1459 3b9f0f87 2020-07-23 stsp }
1460 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1461 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1462 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1463 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1464 507dc3bb 2018-12-29 stsp goto done;
1465 d70b8e30 2018-12-27 stsp } else {
1466 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1467 507dc3bb 2018-12-29 stsp ondisk_path);
1468 507dc3bb 2018-12-29 stsp if (err)
1469 507dc3bb 2018-12-29 stsp goto done;
1470 507dc3bb 2018-12-29 stsp update = 1;
1471 9d31a1d8 2018-03-11 stsp }
1472 507dc3bb 2018-12-29 stsp } else
1473 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1474 9d31a1d8 2018-03-11 stsp }
1475 9d31a1d8 2018-03-11 stsp
1476 bd6aa359 2020-07-23 stsp if (progress_cb) {
1477 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1478 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1479 bd6aa359 2020-07-23 stsp path);
1480 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1481 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 bd6aa359 2020-07-23 stsp path);
1483 bd6aa359 2020-07-23 stsp else
1484 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1485 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1486 bd6aa359 2020-07-23 stsp if (err)
1487 bd6aa359 2020-07-23 stsp goto done;
1488 bd6aa359 2020-07-23 stsp }
1489 d7b62c98 2018-12-27 stsp
1490 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1491 9d31a1d8 2018-03-11 stsp do {
1492 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1493 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1494 9d31a1d8 2018-03-11 stsp if (err)
1495 9d31a1d8 2018-03-11 stsp break;
1496 9d31a1d8 2018-03-11 stsp if (len > 0) {
1497 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1498 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1499 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1501 b87c6f83 2018-12-24 stsp goto done;
1502 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1503 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1504 b87c6f83 2018-12-24 stsp goto done;
1505 9d31a1d8 2018-03-11 stsp }
1506 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1507 9d31a1d8 2018-03-11 stsp }
1508 9d31a1d8 2018-03-11 stsp } while (len != 0);
1509 9d31a1d8 2018-03-11 stsp
1510 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1511 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1512 816dc654 2019-02-16 stsp goto done;
1513 816dc654 2019-02-16 stsp }
1514 9d31a1d8 2018-03-11 stsp
1515 507dc3bb 2018-12-29 stsp if (update) {
1516 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1517 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1518 230a42bd 2019-05-11 jcs ondisk_path);
1519 2a57020b 2019-02-20 stsp unlink(tmppath);
1520 68ed9ba5 2019-02-10 stsp goto done;
1521 68ed9ba5 2019-02-10 stsp }
1522 68ed9ba5 2019-02-10 stsp }
1523 ba8a0d4d 2019-02-10 stsp
1524 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1525 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1526 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1527 1ebedb77 2019-10-19 stsp goto done;
1528 507dc3bb 2018-12-29 stsp }
1529 507dc3bb 2018-12-29 stsp
1530 9d31a1d8 2018-03-11 stsp done:
1531 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1532 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1533 507dc3bb 2018-12-29 stsp free(tmppath);
1534 6353ad76 2019-02-08 stsp return err;
1535 6353ad76 2019-02-08 stsp }
1536 6353ad76 2019-02-08 stsp
1537 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1538 6353ad76 2019-02-08 stsp static const struct got_error *
1539 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1540 7154f6ce 2019-03-27 stsp {
1541 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1542 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1543 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1544 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1545 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1546 7154f6ce 2019-03-27 stsp };
1547 7154f6ce 2019-03-27 stsp int i = 0;
1548 7154f6ce 2019-03-27 stsp char *line;
1549 7154f6ce 2019-03-27 stsp size_t len;
1550 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1551 7154f6ce 2019-03-27 stsp
1552 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1553 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1554 7154f6ce 2019-03-27 stsp if (line == NULL) {
1555 7154f6ce 2019-03-27 stsp if (feof(f))
1556 7154f6ce 2019-03-27 stsp break;
1557 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1558 7154f6ce 2019-03-27 stsp break;
1559 7154f6ce 2019-03-27 stsp }
1560 7154f6ce 2019-03-27 stsp
1561 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1562 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1563 19332e6d 2019-05-13 stsp == 0)
1564 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1565 7154f6ce 2019-03-27 stsp else
1566 7154f6ce 2019-03-27 stsp i++;
1567 7154f6ce 2019-03-27 stsp }
1568 7154f6ce 2019-03-27 stsp }
1569 7154f6ce 2019-03-27 stsp
1570 7154f6ce 2019-03-27 stsp return err;
1571 e2b1e152 2019-07-13 stsp }
1572 e2b1e152 2019-07-13 stsp
1573 e2b1e152 2019-07-13 stsp static int
1574 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1575 1ebedb77 2019-10-19 stsp {
1576 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1577 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1578 1ebedb77 2019-10-19 stsp }
1579 1ebedb77 2019-10-19 stsp
1580 1ebedb77 2019-10-19 stsp static int
1581 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1582 e2b1e152 2019-07-13 stsp {
1583 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1584 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1585 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1586 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1587 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1588 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1589 c363b2c1 2019-08-03 stsp }
1590 c363b2c1 2019-08-03 stsp
1591 c363b2c1 2019-08-03 stsp static unsigned char
1592 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1593 c363b2c1 2019-08-03 stsp {
1594 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1595 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1596 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1597 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1598 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1599 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1600 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1601 c363b2c1 2019-08-03 stsp default:
1602 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1603 a919d5c4 2020-07-23 stsp }
1604 a919d5c4 2020-07-23 stsp }
1605 a919d5c4 2020-07-23 stsp
1606 a919d5c4 2020-07-23 stsp static const struct got_error *
1607 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1608 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1609 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1610 a919d5c4 2020-07-23 stsp {
1611 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1612 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1613 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1614 a919d5c4 2020-07-23 stsp ssize_t elen;
1615 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1616 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1617 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1618 a919d5c4 2020-07-23 stsp
1619 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1620 a919d5c4 2020-07-23 stsp
1621 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1622 a919d5c4 2020-07-23 stsp do {
1623 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1624 a919d5c4 2020-07-23 stsp if (err)
1625 a919d5c4 2020-07-23 stsp return err;
1626 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1627 a919d5c4 2020-07-23 stsp /*
1628 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1629 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1630 a919d5c4 2020-07-23 stsp */
1631 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1632 a919d5c4 2020-07-23 stsp }
1633 a919d5c4 2020-07-23 stsp if (len > 0) {
1634 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1635 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1636 a919d5c4 2020-07-23 stsp len - hdrlen);
1637 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1638 a919d5c4 2020-07-23 stsp hdrlen = 0;
1639 a919d5c4 2020-07-23 stsp }
1640 a919d5c4 2020-07-23 stsp } while (len != 0);
1641 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1642 a919d5c4 2020-07-23 stsp
1643 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1644 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1645 a919d5c4 2020-07-23 stsp if (elen == -1)
1646 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1647 a919d5c4 2020-07-23 stsp } else {
1648 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1649 a919d5c4 2020-07-23 stsp if (elen == -1)
1650 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1651 c363b2c1 2019-08-03 stsp }
1652 a919d5c4 2020-07-23 stsp
1653 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1654 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1655 a919d5c4 2020-07-23 stsp
1656 a919d5c4 2020-07-23 stsp return NULL;
1657 7154f6ce 2019-03-27 stsp }
1658 7154f6ce 2019-03-27 stsp
1659 7154f6ce 2019-03-27 stsp static const struct got_error *
1660 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1661 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1662 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1663 6353ad76 2019-02-08 stsp {
1664 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1665 6353ad76 2019-02-08 stsp struct got_object_id id;
1666 6353ad76 2019-02-08 stsp size_t hdrlen;
1667 1338848f 2019-12-13 stsp int fd = -1;
1668 6353ad76 2019-02-08 stsp FILE *f = NULL;
1669 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1670 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1671 6353ad76 2019-02-08 stsp size_t flen, blen;
1672 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1673 6353ad76 2019-02-08 stsp
1674 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1675 6353ad76 2019-02-08 stsp
1676 7f91a133 2019-12-13 stsp /*
1677 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1678 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1679 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1680 7f91a133 2019-12-13 stsp */
1681 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1682 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1683 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1684 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1685 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1686 882ef1b9 2019-12-13 stsp else
1687 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1688 882ef1b9 2019-12-13 stsp goto done;
1689 882ef1b9 2019-12-13 stsp }
1690 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1691 3d35a492 2019-12-13 stsp goto done;
1692 3d35a492 2019-12-13 stsp }
1693 7f91a133 2019-12-13 stsp } else {
1694 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1695 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1696 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1697 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1698 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1699 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1700 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1701 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1702 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1703 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1704 3d35a492 2019-12-13 stsp else
1705 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1706 3d35a492 2019-12-13 stsp goto done;
1707 3d35a492 2019-12-13 stsp }
1708 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1709 1338848f 2019-12-13 stsp goto done;
1710 a378724f 2019-02-10 stsp }
1711 a378724f 2019-02-10 stsp }
1712 6353ad76 2019-02-08 stsp
1713 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1714 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1715 1338848f 2019-12-13 stsp goto done;
1716 3f148bc6 2019-07-27 stsp }
1717 efdd40df 2019-07-27 stsp
1718 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1719 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1720 1338848f 2019-12-13 stsp goto done;
1721 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1722 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1723 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1724 1338848f 2019-12-13 stsp goto done;
1725 d00136be 2019-03-26 stsp }
1726 b8f41171 2019-02-10 stsp
1727 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1728 1338848f 2019-12-13 stsp goto done;
1729 6353ad76 2019-02-08 stsp
1730 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1731 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1732 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1733 c363b2c1 2019-08-03 stsp else
1734 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1735 c363b2c1 2019-08-03 stsp
1736 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1737 6353ad76 2019-02-08 stsp if (err)
1738 1338848f 2019-12-13 stsp goto done;
1739 6353ad76 2019-02-08 stsp
1740 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1741 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1742 a919d5c4 2020-07-23 stsp de_name, blob);
1743 a919d5c4 2020-07-23 stsp goto done;
1744 a919d5c4 2020-07-23 stsp }
1745 a919d5c4 2020-07-23 stsp
1746 a919d5c4 2020-07-23 stsp
1747 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1748 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1749 ab0d4361 2019-12-13 stsp if (fd == -1) {
1750 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1751 ab0d4361 2019-12-13 stsp goto done;
1752 ab0d4361 2019-12-13 stsp }
1753 3d35a492 2019-12-13 stsp }
1754 3d35a492 2019-12-13 stsp
1755 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1756 6353ad76 2019-02-08 stsp if (f == NULL) {
1757 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1758 6353ad76 2019-02-08 stsp goto done;
1759 6353ad76 2019-02-08 stsp }
1760 1338848f 2019-12-13 stsp fd = -1;
1761 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1762 656b1f76 2019-05-11 jcs for (;;) {
1763 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1764 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1765 6353ad76 2019-02-08 stsp if (err)
1766 7154f6ce 2019-03-27 stsp goto done;
1767 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1768 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1769 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1770 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1771 7154f6ce 2019-03-27 stsp goto done;
1772 80c5c120 2019-02-19 stsp }
1773 6353ad76 2019-02-08 stsp if (blen == 0) {
1774 6353ad76 2019-02-08 stsp if (flen != 0)
1775 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1776 6353ad76 2019-02-08 stsp break;
1777 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1778 6353ad76 2019-02-08 stsp if (blen != 0)
1779 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1780 6353ad76 2019-02-08 stsp break;
1781 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1782 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1783 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1784 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1785 6353ad76 2019-02-08 stsp break;
1786 6353ad76 2019-02-08 stsp }
1787 6353ad76 2019-02-08 stsp } else {
1788 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1789 6353ad76 2019-02-08 stsp break;
1790 6353ad76 2019-02-08 stsp }
1791 6353ad76 2019-02-08 stsp hdrlen = 0;
1792 6353ad76 2019-02-08 stsp }
1793 7154f6ce 2019-03-27 stsp
1794 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1795 7154f6ce 2019-03-27 stsp rewind(f);
1796 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1797 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1798 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1799 6353ad76 2019-02-08 stsp done:
1800 6353ad76 2019-02-08 stsp if (blob)
1801 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1802 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1803 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1804 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1805 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1806 9d31a1d8 2018-03-11 stsp return err;
1807 e2b1e152 2019-07-13 stsp }
1808 e2b1e152 2019-07-13 stsp
1809 e2b1e152 2019-07-13 stsp /*
1810 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1811 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1812 e2b1e152 2019-07-13 stsp */
1813 e2b1e152 2019-07-13 stsp static const struct got_error *
1814 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1815 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1816 e2b1e152 2019-07-13 stsp {
1817 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1818 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1819 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1820 e2b1e152 2019-07-13 stsp
1821 e2b1e152 2019-07-13 stsp return NULL;
1822 9d31a1d8 2018-03-11 stsp }
1823 9d31a1d8 2018-03-11 stsp
1824 9d31a1d8 2018-03-11 stsp static const struct got_error *
1825 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1826 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1827 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1828 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1829 0584f854 2019-04-06 stsp void *progress_arg)
1830 9d31a1d8 2018-03-11 stsp {
1831 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1832 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1833 6353ad76 2019-02-08 stsp char *ondisk_path;
1834 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1835 b8f41171 2019-02-10 stsp struct stat sb;
1836 9d31a1d8 2018-03-11 stsp
1837 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1838 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1839 6353ad76 2019-02-08 stsp
1840 abb4604f 2019-07-27 stsp if (ie) {
1841 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1842 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1843 a76c42e6 2019-08-03 stsp goto done;
1844 a76c42e6 2019-08-03 stsp }
1845 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1846 7f91a133 2019-12-13 stsp repo);
1847 abb4604f 2019-07-27 stsp if (err)
1848 abb4604f 2019-07-27 stsp goto done;
1849 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1850 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1851 c90c8ce3 2020-07-23 stsp } else {
1852 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1854 c90c8ce3 2020-07-23 stsp }
1855 6353ad76 2019-02-08 stsp
1856 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1857 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1858 b8f41171 2019-02-10 stsp goto done;
1859 b8f41171 2019-02-10 stsp }
1860 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1861 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1862 5036ab18 2020-04-18 stsp path);
1863 5036ab18 2020-04-18 stsp goto done;
1864 5036ab18 2020-04-18 stsp }
1865 b8f41171 2019-02-10 stsp
1866 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1867 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1868 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1869 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1871 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1872 e2b1e152 2019-07-13 stsp if (err)
1873 e2b1e152 2019-07-13 stsp goto done;
1874 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1875 b8f41171 2019-02-10 stsp path);
1876 6353ad76 2019-02-08 stsp goto done;
1877 a378724f 2019-02-10 stsp }
1878 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1879 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1880 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1881 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1882 b8f41171 2019-02-10 stsp goto done;
1883 e2b1e152 2019-07-13 stsp }
1884 9d31a1d8 2018-03-11 stsp }
1885 9d31a1d8 2018-03-11 stsp
1886 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 8da9e5f4 2019-01-12 stsp if (err)
1888 6353ad76 2019-02-08 stsp goto done;
1889 512f0d0e 2019-01-02 stsp
1890 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 234035bc 2019-06-01 stsp int update_timestamps;
1892 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1893 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1894 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1895 234035bc 2019-06-01 stsp struct got_object_id id2;
1896 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 234035bc 2019-06-01 stsp if (err)
1899 f69721c3 2019-10-21 stsp goto done;
1900 f69721c3 2019-10-21 stsp }
1901 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1902 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1905 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1906 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1907 f69721c3 2019-10-21 stsp goto done;
1908 f69721c3 2019-10-21 stsp }
1909 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1910 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1912 234035bc 2019-06-01 stsp goto done;
1913 f69721c3 2019-10-21 stsp }
1914 234035bc 2019-06-01 stsp }
1915 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1916 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1917 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1918 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1919 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1920 993e2a1b 2020-07-23 stsp } else {
1921 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1922 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1923 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1924 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1925 993e2a1b 2020-07-23 stsp }
1926 f69721c3 2019-10-21 stsp free(label_orig);
1927 234035bc 2019-06-01 stsp if (blob2)
1928 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1929 909d120e 2019-09-22 stsp if (err)
1930 909d120e 2019-09-22 stsp goto done;
1931 234035bc 2019-06-01 stsp /*
1932 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1933 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1934 234035bc 2019-06-01 stsp * unmodified files again.
1935 234035bc 2019-06-01 stsp */
1936 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1937 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1938 234035bc 2019-06-01 stsp update_timestamps);
1939 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1940 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1941 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1942 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1943 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1944 1ee397ad 2019-07-12 stsp if (err)
1945 1ee397ad 2019-07-12 stsp goto done;
1946 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1947 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1948 13d9040b 2019-03-27 stsp if (err)
1949 13d9040b 2019-03-27 stsp goto done;
1950 13d9040b 2019-03-27 stsp } else {
1951 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1952 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1953 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1954 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1955 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1956 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1957 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1958 2e1fa222 2020-07-23 stsp } else {
1959 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1960 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1961 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1962 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1963 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1964 2e1fa222 2020-07-23 stsp }
1965 13d9040b 2019-03-27 stsp if (err)
1966 13d9040b 2019-03-27 stsp goto done;
1967 65b05cec 2020-07-23 stsp
1968 054041d0 2020-06-24 stsp if (ie) {
1969 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1970 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1971 054041d0 2020-06-24 stsp } else {
1972 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1973 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1974 054041d0 2020-06-24 stsp &blob->id);
1975 054041d0 2020-06-24 stsp }
1976 13d9040b 2019-03-27 stsp if (err)
1977 13d9040b 2019-03-27 stsp goto done;
1978 65b05cec 2020-07-23 stsp
1979 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1980 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1981 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 2e1fa222 2020-07-23 stsp }
1983 13d9040b 2019-03-27 stsp }
1984 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1985 6353ad76 2019-02-08 stsp done:
1986 6353ad76 2019-02-08 stsp free(ondisk_path);
1987 8da9e5f4 2019-01-12 stsp return err;
1988 90285c3b 2019-01-08 stsp }
1989 90285c3b 2019-01-08 stsp
1990 90285c3b 2019-01-08 stsp static const struct got_error *
1991 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1992 90285c3b 2019-01-08 stsp {
1993 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1994 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1995 90285c3b 2019-01-08 stsp
1996 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1997 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1998 90285c3b 2019-01-08 stsp
1999 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2000 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2001 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2002 c97665ae 2019-01-13 stsp } else {
2003 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2004 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2005 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2006 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2007 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2008 230a42bd 2019-05-11 jcs parent);
2009 90285c3b 2019-01-08 stsp break;
2010 90285c3b 2019-01-08 stsp }
2011 90285c3b 2019-01-08 stsp parent = dirname(parent);
2012 90285c3b 2019-01-08 stsp }
2013 90285c3b 2019-01-08 stsp }
2014 90285c3b 2019-01-08 stsp free(ondisk_path);
2015 90285c3b 2019-01-08 stsp return err;
2016 512f0d0e 2019-01-02 stsp }
2017 512f0d0e 2019-01-02 stsp
2018 708d8e67 2019-03-27 stsp static const struct got_error *
2019 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2020 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2021 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2022 708d8e67 2019-03-27 stsp {
2023 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2024 708d8e67 2019-03-27 stsp unsigned char status;
2025 708d8e67 2019-03-27 stsp struct stat sb;
2026 708d8e67 2019-03-27 stsp char *ondisk_path;
2027 708d8e67 2019-03-27 stsp
2028 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2029 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2030 a76c42e6 2019-08-03 stsp
2031 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2032 708d8e67 2019-03-27 stsp == -1)
2033 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2034 708d8e67 2019-03-27 stsp
2035 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2036 708d8e67 2019-03-27 stsp if (err)
2037 5a58a424 2020-06-23 stsp goto done;
2038 708d8e67 2019-03-27 stsp
2039 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2040 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2041 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2042 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2043 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2044 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2045 993e2a1b 2020-07-23 stsp goto done;
2046 993e2a1b 2020-07-23 stsp }
2047 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2048 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2049 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2050 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2051 993e2a1b 2020-07-23 stsp if (err)
2052 993e2a1b 2020-07-23 stsp goto done;
2053 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2054 993e2a1b 2020-07-23 stsp ie->path);
2055 993e2a1b 2020-07-23 stsp goto done;
2056 993e2a1b 2020-07-23 stsp }
2057 993e2a1b 2020-07-23 stsp
2058 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2059 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2060 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2061 1ee397ad 2019-07-12 stsp if (err)
2062 5a58a424 2020-06-23 stsp goto done;
2063 fc6346c4 2019-03-27 stsp /*
2064 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2065 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2066 fc6346c4 2019-03-27 stsp */
2067 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2068 fc6346c4 2019-03-27 stsp 0);
2069 fc6346c4 2019-03-27 stsp } else {
2070 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2071 1ee397ad 2019-07-12 stsp if (err)
2072 5a58a424 2020-06-23 stsp goto done;
2073 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2074 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2075 fc6346c4 2019-03-27 stsp if (err)
2076 5a58a424 2020-06-23 stsp goto done;
2077 fc6346c4 2019-03-27 stsp }
2078 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2079 708d8e67 2019-03-27 stsp }
2080 5a58a424 2020-06-23 stsp done:
2081 5a58a424 2020-06-23 stsp free(ondisk_path);
2082 fc6346c4 2019-03-27 stsp return err;
2083 708d8e67 2019-03-27 stsp }
2084 708d8e67 2019-03-27 stsp
2085 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2086 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2087 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2088 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2089 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2090 8da9e5f4 2019-01-12 stsp void *progress_arg;
2091 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2092 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2093 8da9e5f4 2019-01-12 stsp };
2094 8da9e5f4 2019-01-12 stsp
2095 512f0d0e 2019-01-02 stsp static const struct got_error *
2096 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2097 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2098 512f0d0e 2019-01-02 stsp {
2099 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2100 0584f854 2019-04-06 stsp
2101 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2102 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2103 512f0d0e 2019-01-02 stsp
2104 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2105 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2106 8da9e5f4 2019-01-12 stsp }
2107 512f0d0e 2019-01-02 stsp
2108 8da9e5f4 2019-01-12 stsp static const struct got_error *
2109 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2110 8da9e5f4 2019-01-12 stsp {
2111 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2112 7a9df742 2019-01-08 stsp
2113 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2114 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2115 0584f854 2019-04-06 stsp
2116 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2117 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2118 9d31a1d8 2018-03-11 stsp }
2119 9d31a1d8 2018-03-11 stsp
2120 9d31a1d8 2018-03-11 stsp static const struct got_error *
2121 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2122 9d31a1d8 2018-03-11 stsp {
2123 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2124 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2125 8da9e5f4 2019-01-12 stsp char *path;
2126 9d31a1d8 2018-03-11 stsp
2127 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2128 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2129 0584f854 2019-04-06 stsp
2130 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2131 63c5ca5d 2019-08-24 stsp return NULL;
2132 63c5ca5d 2019-08-24 stsp
2133 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2134 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2135 8da9e5f4 2019-01-12 stsp == -1)
2136 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2137 9d31a1d8 2018-03-11 stsp
2138 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2139 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2140 8da9e5f4 2019-01-12 stsp else
2141 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2142 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2143 9d31a1d8 2018-03-11 stsp
2144 8da9e5f4 2019-01-12 stsp free(path);
2145 0cd1c46a 2019-03-11 stsp return err;
2146 0cd1c46a 2019-03-11 stsp }
2147 0cd1c46a 2019-03-11 stsp
2148 818c7501 2019-07-11 stsp static const struct got_error *
2149 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2150 0cd1c46a 2019-03-11 stsp {
2151 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2152 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2153 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2154 0cd1c46a 2019-03-11 stsp
2155 517bab73 2019-03-11 stsp *refname = NULL;
2156 517bab73 2019-03-11 stsp
2157 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2158 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2159 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2160 0cd1c46a 2019-03-11 stsp
2161 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2162 0647c563 2019-03-11 stsp == -1) {
2163 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2164 0647c563 2019-03-11 stsp *refname = NULL;
2165 0cd1c46a 2019-03-11 stsp }
2166 517bab73 2019-03-11 stsp free(uuidstr);
2167 517bab73 2019-03-11 stsp return err;
2168 517bab73 2019-03-11 stsp }
2169 517bab73 2019-03-11 stsp
2170 818c7501 2019-07-11 stsp const struct got_error *
2171 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2172 818c7501 2019-07-11 stsp {
2173 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2174 818c7501 2019-07-11 stsp }
2175 818c7501 2019-07-11 stsp
2176 818c7501 2019-07-11 stsp static const struct got_error *
2177 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2178 818c7501 2019-07-11 stsp {
2179 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2180 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2181 818c7501 2019-07-11 stsp }
2182 818c7501 2019-07-11 stsp
2183 818c7501 2019-07-11 stsp static const struct got_error *
2184 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2185 818c7501 2019-07-11 stsp {
2186 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2187 818c7501 2019-07-11 stsp }
2188 818c7501 2019-07-11 stsp
2189 818c7501 2019-07-11 stsp static const struct got_error *
2190 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2191 818c7501 2019-07-11 stsp {
2192 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2193 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2194 818c7501 2019-07-11 stsp }
2195 818c7501 2019-07-11 stsp
2196 818c7501 2019-07-11 stsp static const struct got_error *
2197 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2198 818c7501 2019-07-11 stsp {
2199 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2200 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2201 0ebf8283 2019-07-24 stsp }
2202 0ebf8283 2019-07-24 stsp
2203 0ebf8283 2019-07-24 stsp static const struct got_error *
2204 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2205 0ebf8283 2019-07-24 stsp {
2206 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2207 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2208 0ebf8283 2019-07-24 stsp }
2209 0ebf8283 2019-07-24 stsp
2210 0ebf8283 2019-07-24 stsp static const struct got_error *
2211 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2212 0ebf8283 2019-07-24 stsp {
2213 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2214 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2215 0ebf8283 2019-07-24 stsp }
2216 0ebf8283 2019-07-24 stsp
2217 0ebf8283 2019-07-24 stsp static const struct got_error *
2218 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2219 0ebf8283 2019-07-24 stsp {
2220 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2221 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2222 818c7501 2019-07-11 stsp }
2223 818c7501 2019-07-11 stsp
2224 0ebf8283 2019-07-24 stsp static const struct got_error *
2225 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2226 0ebf8283 2019-07-24 stsp {
2227 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2228 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2229 0ebf8283 2019-07-24 stsp }
2230 818c7501 2019-07-11 stsp
2231 0ebf8283 2019-07-24 stsp const struct got_error *
2232 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2233 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2234 0ebf8283 2019-07-24 stsp {
2235 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2236 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2237 0ebf8283 2019-07-24 stsp *path = NULL;
2238 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2239 0ebf8283 2019-07-24 stsp }
2240 0ebf8283 2019-07-24 stsp return NULL;
2241 0ebf8283 2019-07-24 stsp }
2242 0ebf8283 2019-07-24 stsp
2243 517bab73 2019-03-11 stsp /*
2244 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2245 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2246 517bab73 2019-03-11 stsp */
2247 517bab73 2019-03-11 stsp static const struct got_error *
2248 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2249 517bab73 2019-03-11 stsp {
2250 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2251 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2252 517bab73 2019-03-11 stsp char *refname;
2253 517bab73 2019-03-11 stsp
2254 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2255 517bab73 2019-03-11 stsp if (err)
2256 517bab73 2019-03-11 stsp return err;
2257 0cd1c46a 2019-03-11 stsp
2258 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2259 0cd1c46a 2019-03-11 stsp if (err)
2260 0cd1c46a 2019-03-11 stsp goto done;
2261 0cd1c46a 2019-03-11 stsp
2262 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2263 0cd1c46a 2019-03-11 stsp done:
2264 0cd1c46a 2019-03-11 stsp free(refname);
2265 0cd1c46a 2019-03-11 stsp if (ref)
2266 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2267 3e3a69f1 2019-07-25 stsp return err;
2268 3e3a69f1 2019-07-25 stsp }
2269 3e3a69f1 2019-07-25 stsp
2270 3e3a69f1 2019-07-25 stsp static const struct got_error *
2271 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2272 3e3a69f1 2019-07-25 stsp {
2273 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2274 3e3a69f1 2019-07-25 stsp
2275 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2276 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2277 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2278 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2279 3e3a69f1 2019-07-25 stsp }
2280 9d31a1d8 2018-03-11 stsp return err;
2281 9d31a1d8 2018-03-11 stsp }
2282 9d31a1d8 2018-03-11 stsp
2283 3e3a69f1 2019-07-25 stsp
2284 ebf99748 2019-05-09 stsp static const struct got_error *
2285 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2286 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2287 ebf99748 2019-05-09 stsp {
2288 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2289 ebf99748 2019-05-09 stsp FILE *index = NULL;
2290 0cd1c46a 2019-03-11 stsp
2291 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2292 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2293 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2294 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2295 ebf99748 2019-05-09 stsp
2296 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2297 3e3a69f1 2019-07-25 stsp if (err)
2298 ebf99748 2019-05-09 stsp goto done;
2299 ebf99748 2019-05-09 stsp
2300 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2301 ebf99748 2019-05-09 stsp if (index == NULL) {
2302 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2303 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2304 ebf99748 2019-05-09 stsp } else {
2305 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2306 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2307 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2308 ebf99748 2019-05-09 stsp }
2309 ebf99748 2019-05-09 stsp done:
2310 ebf99748 2019-05-09 stsp if (err) {
2311 ebf99748 2019-05-09 stsp free(*fileindex_path);
2312 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2313 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2314 ebf99748 2019-05-09 stsp *fileindex = NULL;
2315 ebf99748 2019-05-09 stsp }
2316 ebf99748 2019-05-09 stsp return err;
2317 ebf99748 2019-05-09 stsp }
2318 c932eeeb 2019-05-22 stsp
2319 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2320 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2321 c932eeeb 2019-05-22 stsp const char *path;
2322 c932eeeb 2019-05-22 stsp size_t path_len;
2323 c932eeeb 2019-05-22 stsp const char *entry_name;
2324 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2325 a484d721 2019-06-10 stsp void *progress_arg;
2326 c932eeeb 2019-05-22 stsp };
2327 ebf99748 2019-05-09 stsp
2328 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2329 c932eeeb 2019-05-22 stsp static const struct got_error *
2330 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2331 c932eeeb 2019-05-22 stsp {
2332 1ee397ad 2019-07-12 stsp const struct got_error *err;
2333 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2334 c932eeeb 2019-05-22 stsp
2335 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2336 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2337 c932eeeb 2019-05-22 stsp return NULL;
2338 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2339 102ff934 2019-06-11 stsp return NULL;
2340 102ff934 2019-06-11 stsp
2341 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2342 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2343 c932eeeb 2019-05-22 stsp return NULL;
2344 c932eeeb 2019-05-22 stsp
2345 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2346 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2347 edd02c5e 2019-07-11 stsp ie->path);
2348 1ee397ad 2019-07-12 stsp if (err)
2349 1ee397ad 2019-07-12 stsp return err;
2350 1ee397ad 2019-07-12 stsp }
2351 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2352 c932eeeb 2019-05-22 stsp return NULL;
2353 9c6338c4 2019-06-02 stsp }
2354 9c6338c4 2019-06-02 stsp
2355 9c6338c4 2019-06-02 stsp static const struct got_error *
2356 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2357 9c6338c4 2019-06-02 stsp {
2358 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2359 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2360 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2361 867630bb 2020-01-17 stsp struct timespec timeout;
2362 9c6338c4 2019-06-02 stsp
2363 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2364 9c6338c4 2019-06-02 stsp fileindex_path);
2365 9c6338c4 2019-06-02 stsp if (err)
2366 9c6338c4 2019-06-02 stsp goto done;
2367 9c6338c4 2019-06-02 stsp
2368 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2369 9c6338c4 2019-06-02 stsp if (err)
2370 9c6338c4 2019-06-02 stsp goto done;
2371 9c6338c4 2019-06-02 stsp
2372 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2373 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2374 9c6338c4 2019-06-02 stsp fileindex_path);
2375 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2376 9c6338c4 2019-06-02 stsp }
2377 867630bb 2020-01-17 stsp
2378 867630bb 2020-01-17 stsp /*
2379 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2380 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2381 867630bb 2020-01-17 stsp * was recorded in the file index.
2382 867630bb 2020-01-17 stsp */
2383 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2384 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2385 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2386 9c6338c4 2019-06-02 stsp done:
2387 9c6338c4 2019-06-02 stsp if (new_index)
2388 9c6338c4 2019-06-02 stsp fclose(new_index);
2389 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2390 9c6338c4 2019-06-02 stsp return err;
2391 c932eeeb 2019-05-22 stsp }
2392 6ced4176 2019-07-12 stsp
2393 6ced4176 2019-07-12 stsp static const struct got_error *
2394 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2395 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2396 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2397 6ced4176 2019-07-12 stsp {
2398 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2399 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2400 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2401 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2402 6ced4176 2019-07-12 stsp
2403 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2404 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2405 6ced4176 2019-07-12 stsp *tree_id = NULL;
2406 6ced4176 2019-07-12 stsp
2407 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2408 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2409 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2410 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2411 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2412 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2413 6ced4176 2019-07-12 stsp goto done;
2414 6ced4176 2019-07-12 stsp }
2415 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2416 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2417 6ced4176 2019-07-12 stsp if (err)
2418 6ced4176 2019-07-12 stsp goto done;
2419 6ced4176 2019-07-12 stsp return NULL;
2420 6ced4176 2019-07-12 stsp }
2421 6ced4176 2019-07-12 stsp
2422 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2423 6ced4176 2019-07-12 stsp
2424 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2425 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2426 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2427 6ced4176 2019-07-12 stsp goto done;
2428 6ced4176 2019-07-12 stsp }
2429 6ced4176 2019-07-12 stsp
2430 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2431 6ced4176 2019-07-12 stsp in_repo_path);
2432 6ced4176 2019-07-12 stsp if (err)
2433 6ced4176 2019-07-12 stsp goto done;
2434 6ced4176 2019-07-12 stsp
2435 6ced4176 2019-07-12 stsp free(in_repo_path);
2436 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2437 6ced4176 2019-07-12 stsp
2438 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2439 6ced4176 2019-07-12 stsp if (err)
2440 6ced4176 2019-07-12 stsp goto done;
2441 c932eeeb 2019-05-22 stsp
2442 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2443 6ced4176 2019-07-12 stsp /* Check out a single file. */
2444 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2445 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2446 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2447 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2448 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2449 6ced4176 2019-07-12 stsp goto done;
2450 6ced4176 2019-07-12 stsp }
2451 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2452 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2453 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2454 6ced4176 2019-07-12 stsp goto done;
2455 6ced4176 2019-07-12 stsp }
2456 6ced4176 2019-07-12 stsp } else {
2457 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2458 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2459 6ced4176 2019-07-12 stsp if (err)
2460 6ced4176 2019-07-12 stsp return err;
2461 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2462 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2463 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2464 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2465 6ced4176 2019-07-12 stsp goto done;
2466 6ced4176 2019-07-12 stsp }
2467 6ced4176 2019-07-12 stsp }
2468 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2469 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2470 6ced4176 2019-07-12 stsp } else {
2471 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2472 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2473 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2474 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2475 6ced4176 2019-07-12 stsp goto done;
2476 6ced4176 2019-07-12 stsp }
2477 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2478 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2479 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2480 6ced4176 2019-07-12 stsp goto done;
2481 6ced4176 2019-07-12 stsp }
2482 6ced4176 2019-07-12 stsp }
2483 6ced4176 2019-07-12 stsp done:
2484 6ced4176 2019-07-12 stsp free(id);
2485 6ced4176 2019-07-12 stsp free(in_repo_path);
2486 6ced4176 2019-07-12 stsp if (err) {
2487 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2488 6ced4176 2019-07-12 stsp free(*tree_relpath);
2489 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2490 6ced4176 2019-07-12 stsp free(*tree_id);
2491 6ced4176 2019-07-12 stsp *tree_id = NULL;
2492 6ced4176 2019-07-12 stsp }
2493 07ed472d 2019-07-12 stsp return err;
2494 07ed472d 2019-07-12 stsp }
2495 07ed472d 2019-07-12 stsp
2496 07ed472d 2019-07-12 stsp static const struct got_error *
2497 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2498 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2499 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2500 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2501 07ed472d 2019-07-12 stsp {
2502 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2503 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2504 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2505 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2506 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2507 07ed472d 2019-07-12 stsp
2508 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2509 7f47418f 2019-12-20 stsp if (err) {
2510 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2511 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2512 7f47418f 2019-12-20 stsp goto done;
2513 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2514 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2515 7f47418f 2019-12-20 stsp if (err)
2516 7f47418f 2019-12-20 stsp return err;
2517 7f47418f 2019-12-20 stsp }
2518 07ed472d 2019-07-12 stsp
2519 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2520 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2521 07ed472d 2019-07-12 stsp if (err)
2522 07ed472d 2019-07-12 stsp goto done;
2523 07ed472d 2019-07-12 stsp
2524 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2525 07ed472d 2019-07-12 stsp if (err)
2526 07ed472d 2019-07-12 stsp goto done;
2527 07ed472d 2019-07-12 stsp
2528 07ed472d 2019-07-12 stsp if (entry_name &&
2529 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2530 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2531 07ed472d 2019-07-12 stsp goto done;
2532 07ed472d 2019-07-12 stsp }
2533 07ed472d 2019-07-12 stsp
2534 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2535 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2536 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2537 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2538 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2539 07ed472d 2019-07-12 stsp arg.repo = repo;
2540 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2541 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2542 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2543 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2544 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2545 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2546 07ed472d 2019-07-12 stsp done:
2547 07ed472d 2019-07-12 stsp if (tree)
2548 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2549 07ed472d 2019-07-12 stsp if (commit)
2550 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2551 6ced4176 2019-07-12 stsp return err;
2552 6ced4176 2019-07-12 stsp }
2553 6ced4176 2019-07-12 stsp
2554 9d31a1d8 2018-03-11 stsp const struct got_error *
2555 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2556 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2557 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2558 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2559 9d31a1d8 2018-03-11 stsp {
2560 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2561 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2562 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2563 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2564 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2565 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2566 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2567 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2568 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2569 f2ea84fa 2019-07-27 stsp int entry_type;
2570 f2ea84fa 2019-07-27 stsp char *relpath;
2571 f2ea84fa 2019-07-27 stsp char *entry_name;
2572 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2573 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2574 9d31a1d8 2018-03-11 stsp
2575 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2576 f2ea84fa 2019-07-27 stsp
2577 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2578 9d31a1d8 2018-03-11 stsp if (err)
2579 9d31a1d8 2018-03-11 stsp return err;
2580 93a30277 2018-12-24 stsp
2581 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2582 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2583 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2584 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2585 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2586 f2ea84fa 2019-07-27 stsp goto done;
2587 f2ea84fa 2019-07-27 stsp }
2588 6ced4176 2019-07-12 stsp
2589 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2590 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2591 f2ea84fa 2019-07-27 stsp if (err) {
2592 f2ea84fa 2019-07-27 stsp free(tpd);
2593 6ced4176 2019-07-12 stsp goto done;
2594 6ced4176 2019-07-12 stsp }
2595 f2ea84fa 2019-07-27 stsp
2596 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2597 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2598 f2ea84fa 2019-07-27 stsp if (err) {
2599 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2600 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2601 f2ea84fa 2019-07-27 stsp free(tpd);
2602 f2ea84fa 2019-07-27 stsp goto done;
2603 f2ea84fa 2019-07-27 stsp }
2604 f2ea84fa 2019-07-27 stsp } else
2605 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2606 f2ea84fa 2019-07-27 stsp
2607 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2608 6ced4176 2019-07-12 stsp }
2609 6ced4176 2019-07-12 stsp
2610 51514078 2018-12-25 stsp /*
2611 51514078 2018-12-25 stsp * Read the file index.
2612 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2613 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2614 51514078 2018-12-25 stsp */
2615 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2616 8da9e5f4 2019-01-12 stsp if (err)
2617 c4cdcb68 2019-04-03 stsp goto done;
2618 c4cdcb68 2019-04-03 stsp
2619 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2620 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2621 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2622 f2ea84fa 2019-07-27 stsp
2623 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2624 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2625 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2626 f2ea84fa 2019-07-27 stsp if (err)
2627 f2ea84fa 2019-07-27 stsp break;
2628 f2ea84fa 2019-07-27 stsp
2629 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2630 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2631 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2632 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2633 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2634 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2635 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2636 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2637 f2ea84fa 2019-07-27 stsp if (err)
2638 f2ea84fa 2019-07-27 stsp break;
2639 f2ea84fa 2019-07-27 stsp
2640 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2641 711d9cd9 2019-07-12 stsp }
2642 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2643 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2644 af12c6b9 2019-06-04 stsp err = sync_err;
2645 9d31a1d8 2018-03-11 stsp done:
2646 9c6338c4 2019-06-02 stsp free(fileindex_path);
2647 edfa7d7f 2018-09-11 stsp if (tree)
2648 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2649 9d31a1d8 2018-03-11 stsp if (commit)
2650 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2651 5ade8233 2019-07-12 stsp if (fileindex)
2652 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2653 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2654 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2655 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2656 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2657 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2658 f2ea84fa 2019-07-27 stsp free(tpd);
2659 f2ea84fa 2019-07-27 stsp }
2660 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2661 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2662 9d31a1d8 2018-03-11 stsp err = unlockerr;
2663 f8d1f275 2019-02-04 stsp return err;
2664 f8d1f275 2019-02-04 stsp }
2665 f8d1f275 2019-02-04 stsp
2666 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2667 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2668 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2669 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2670 234035bc 2019-06-01 stsp void *progress_arg;
2671 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2672 234035bc 2019-06-01 stsp void *cancel_arg;
2673 f69721c3 2019-10-21 stsp const char *label_orig;
2674 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2675 234035bc 2019-06-01 stsp };
2676 234035bc 2019-06-01 stsp
2677 234035bc 2019-06-01 stsp static const struct got_error *
2678 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2679 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2680 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2681 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2682 234035bc 2019-06-01 stsp {
2683 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2684 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2685 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2686 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2687 234035bc 2019-06-01 stsp struct stat sb;
2688 234035bc 2019-06-01 stsp unsigned char status;
2689 234035bc 2019-06-01 stsp int local_changes_subsumed;
2690 234035bc 2019-06-01 stsp
2691 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2692 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2693 d6c87207 2019-08-02 stsp strlen(path2));
2694 1ee397ad 2019-07-12 stsp if (ie == NULL)
2695 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2696 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2697 234035bc 2019-06-01 stsp
2698 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2699 234035bc 2019-06-01 stsp path2) == -1)
2700 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2701 234035bc 2019-06-01 stsp
2702 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2703 7f91a133 2019-12-13 stsp repo);
2704 234035bc 2019-06-01 stsp if (err)
2705 234035bc 2019-06-01 stsp goto done;
2706 234035bc 2019-06-01 stsp
2707 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2708 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2709 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2710 234035bc 2019-06-01 stsp goto done;
2711 234035bc 2019-06-01 stsp }
2712 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2713 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2714 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2715 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2716 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2717 234035bc 2019-06-01 stsp goto done;
2718 234035bc 2019-06-01 stsp }
2719 234035bc 2019-06-01 stsp
2720 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2721 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2722 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2723 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2724 af57b12a 2020-07-23 stsp a->progress_arg);
2725 af57b12a 2020-07-23 stsp } else {
2726 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2727 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2728 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2729 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2730 af57b12a 2020-07-23 stsp }
2731 234035bc 2019-06-01 stsp } else if (blob1) {
2732 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2733 d6c87207 2019-08-02 stsp strlen(path1));
2734 1ee397ad 2019-07-12 stsp if (ie == NULL)
2735 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2736 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2737 2b92fad7 2019-06-02 stsp
2738 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2739 2b92fad7 2019-06-02 stsp path1) == -1)
2740 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2741 2b92fad7 2019-06-02 stsp
2742 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2743 7f91a133 2019-12-13 stsp repo);
2744 2b92fad7 2019-06-02 stsp if (err)
2745 2b92fad7 2019-06-02 stsp goto done;
2746 2b92fad7 2019-06-02 stsp
2747 2b92fad7 2019-06-02 stsp switch (status) {
2748 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2749 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2750 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2751 1ee397ad 2019-07-12 stsp if (err)
2752 1ee397ad 2019-07-12 stsp goto done;
2753 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2754 2b92fad7 2019-06-02 stsp if (err)
2755 2b92fad7 2019-06-02 stsp goto done;
2756 2b92fad7 2019-06-02 stsp if (ie)
2757 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2758 2b92fad7 2019-06-02 stsp break;
2759 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2760 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2761 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2762 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2763 1ee397ad 2019-07-12 stsp if (err)
2764 1ee397ad 2019-07-12 stsp goto done;
2765 2b92fad7 2019-06-02 stsp if (ie)
2766 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2767 2b92fad7 2019-06-02 stsp break;
2768 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2769 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2770 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2771 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2772 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2773 1ee397ad 2019-07-12 stsp if (err)
2774 1ee397ad 2019-07-12 stsp goto done;
2775 2b92fad7 2019-06-02 stsp break;
2776 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2777 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2778 1ee397ad 2019-07-12 stsp if (err)
2779 1ee397ad 2019-07-12 stsp goto done;
2780 2b92fad7 2019-06-02 stsp break;
2781 2b92fad7 2019-06-02 stsp default:
2782 2b92fad7 2019-06-02 stsp break;
2783 2b92fad7 2019-06-02 stsp }
2784 234035bc 2019-06-01 stsp } else if (blob2) {
2785 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2786 234035bc 2019-06-01 stsp path2) == -1)
2787 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2788 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2789 d6c87207 2019-08-02 stsp strlen(path2));
2790 234035bc 2019-06-01 stsp if (ie) {
2791 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2792 7f91a133 2019-12-13 stsp -1, NULL, repo);
2793 234035bc 2019-06-01 stsp if (err)
2794 234035bc 2019-06-01 stsp goto done;
2795 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2796 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2797 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2798 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2799 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2800 1ee397ad 2019-07-12 stsp status, path2);
2801 234035bc 2019-06-01 stsp goto done;
2802 234035bc 2019-06-01 stsp }
2803 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2804 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2805 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2806 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2807 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2808 526a746f 2020-07-23 stsp if (err)
2809 526a746f 2020-07-23 stsp goto done;
2810 526a746f 2020-07-23 stsp } else {
2811 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2812 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2813 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2814 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2815 526a746f 2020-07-23 stsp a->progress_arg);
2816 526a746f 2020-07-23 stsp if (err)
2817 526a746f 2020-07-23 stsp goto done;
2818 526a746f 2020-07-23 stsp }
2819 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2820 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2821 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2822 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2823 234035bc 2019-06-01 stsp if (err)
2824 234035bc 2019-06-01 stsp goto done;
2825 234035bc 2019-06-01 stsp }
2826 234035bc 2019-06-01 stsp } else {
2827 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2828 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2829 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2830 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2831 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2832 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2833 2e1fa222 2020-07-23 stsp } else {
2834 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2835 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2836 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2837 2e1fa222 2020-07-23 stsp }
2838 234035bc 2019-06-01 stsp if (err)
2839 234035bc 2019-06-01 stsp goto done;
2840 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2841 234035bc 2019-06-01 stsp if (err)
2842 234035bc 2019-06-01 stsp goto done;
2843 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2844 3969253a 2020-03-07 stsp NULL, NULL, 1);
2845 3969253a 2020-03-07 stsp if (err) {
2846 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2847 3969253a 2020-03-07 stsp goto done;
2848 3969253a 2020-03-07 stsp }
2849 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2850 2b92fad7 2019-06-02 stsp if (err) {
2851 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2852 2b92fad7 2019-06-02 stsp goto done;
2853 2b92fad7 2019-06-02 stsp }
2854 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2855 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2856 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2857 2e1fa222 2020-07-23 stsp }
2858 234035bc 2019-06-01 stsp }
2859 234035bc 2019-06-01 stsp }
2860 234035bc 2019-06-01 stsp done:
2861 234035bc 2019-06-01 stsp free(ondisk_path);
2862 234035bc 2019-06-01 stsp return err;
2863 234035bc 2019-06-01 stsp }
2864 234035bc 2019-06-01 stsp
2865 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2866 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2867 234035bc 2019-06-01 stsp struct got_repository *repo;
2868 234035bc 2019-06-01 stsp };
2869 234035bc 2019-06-01 stsp
2870 234035bc 2019-06-01 stsp static const struct got_error *
2871 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2872 234035bc 2019-06-01 stsp {
2873 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2874 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2875 234035bc 2019-06-01 stsp unsigned char status;
2876 234035bc 2019-06-01 stsp struct stat sb;
2877 234035bc 2019-06-01 stsp char *ondisk_path;
2878 234035bc 2019-06-01 stsp
2879 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2880 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2881 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2882 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2883 234035bc 2019-06-01 stsp
2884 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2885 234035bc 2019-06-01 stsp == -1)
2886 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2887 234035bc 2019-06-01 stsp
2888 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2889 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2890 234035bc 2019-06-01 stsp if (err)
2891 234035bc 2019-06-01 stsp return err;
2892 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2893 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2894 234035bc 2019-06-01 stsp
2895 234035bc 2019-06-01 stsp return NULL;
2896 234035bc 2019-06-01 stsp }
2897 234035bc 2019-06-01 stsp
2898 818c7501 2019-07-11 stsp static const struct got_error *
2899 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2900 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2901 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2902 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2903 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2904 234035bc 2019-06-01 stsp {
2905 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2906 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2907 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2908 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2909 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2910 234035bc 2019-06-01 stsp
2911 03415a1a 2019-06-02 stsp if (commit_id1) {
2912 f69721c3 2019-10-21 stsp char *id_str;
2913 f69721c3 2019-10-21 stsp
2914 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2915 03415a1a 2019-06-02 stsp worktree->path_prefix);
2916 03415a1a 2019-06-02 stsp if (err)
2917 03415a1a 2019-06-02 stsp goto done;
2918 03415a1a 2019-06-02 stsp
2919 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2920 03415a1a 2019-06-02 stsp if (err)
2921 03415a1a 2019-06-02 stsp goto done;
2922 f69721c3 2019-10-21 stsp
2923 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2924 f69721c3 2019-10-21 stsp if (err)
2925 f69721c3 2019-10-21 stsp goto done;
2926 f69721c3 2019-10-21 stsp
2927 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2928 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2929 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2930 f69721c3 2019-10-21 stsp free(id_str);
2931 f69721c3 2019-10-21 stsp goto done;
2932 f69721c3 2019-10-21 stsp }
2933 f69721c3 2019-10-21 stsp free(id_str);
2934 03415a1a 2019-06-02 stsp }
2935 234035bc 2019-06-01 stsp
2936 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2937 234035bc 2019-06-01 stsp worktree->path_prefix);
2938 234035bc 2019-06-01 stsp if (err)
2939 234035bc 2019-06-01 stsp goto done;
2940 234035bc 2019-06-01 stsp
2941 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2942 234035bc 2019-06-01 stsp if (err)
2943 234035bc 2019-06-01 stsp goto done;
2944 234035bc 2019-06-01 stsp
2945 234035bc 2019-06-01 stsp arg.worktree = worktree;
2946 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2947 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2948 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2949 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2950 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2951 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2952 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2953 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2954 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2955 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2956 af12c6b9 2019-06-04 stsp err = sync_err;
2957 234035bc 2019-06-01 stsp done:
2958 234035bc 2019-06-01 stsp if (tree1)
2959 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2960 234035bc 2019-06-01 stsp if (tree2)
2961 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2962 f69721c3 2019-10-21 stsp free(label_orig);
2963 818c7501 2019-07-11 stsp return err;
2964 818c7501 2019-07-11 stsp }
2965 234035bc 2019-06-01 stsp
2966 818c7501 2019-07-11 stsp const struct got_error *
2967 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2968 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2969 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2970 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2971 818c7501 2019-07-11 stsp {
2972 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2973 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2974 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2975 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2976 818c7501 2019-07-11 stsp
2977 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2978 818c7501 2019-07-11 stsp if (err)
2979 818c7501 2019-07-11 stsp return err;
2980 818c7501 2019-07-11 stsp
2981 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2982 818c7501 2019-07-11 stsp if (err)
2983 818c7501 2019-07-11 stsp goto done;
2984 818c7501 2019-07-11 stsp
2985 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2986 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2987 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2988 818c7501 2019-07-11 stsp &mok_arg);
2989 818c7501 2019-07-11 stsp if (err)
2990 818c7501 2019-07-11 stsp goto done;
2991 818c7501 2019-07-11 stsp
2992 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2993 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2994 818c7501 2019-07-11 stsp done:
2995 818c7501 2019-07-11 stsp if (fileindex)
2996 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2997 818c7501 2019-07-11 stsp free(fileindex_path);
2998 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2999 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3000 234035bc 2019-06-01 stsp err = unlockerr;
3001 234035bc 2019-06-01 stsp return err;
3002 234035bc 2019-06-01 stsp }
3003 234035bc 2019-06-01 stsp
3004 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3005 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3006 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3007 927df6b7 2019-02-10 stsp const char *status_path;
3008 927df6b7 2019-02-10 stsp size_t status_path_len;
3009 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3010 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3011 f8d1f275 2019-02-04 stsp void *status_arg;
3012 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3013 f8d1f275 2019-02-04 stsp void *cancel_arg;
3014 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3015 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3016 f2a9dc41 2019-12-13 tracey int report_unchanged;
3017 3143d852 2020-06-25 stsp int no_ignores;
3018 f8d1f275 2019-02-04 stsp };
3019 88d0e355 2019-08-03 stsp
3020 f8d1f275 2019-02-04 stsp static const struct got_error *
3021 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3022 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3023 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3024 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3025 927df6b7 2019-02-10 stsp {
3026 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3027 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3028 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3029 927df6b7 2019-02-10 stsp struct stat sb;
3030 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3031 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3032 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3033 927df6b7 2019-02-10 stsp
3034 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3035 98eaaa12 2019-08-03 stsp if (err)
3036 98eaaa12 2019-08-03 stsp return err;
3037 98eaaa12 2019-08-03 stsp
3038 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3039 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3040 98eaaa12 2019-08-03 stsp return NULL;
3041 98eaaa12 2019-08-03 stsp
3042 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3043 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3044 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3045 98eaaa12 2019-08-03 stsp }
3046 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3047 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3048 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3049 927df6b7 2019-02-10 stsp }
3050 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3051 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3052 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3053 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3054 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3055 98eaaa12 2019-08-03 stsp }
3056 98eaaa12 2019-08-03 stsp
3057 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3058 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3059 927df6b7 2019-02-10 stsp }
3060 927df6b7 2019-02-10 stsp
3061 927df6b7 2019-02-10 stsp static const struct got_error *
3062 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3063 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3064 f8d1f275 2019-02-04 stsp {
3065 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3066 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3067 f8d1f275 2019-02-04 stsp char *abspath;
3068 f8d1f275 2019-02-04 stsp
3069 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3070 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3071 0584f854 2019-04-06 stsp
3072 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3073 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3074 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3075 927df6b7 2019-02-10 stsp return NULL;
3076 927df6b7 2019-02-10 stsp
3077 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3078 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3079 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3080 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3081 f8d1f275 2019-02-04 stsp } else {
3082 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3083 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3084 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3085 f8d1f275 2019-02-04 stsp }
3086 f8d1f275 2019-02-04 stsp
3087 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3088 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3089 f8d1f275 2019-02-04 stsp free(abspath);
3090 9d31a1d8 2018-03-11 stsp return err;
3091 9d31a1d8 2018-03-11 stsp }
3092 f8d1f275 2019-02-04 stsp
3093 f8d1f275 2019-02-04 stsp static const struct got_error *
3094 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3095 f8d1f275 2019-02-04 stsp {
3096 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3097 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3098 2ec1f75b 2019-03-26 stsp unsigned char status;
3099 927df6b7 2019-02-10 stsp
3100 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3101 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3102 0584f854 2019-04-06 stsp
3103 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3104 927df6b7 2019-02-10 stsp return NULL;
3105 927df6b7 2019-02-10 stsp
3106 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3107 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3108 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3109 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3110 2ec1f75b 2019-03-26 stsp else
3111 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3112 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3113 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3114 6841da00 2019-08-08 stsp }
3115 6841da00 2019-08-08 stsp
3116 6841da00 2019-08-08 stsp void
3117 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3118 6841da00 2019-08-08 stsp {
3119 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3120 6841da00 2019-08-08 stsp
3121 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3122 6841da00 2019-08-08 stsp free((char *)pe->path);
3123 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3124 6841da00 2019-08-08 stsp }
3125 6841da00 2019-08-08 stsp
3126 6841da00 2019-08-08 stsp void
3127 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3128 6841da00 2019-08-08 stsp {
3129 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3130 6841da00 2019-08-08 stsp
3131 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3132 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3133 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3134 6841da00 2019-08-08 stsp free((char *)pe->path);
3135 6841da00 2019-08-08 stsp }
3136 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3137 6841da00 2019-08-08 stsp }
3138 6841da00 2019-08-08 stsp
3139 6841da00 2019-08-08 stsp static const struct got_error *
3140 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3141 6841da00 2019-08-08 stsp {
3142 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3143 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3144 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3145 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3146 6841da00 2019-08-08 stsp size_t linesize = 0;
3147 6841da00 2019-08-08 stsp ssize_t linelen;
3148 6841da00 2019-08-08 stsp
3149 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3150 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3151 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3152 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3153 6841da00 2019-08-08 stsp
3154 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3155 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3156 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3157 bd8de430 2019-10-04 stsp
3158 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3159 bd8de430 2019-10-04 stsp if (line[0] == '#')
3160 bd8de430 2019-10-04 stsp continue;
3161 bd8de430 2019-10-04 stsp
3162 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3163 bd8de430 2019-10-04 stsp if (line[0] == '!')
3164 bd8de430 2019-10-04 stsp continue;
3165 bd8de430 2019-10-04 stsp
3166 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3167 6841da00 2019-08-08 stsp line) == -1) {
3168 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3169 6841da00 2019-08-08 stsp goto done;
3170 6841da00 2019-08-08 stsp }
3171 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3172 6841da00 2019-08-08 stsp if (err)
3173 6841da00 2019-08-08 stsp goto done;
3174 6841da00 2019-08-08 stsp }
3175 6841da00 2019-08-08 stsp if (ferror(f)) {
3176 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3177 6841da00 2019-08-08 stsp goto done;
3178 6841da00 2019-08-08 stsp }
3179 6841da00 2019-08-08 stsp
3180 6841da00 2019-08-08 stsp dirpath = strdup(path);
3181 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3182 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3183 6841da00 2019-08-08 stsp goto done;
3184 6841da00 2019-08-08 stsp }
3185 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3186 6841da00 2019-08-08 stsp done:
3187 6841da00 2019-08-08 stsp free(line);
3188 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3189 6841da00 2019-08-08 stsp free(dirpath);
3190 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3191 6841da00 2019-08-08 stsp }
3192 6841da00 2019-08-08 stsp return err;
3193 6841da00 2019-08-08 stsp }
3194 6841da00 2019-08-08 stsp
3195 6841da00 2019-08-08 stsp int
3196 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3197 6841da00 2019-08-08 stsp {
3198 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3199 bd8de430 2019-10-04 stsp
3200 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3201 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3202 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3203 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3204 bd8de430 2019-10-04 stsp
3205 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3206 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3207 6841da00 2019-08-08 stsp
3208 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3209 bd8de430 2019-10-04 stsp continue;
3210 bd8de430 2019-10-04 stsp pattern += 3;
3211 bd8de430 2019-10-04 stsp p = path;
3212 bd8de430 2019-10-04 stsp while (*p) {
3213 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3214 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3215 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3216 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3217 bd8de430 2019-10-04 stsp p++;
3218 bd8de430 2019-10-04 stsp while (*p == '/')
3219 bd8de430 2019-10-04 stsp p++;
3220 bd8de430 2019-10-04 stsp continue;
3221 bd8de430 2019-10-04 stsp }
3222 bd8de430 2019-10-04 stsp return 1;
3223 bd8de430 2019-10-04 stsp }
3224 bd8de430 2019-10-04 stsp }
3225 bd8de430 2019-10-04 stsp }
3226 bd8de430 2019-10-04 stsp
3227 6841da00 2019-08-08 stsp /*
3228 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3229 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3230 6841da00 2019-08-08 stsp * ignores backwards.
3231 6841da00 2019-08-08 stsp */
3232 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3233 6841da00 2019-08-08 stsp while (pe) {
3234 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3235 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3236 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3237 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3238 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3239 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3240 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3241 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3242 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3243 6841da00 2019-08-08 stsp continue;
3244 6841da00 2019-08-08 stsp return 1;
3245 6841da00 2019-08-08 stsp }
3246 6841da00 2019-08-08 stsp }
3247 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3248 6841da00 2019-08-08 stsp }
3249 6841da00 2019-08-08 stsp
3250 6841da00 2019-08-08 stsp return 0;
3251 6841da00 2019-08-08 stsp }
3252 6841da00 2019-08-08 stsp
3253 6841da00 2019-08-08 stsp static const struct got_error *
3254 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3255 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3256 6841da00 2019-08-08 stsp {
3257 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3258 6841da00 2019-08-08 stsp char *ignorespath;
3259 886cec17 2019-12-15 stsp int fd = -1;
3260 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3261 6841da00 2019-08-08 stsp
3262 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3263 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3264 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3265 6841da00 2019-08-08 stsp
3266 886cec17 2019-12-15 stsp if (dirfd != -1) {
3267 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3268 886cec17 2019-12-15 stsp if (fd == -1) {
3269 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3270 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3271 886cec17 2019-12-15 stsp ignorespath);
3272 886cec17 2019-12-15 stsp } else {
3273 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3274 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3275 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3276 886cec17 2019-12-15 stsp ignorespath);
3277 886cec17 2019-12-15 stsp else {
3278 886cec17 2019-12-15 stsp fd = -1;
3279 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3280 886cec17 2019-12-15 stsp }
3281 886cec17 2019-12-15 stsp }
3282 886cec17 2019-12-15 stsp } else {
3283 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3284 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3285 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3286 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3287 886cec17 2019-12-15 stsp ignorespath);
3288 886cec17 2019-12-15 stsp } else
3289 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3290 886cec17 2019-12-15 stsp }
3291 6841da00 2019-08-08 stsp
3292 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3293 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3294 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3295 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3296 6841da00 2019-08-08 stsp free(ignorespath);
3297 6841da00 2019-08-08 stsp return err;
3298 f8d1f275 2019-02-04 stsp }
3299 f8d1f275 2019-02-04 stsp
3300 f8d1f275 2019-02-04 stsp static const struct got_error *
3301 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3302 f8d1f275 2019-02-04 stsp {
3303 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3304 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3305 f8d1f275 2019-02-04 stsp char *path = NULL;
3306 f8d1f275 2019-02-04 stsp
3307 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3308 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3309 0584f854 2019-04-06 stsp
3310 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3311 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3312 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3313 f8d1f275 2019-02-04 stsp } else {
3314 f8d1f275 2019-02-04 stsp path = de->d_name;
3315 f8d1f275 2019-02-04 stsp }
3316 f8d1f275 2019-02-04 stsp
3317 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3318 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3319 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3320 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3321 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3322 f8d1f275 2019-02-04 stsp if (parent_path[0])
3323 f8d1f275 2019-02-04 stsp free(path);
3324 b72f483a 2019-02-05 stsp return err;
3325 f8d1f275 2019-02-04 stsp }
3326 f8d1f275 2019-02-04 stsp
3327 347d1d3e 2019-07-12 stsp static const struct got_error *
3328 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3329 3143d852 2020-06-25 stsp {
3330 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3331 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3332 3143d852 2020-06-25 stsp
3333 3143d852 2020-06-25 stsp if (a->no_ignores)
3334 3143d852 2020-06-25 stsp return NULL;
3335 3143d852 2020-06-25 stsp
3336 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3337 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3338 3143d852 2020-06-25 stsp if (err)
3339 3143d852 2020-06-25 stsp return err;
3340 3143d852 2020-06-25 stsp
3341 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3342 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3343 3143d852 2020-06-25 stsp
3344 3143d852 2020-06-25 stsp return err;
3345 3143d852 2020-06-25 stsp }
3346 3143d852 2020-06-25 stsp
3347 3143d852 2020-06-25 stsp static const struct got_error *
3348 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3349 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3350 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3351 abb4604f 2019-07-27 stsp {
3352 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3353 abb4604f 2019-07-27 stsp struct stat sb;
3354 abb4604f 2019-07-27 stsp
3355 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3356 abb4604f 2019-07-27 stsp if (ie)
3357 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3358 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3359 abb4604f 2019-07-27 stsp
3360 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3361 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3362 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3363 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3364 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3365 abb4604f 2019-07-27 stsp return NULL;
3366 abb4604f 2019-07-27 stsp }
3367 abb4604f 2019-07-27 stsp
3368 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3369 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3370 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3371 abb4604f 2019-07-27 stsp
3372 abb4604f 2019-07-27 stsp return NULL;
3373 3143d852 2020-06-25 stsp }
3374 3143d852 2020-06-25 stsp
3375 3143d852 2020-06-25 stsp static const struct got_error *
3376 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3377 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3378 3143d852 2020-06-25 stsp {
3379 3143d852 2020-06-25 stsp const struct got_error *err;
3380 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3381 3143d852 2020-06-25 stsp
3382 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3383 3143d852 2020-06-25 stsp ".cvsignore");
3384 3143d852 2020-06-25 stsp if (err)
3385 3143d852 2020-06-25 stsp return err;
3386 3143d852 2020-06-25 stsp
3387 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3388 3143d852 2020-06-25 stsp ".gitignore");
3389 3143d852 2020-06-25 stsp if (err)
3390 3143d852 2020-06-25 stsp return err;
3391 3143d852 2020-06-25 stsp
3392 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3393 3143d852 2020-06-25 stsp if (err) {
3394 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3395 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3396 3143d852 2020-06-25 stsp return err;
3397 3143d852 2020-06-25 stsp }
3398 3143d852 2020-06-25 stsp for (;;) {
3399 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3400 3143d852 2020-06-25 stsp ".cvsignore");
3401 3143d852 2020-06-25 stsp if (err)
3402 3143d852 2020-06-25 stsp break;
3403 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3404 3143d852 2020-06-25 stsp ".gitignore");
3405 3143d852 2020-06-25 stsp if (err)
3406 3143d852 2020-06-25 stsp break;
3407 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3408 3143d852 2020-06-25 stsp if (err) {
3409 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3410 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3411 3143d852 2020-06-25 stsp break;
3412 3143d852 2020-06-25 stsp }
3413 b737c85a 2020-06-26 stsp free(parent_path);
3414 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3415 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3416 3143d852 2020-06-25 stsp }
3417 3143d852 2020-06-25 stsp
3418 b737c85a 2020-06-26 stsp free(parent_path);
3419 b737c85a 2020-06-26 stsp free(next_parent_path);
3420 3143d852 2020-06-25 stsp return err;
3421 abb4604f 2019-07-27 stsp }
3422 abb4604f 2019-07-27 stsp
3423 abb4604f 2019-07-27 stsp static const struct got_error *
3424 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3425 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3426 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3427 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3428 f2a9dc41 2019-12-13 tracey int report_unchanged)
3429 f8d1f275 2019-02-04 stsp {
3430 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3431 6fc93f37 2019-12-13 stsp int fd = -1;
3432 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3433 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3434 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3435 3143d852 2020-06-25 stsp
3436 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3437 f8d1f275 2019-02-04 stsp
3438 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3439 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3440 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3441 8dc303cc 2019-07-27 stsp
3442 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3443 6fc93f37 2019-12-13 stsp if (fd == -1) {
3444 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3445 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3446 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3447 abb4604f 2019-07-27 stsp else
3448 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3449 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3450 f2a9dc41 2019-12-13 tracey report_unchanged);
3451 abb4604f 2019-07-27 stsp } else {
3452 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3453 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3454 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3455 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3456 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3457 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3458 abb4604f 2019-07-27 stsp arg.status_path = path;
3459 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3460 abb4604f 2019-07-27 stsp arg.repo = repo;
3461 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3462 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3463 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3464 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3465 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3466 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3467 022fae89 2019-12-06 tracey if (!no_ignores) {
3468 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3469 3143d852 2020-06-25 stsp worktree->root_path, path);
3470 3143d852 2020-06-25 stsp if (err)
3471 3143d852 2020-06-25 stsp goto done;
3472 022fae89 2019-12-06 tracey }
3473 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3474 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3475 927df6b7 2019-02-10 stsp }
3476 3143d852 2020-06-25 stsp done:
3477 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3478 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3479 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3480 927df6b7 2019-02-10 stsp free(ondisk_path);
3481 347d1d3e 2019-07-12 stsp return err;
3482 347d1d3e 2019-07-12 stsp }
3483 347d1d3e 2019-07-12 stsp
3484 347d1d3e 2019-07-12 stsp const struct got_error *
3485 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3486 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3487 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3488 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3489 347d1d3e 2019-07-12 stsp {
3490 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3491 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3492 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3493 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3494 347d1d3e 2019-07-12 stsp
3495 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3496 347d1d3e 2019-07-12 stsp if (err)
3497 347d1d3e 2019-07-12 stsp return err;
3498 347d1d3e 2019-07-12 stsp
3499 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3500 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3501 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3502 72ea6654 2019-07-27 stsp if (err)
3503 72ea6654 2019-07-27 stsp break;
3504 72ea6654 2019-07-27 stsp }
3505 f8d1f275 2019-02-04 stsp free(fileindex_path);
3506 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3507 f8d1f275 2019-02-04 stsp return err;
3508 f8d1f275 2019-02-04 stsp }
3509 6c7ab921 2019-03-18 stsp
3510 6c7ab921 2019-03-18 stsp const struct got_error *
3511 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3512 6c7ab921 2019-03-18 stsp const char *arg)
3513 6c7ab921 2019-03-18 stsp {
3514 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3515 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3516 6c7ab921 2019-03-18 stsp size_t len;
3517 00bb5ea0 2020-07-23 stsp struct stat sb;
3518 6c7ab921 2019-03-18 stsp
3519 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3520 6c7ab921 2019-03-18 stsp
3521 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3522 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3523 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3524 00bb5ea0 2020-07-23 stsp
3525 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3526 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3527 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3528 00bb5ea0 2020-07-23 stsp goto done;
3529 00bb5ea0 2020-07-23 stsp }
3530 00bb5ea0 2020-07-23 stsp }
3531 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3532 00bb5ea0 2020-07-23 stsp /*
3533 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3534 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3535 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3536 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3537 00bb5ea0 2020-07-23 stsp */
3538 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3539 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3540 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3541 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3542 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3543 00bb5ea0 2020-07-23 stsp goto done;
3544 00bb5ea0 2020-07-23 stsp }
3545 00bb5ea0 2020-07-23 stsp
3546 00bb5ea0 2020-07-23 stsp }
3547 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3548 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3549 00bb5ea0 2020-07-23 stsp if (err)
3550 d0710d08 2019-07-22 stsp goto done;
3551 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3552 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3553 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3554 00bb5ea0 2020-07-23 stsp goto done;
3555 00bb5ea0 2020-07-23 stsp }
3556 00bb5ea0 2020-07-23 stsp } else {
3557 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3558 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3559 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3560 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3561 00bb5ea0 2020-07-23 stsp goto done;
3562 00bb5ea0 2020-07-23 stsp }
3563 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3564 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3565 00bb5ea0 2020-07-23 stsp goto done;
3566 00bb5ea0 2020-07-23 stsp }
3567 d0710d08 2019-07-22 stsp }
3568 d0710d08 2019-07-22 stsp }
3569 6c7ab921 2019-03-18 stsp
3570 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3571 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3572 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3573 6c7ab921 2019-03-18 stsp goto done;
3574 6c7ab921 2019-03-18 stsp }
3575 6c7ab921 2019-03-18 stsp
3576 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3577 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3578 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3579 f6d88e1a 2019-05-29 stsp if (err)
3580 f6d88e1a 2019-05-29 stsp goto done;
3581 f6d88e1a 2019-05-29 stsp } else {
3582 f6d88e1a 2019-05-29 stsp path = strdup("");
3583 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3584 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3585 f6d88e1a 2019-05-29 stsp goto done;
3586 f6d88e1a 2019-05-29 stsp }
3587 6c7ab921 2019-03-18 stsp }
3588 6c7ab921 2019-03-18 stsp
3589 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3590 6c7ab921 2019-03-18 stsp len = strlen(path);
3591 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3592 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3593 6c7ab921 2019-03-18 stsp len--;
3594 6c7ab921 2019-03-18 stsp }
3595 6c7ab921 2019-03-18 stsp done:
3596 6c7ab921 2019-03-18 stsp free(resolved);
3597 d0710d08 2019-07-22 stsp free(cwd);
3598 6c7ab921 2019-03-18 stsp if (err == NULL)
3599 6c7ab921 2019-03-18 stsp *wt_path = path;
3600 6c7ab921 2019-03-18 stsp else
3601 6c7ab921 2019-03-18 stsp free(path);
3602 d00136be 2019-03-26 stsp return err;
3603 d00136be 2019-03-26 stsp }
3604 d00136be 2019-03-26 stsp
3605 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3606 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3607 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3608 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3609 4e68cba3 2019-11-23 stsp void *progress_arg;
3610 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3611 4e68cba3 2019-11-23 stsp };
3612 4e68cba3 2019-11-23 stsp
3613 4e68cba3 2019-11-23 stsp static const struct got_error *
3614 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3615 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3616 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3617 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3618 07f5b47a 2019-06-02 stsp {
3619 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3620 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3621 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3622 6d022e97 2019-08-04 stsp struct stat sb;
3623 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3624 07f5b47a 2019-06-02 stsp
3625 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3626 dbb83fbd 2019-12-12 stsp relpath) == -1)
3627 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3628 dbb83fbd 2019-12-12 stsp
3629 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3630 6d022e97 2019-08-04 stsp if (ie) {
3631 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3632 12463d8b 2019-12-13 stsp de_name, a->repo);
3633 6d022e97 2019-08-04 stsp if (err)
3634 dbb83fbd 2019-12-12 stsp goto done;
3635 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3636 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3637 dbb83fbd 2019-12-12 stsp goto done;
3638 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3639 dbb83fbd 2019-12-12 stsp if (err)
3640 dbb83fbd 2019-12-12 stsp goto done;
3641 6d022e97 2019-08-04 stsp }
3642 07f5b47a 2019-06-02 stsp
3643 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3644 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3645 dbb83fbd 2019-12-12 stsp goto done;
3646 4e68cba3 2019-11-23 stsp }
3647 07f5b47a 2019-06-02 stsp
3648 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3649 4e68cba3 2019-11-23 stsp if (err)
3650 4e68cba3 2019-11-23 stsp goto done;
3651 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3652 3969253a 2020-03-07 stsp if (err) {
3653 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3654 3969253a 2020-03-07 stsp goto done;
3655 3969253a 2020-03-07 stsp }
3656 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3657 07f5b47a 2019-06-02 stsp if (err) {
3658 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3659 4e68cba3 2019-11-23 stsp goto done;
3660 07f5b47a 2019-06-02 stsp }
3661 4e68cba3 2019-11-23 stsp done:
3662 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3663 dbb83fbd 2019-12-12 stsp if (err)
3664 dbb83fbd 2019-12-12 stsp return err;
3665 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3666 dbb83fbd 2019-12-12 stsp return NULL;
3667 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3668 07f5b47a 2019-06-02 stsp }
3669 07f5b47a 2019-06-02 stsp
3670 d00136be 2019-03-26 stsp const struct got_error *
3671 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3672 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3673 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3674 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3675 d00136be 2019-03-26 stsp {
3676 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3677 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3678 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3680 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3681 d00136be 2019-03-26 stsp
3682 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3683 d00136be 2019-03-26 stsp if (err)
3684 d00136be 2019-03-26 stsp return err;
3685 d00136be 2019-03-26 stsp
3686 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3687 d00136be 2019-03-26 stsp if (err)
3688 d00136be 2019-03-26 stsp goto done;
3689 d00136be 2019-03-26 stsp
3690 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3691 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3692 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3693 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3694 4e68cba3 2019-11-23 stsp saa.repo = repo;
3695 4e68cba3 2019-11-23 stsp
3696 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3697 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3698 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3699 1dd54920 2019-05-11 stsp if (err)
3700 af12c6b9 2019-06-04 stsp break;
3701 1dd54920 2019-05-11 stsp }
3702 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3703 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3704 af12c6b9 2019-06-04 stsp err = sync_err;
3705 d00136be 2019-03-26 stsp done:
3706 fb399478 2019-07-12 stsp free(fileindex_path);
3707 d00136be 2019-03-26 stsp if (fileindex)
3708 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3709 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3710 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3711 d00136be 2019-03-26 stsp err = unlockerr;
3712 6c7ab921 2019-03-18 stsp return err;
3713 6c7ab921 2019-03-18 stsp }
3714 17ed4618 2019-06-02 stsp
3715 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3716 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3717 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3718 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3719 f2a9dc41 2019-12-13 tracey void *progress_arg;
3720 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3721 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3722 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3723 f2a9dc41 2019-12-13 tracey };
3724 f2a9dc41 2019-12-13 tracey
3725 f2a9dc41 2019-12-13 tracey static const struct got_error *
3726 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3727 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3728 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3729 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3730 17ed4618 2019-06-02 stsp {
3731 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3732 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3733 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3734 17ed4618 2019-06-02 stsp struct stat sb;
3735 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3736 17ed4618 2019-06-02 stsp
3737 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3738 17ed4618 2019-06-02 stsp if (ie == NULL)
3739 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3740 2ec1f75b 2019-03-26 stsp
3741 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3742 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3743 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3744 9acbc4fa 2019-08-03 stsp return NULL;
3745 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3746 9acbc4fa 2019-08-03 stsp }
3747 9acbc4fa 2019-08-03 stsp
3748 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3749 f2a9dc41 2019-12-13 tracey relpath) == -1)
3750 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3751 f2a9dc41 2019-12-13 tracey
3752 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3753 12463d8b 2019-12-13 stsp a->repo);
3754 17ed4618 2019-06-02 stsp if (err)
3755 f2a9dc41 2019-12-13 tracey goto done;
3756 17ed4618 2019-06-02 stsp
3757 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3758 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3759 f2a9dc41 2019-12-13 tracey goto done;
3760 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3761 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3762 f2a9dc41 2019-12-13 tracey goto done;
3763 f2a9dc41 2019-12-13 tracey }
3764 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3765 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3766 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3767 f2a9dc41 2019-12-13 tracey goto done;
3768 f2a9dc41 2019-12-13 tracey }
3769 17ed4618 2019-06-02 stsp }
3770 17ed4618 2019-06-02 stsp
3771 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3772 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3773 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3774 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3775 12463d8b 2019-12-13 stsp ondisk_path);
3776 12463d8b 2019-12-13 stsp goto done;
3777 12463d8b 2019-12-13 stsp }
3778 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3779 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3780 12463d8b 2019-12-13 stsp goto done;
3781 15341bfd 2020-03-05 tracey }
3782 15341bfd 2020-03-05 tracey
3783 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3784 15341bfd 2020-03-05 tracey
3785 15341bfd 2020-03-05 tracey if (parent == NULL) {
3786 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3787 15341bfd 2020-03-05 tracey goto done;
3788 15341bfd 2020-03-05 tracey }
3789 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3790 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3791 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3792 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3793 15341bfd 2020-03-05 tracey parent);
3794 15341bfd 2020-03-05 tracey break;
3795 15341bfd 2020-03-05 tracey }
3796 15341bfd 2020-03-05 tracey parent = dirname(parent);
3797 15341bfd 2020-03-05 tracey if (parent == NULL) {
3798 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3799 15341bfd 2020-03-05 tracey goto done;
3800 15341bfd 2020-03-05 tracey }
3801 12463d8b 2019-12-13 stsp }
3802 f2a9dc41 2019-12-13 tracey }
3803 17ed4618 2019-06-02 stsp
3804 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3805 f2a9dc41 2019-12-13 tracey done:
3806 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3807 f2a9dc41 2019-12-13 tracey if (err)
3808 f2a9dc41 2019-12-13 tracey return err;
3809 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3810 f2a9dc41 2019-12-13 tracey return NULL;
3811 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3812 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3813 17ed4618 2019-06-02 stsp }
3814 17ed4618 2019-06-02 stsp
3815 2ec1f75b 2019-03-26 stsp const struct got_error *
3816 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3817 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3818 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3819 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3820 2ec1f75b 2019-03-26 stsp {
3821 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3822 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3823 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3824 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3825 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3826 2ec1f75b 2019-03-26 stsp
3827 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3828 2ec1f75b 2019-03-26 stsp if (err)
3829 2ec1f75b 2019-03-26 stsp return err;
3830 2ec1f75b 2019-03-26 stsp
3831 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3832 2ec1f75b 2019-03-26 stsp if (err)
3833 2ec1f75b 2019-03-26 stsp goto done;
3834 f2a9dc41 2019-12-13 tracey
3835 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3836 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3837 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3838 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3839 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3840 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3841 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3842 2ec1f75b 2019-03-26 stsp
3843 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3844 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3845 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3846 17ed4618 2019-06-02 stsp if (err)
3847 af12c6b9 2019-06-04 stsp break;
3848 2ec1f75b 2019-03-26 stsp }
3849 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3850 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3851 af12c6b9 2019-06-04 stsp err = sync_err;
3852 2ec1f75b 2019-03-26 stsp done:
3853 fb399478 2019-07-12 stsp free(fileindex_path);
3854 2ec1f75b 2019-03-26 stsp if (fileindex)
3855 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3856 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3857 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3858 2ec1f75b 2019-03-26 stsp err = unlockerr;
3859 33aa809d 2019-08-08 stsp return err;
3860 33aa809d 2019-08-08 stsp }
3861 33aa809d 2019-08-08 stsp
3862 33aa809d 2019-08-08 stsp static const struct got_error *
3863 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3864 33aa809d 2019-08-08 stsp {
3865 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3866 33aa809d 2019-08-08 stsp char *line = NULL;
3867 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3868 33aa809d 2019-08-08 stsp ssize_t linelen;
3869 33aa809d 2019-08-08 stsp
3870 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3871 33aa809d 2019-08-08 stsp if (linelen == -1) {
3872 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3873 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3874 33aa809d 2019-08-08 stsp goto done;
3875 33aa809d 2019-08-08 stsp }
3876 33aa809d 2019-08-08 stsp return NULL;
3877 33aa809d 2019-08-08 stsp }
3878 33aa809d 2019-08-08 stsp if (outfile) {
3879 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3880 33aa809d 2019-08-08 stsp if (n != linelen) {
3881 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3882 33aa809d 2019-08-08 stsp goto done;
3883 33aa809d 2019-08-08 stsp }
3884 33aa809d 2019-08-08 stsp }
3885 33aa809d 2019-08-08 stsp if (rejectfile) {
3886 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3887 33aa809d 2019-08-08 stsp if (n != linelen)
3888 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3889 33aa809d 2019-08-08 stsp }
3890 33aa809d 2019-08-08 stsp done:
3891 33aa809d 2019-08-08 stsp free(line);
3892 2ec1f75b 2019-03-26 stsp return err;
3893 2ec1f75b 2019-03-26 stsp }
3894 1f1abb7e 2019-08-08 stsp
3895 33aa809d 2019-08-08 stsp static const struct got_error *
3896 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3897 33aa809d 2019-08-08 stsp {
3898 33aa809d 2019-08-08 stsp char *line = NULL;
3899 33aa809d 2019-08-08 stsp size_t linesize = 0;
3900 33aa809d 2019-08-08 stsp ssize_t linelen;
3901 33aa809d 2019-08-08 stsp
3902 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3903 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3904 500467ff 2019-09-25 hiltjo if (ferror(f))
3905 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3906 500467ff 2019-09-25 hiltjo return NULL;
3907 500467ff 2019-09-25 hiltjo }
3908 33aa809d 2019-08-08 stsp free(line);
3909 33aa809d 2019-08-08 stsp return NULL;
3910 33aa809d 2019-08-08 stsp }
3911 33aa809d 2019-08-08 stsp
3912 33aa809d 2019-08-08 stsp static const struct got_error *
3913 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3914 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3915 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3916 33aa809d 2019-08-08 stsp {
3917 33aa809d 2019-08-08 stsp const struct got_error *err;
3918 33aa809d 2019-08-08 stsp
3919 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3920 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3921 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3922 33aa809d 2019-08-08 stsp if (err)
3923 33aa809d 2019-08-08 stsp return err;
3924 33aa809d 2019-08-08 stsp (*line_cur1)++;
3925 33aa809d 2019-08-08 stsp }
3926 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3927 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3928 33aa809d 2019-08-08 stsp if (rejectfile)
3929 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3930 33aa809d 2019-08-08 stsp else
3931 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3932 33aa809d 2019-08-08 stsp if (err)
3933 33aa809d 2019-08-08 stsp return err;
3934 33aa809d 2019-08-08 stsp (*line_cur2)++;
3935 33aa809d 2019-08-08 stsp }
3936 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3937 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3938 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3939 33aa809d 2019-08-08 stsp if (err)
3940 33aa809d 2019-08-08 stsp return err;
3941 33aa809d 2019-08-08 stsp (*line_cur2)++;
3942 33aa809d 2019-08-08 stsp }
3943 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3944 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3945 33aa809d 2019-08-08 stsp if (rejectfile)
3946 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3947 33aa809d 2019-08-08 stsp else
3948 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3949 33aa809d 2019-08-08 stsp if (err)
3950 33aa809d 2019-08-08 stsp return err;
3951 33aa809d 2019-08-08 stsp (*line_cur1)++;
3952 33aa809d 2019-08-08 stsp }
3953 f1e81a05 2019-08-10 stsp
3954 f1e81a05 2019-08-10 stsp return NULL;
3955 f1e81a05 2019-08-10 stsp }
3956 f1e81a05 2019-08-10 stsp
3957 f1e81a05 2019-08-10 stsp static const struct got_error *
3958 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3959 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3960 f1e81a05 2019-08-10 stsp {
3961 f1e81a05 2019-08-10 stsp const struct got_error *err;
3962 f1e81a05 2019-08-10 stsp
3963 f1e81a05 2019-08-10 stsp if (outfile) {
3964 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3965 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3966 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3967 f1e81a05 2019-08-10 stsp if (err)
3968 f1e81a05 2019-08-10 stsp return err;
3969 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3970 f1e81a05 2019-08-10 stsp }
3971 f1e81a05 2019-08-10 stsp }
3972 f1e81a05 2019-08-10 stsp if (rejectfile) {
3973 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3974 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3975 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3976 f1e81a05 2019-08-10 stsp if (err)
3977 f1e81a05 2019-08-10 stsp return err;
3978 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3979 f1e81a05 2019-08-10 stsp }
3980 33aa809d 2019-08-08 stsp }
3981 33aa809d 2019-08-08 stsp
3982 33aa809d 2019-08-08 stsp return NULL;
3983 33aa809d 2019-08-08 stsp }
3984 33aa809d 2019-08-08 stsp
3985 33aa809d 2019-08-08 stsp static const struct got_error *
3986 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3987 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3988 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3989 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3990 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3991 33aa809d 2019-08-08 stsp {
3992 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3993 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3994 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3995 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3996 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3997 33aa809d 2019-08-08 stsp long pos1, pos2;
3998 33aa809d 2019-08-08 stsp FILE *hunkfile;
3999 33aa809d 2019-08-08 stsp
4000 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4001 33aa809d 2019-08-08 stsp
4002 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4003 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4004 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4005 33aa809d 2019-08-08 stsp
4006 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4007 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4008 33aa809d 2019-08-08 stsp
4009 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4010 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4011 33aa809d 2019-08-08 stsp
4012 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4013 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4014 33aa809d 2019-08-08 stsp goto done;
4015 33aa809d 2019-08-08 stsp }
4016 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4017 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4018 33aa809d 2019-08-08 stsp goto done;
4019 33aa809d 2019-08-08 stsp }
4020 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4021 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4022 33aa809d 2019-08-08 stsp goto done;
4023 33aa809d 2019-08-08 stsp }
4024 33aa809d 2019-08-08 stsp
4025 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4026 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4027 33aa809d 2019-08-08 stsp if (err)
4028 33aa809d 2019-08-08 stsp goto done;
4029 33aa809d 2019-08-08 stsp
4030 33aa809d 2019-08-08 stsp switch (*choice) {
4031 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4032 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4033 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4034 33aa809d 2019-08-08 stsp break;
4035 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4036 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4038 33aa809d 2019-08-08 stsp break;
4039 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4040 33aa809d 2019-08-08 stsp break;
4041 33aa809d 2019-08-08 stsp default:
4042 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4043 33aa809d 2019-08-08 stsp break;
4044 33aa809d 2019-08-08 stsp }
4045 33aa809d 2019-08-08 stsp done:
4046 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4047 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4048 33aa809d 2019-08-08 stsp return err;
4049 33aa809d 2019-08-08 stsp }
4050 33aa809d 2019-08-08 stsp
4051 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4052 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4053 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4054 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4055 1f1abb7e 2019-08-08 stsp void *progress_arg;
4056 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4057 33aa809d 2019-08-08 stsp void *patch_arg;
4058 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4059 1f1abb7e 2019-08-08 stsp };
4060 a129376b 2019-03-28 stsp
4061 e20a8b6f 2019-06-04 stsp static const struct got_error *
4062 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4063 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4064 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4065 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4066 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4067 33aa809d 2019-08-08 stsp {
4068 33aa809d 2019-08-08 stsp const struct got_error *err;
4069 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4070 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4071 1ebedb77 2019-10-19 stsp int fd2 = -1;
4072 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4073 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4074 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4075 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4076 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4077 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4078 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4079 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4080 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4081 33aa809d 2019-08-08 stsp int n = 0;
4082 33aa809d 2019-08-08 stsp
4083 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4084 33aa809d 2019-08-08 stsp
4085 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4086 33aa809d 2019-08-08 stsp if (err)
4087 33aa809d 2019-08-08 stsp return err;
4088 33aa809d 2019-08-08 stsp
4089 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4090 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4091 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4092 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4093 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4094 fa3cef63 2020-07-23 stsp goto done;
4095 fa3cef63 2020-07-23 stsp }
4096 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4097 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4098 fa3cef63 2020-07-23 stsp if (link_len == -1)
4099 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4100 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4101 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4102 12463d8b 2019-12-13 stsp }
4103 12463d8b 2019-12-13 stsp } else {
4104 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4105 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4106 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4107 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4108 fa3cef63 2020-07-23 stsp goto done;
4109 fa3cef63 2020-07-23 stsp }
4110 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4111 fa3cef63 2020-07-23 stsp sizeof(link_target));
4112 fa3cef63 2020-07-23 stsp if (link_len == -1)
4113 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4114 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4115 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4116 12463d8b 2019-12-13 stsp }
4117 1ebedb77 2019-10-19 stsp }
4118 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4119 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4120 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4121 fa3cef63 2020-07-23 stsp goto done;
4122 fa3cef63 2020-07-23 stsp }
4123 1ebedb77 2019-10-19 stsp
4124 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4125 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4126 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4127 fa3cef63 2020-07-23 stsp goto done;
4128 fa3cef63 2020-07-23 stsp }
4129 fa3cef63 2020-07-23 stsp fd2 = -1;
4130 fa3cef63 2020-07-23 stsp } else {
4131 fa3cef63 2020-07-23 stsp size_t n;
4132 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4133 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4134 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4135 fa3cef63 2020-07-23 stsp goto done;
4136 fa3cef63 2020-07-23 stsp }
4137 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4138 fa3cef63 2020-07-23 stsp if (n != link_len) {
4139 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4140 fa3cef63 2020-07-23 stsp goto done;
4141 fa3cef63 2020-07-23 stsp }
4142 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4143 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4144 fa3cef63 2020-07-23 stsp goto done;
4145 fa3cef63 2020-07-23 stsp }
4146 fa3cef63 2020-07-23 stsp rewind(f2);
4147 33aa809d 2019-08-08 stsp }
4148 33aa809d 2019-08-08 stsp
4149 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4150 33aa809d 2019-08-08 stsp if (err)
4151 33aa809d 2019-08-08 stsp goto done;
4152 33aa809d 2019-08-08 stsp
4153 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4154 33aa809d 2019-08-08 stsp if (err)
4155 33aa809d 2019-08-08 stsp goto done;
4156 33aa809d 2019-08-08 stsp
4157 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4158 33aa809d 2019-08-08 stsp if (err)
4159 33aa809d 2019-08-08 stsp goto done;
4160 33aa809d 2019-08-08 stsp
4161 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4162 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4163 33aa809d 2019-08-08 stsp goto done;
4164 33aa809d 2019-08-08 stsp }
4165 33aa809d 2019-08-08 stsp
4166 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4167 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4168 33aa809d 2019-08-08 stsp if (err)
4169 33aa809d 2019-08-08 stsp goto done;
4170 33aa809d 2019-08-08 stsp
4171 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4172 33aa809d 2019-08-08 stsp if (err)
4173 33aa809d 2019-08-08 stsp goto done;
4174 33aa809d 2019-08-08 stsp
4175 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4176 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4177 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4178 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4179 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4180 33aa809d 2019-08-08 stsp int choice;
4181 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4182 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4183 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4184 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4185 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4186 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4187 33aa809d 2019-08-08 stsp if (err)
4188 33aa809d 2019-08-08 stsp goto done;
4189 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4190 33aa809d 2019-08-08 stsp have_content = 1;
4191 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4192 33aa809d 2019-08-08 stsp break;
4193 33aa809d 2019-08-08 stsp }
4194 1ebedb77 2019-10-19 stsp if (have_content) {
4195 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4196 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4197 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4198 1ebedb77 2019-10-19 stsp if (err)
4199 1ebedb77 2019-10-19 stsp goto done;
4200 1ebedb77 2019-10-19 stsp
4201 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4202 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4203 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4204 fa3cef63 2020-07-23 stsp goto done;
4205 fa3cef63 2020-07-23 stsp }
4206 1ebedb77 2019-10-19 stsp }
4207 1ebedb77 2019-10-19 stsp }
4208 33aa809d 2019-08-08 stsp done:
4209 33aa809d 2019-08-08 stsp free(id_str);
4210 33aa809d 2019-08-08 stsp if (blob)
4211 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4212 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4213 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4214 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4215 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4216 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4217 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4218 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4219 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4220 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4221 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4222 33aa809d 2019-08-08 stsp if (err || !have_content) {
4223 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4224 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4225 33aa809d 2019-08-08 stsp free(*path_outfile);
4226 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4227 33aa809d 2019-08-08 stsp }
4228 33aa809d 2019-08-08 stsp free(args);
4229 33aa809d 2019-08-08 stsp if (ds) {
4230 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4231 33aa809d 2019-08-08 stsp free(ds);
4232 33aa809d 2019-08-08 stsp }
4233 33aa809d 2019-08-08 stsp if (changes)
4234 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4235 33aa809d 2019-08-08 stsp free(path1);
4236 33aa809d 2019-08-08 stsp return err;
4237 33aa809d 2019-08-08 stsp }
4238 33aa809d 2019-08-08 stsp
4239 33aa809d 2019-08-08 stsp static const struct got_error *
4240 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4241 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4242 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4243 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4244 a129376b 2019-03-28 stsp {
4245 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4246 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4247 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4248 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4249 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4250 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4251 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4252 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4253 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4254 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4255 a129376b 2019-03-28 stsp
4256 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4257 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4258 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4259 d3bcc3d1 2019-08-08 stsp return NULL;
4260 3d69ad8d 2019-08-17 semarie
4261 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4262 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4263 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4264 d3bcc3d1 2019-08-08 stsp
4265 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4266 65084dad 2019-08-08 stsp if (ie == NULL)
4267 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4268 a129376b 2019-03-28 stsp
4269 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4270 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4271 a129376b 2019-03-28 stsp if (err) {
4272 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4273 a129376b 2019-03-28 stsp goto done;
4274 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4275 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4276 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4277 e20a8b6f 2019-06-04 stsp goto done;
4278 e20a8b6f 2019-06-04 stsp }
4279 a129376b 2019-03-28 stsp }
4280 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4281 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4282 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4283 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4284 a129376b 2019-03-28 stsp goto done;
4285 a129376b 2019-03-28 stsp }
4286 a129376b 2019-03-28 stsp } else {
4287 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4288 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4289 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4290 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4291 a129376b 2019-03-28 stsp goto done;
4292 a129376b 2019-03-28 stsp }
4293 a129376b 2019-03-28 stsp } else {
4294 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4295 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4296 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4297 a129376b 2019-03-28 stsp goto done;
4298 a129376b 2019-03-28 stsp }
4299 a129376b 2019-03-28 stsp }
4300 a129376b 2019-03-28 stsp }
4301 a129376b 2019-03-28 stsp
4302 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4303 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4304 a9fa2909 2019-07-27 stsp if (err) {
4305 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4306 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4307 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4308 a9fa2909 2019-07-27 stsp goto done;
4309 a9fa2909 2019-07-27 stsp } else {
4310 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4311 a9fa2909 2019-07-27 stsp if (err)
4312 a9fa2909 2019-07-27 stsp goto done;
4313 a9fa2909 2019-07-27 stsp
4314 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4315 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4316 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4317 a9fa2909 2019-07-27 stsp goto done;
4318 a9fa2909 2019-07-27 stsp }
4319 a9fa2909 2019-07-27 stsp
4320 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4321 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4322 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4323 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4324 a9fa2909 2019-07-27 stsp goto done;
4325 a9fa2909 2019-07-27 stsp }
4326 a129376b 2019-03-28 stsp }
4327 a129376b 2019-03-28 stsp
4328 a129376b 2019-03-28 stsp switch (status) {
4329 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4330 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4331 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4332 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4333 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4334 33aa809d 2019-08-08 stsp if (err)
4335 33aa809d 2019-08-08 stsp goto done;
4336 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4337 33aa809d 2019-08-08 stsp break;
4338 33aa809d 2019-08-08 stsp }
4339 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4340 1f1abb7e 2019-08-08 stsp ie->path);
4341 1ee397ad 2019-07-12 stsp if (err)
4342 1ee397ad 2019-07-12 stsp goto done;
4343 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4344 a129376b 2019-03-28 stsp break;
4345 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4346 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4347 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4348 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4349 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4350 33aa809d 2019-08-08 stsp if (err)
4351 33aa809d 2019-08-08 stsp goto done;
4352 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4353 33aa809d 2019-08-08 stsp break;
4354 33aa809d 2019-08-08 stsp }
4355 33aa809d 2019-08-08 stsp /* fall through */
4356 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4357 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4358 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4359 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4360 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4361 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4362 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4363 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4364 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4365 24278f30 2019-08-03 stsp } else
4366 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4367 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4368 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4369 a129376b 2019-03-28 stsp if (err)
4370 65084dad 2019-08-08 stsp goto done;
4371 65084dad 2019-08-08 stsp
4372 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4373 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4374 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4375 a129376b 2019-03-28 stsp goto done;
4376 65084dad 2019-08-08 stsp }
4377 65084dad 2019-08-08 stsp
4378 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4379 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4380 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4381 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4382 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4383 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4384 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4385 33aa809d 2019-08-08 stsp break;
4386 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4387 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4388 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4389 369fd7e5 2020-07-23 stsp path_content);
4390 369fd7e5 2020-07-23 stsp break;
4391 369fd7e5 2020-07-23 stsp }
4392 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4393 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4394 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4395 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4396 369fd7e5 2020-07-23 stsp } else {
4397 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4398 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4399 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4400 369fd7e5 2020-07-23 stsp goto done;
4401 369fd7e5 2020-07-23 stsp }
4402 33aa809d 2019-08-08 stsp }
4403 33aa809d 2019-08-08 stsp } else {
4404 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4405 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4406 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4407 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4408 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4409 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4410 2e1fa222 2020-07-23 stsp } else {
4411 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4412 2e1fa222 2020-07-23 stsp ie->path,
4413 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4414 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4415 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4416 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4417 2e1fa222 2020-07-23 stsp }
4418 a129376b 2019-03-28 stsp if (err)
4419 a129376b 2019-03-28 stsp goto done;
4420 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4421 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4422 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4423 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4424 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4425 2e1fa222 2020-07-23 stsp if (err)
4426 2e1fa222 2020-07-23 stsp goto done;
4427 2e1fa222 2020-07-23 stsp }
4428 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4429 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4430 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4431 33aa809d 2019-08-08 stsp }
4432 a129376b 2019-03-28 stsp }
4433 a129376b 2019-03-28 stsp break;
4434 e20a8b6f 2019-06-04 stsp }
4435 a129376b 2019-03-28 stsp default:
4436 1f1abb7e 2019-08-08 stsp break;
4437 a129376b 2019-03-28 stsp }
4438 a129376b 2019-03-28 stsp done:
4439 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4440 33aa809d 2019-08-08 stsp free(path_content);
4441 e20a8b6f 2019-06-04 stsp free(parent_path);
4442 a129376b 2019-03-28 stsp free(tree_path);
4443 a129376b 2019-03-28 stsp if (blob)
4444 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4445 a129376b 2019-03-28 stsp if (tree)
4446 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4447 a129376b 2019-03-28 stsp free(tree_id);
4448 e20a8b6f 2019-06-04 stsp return err;
4449 e20a8b6f 2019-06-04 stsp }
4450 e20a8b6f 2019-06-04 stsp
4451 e20a8b6f 2019-06-04 stsp const struct got_error *
4452 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4453 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4454 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4455 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4456 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4457 e20a8b6f 2019-06-04 stsp {
4458 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4459 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4460 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4461 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4462 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4463 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4464 e20a8b6f 2019-06-04 stsp
4465 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4466 e20a8b6f 2019-06-04 stsp if (err)
4467 e20a8b6f 2019-06-04 stsp return err;
4468 e20a8b6f 2019-06-04 stsp
4469 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4470 e20a8b6f 2019-06-04 stsp if (err)
4471 e20a8b6f 2019-06-04 stsp goto done;
4472 e20a8b6f 2019-06-04 stsp
4473 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4474 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4475 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4476 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4477 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4478 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4479 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4480 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4481 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4482 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4483 e20a8b6f 2019-06-04 stsp if (err)
4484 af12c6b9 2019-06-04 stsp break;
4485 e20a8b6f 2019-06-04 stsp }
4486 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4487 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4488 e20a8b6f 2019-06-04 stsp err = sync_err;
4489 af12c6b9 2019-06-04 stsp done:
4490 fb399478 2019-07-12 stsp free(fileindex_path);
4491 a129376b 2019-03-28 stsp if (fileindex)
4492 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4493 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4494 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4495 a129376b 2019-03-28 stsp err = unlockerr;
4496 c4296144 2019-05-09 stsp return err;
4497 c4296144 2019-05-09 stsp }
4498 c4296144 2019-05-09 stsp
4499 cf066bf8 2019-05-09 stsp static void
4500 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4501 cf066bf8 2019-05-09 stsp {
4502 24519714 2019-05-09 stsp free(ct->path);
4503 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4504 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4505 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4506 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4507 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4508 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4509 cf066bf8 2019-05-09 stsp free(ct);
4510 cf066bf8 2019-05-09 stsp }
4511 24519714 2019-05-09 stsp
4512 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4513 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4514 24519714 2019-05-09 stsp struct got_repository *repo;
4515 24519714 2019-05-09 stsp struct got_worktree *worktree;
4516 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4517 5f8a88c6 2019-08-03 stsp int have_staged_files;
4518 24519714 2019-05-09 stsp };
4519 cf066bf8 2019-05-09 stsp
4520 c4296144 2019-05-09 stsp static const struct got_error *
4521 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4522 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4523 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4524 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4525 c4296144 2019-05-09 stsp {
4526 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4527 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4528 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4529 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4530 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4531 768aea60 2019-05-09 stsp struct stat sb;
4532 c4296144 2019-05-09 stsp
4533 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4534 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4535 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4536 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4537 5f8a88c6 2019-08-03 stsp return NULL;
4538 5f8a88c6 2019-08-03 stsp } else {
4539 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4540 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4541 c4296144 2019-05-09 stsp
4542 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4543 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4544 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4545 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4546 5f8a88c6 2019-08-03 stsp return NULL;
4547 5f8a88c6 2019-08-03 stsp }
4548 0b5cc0d6 2019-05-09 stsp
4549 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4550 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4551 036813ee 2019-05-09 stsp goto done;
4552 036813ee 2019-05-09 stsp }
4553 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4554 036813ee 2019-05-09 stsp parent_path = strdup("");
4555 69960a46 2019-05-09 stsp if (parent_path == NULL)
4556 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4557 69960a46 2019-05-09 stsp } else {
4558 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4559 69960a46 2019-05-09 stsp if (err)
4560 69960a46 2019-05-09 stsp return err;
4561 69960a46 2019-05-09 stsp }
4562 c4296144 2019-05-09 stsp
4563 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4564 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4565 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4566 c4296144 2019-05-09 stsp goto done;
4567 768aea60 2019-05-09 stsp }
4568 768aea60 2019-05-09 stsp
4569 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4570 768aea60 2019-05-09 stsp relpath) == -1) {
4571 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4572 768aea60 2019-05-09 stsp goto done;
4573 768aea60 2019-05-09 stsp }
4574 0aeb8099 2020-07-23 stsp
4575 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4576 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4577 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4578 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4579 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4580 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4581 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4582 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4583 0aeb8099 2020-07-23 stsp break;
4584 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4585 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4586 0aeb8099 2020-07-23 stsp break;
4587 0aeb8099 2020-07-23 stsp default:
4588 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4589 0aeb8099 2020-07-23 stsp goto done;
4590 0aeb8099 2020-07-23 stsp }
4591 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4592 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4593 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4594 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4595 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4596 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4597 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4598 12463d8b 2019-12-13 stsp ct->ondisk_path);
4599 12463d8b 2019-12-13 stsp goto done;
4600 12463d8b 2019-12-13 stsp }
4601 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4602 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4603 768aea60 2019-05-09 stsp goto done;
4604 768aea60 2019-05-09 stsp }
4605 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4606 c4296144 2019-05-09 stsp }
4607 c4296144 2019-05-09 stsp
4608 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4609 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4610 44d03001 2019-05-09 stsp relpath) == -1) {
4611 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4612 44d03001 2019-05-09 stsp goto done;
4613 44d03001 2019-05-09 stsp }
4614 44d03001 2019-05-09 stsp
4615 cf066bf8 2019-05-09 stsp ct->status = status;
4616 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4617 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4618 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4619 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4620 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4621 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4622 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4623 b416585c 2019-05-13 stsp goto done;
4624 b416585c 2019-05-13 stsp }
4625 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4626 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4627 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4628 f0b75401 2019-08-03 stsp goto done;
4629 f0b75401 2019-08-03 stsp }
4630 f0b75401 2019-08-03 stsp }
4631 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4632 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4633 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4634 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4635 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4636 036813ee 2019-05-09 stsp goto done;
4637 036813ee 2019-05-09 stsp }
4638 ca2503ea 2019-05-09 stsp }
4639 24519714 2019-05-09 stsp ct->path = strdup(path);
4640 24519714 2019-05-09 stsp if (ct->path == NULL) {
4641 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4642 24519714 2019-05-09 stsp goto done;
4643 24519714 2019-05-09 stsp }
4644 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4645 c4296144 2019-05-09 stsp done:
4646 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4647 ed175427 2019-05-09 stsp free_commitable(ct);
4648 24519714 2019-05-09 stsp free(parent_path);
4649 036813ee 2019-05-09 stsp free(path);
4650 a129376b 2019-03-28 stsp return err;
4651 a129376b 2019-03-28 stsp }
4652 c4296144 2019-05-09 stsp
4653 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4654 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4655 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4656 036813ee 2019-05-09 stsp struct got_repository *);
4657 ed175427 2019-05-09 stsp
4658 ed175427 2019-05-09 stsp static const struct got_error *
4659 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4660 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4661 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4662 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4663 afa376bf 2019-05-09 stsp struct got_repository *repo)
4664 ed175427 2019-05-09 stsp {
4665 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4666 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4667 036813ee 2019-05-09 stsp char *subpath;
4668 ed175427 2019-05-09 stsp
4669 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4670 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4672 ed175427 2019-05-09 stsp
4673 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4674 ed175427 2019-05-09 stsp if (err)
4675 ed175427 2019-05-09 stsp return err;
4676 ed175427 2019-05-09 stsp
4677 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4678 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4679 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4680 036813ee 2019-05-09 stsp free(subpath);
4681 036813ee 2019-05-09 stsp return err;
4682 036813ee 2019-05-09 stsp }
4683 ed175427 2019-05-09 stsp
4684 036813ee 2019-05-09 stsp static const struct got_error *
4685 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4686 036813ee 2019-05-09 stsp {
4687 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4688 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4689 ed175427 2019-05-09 stsp
4690 036813ee 2019-05-09 stsp *match = 0;
4691 ed175427 2019-05-09 stsp
4692 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4693 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4694 0f63689d 2019-05-10 stsp return NULL;
4695 036813ee 2019-05-09 stsp }
4696 0b5cc0d6 2019-05-09 stsp
4697 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4698 0f63689d 2019-05-10 stsp if (err)
4699 0f63689d 2019-05-10 stsp return err;
4700 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4701 036813ee 2019-05-09 stsp free(ct_parent_path);
4702 036813ee 2019-05-09 stsp return err;
4703 036813ee 2019-05-09 stsp }
4704 0b5cc0d6 2019-05-09 stsp
4705 768aea60 2019-05-09 stsp static mode_t
4706 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4707 768aea60 2019-05-09 stsp {
4708 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4709 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4710 3d9a4ec4 2020-07-23 stsp
4711 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4712 768aea60 2019-05-09 stsp }
4713 768aea60 2019-05-09 stsp
4714 036813ee 2019-05-09 stsp static const struct got_error *
4715 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4716 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4717 036813ee 2019-05-09 stsp {
4718 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4719 ca2503ea 2019-05-09 stsp
4720 036813ee 2019-05-09 stsp *new_te = NULL;
4721 0b5cc0d6 2019-05-09 stsp
4722 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4723 036813ee 2019-05-09 stsp if (err)
4724 036813ee 2019-05-09 stsp goto done;
4725 0b5cc0d6 2019-05-09 stsp
4726 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4727 036813ee 2019-05-09 stsp
4728 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4729 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4730 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4731 5f8a88c6 2019-08-03 stsp else
4732 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4733 036813ee 2019-05-09 stsp done:
4734 036813ee 2019-05-09 stsp if (err && *new_te) {
4735 56e0773d 2019-11-28 stsp free(*new_te);
4736 036813ee 2019-05-09 stsp *new_te = NULL;
4737 036813ee 2019-05-09 stsp }
4738 036813ee 2019-05-09 stsp return err;
4739 036813ee 2019-05-09 stsp }
4740 036813ee 2019-05-09 stsp
4741 036813ee 2019-05-09 stsp static const struct got_error *
4742 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4743 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4744 036813ee 2019-05-09 stsp {
4745 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4746 036813ee 2019-05-09 stsp char *ct_name;
4747 036813ee 2019-05-09 stsp
4748 036813ee 2019-05-09 stsp *new_te = NULL;
4749 036813ee 2019-05-09 stsp
4750 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4751 036813ee 2019-05-09 stsp if (*new_te == NULL)
4752 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4753 036813ee 2019-05-09 stsp
4754 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4755 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4756 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4757 036813ee 2019-05-09 stsp goto done;
4758 036813ee 2019-05-09 stsp }
4759 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4760 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4761 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4762 036813ee 2019-05-09 stsp goto done;
4763 036813ee 2019-05-09 stsp }
4764 036813ee 2019-05-09 stsp
4765 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4766 036813ee 2019-05-09 stsp
4767 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4768 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4769 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4770 5f8a88c6 2019-08-03 stsp else
4771 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4772 036813ee 2019-05-09 stsp done:
4773 036813ee 2019-05-09 stsp if (err && *new_te) {
4774 56e0773d 2019-11-28 stsp free(*new_te);
4775 036813ee 2019-05-09 stsp *new_te = NULL;
4776 036813ee 2019-05-09 stsp }
4777 036813ee 2019-05-09 stsp return err;
4778 036813ee 2019-05-09 stsp }
4779 036813ee 2019-05-09 stsp
4780 036813ee 2019-05-09 stsp static const struct got_error *
4781 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4782 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4783 036813ee 2019-05-09 stsp {
4784 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4785 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4786 036813ee 2019-05-09 stsp
4787 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4788 036813ee 2019-05-09 stsp if (err)
4789 036813ee 2019-05-09 stsp return err;
4790 036813ee 2019-05-09 stsp if (new_pe == NULL)
4791 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4792 036813ee 2019-05-09 stsp return NULL;
4793 afa376bf 2019-05-09 stsp }
4794 afa376bf 2019-05-09 stsp
4795 afa376bf 2019-05-09 stsp static const struct got_error *
4796 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4797 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4798 afa376bf 2019-05-09 stsp {
4799 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4800 5f8a88c6 2019-08-03 stsp unsigned char status;
4801 5f8a88c6 2019-08-03 stsp
4802 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4803 afa376bf 2019-05-09 stsp ct_path++;
4804 5f8a88c6 2019-08-03 stsp
4805 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4806 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4807 5f8a88c6 2019-08-03 stsp else
4808 5f8a88c6 2019-08-03 stsp status = ct->status;
4809 5f8a88c6 2019-08-03 stsp
4810 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4811 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4812 036813ee 2019-05-09 stsp }
4813 036813ee 2019-05-09 stsp
4814 036813ee 2019-05-09 stsp static const struct got_error *
4815 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4816 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4817 44d03001 2019-05-09 stsp {
4818 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4819 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4820 44d03001 2019-05-09 stsp char *te_path;
4821 44d03001 2019-05-09 stsp
4822 44d03001 2019-05-09 stsp *modified = 0;
4823 44d03001 2019-05-09 stsp
4824 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4825 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4826 44d03001 2019-05-09 stsp te->name) == -1)
4827 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4828 44d03001 2019-05-09 stsp
4829 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4830 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4831 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4832 44d03001 2019-05-09 stsp strlen(te_path));
4833 44d03001 2019-05-09 stsp if (*modified)
4834 44d03001 2019-05-09 stsp break;
4835 44d03001 2019-05-09 stsp }
4836 44d03001 2019-05-09 stsp
4837 44d03001 2019-05-09 stsp free(te_path);
4838 44d03001 2019-05-09 stsp return err;
4839 44d03001 2019-05-09 stsp }
4840 44d03001 2019-05-09 stsp
4841 44d03001 2019-05-09 stsp static const struct got_error *
4842 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4843 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4844 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4845 036813ee 2019-05-09 stsp {
4846 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4847 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4848 036813ee 2019-05-09 stsp
4849 036813ee 2019-05-09 stsp *ctp = NULL;
4850 036813ee 2019-05-09 stsp
4851 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4852 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4853 036813ee 2019-05-09 stsp char *ct_name = NULL;
4854 036813ee 2019-05-09 stsp int path_matches;
4855 036813ee 2019-05-09 stsp
4856 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4857 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4858 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4859 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4860 5f8a88c6 2019-08-03 stsp continue;
4861 5f8a88c6 2019-08-03 stsp } else {
4862 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4863 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4864 5f8a88c6 2019-08-03 stsp continue;
4865 5f8a88c6 2019-08-03 stsp }
4866 036813ee 2019-05-09 stsp
4867 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4868 036813ee 2019-05-09 stsp continue;
4869 036813ee 2019-05-09 stsp
4870 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4871 036813ee 2019-05-09 stsp if (err)
4872 036813ee 2019-05-09 stsp return err;
4873 036813ee 2019-05-09 stsp if (!path_matches)
4874 036813ee 2019-05-09 stsp continue;
4875 036813ee 2019-05-09 stsp
4876 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4877 036813ee 2019-05-09 stsp if (ct_name == NULL)
4878 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4879 036813ee 2019-05-09 stsp
4880 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4881 036813ee 2019-05-09 stsp continue;
4882 036813ee 2019-05-09 stsp
4883 036813ee 2019-05-09 stsp *ctp = ct;
4884 036813ee 2019-05-09 stsp break;
4885 036813ee 2019-05-09 stsp }
4886 2b496619 2019-07-10 stsp
4887 2b496619 2019-07-10 stsp return err;
4888 2b496619 2019-07-10 stsp }
4889 2b496619 2019-07-10 stsp
4890 2b496619 2019-07-10 stsp static const struct got_error *
4891 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4892 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4893 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4894 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4895 2b496619 2019-07-10 stsp struct got_repository *repo)
4896 2b496619 2019-07-10 stsp {
4897 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4898 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4899 2b496619 2019-07-10 stsp char *subtree_path;
4900 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4901 ba580f68 2020-03-22 stsp int nentries;
4902 2b496619 2019-07-10 stsp
4903 2b496619 2019-07-10 stsp *new_tep = NULL;
4904 2b496619 2019-07-10 stsp
4905 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4906 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4907 2b496619 2019-07-10 stsp child_path) == -1)
4908 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4909 036813ee 2019-05-09 stsp
4910 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4911 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4912 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4913 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4914 56e0773d 2019-11-28 stsp
4915 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4916 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4917 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4918 2b496619 2019-07-10 stsp goto done;
4919 2b496619 2019-07-10 stsp }
4920 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4921 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4922 2b496619 2019-07-10 stsp if (err) {
4923 56e0773d 2019-11-28 stsp free(new_te);
4924 2b496619 2019-07-10 stsp goto done;
4925 2b496619 2019-07-10 stsp }
4926 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4927 2b496619 2019-07-10 stsp done:
4928 56e0773d 2019-11-28 stsp free(id);
4929 2b496619 2019-07-10 stsp free(subtree_path);
4930 2b496619 2019-07-10 stsp if (err == NULL)
4931 2b496619 2019-07-10 stsp *new_tep = new_te;
4932 036813ee 2019-05-09 stsp return err;
4933 036813ee 2019-05-09 stsp }
4934 036813ee 2019-05-09 stsp
4935 036813ee 2019-05-09 stsp static const struct got_error *
4936 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4937 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4938 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4939 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4940 036813ee 2019-05-09 stsp struct got_repository *repo)
4941 036813ee 2019-05-09 stsp {
4942 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4943 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4944 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4945 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4946 036813ee 2019-05-09 stsp
4947 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4948 ba580f68 2020-03-22 stsp *nentries = 0;
4949 036813ee 2019-05-09 stsp
4950 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4951 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4952 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4953 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4954 036813ee 2019-05-09 stsp
4955 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4956 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4957 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4958 036813ee 2019-05-09 stsp continue;
4959 036813ee 2019-05-09 stsp
4960 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4961 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4962 036813ee 2019-05-09 stsp continue;
4963 036813ee 2019-05-09 stsp
4964 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4965 036813ee 2019-05-09 stsp pe->path);
4966 036813ee 2019-05-09 stsp if (err)
4967 036813ee 2019-05-09 stsp goto done;
4968 036813ee 2019-05-09 stsp
4969 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4970 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4971 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4972 036813ee 2019-05-09 stsp if (err)
4973 036813ee 2019-05-09 stsp goto done;
4974 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4975 afa376bf 2019-05-09 stsp if (err)
4976 afa376bf 2019-05-09 stsp goto done;
4977 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4978 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4979 2b496619 2019-07-10 stsp if (err)
4980 2f51b5b3 2019-05-09 stsp goto done;
4981 ba580f68 2020-03-22 stsp (*nentries)++;
4982 2b496619 2019-07-10 stsp } else {
4983 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4984 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4985 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4986 2b496619 2019-07-10 stsp == NULL) {
4987 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4988 2b496619 2019-07-10 stsp child_path, path_base_tree,
4989 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4990 2b496619 2019-07-10 stsp repo);
4991 2b496619 2019-07-10 stsp if (err)
4992 2b496619 2019-07-10 stsp goto done;
4993 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4994 2b496619 2019-07-10 stsp if (err)
4995 2b496619 2019-07-10 stsp goto done;
4996 ba580f68 2020-03-22 stsp (*nentries)++;
4997 9ba0479c 2019-05-10 stsp }
4998 ed175427 2019-05-09 stsp }
4999 2f51b5b3 2019-05-09 stsp }
5000 2f51b5b3 2019-05-09 stsp
5001 2f51b5b3 2019-05-09 stsp if (base_tree) {
5002 56e0773d 2019-11-28 stsp int i, nbase_entries;
5003 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5004 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5005 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5006 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5007 63c5ca5d 2019-08-24 stsp
5008 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5009 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5010 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5011 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5012 63c5ca5d 2019-08-24 stsp if (err)
5013 63c5ca5d 2019-08-24 stsp goto done;
5014 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5015 63c5ca5d 2019-08-24 stsp if (err)
5016 63c5ca5d 2019-08-24 stsp goto done;
5017 ba580f68 2020-03-22 stsp (*nentries)++;
5018 63c5ca5d 2019-08-24 stsp continue;
5019 63c5ca5d 2019-08-24 stsp }
5020 2f51b5b3 2019-05-09 stsp
5021 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5022 44d03001 2019-05-09 stsp int modified;
5023 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5024 036813ee 2019-05-09 stsp if (err)
5025 036813ee 2019-05-09 stsp goto done;
5026 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5027 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5028 2f51b5b3 2019-05-09 stsp if (err)
5029 2f51b5b3 2019-05-09 stsp goto done;
5030 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5031 44d03001 2019-05-09 stsp if (modified) {
5032 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5033 ba580f68 2020-03-22 stsp int nsubentries;
5034 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5035 ba580f68 2020-03-22 stsp &nsubentries, te,
5036 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5037 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5038 44d03001 2019-05-09 stsp if (err)
5039 44d03001 2019-05-09 stsp goto done;
5040 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5041 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5042 ba580f68 2020-03-22 stsp free(new_id);
5043 ba580f68 2020-03-22 stsp continue;
5044 ba580f68 2020-03-22 stsp }
5045 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5046 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5047 56e0773d 2019-11-28 stsp free(new_id);
5048 44d03001 2019-05-09 stsp }
5049 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5050 036813ee 2019-05-09 stsp if (err)
5051 036813ee 2019-05-09 stsp goto done;
5052 ba580f68 2020-03-22 stsp (*nentries)++;
5053 2f51b5b3 2019-05-09 stsp continue;
5054 036813ee 2019-05-09 stsp }
5055 2f51b5b3 2019-05-09 stsp
5056 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5057 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5058 f66c734c 2019-09-22 stsp if (err)
5059 f66c734c 2019-09-22 stsp goto done;
5060 2f51b5b3 2019-05-09 stsp if (ct) {
5061 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5062 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5063 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5064 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5065 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5066 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5067 2f51b5b3 2019-05-09 stsp if (err)
5068 2f51b5b3 2019-05-09 stsp goto done;
5069 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5070 2f51b5b3 2019-05-09 stsp if (err)
5071 2f51b5b3 2019-05-09 stsp goto done;
5072 ba580f68 2020-03-22 stsp (*nentries)++;
5073 2f51b5b3 2019-05-09 stsp }
5074 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5075 b416585c 2019-05-13 stsp status_arg);
5076 afa376bf 2019-05-09 stsp if (err)
5077 afa376bf 2019-05-09 stsp goto done;
5078 2f51b5b3 2019-05-09 stsp } else {
5079 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5080 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5081 2f51b5b3 2019-05-09 stsp if (err)
5082 2f51b5b3 2019-05-09 stsp goto done;
5083 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5084 2f51b5b3 2019-05-09 stsp if (err)
5085 2f51b5b3 2019-05-09 stsp goto done;
5086 ba580f68 2020-03-22 stsp (*nentries)++;
5087 2f51b5b3 2019-05-09 stsp }
5088 ca2503ea 2019-05-09 stsp }
5089 ed175427 2019-05-09 stsp }
5090 0b5cc0d6 2019-05-09 stsp
5091 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5092 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5093 ed175427 2019-05-09 stsp done:
5094 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5095 ed175427 2019-05-09 stsp return err;
5096 ed175427 2019-05-09 stsp }
5097 ed175427 2019-05-09 stsp
5098 ebf99748 2019-05-09 stsp static const struct got_error *
5099 2e1fa222 2020-07-23 stsp reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5100 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5101 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie, struct got_repository *repo)
5102 2e1fa222 2020-07-23 stsp {
5103 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5104 2e1fa222 2020-07-23 stsp struct got_blob_object *blob = NULL;
5105 2e1fa222 2020-07-23 stsp struct got_object_id *tree_id = NULL;
5106 2e1fa222 2020-07-23 stsp char *tree_path = NULL;
5107 2e1fa222 2020-07-23 stsp struct got_tree_object *tree = NULL;
5108 2e1fa222 2020-07-23 stsp struct got_tree_entry *te;
5109 2e1fa222 2020-07-23 stsp char *entry_name;
5110 0aeb8099 2020-07-23 stsp unsigned char status;
5111 0aeb8099 2020-07-23 stsp struct stat sb;
5112 0aeb8099 2020-07-23 stsp
5113 0aeb8099 2020-07-23 stsp err = get_file_status(&status, &sb, ie, ct->ondisk_path,
5114 0aeb8099 2020-07-23 stsp -1, NULL, repo);
5115 0aeb8099 2020-07-23 stsp if (err)
5116 0aeb8099 2020-07-23 stsp return err;
5117 0aeb8099 2020-07-23 stsp if (status != GOT_STATUS_NO_CHANGE)
5118 0aeb8099 2020-07-23 stsp return NULL;
5119 2e1fa222 2020-07-23 stsp
5120 c631b115 2020-07-23 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5121 c631b115 2020-07-23 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5122 c631b115 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->staged_blob_id,
5123 c631b115 2020-07-23 stsp PATH_MAX);
5124 c631b115 2020-07-23 stsp if (err)
5125 c631b115 2020-07-23 stsp return err;
5126 c631b115 2020-07-23 stsp } else {
5127 c631b115 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5128 c631b115 2020-07-23 stsp PATH_MAX);
5129 c631b115 2020-07-23 stsp if (err)
5130 c631b115 2020-07-23 stsp return err;
5131 c631b115 2020-07-23 stsp }
5132 2e1fa222 2020-07-23 stsp
5133 2e1fa222 2020-07-23 stsp err = got_path_dirname(&tree_path, ct->in_repo_path);
5134 2e1fa222 2020-07-23 stsp if (err) {
5135 2e1fa222 2020-07-23 stsp if (err->code != GOT_ERR_BAD_PATH)
5136 2e1fa222 2020-07-23 stsp goto done;
5137 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
5138 2e1fa222 2020-07-23 stsp new_base_commit_id, "");
5139 2e1fa222 2020-07-23 stsp if (err)
5140 2e1fa222 2020-07-23 stsp goto done;
5141 2e1fa222 2020-07-23 stsp } else {
5142 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
5143 2e1fa222 2020-07-23 stsp new_base_commit_id, tree_path);
5144 2e1fa222 2020-07-23 stsp if (err)
5145 2e1fa222 2020-07-23 stsp goto done;
5146 2e1fa222 2020-07-23 stsp }
5147 2e1fa222 2020-07-23 stsp
5148 2e1fa222 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5149 2e1fa222 2020-07-23 stsp if (err)
5150 2e1fa222 2020-07-23 stsp goto done;
5151 2e1fa222 2020-07-23 stsp
5152 2e1fa222 2020-07-23 stsp entry_name = basename(ct->path);
5153 2e1fa222 2020-07-23 stsp if (entry_name == NULL) {
5154 2e1fa222 2020-07-23 stsp err = got_error_from_errno2("basename", ct->path);
5155 2e1fa222 2020-07-23 stsp goto done;
5156 2e1fa222 2020-07-23 stsp }
5157 2e1fa222 2020-07-23 stsp
5158 2e1fa222 2020-07-23 stsp te = got_object_tree_find_entry(tree, entry_name);
5159 2e1fa222 2020-07-23 stsp if (te == NULL) {
5160 2e1fa222 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5161 2e1fa222 2020-07-23 stsp goto done;
5162 2e1fa222 2020-07-23 stsp }
5163 2e1fa222 2020-07-23 stsp
5164 2e1fa222 2020-07-23 stsp err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5165 c90c8ce3 2020-07-23 stsp ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5166 2e1fa222 2020-07-23 stsp done:
5167 2e1fa222 2020-07-23 stsp if (blob)
5168 2e1fa222 2020-07-23 stsp got_object_blob_close(blob);
5169 2e1fa222 2020-07-23 stsp if (tree)
5170 2e1fa222 2020-07-23 stsp got_object_tree_close(tree);
5171 2e1fa222 2020-07-23 stsp free(tree_id);
5172 2e1fa222 2020-07-23 stsp free(tree_path);
5173 2e1fa222 2020-07-23 stsp return err;
5174 2e1fa222 2020-07-23 stsp }
5175 2e1fa222 2020-07-23 stsp
5176 2e1fa222 2020-07-23 stsp /*
5177 2e1fa222 2020-07-23 stsp * After comitting a symlink we have a chance to convert "bad" symlinks
5178 2e1fa222 2020-07-23 stsp * (those which point outside the work tree or into .got) to regular files.
5179 2e1fa222 2020-07-23 stsp * This way, the post-commit work tree state matches a fresh checkout of
5180 2e1fa222 2020-07-23 stsp * the tree which was just committed. We also mark such newly committed
5181 2e1fa222 2020-07-23 stsp * symlinks as "bad" in the work tree's fileindex.
5182 2e1fa222 2020-07-23 stsp */
5183 2e1fa222 2020-07-23 stsp static const struct got_error *
5184 2e1fa222 2020-07-23 stsp reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5185 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5186 2e1fa222 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo)
5187 2e1fa222 2020-07-23 stsp {
5188 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5189 2e1fa222 2020-07-23 stsp struct got_pathlist_entry *pe;
5190 2e1fa222 2020-07-23 stsp
5191 2e1fa222 2020-07-23 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5192 2e1fa222 2020-07-23 stsp struct got_commitable *ct = pe->data;
5193 2e1fa222 2020-07-23 stsp struct got_fileindex_entry *ie;
5194 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5195 2e1fa222 2020-07-23 stsp
5196 c631b115 2020-07-23 stsp if (!S_ISLNK(get_ct_file_mode(ct)) ||
5197 c631b115 2020-07-23 stsp ct->staged_status == GOT_STATUS_DELETE ||
5198 c631b115 2020-07-23 stsp ct->status == GOT_STATUS_DELETE)
5199 2e1fa222 2020-07-23 stsp continue;
5200 2e1fa222 2020-07-23 stsp
5201 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, ct->path,
5202 0aeb8099 2020-07-23 stsp strlen(ct->path));
5203 0aeb8099 2020-07-23 stsp if (ie == NULL) {
5204 0aeb8099 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_BAD_PATH);
5205 0aeb8099 2020-07-23 stsp break;
5206 0aeb8099 2020-07-23 stsp }
5207 2e1fa222 2020-07-23 stsp err = reinstall_symlink_after_commit(&is_bad_symlink,
5208 0aeb8099 2020-07-23 stsp ct, new_base_commit_id, worktree, ie, repo);
5209 2e1fa222 2020-07-23 stsp if (err)
5210 2e1fa222 2020-07-23 stsp break;
5211 2e1fa222 2020-07-23 stsp if (ie && is_bad_symlink) {
5212 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5213 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5214 2e1fa222 2020-07-23 stsp }
5215 2e1fa222 2020-07-23 stsp }
5216 2e1fa222 2020-07-23 stsp
5217 2e1fa222 2020-07-23 stsp return err;
5218 2e1fa222 2020-07-23 stsp }
5219 2e1fa222 2020-07-23 stsp
5220 2e1fa222 2020-07-23 stsp static const struct got_error *
5221 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5222 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5223 72fd46fa 2019-09-06 stsp int have_staged_files)
5224 ebf99748 2019-05-09 stsp {
5225 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5226 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5227 ebf99748 2019-05-09 stsp
5228 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5229 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5230 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5231 ebf99748 2019-05-09 stsp
5232 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5233 ebf99748 2019-05-09 stsp if (ie) {
5234 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5235 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5236 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5237 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5238 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5239 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5240 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5241 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5242 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5243 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5244 72fd46fa 2019-09-06 stsp !have_staged_files);
5245 ebf99748 2019-05-09 stsp } else
5246 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5247 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5248 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5249 72fd46fa 2019-09-06 stsp !have_staged_files);
5250 ebf99748 2019-05-09 stsp } else {
5251 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5252 ebf99748 2019-05-09 stsp if (err)
5253 af12c6b9 2019-06-04 stsp break;
5254 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5255 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5256 3969253a 2020-03-07 stsp if (err) {
5257 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5258 3969253a 2020-03-07 stsp break;
5259 3969253a 2020-03-07 stsp }
5260 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5261 3969253a 2020-03-07 stsp if (err) {
5262 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5263 af12c6b9 2019-06-04 stsp break;
5264 3969253a 2020-03-07 stsp }
5265 ebf99748 2019-05-09 stsp }
5266 ebf99748 2019-05-09 stsp }
5267 d56d26ce 2019-05-10 stsp return err;
5268 d56d26ce 2019-05-10 stsp }
5269 735ef5ac 2019-08-03 stsp
5270 d56d26ce 2019-05-10 stsp
5271 d56d26ce 2019-05-10 stsp static const struct got_error *
5272 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5273 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5274 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5275 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5276 735ef5ac 2019-08-03 stsp int ood_errcode)
5277 d56d26ce 2019-05-10 stsp {
5278 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5279 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5280 1a36436d 2019-06-10 stsp
5281 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5282 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5283 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5284 1a36436d 2019-06-10 stsp return NULL;
5285 9bead371 2019-07-28 stsp /*
5286 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5287 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5288 9bead371 2019-07-28 stsp */
5289 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5290 735ef5ac 2019-08-03 stsp in_repo_path);
5291 9bead371 2019-07-28 stsp if (err) {
5292 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5293 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5294 1a36436d 2019-06-10 stsp goto done;
5295 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5296 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5297 1a36436d 2019-06-10 stsp } else {
5298 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5299 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5300 735ef5ac 2019-08-03 stsp in_repo_path);
5301 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5302 1a36436d 2019-06-10 stsp goto done;
5303 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5304 1a36436d 2019-06-10 stsp }
5305 1a36436d 2019-06-10 stsp done:
5306 1a36436d 2019-06-10 stsp free(id);
5307 1a36436d 2019-06-10 stsp return err;
5308 ebf99748 2019-05-09 stsp }
5309 ebf99748 2019-05-09 stsp
5310 c4296144 2019-05-09 stsp const struct got_error *
5311 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5312 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5313 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5314 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5315 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5316 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5317 afa376bf 2019-05-09 stsp struct got_repository *repo)
5318 c4296144 2019-05-09 stsp {
5319 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5320 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5321 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5322 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5323 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5324 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5325 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5326 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5327 ba580f68 2020-03-22 stsp int nentries;
5328 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5329 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5330 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5331 c4296144 2019-05-09 stsp
5332 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5333 c4296144 2019-05-09 stsp
5334 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5335 675c7539 2019-05-09 stsp
5336 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5337 588edf97 2019-05-10 stsp if (err)
5338 588edf97 2019-05-10 stsp goto done;
5339 de18fc63 2019-05-09 stsp
5340 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5341 588edf97 2019-05-10 stsp if (err)
5342 588edf97 2019-05-10 stsp goto done;
5343 588edf97 2019-05-10 stsp
5344 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5345 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5346 33ad4cbe 2019-05-12 jcs if (err)
5347 33ad4cbe 2019-05-12 jcs goto done;
5348 33ad4cbe 2019-05-12 jcs }
5349 c4296144 2019-05-09 stsp
5350 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5351 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5352 33ad4cbe 2019-05-12 jcs goto done;
5353 33ad4cbe 2019-05-12 jcs }
5354 33ad4cbe 2019-05-12 jcs
5355 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5356 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5357 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5358 cf066bf8 2019-05-09 stsp char *ondisk_path;
5359 cf066bf8 2019-05-09 stsp
5360 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5361 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5362 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5363 5f8a88c6 2019-08-03 stsp continue;
5364 5f8a88c6 2019-08-03 stsp
5365 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5366 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5367 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5368 cf066bf8 2019-05-09 stsp continue;
5369 cf066bf8 2019-05-09 stsp
5370 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5371 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5372 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5373 cf066bf8 2019-05-09 stsp goto done;
5374 cf066bf8 2019-05-09 stsp }
5375 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5376 2e1fa222 2020-07-23 stsp free(ondisk_path);
5377 2e1fa222 2020-07-23 stsp if (err)
5378 cf066bf8 2019-05-09 stsp goto done;
5379 cf066bf8 2019-05-09 stsp }
5380 036813ee 2019-05-09 stsp
5381 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5382 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5383 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5384 036813ee 2019-05-09 stsp if (err)
5385 036813ee 2019-05-09 stsp goto done;
5386 036813ee 2019-05-09 stsp
5387 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5388 de18fc63 2019-05-09 stsp if (err)
5389 de18fc63 2019-05-09 stsp goto done;
5390 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5391 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5392 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5393 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5394 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5395 33ad4cbe 2019-05-12 jcs free(logmsg);
5396 9d40349a 2019-05-09 stsp if (err)
5397 9d40349a 2019-05-09 stsp goto done;
5398 9d40349a 2019-05-09 stsp
5399 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5400 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5401 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5402 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5403 09f5bd90 2019-05-09 stsp goto done;
5404 09f5bd90 2019-05-09 stsp }
5405 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5406 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5407 09f5bd90 2019-05-09 stsp if (err)
5408 09f5bd90 2019-05-09 stsp goto done;
5409 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5410 ebf99748 2019-05-09 stsp if (err)
5411 ebf99748 2019-05-09 stsp goto done;
5412 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5413 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5414 09f5bd90 2019-05-09 stsp goto done;
5415 09f5bd90 2019-05-09 stsp }
5416 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5417 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5418 f2c16586 2019-05-09 stsp if (err)
5419 f2c16586 2019-05-09 stsp goto done;
5420 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5421 f2c16586 2019-05-09 stsp if (err)
5422 f2c16586 2019-05-09 stsp goto done;
5423 f2c16586 2019-05-09 stsp
5424 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5425 09f5bd90 2019-05-09 stsp if (err)
5426 09f5bd90 2019-05-09 stsp goto done;
5427 09f5bd90 2019-05-09 stsp
5428 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5429 ebf99748 2019-05-09 stsp if (err)
5430 ebf99748 2019-05-09 stsp goto done;
5431 c4296144 2019-05-09 stsp done:
5432 588edf97 2019-05-10 stsp if (head_tree)
5433 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5434 588edf97 2019-05-10 stsp if (head_commit)
5435 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5436 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5437 2f17228e 2019-05-12 stsp if (head_ref2) {
5438 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5439 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5440 2f17228e 2019-05-12 stsp err = unlockerr;
5441 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5442 39cd0ff6 2019-07-12 stsp }
5443 39cd0ff6 2019-07-12 stsp return err;
5444 39cd0ff6 2019-07-12 stsp }
5445 5c1e53bc 2019-07-28 stsp
5446 5c1e53bc 2019-07-28 stsp static const struct got_error *
5447 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5448 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5449 5c1e53bc 2019-07-28 stsp {
5450 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5451 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5452 39cd0ff6 2019-07-12 stsp
5453 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5454 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5455 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5456 5c1e53bc 2019-07-28 stsp
5457 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5458 5c1e53bc 2019-07-28 stsp ct_path++;
5459 5c1e53bc 2019-07-28 stsp
5460 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5461 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5462 5c1e53bc 2019-07-28 stsp break;
5463 5c1e53bc 2019-07-28 stsp }
5464 5c1e53bc 2019-07-28 stsp
5465 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5466 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5467 5c1e53bc 2019-07-28 stsp
5468 5c1e53bc 2019-07-28 stsp return NULL;
5469 5c1e53bc 2019-07-28 stsp }
5470 5c1e53bc 2019-07-28 stsp
5471 f0b75401 2019-08-03 stsp static const struct got_error *
5472 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5473 f0b75401 2019-08-03 stsp {
5474 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5475 f0b75401 2019-08-03 stsp
5476 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5477 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5478 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5479 f0b75401 2019-08-03 stsp }
5480 f0b75401 2019-08-03 stsp
5481 f0b75401 2019-08-03 stsp return NULL;
5482 f0b75401 2019-08-03 stsp }
5483 f0b75401 2019-08-03 stsp
5484 5f8a88c6 2019-08-03 stsp static const struct got_error *
5485 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5486 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5487 5f8a88c6 2019-08-03 stsp {
5488 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5489 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5490 5f8a88c6 2019-08-03 stsp
5491 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5492 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5493 5f8a88c6 2019-08-03 stsp continue;
5494 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5495 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5496 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5497 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5498 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5499 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5500 5f8a88c6 2019-08-03 stsp }
5501 5f8a88c6 2019-08-03 stsp
5502 5f8a88c6 2019-08-03 stsp return NULL;
5503 5f8a88c6 2019-08-03 stsp }
5504 5f8a88c6 2019-08-03 stsp
5505 39cd0ff6 2019-07-12 stsp const struct got_error *
5506 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5507 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5508 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5509 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5510 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5511 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5512 39cd0ff6 2019-07-12 stsp {
5513 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5514 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5515 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5516 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5517 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5518 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5519 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5520 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5521 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5522 39cd0ff6 2019-07-12 stsp
5523 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5524 39cd0ff6 2019-07-12 stsp
5525 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5526 39cd0ff6 2019-07-12 stsp
5527 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5528 39cd0ff6 2019-07-12 stsp if (err)
5529 39cd0ff6 2019-07-12 stsp goto done;
5530 39cd0ff6 2019-07-12 stsp
5531 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5532 39cd0ff6 2019-07-12 stsp if (err)
5533 39cd0ff6 2019-07-12 stsp goto done;
5534 39cd0ff6 2019-07-12 stsp
5535 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5536 39cd0ff6 2019-07-12 stsp if (err)
5537 39cd0ff6 2019-07-12 stsp goto done;
5538 39cd0ff6 2019-07-12 stsp
5539 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5540 39cd0ff6 2019-07-12 stsp if (err)
5541 39cd0ff6 2019-07-12 stsp goto done;
5542 39cd0ff6 2019-07-12 stsp
5543 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5544 5f8a88c6 2019-08-03 stsp &have_staged_files);
5545 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5546 5f8a88c6 2019-08-03 stsp goto done;
5547 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5548 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5549 5f8a88c6 2019-08-03 stsp if (err)
5550 5f8a88c6 2019-08-03 stsp goto done;
5551 5f8a88c6 2019-08-03 stsp }
5552 5f8a88c6 2019-08-03 stsp
5553 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5554 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5555 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5556 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5557 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5558 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5559 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5560 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5561 5c1e53bc 2019-07-28 stsp if (err)
5562 5c1e53bc 2019-07-28 stsp goto done;
5563 5c1e53bc 2019-07-28 stsp }
5564 39cd0ff6 2019-07-12 stsp
5565 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5566 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5567 39cd0ff6 2019-07-12 stsp goto done;
5568 5c1e53bc 2019-07-28 stsp }
5569 5c1e53bc 2019-07-28 stsp
5570 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5571 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5572 5c1e53bc 2019-07-28 stsp if (err)
5573 5c1e53bc 2019-07-28 stsp goto done;
5574 39cd0ff6 2019-07-12 stsp }
5575 f0b75401 2019-08-03 stsp
5576 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5577 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5578 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5579 f0b75401 2019-08-03 stsp
5580 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5581 f0b75401 2019-08-03 stsp ct_path++;
5582 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5583 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5584 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5585 b50cabdf 2019-07-12 stsp if (err)
5586 b50cabdf 2019-07-12 stsp goto done;
5587 f0b75401 2019-08-03 stsp
5588 b50cabdf 2019-07-12 stsp }
5589 b50cabdf 2019-07-12 stsp
5590 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5591 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5592 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5593 39cd0ff6 2019-07-12 stsp if (err)
5594 39cd0ff6 2019-07-12 stsp goto done;
5595 39cd0ff6 2019-07-12 stsp
5596 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5597 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5598 2e1fa222 2020-07-23 stsp if (err == NULL) {
5599 2e1fa222 2020-07-23 stsp err = reinstall_symlinks_after_commit(&commitable_paths,
5600 2e1fa222 2020-07-23 stsp *new_commit_id, fileindex, worktree, repo);
5601 2e1fa222 2020-07-23 stsp }
5602 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5603 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5604 39cd0ff6 2019-07-12 stsp err = sync_err;
5605 39cd0ff6 2019-07-12 stsp done:
5606 39cd0ff6 2019-07-12 stsp if (fileindex)
5607 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5608 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5609 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5610 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5611 39cd0ff6 2019-07-12 stsp err = unlockerr;
5612 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5613 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5614 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5615 39cd0ff6 2019-07-12 stsp }
5616 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5617 c4296144 2019-05-09 stsp return err;
5618 8656d6c4 2019-05-20 stsp }
5619 8656d6c4 2019-05-20 stsp
5620 8656d6c4 2019-05-20 stsp const char *
5621 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5622 8656d6c4 2019-05-20 stsp {
5623 8656d6c4 2019-05-20 stsp return ct->path;
5624 8656d6c4 2019-05-20 stsp }
5625 8656d6c4 2019-05-20 stsp
5626 8656d6c4 2019-05-20 stsp unsigned int
5627 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5628 8656d6c4 2019-05-20 stsp {
5629 8656d6c4 2019-05-20 stsp return ct->status;
5630 818c7501 2019-07-11 stsp }
5631 818c7501 2019-07-11 stsp
5632 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5633 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5634 818c7501 2019-07-11 stsp struct got_repository *repo;
5635 818c7501 2019-07-11 stsp };
5636 818c7501 2019-07-11 stsp
5637 818c7501 2019-07-11 stsp static const struct got_error *
5638 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5639 818c7501 2019-07-11 stsp {
5640 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5641 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5642 818c7501 2019-07-11 stsp unsigned char status;
5643 818c7501 2019-07-11 stsp struct stat sb;
5644 818c7501 2019-07-11 stsp char *ondisk_path;
5645 818c7501 2019-07-11 stsp
5646 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5647 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5648 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5649 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5650 818c7501 2019-07-11 stsp
5651 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5652 818c7501 2019-07-11 stsp == -1)
5653 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5654 818c7501 2019-07-11 stsp
5655 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5656 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5657 818c7501 2019-07-11 stsp free(ondisk_path);
5658 818c7501 2019-07-11 stsp if (err)
5659 818c7501 2019-07-11 stsp return err;
5660 818c7501 2019-07-11 stsp
5661 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5662 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5663 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5664 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5665 818c7501 2019-07-11 stsp
5666 818c7501 2019-07-11 stsp return NULL;
5667 818c7501 2019-07-11 stsp }
5668 818c7501 2019-07-11 stsp
5669 818c7501 2019-07-11 stsp const struct got_error *
5670 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5671 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5672 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5673 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5674 818c7501 2019-07-11 stsp {
5675 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5676 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5677 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5678 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5679 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5680 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5681 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5682 818c7501 2019-07-11 stsp
5683 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5684 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5685 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5686 818c7501 2019-07-11 stsp
5687 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5688 818c7501 2019-07-11 stsp if (err)
5689 818c7501 2019-07-11 stsp return err;
5690 818c7501 2019-07-11 stsp
5691 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5692 818c7501 2019-07-11 stsp if (err)
5693 818c7501 2019-07-11 stsp goto done;
5694 818c7501 2019-07-11 stsp
5695 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5696 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5697 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5698 818c7501 2019-07-11 stsp &ok_arg);
5699 818c7501 2019-07-11 stsp if (err)
5700 818c7501 2019-07-11 stsp goto done;
5701 818c7501 2019-07-11 stsp
5702 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5703 818c7501 2019-07-11 stsp if (err)
5704 818c7501 2019-07-11 stsp goto done;
5705 818c7501 2019-07-11 stsp
5706 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5707 818c7501 2019-07-11 stsp if (err)
5708 818c7501 2019-07-11 stsp goto done;
5709 818c7501 2019-07-11 stsp
5710 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5711 818c7501 2019-07-11 stsp if (err)
5712 818c7501 2019-07-11 stsp goto done;
5713 818c7501 2019-07-11 stsp
5714 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5715 818c7501 2019-07-11 stsp 0);
5716 e51d7b55 2020-01-04 stsp if (err)
5717 e51d7b55 2020-01-04 stsp goto done;
5718 e51d7b55 2020-01-04 stsp
5719 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5720 818c7501 2019-07-11 stsp if (err)
5721 818c7501 2019-07-11 stsp goto done;
5722 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5723 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5724 e51d7b55 2020-01-04 stsp goto done;
5725 e51d7b55 2020-01-04 stsp }
5726 818c7501 2019-07-11 stsp
5727 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5728 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5729 818c7501 2019-07-11 stsp if (err)
5730 818c7501 2019-07-11 stsp goto done;
5731 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5732 818c7501 2019-07-11 stsp if (err)
5733 818c7501 2019-07-11 stsp goto done;
5734 818c7501 2019-07-11 stsp
5735 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5736 818c7501 2019-07-11 stsp
5737 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5738 818c7501 2019-07-11 stsp if (err)
5739 818c7501 2019-07-11 stsp goto done;
5740 818c7501 2019-07-11 stsp
5741 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5742 818c7501 2019-07-11 stsp if (err)
5743 818c7501 2019-07-11 stsp goto done;
5744 818c7501 2019-07-11 stsp
5745 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5746 818c7501 2019-07-11 stsp worktree->base_commit_id);
5747 818c7501 2019-07-11 stsp if (err)
5748 818c7501 2019-07-11 stsp goto done;
5749 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5750 818c7501 2019-07-11 stsp if (err)
5751 818c7501 2019-07-11 stsp goto done;
5752 818c7501 2019-07-11 stsp
5753 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5754 818c7501 2019-07-11 stsp if (err)
5755 818c7501 2019-07-11 stsp goto done;
5756 818c7501 2019-07-11 stsp done:
5757 818c7501 2019-07-11 stsp free(fileindex_path);
5758 818c7501 2019-07-11 stsp free(tmp_branch_name);
5759 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5760 818c7501 2019-07-11 stsp free(branch_ref_name);
5761 818c7501 2019-07-11 stsp if (branch_ref)
5762 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5763 818c7501 2019-07-11 stsp if (wt_branch)
5764 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5765 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5766 818c7501 2019-07-11 stsp if (err) {
5767 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5768 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5769 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5770 818c7501 2019-07-11 stsp }
5771 818c7501 2019-07-11 stsp if (*tmp_branch) {
5772 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5773 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5774 818c7501 2019-07-11 stsp }
5775 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5776 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5777 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5778 3e3a69f1 2019-07-25 stsp }
5779 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5780 818c7501 2019-07-11 stsp }
5781 818c7501 2019-07-11 stsp return err;
5782 818c7501 2019-07-11 stsp }
5783 818c7501 2019-07-11 stsp
5784 818c7501 2019-07-11 stsp const struct got_error *
5785 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5786 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5787 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5788 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5789 818c7501 2019-07-11 stsp {
5790 818c7501 2019-07-11 stsp const struct got_error *err;
5791 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5792 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5793 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5794 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5795 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5796 818c7501 2019-07-11 stsp
5797 818c7501 2019-07-11 stsp *commit_id = NULL;
5798 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5799 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5800 3e3a69f1 2019-07-25 stsp *branch = NULL;
5801 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5802 3e3a69f1 2019-07-25 stsp
5803 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5804 3e3a69f1 2019-07-25 stsp if (err)
5805 3e3a69f1 2019-07-25 stsp return err;
5806 818c7501 2019-07-11 stsp
5807 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5808 3e3a69f1 2019-07-25 stsp if (err)
5809 f032f1f7 2019-08-04 stsp goto done;
5810 f032f1f7 2019-08-04 stsp
5811 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5812 f032f1f7 2019-08-04 stsp &have_staged_files);
5813 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5814 f032f1f7 2019-08-04 stsp goto done;
5815 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5816 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5817 3e3a69f1 2019-07-25 stsp goto done;
5818 f032f1f7 2019-08-04 stsp }
5819 3e3a69f1 2019-07-25 stsp
5820 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5821 818c7501 2019-07-11 stsp if (err)
5822 f032f1f7 2019-08-04 stsp goto done;
5823 818c7501 2019-07-11 stsp
5824 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5825 818c7501 2019-07-11 stsp if (err)
5826 818c7501 2019-07-11 stsp goto done;
5827 818c7501 2019-07-11 stsp
5828 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5829 818c7501 2019-07-11 stsp if (err)
5830 818c7501 2019-07-11 stsp goto done;
5831 818c7501 2019-07-11 stsp
5832 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5833 818c7501 2019-07-11 stsp if (err)
5834 818c7501 2019-07-11 stsp goto done;
5835 818c7501 2019-07-11 stsp
5836 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5837 818c7501 2019-07-11 stsp if (err)
5838 818c7501 2019-07-11 stsp goto done;
5839 818c7501 2019-07-11 stsp
5840 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5841 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5842 818c7501 2019-07-11 stsp if (err)
5843 818c7501 2019-07-11 stsp goto done;
5844 818c7501 2019-07-11 stsp
5845 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5846 818c7501 2019-07-11 stsp if (err)
5847 818c7501 2019-07-11 stsp goto done;
5848 818c7501 2019-07-11 stsp
5849 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5850 818c7501 2019-07-11 stsp if (err)
5851 818c7501 2019-07-11 stsp goto done;
5852 818c7501 2019-07-11 stsp
5853 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5854 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5855 818c7501 2019-07-11 stsp if (err)
5856 818c7501 2019-07-11 stsp goto done;
5857 818c7501 2019-07-11 stsp
5858 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5859 818c7501 2019-07-11 stsp if (err)
5860 818c7501 2019-07-11 stsp goto done;
5861 818c7501 2019-07-11 stsp done:
5862 818c7501 2019-07-11 stsp free(commit_ref_name);
5863 818c7501 2019-07-11 stsp free(branch_ref_name);
5864 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5865 818c7501 2019-07-11 stsp if (commit_ref)
5866 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5867 818c7501 2019-07-11 stsp if (branch_ref)
5868 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5869 818c7501 2019-07-11 stsp if (err) {
5870 818c7501 2019-07-11 stsp free(*commit_id);
5871 818c7501 2019-07-11 stsp *commit_id = NULL;
5872 818c7501 2019-07-11 stsp if (*tmp_branch) {
5873 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5874 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5875 818c7501 2019-07-11 stsp }
5876 818c7501 2019-07-11 stsp if (*new_base_branch) {
5877 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5878 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5879 818c7501 2019-07-11 stsp }
5880 818c7501 2019-07-11 stsp if (*branch) {
5881 818c7501 2019-07-11 stsp got_ref_close(*branch);
5882 818c7501 2019-07-11 stsp *branch = NULL;
5883 818c7501 2019-07-11 stsp }
5884 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5885 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5886 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5887 3e3a69f1 2019-07-25 stsp }
5888 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5889 818c7501 2019-07-11 stsp }
5890 818c7501 2019-07-11 stsp return err;
5891 c4296144 2019-05-09 stsp }
5892 818c7501 2019-07-11 stsp
5893 818c7501 2019-07-11 stsp const struct got_error *
5894 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5895 818c7501 2019-07-11 stsp {
5896 818c7501 2019-07-11 stsp const struct got_error *err;
5897 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5898 818c7501 2019-07-11 stsp
5899 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5900 818c7501 2019-07-11 stsp if (err)
5901 818c7501 2019-07-11 stsp return err;
5902 818c7501 2019-07-11 stsp
5903 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5904 818c7501 2019-07-11 stsp free(tmp_branch_name);
5905 818c7501 2019-07-11 stsp return NULL;
5906 818c7501 2019-07-11 stsp }
5907 818c7501 2019-07-11 stsp
5908 818c7501 2019-07-11 stsp static const struct got_error *
5909 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5910 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5911 818c7501 2019-07-11 stsp {
5912 0ebf8283 2019-07-24 stsp *logmsg = arg;
5913 818c7501 2019-07-11 stsp return NULL;
5914 818c7501 2019-07-11 stsp }
5915 818c7501 2019-07-11 stsp
5916 818c7501 2019-07-11 stsp static const struct got_error *
5917 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5918 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5919 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5920 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5921 818c7501 2019-07-11 stsp {
5922 818c7501 2019-07-11 stsp return NULL;
5923 01757395 2019-07-12 stsp }
5924 01757395 2019-07-12 stsp
5925 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5926 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5927 01757395 2019-07-12 stsp void *progress_arg;
5928 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5929 01757395 2019-07-12 stsp };
5930 01757395 2019-07-12 stsp
5931 01757395 2019-07-12 stsp static const struct got_error *
5932 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5933 01757395 2019-07-12 stsp {
5934 01757395 2019-07-12 stsp const struct got_error *err;
5935 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5936 01757395 2019-07-12 stsp char *p;
5937 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5938 01757395 2019-07-12 stsp
5939 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5940 01757395 2019-07-12 stsp if (err)
5941 01757395 2019-07-12 stsp return err;
5942 01757395 2019-07-12 stsp
5943 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5944 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5945 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5946 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5947 01757395 2019-07-12 stsp return NULL;
5948 01757395 2019-07-12 stsp
5949 01757395 2019-07-12 stsp p = strdup(path);
5950 01757395 2019-07-12 stsp if (p == NULL)
5951 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5952 01757395 2019-07-12 stsp
5953 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5954 01757395 2019-07-12 stsp if (err || new == NULL)
5955 01757395 2019-07-12 stsp free(p);
5956 01757395 2019-07-12 stsp return err;
5957 01757395 2019-07-12 stsp }
5958 01757395 2019-07-12 stsp
5959 01757395 2019-07-12 stsp void
5960 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5961 01757395 2019-07-12 stsp {
5962 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5963 01757395 2019-07-12 stsp
5964 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5965 01757395 2019-07-12 stsp free((char *)pe->path);
5966 01757395 2019-07-12 stsp
5967 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5968 818c7501 2019-07-11 stsp }
5969 818c7501 2019-07-11 stsp
5970 0ebf8283 2019-07-24 stsp static const struct got_error *
5971 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5972 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5973 818c7501 2019-07-11 stsp {
5974 818c7501 2019-07-11 stsp const struct got_error *err;
5975 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5976 818c7501 2019-07-11 stsp
5977 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5978 818c7501 2019-07-11 stsp if (err) {
5979 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5980 818c7501 2019-07-11 stsp goto done;
5981 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5982 818c7501 2019-07-11 stsp if (err)
5983 818c7501 2019-07-11 stsp goto done;
5984 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5985 818c7501 2019-07-11 stsp if (err)
5986 818c7501 2019-07-11 stsp goto done;
5987 de05890f 2020-03-05 stsp } else if (is_rebase) {
5988 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5989 818c7501 2019-07-11 stsp int cmp;
5990 818c7501 2019-07-11 stsp
5991 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5992 818c7501 2019-07-11 stsp if (err)
5993 818c7501 2019-07-11 stsp goto done;
5994 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5995 818c7501 2019-07-11 stsp free(stored_id);
5996 818c7501 2019-07-11 stsp if (cmp != 0) {
5997 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5998 818c7501 2019-07-11 stsp goto done;
5999 818c7501 2019-07-11 stsp }
6000 818c7501 2019-07-11 stsp }
6001 0ebf8283 2019-07-24 stsp done:
6002 0ebf8283 2019-07-24 stsp if (commit_ref)
6003 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6004 0ebf8283 2019-07-24 stsp return err;
6005 0ebf8283 2019-07-24 stsp }
6006 0ebf8283 2019-07-24 stsp
6007 0ebf8283 2019-07-24 stsp static const struct got_error *
6008 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6009 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6010 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6011 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6012 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6013 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6014 0ebf8283 2019-07-24 stsp {
6015 0ebf8283 2019-07-24 stsp const struct got_error *err;
6016 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6017 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6018 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6019 818c7501 2019-07-11 stsp
6020 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6021 0ebf8283 2019-07-24 stsp
6022 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6023 0ebf8283 2019-07-24 stsp if (err)
6024 0ebf8283 2019-07-24 stsp return err;
6025 0ebf8283 2019-07-24 stsp
6026 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6027 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6028 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6029 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6030 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6031 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6032 818c7501 2019-07-11 stsp if (commit_ref)
6033 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6034 818c7501 2019-07-11 stsp return err;
6035 818c7501 2019-07-11 stsp }
6036 818c7501 2019-07-11 stsp
6037 818c7501 2019-07-11 stsp const struct got_error *
6038 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6039 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6040 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6041 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6042 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6043 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6044 818c7501 2019-07-11 stsp {
6045 0ebf8283 2019-07-24 stsp const struct got_error *err;
6046 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6047 0ebf8283 2019-07-24 stsp
6048 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6049 0ebf8283 2019-07-24 stsp if (err)
6050 0ebf8283 2019-07-24 stsp return err;
6051 0ebf8283 2019-07-24 stsp
6052 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6053 0ebf8283 2019-07-24 stsp if (err)
6054 0ebf8283 2019-07-24 stsp goto done;
6055 0ebf8283 2019-07-24 stsp
6056 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6057 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6058 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6059 0ebf8283 2019-07-24 stsp done:
6060 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6061 0ebf8283 2019-07-24 stsp return err;
6062 0ebf8283 2019-07-24 stsp }
6063 0ebf8283 2019-07-24 stsp
6064 0ebf8283 2019-07-24 stsp const struct got_error *
6065 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6066 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6067 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6068 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6069 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6070 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6071 0ebf8283 2019-07-24 stsp {
6072 0ebf8283 2019-07-24 stsp const struct got_error *err;
6073 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6074 0ebf8283 2019-07-24 stsp
6075 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6076 0ebf8283 2019-07-24 stsp if (err)
6077 0ebf8283 2019-07-24 stsp return err;
6078 0ebf8283 2019-07-24 stsp
6079 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6080 0ebf8283 2019-07-24 stsp if (err)
6081 0ebf8283 2019-07-24 stsp goto done;
6082 0ebf8283 2019-07-24 stsp
6083 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6084 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6085 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6086 0ebf8283 2019-07-24 stsp done:
6087 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6088 0ebf8283 2019-07-24 stsp return err;
6089 0ebf8283 2019-07-24 stsp }
6090 0ebf8283 2019-07-24 stsp
6091 0ebf8283 2019-07-24 stsp static const struct got_error *
6092 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6093 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6094 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6096 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6097 0ebf8283 2019-07-24 stsp {
6098 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6099 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6100 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6101 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6102 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6103 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6104 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6105 818c7501 2019-07-11 stsp
6106 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6107 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6108 a0e95631 2019-07-12 stsp
6109 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6110 818c7501 2019-07-11 stsp
6111 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6112 a0e95631 2019-07-12 stsp if (err)
6113 3e3a69f1 2019-07-25 stsp return err;
6114 a0e95631 2019-07-12 stsp
6115 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6116 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6117 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6118 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6119 01757395 2019-07-12 stsp /*
6120 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6121 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6122 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6123 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6124 01757395 2019-07-12 stsp */
6125 01757395 2019-07-12 stsp if (merged_paths) {
6126 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6127 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6128 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6129 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6130 f2a9dc41 2019-12-13 tracey 0);
6131 01757395 2019-07-12 stsp if (err)
6132 01757395 2019-07-12 stsp goto done;
6133 01757395 2019-07-12 stsp }
6134 01757395 2019-07-12 stsp } else {
6135 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6136 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6137 01757395 2019-07-12 stsp if (err)
6138 01757395 2019-07-12 stsp goto done;
6139 01757395 2019-07-12 stsp }
6140 a0e95631 2019-07-12 stsp
6141 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6142 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6143 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6144 a0e95631 2019-07-12 stsp if (err)
6145 a0e95631 2019-07-12 stsp goto done;
6146 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6147 a0e95631 2019-07-12 stsp goto done;
6148 ff0d2220 2019-07-11 stsp }
6149 818c7501 2019-07-11 stsp
6150 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6151 edd02c5e 2019-07-11 stsp if (err)
6152 edd02c5e 2019-07-11 stsp goto done;
6153 edd02c5e 2019-07-11 stsp
6154 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6155 818c7501 2019-07-11 stsp if (err)
6156 818c7501 2019-07-11 stsp goto done;
6157 818c7501 2019-07-11 stsp
6158 5943eee2 2019-08-13 stsp if (new_logmsg) {
6159 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6160 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6161 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6162 5943eee2 2019-08-13 stsp goto done;
6163 5943eee2 2019-08-13 stsp }
6164 5943eee2 2019-08-13 stsp } else {
6165 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6166 5943eee2 2019-08-13 stsp if (err)
6167 5943eee2 2019-08-13 stsp goto done;
6168 5943eee2 2019-08-13 stsp }
6169 0ebf8283 2019-07-24 stsp
6170 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6171 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6172 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6173 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6174 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6175 a0e95631 2019-07-12 stsp if (err)
6176 a0e95631 2019-07-12 stsp goto done;
6177 a0e95631 2019-07-12 stsp
6178 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6179 a0e95631 2019-07-12 stsp if (err)
6180 a0e95631 2019-07-12 stsp goto done;
6181 a0e95631 2019-07-12 stsp
6182 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6183 a0e95631 2019-07-12 stsp if (err)
6184 a0e95631 2019-07-12 stsp goto done;
6185 a0e95631 2019-07-12 stsp
6186 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6187 72fd46fa 2019-09-06 stsp fileindex, 0);
6188 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6189 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6190 a0e95631 2019-07-12 stsp err = sync_err;
6191 818c7501 2019-07-11 stsp done:
6192 a0e95631 2019-07-12 stsp free(fileindex_path);
6193 a0e95631 2019-07-12 stsp free(head_commit_id);
6194 a0e95631 2019-07-12 stsp if (head_ref)
6195 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6196 818c7501 2019-07-11 stsp if (err) {
6197 818c7501 2019-07-11 stsp free(*new_commit_id);
6198 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6199 818c7501 2019-07-11 stsp }
6200 818c7501 2019-07-11 stsp return err;
6201 818c7501 2019-07-11 stsp }
6202 818c7501 2019-07-11 stsp
6203 818c7501 2019-07-11 stsp const struct got_error *
6204 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6205 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6206 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6207 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6208 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6209 0ebf8283 2019-07-24 stsp {
6210 0ebf8283 2019-07-24 stsp const struct got_error *err;
6211 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6212 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6213 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6214 0ebf8283 2019-07-24 stsp
6215 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6216 0ebf8283 2019-07-24 stsp if (err)
6217 0ebf8283 2019-07-24 stsp return err;
6218 0ebf8283 2019-07-24 stsp
6219 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6220 0ebf8283 2019-07-24 stsp if (err)
6221 0ebf8283 2019-07-24 stsp goto done;
6222 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6223 0ebf8283 2019-07-24 stsp if (err)
6224 0ebf8283 2019-07-24 stsp goto done;
6225 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6226 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6227 0ebf8283 2019-07-24 stsp goto done;
6228 0ebf8283 2019-07-24 stsp }
6229 0ebf8283 2019-07-24 stsp
6230 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6231 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6232 0ebf8283 2019-07-24 stsp done:
6233 0ebf8283 2019-07-24 stsp if (commit_ref)
6234 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6235 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6236 0ebf8283 2019-07-24 stsp free(commit_id);
6237 0ebf8283 2019-07-24 stsp return err;
6238 0ebf8283 2019-07-24 stsp }
6239 0ebf8283 2019-07-24 stsp
6240 0ebf8283 2019-07-24 stsp const struct got_error *
6241 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6242 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6243 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6244 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6245 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6246 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6247 0ebf8283 2019-07-24 stsp {
6248 0ebf8283 2019-07-24 stsp const struct got_error *err;
6249 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6250 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6251 0ebf8283 2019-07-24 stsp
6252 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6253 0ebf8283 2019-07-24 stsp if (err)
6254 0ebf8283 2019-07-24 stsp return err;
6255 0ebf8283 2019-07-24 stsp
6256 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6257 0ebf8283 2019-07-24 stsp if (err)
6258 0ebf8283 2019-07-24 stsp goto done;
6259 0ebf8283 2019-07-24 stsp
6260 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6261 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6262 0ebf8283 2019-07-24 stsp done:
6263 0ebf8283 2019-07-24 stsp if (commit_ref)
6264 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6265 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6266 0ebf8283 2019-07-24 stsp return err;
6267 0ebf8283 2019-07-24 stsp }
6268 0ebf8283 2019-07-24 stsp
6269 0ebf8283 2019-07-24 stsp const struct got_error *
6270 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6271 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6272 818c7501 2019-07-11 stsp {
6273 3e3a69f1 2019-07-25 stsp if (fileindex)
6274 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6275 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6276 69844fba 2019-07-11 stsp }
6277 69844fba 2019-07-11 stsp
6278 69844fba 2019-07-11 stsp static const struct got_error *
6279 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6280 69844fba 2019-07-11 stsp {
6281 69844fba 2019-07-11 stsp const struct got_error *err;
6282 69844fba 2019-07-11 stsp struct got_reference *ref;
6283 69844fba 2019-07-11 stsp
6284 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6285 69844fba 2019-07-11 stsp if (err) {
6286 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6287 69844fba 2019-07-11 stsp return NULL;
6288 69844fba 2019-07-11 stsp return err;
6289 69844fba 2019-07-11 stsp }
6290 69844fba 2019-07-11 stsp
6291 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6292 69844fba 2019-07-11 stsp got_ref_close(ref);
6293 69844fba 2019-07-11 stsp return err;
6294 818c7501 2019-07-11 stsp }
6295 818c7501 2019-07-11 stsp
6296 69844fba 2019-07-11 stsp static const struct got_error *
6297 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6298 69844fba 2019-07-11 stsp {
6299 69844fba 2019-07-11 stsp const struct got_error *err;
6300 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6301 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6302 69844fba 2019-07-11 stsp
6303 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6304 69844fba 2019-07-11 stsp if (err)
6305 69844fba 2019-07-11 stsp goto done;
6306 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6307 69844fba 2019-07-11 stsp if (err)
6308 69844fba 2019-07-11 stsp goto done;
6309 69844fba 2019-07-11 stsp
6310 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6311 69844fba 2019-07-11 stsp if (err)
6312 69844fba 2019-07-11 stsp goto done;
6313 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6314 69844fba 2019-07-11 stsp if (err)
6315 69844fba 2019-07-11 stsp goto done;
6316 69844fba 2019-07-11 stsp
6317 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6318 69844fba 2019-07-11 stsp if (err)
6319 69844fba 2019-07-11 stsp goto done;
6320 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6321 69844fba 2019-07-11 stsp if (err)
6322 69844fba 2019-07-11 stsp goto done;
6323 69844fba 2019-07-11 stsp
6324 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6325 69844fba 2019-07-11 stsp if (err)
6326 69844fba 2019-07-11 stsp goto done;
6327 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6328 69844fba 2019-07-11 stsp if (err)
6329 69844fba 2019-07-11 stsp goto done;
6330 69844fba 2019-07-11 stsp
6331 69844fba 2019-07-11 stsp done:
6332 69844fba 2019-07-11 stsp free(tmp_branch_name);
6333 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6334 69844fba 2019-07-11 stsp free(branch_ref_name);
6335 69844fba 2019-07-11 stsp free(commit_ref_name);
6336 69844fba 2019-07-11 stsp return err;
6337 69844fba 2019-07-11 stsp }
6338 69844fba 2019-07-11 stsp
6339 818c7501 2019-07-11 stsp const struct got_error *
6340 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6341 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6342 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6343 818c7501 2019-07-11 stsp struct got_repository *repo)
6344 818c7501 2019-07-11 stsp {
6345 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6346 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6347 818c7501 2019-07-11 stsp
6348 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6349 818c7501 2019-07-11 stsp if (err)
6350 818c7501 2019-07-11 stsp return err;
6351 818c7501 2019-07-11 stsp
6352 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6353 818c7501 2019-07-11 stsp if (err)
6354 818c7501 2019-07-11 stsp goto done;
6355 818c7501 2019-07-11 stsp
6356 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6357 818c7501 2019-07-11 stsp if (err)
6358 818c7501 2019-07-11 stsp goto done;
6359 818c7501 2019-07-11 stsp
6360 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6361 818c7501 2019-07-11 stsp if (err)
6362 818c7501 2019-07-11 stsp goto done;
6363 818c7501 2019-07-11 stsp
6364 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6365 818c7501 2019-07-11 stsp done:
6366 3e3a69f1 2019-07-25 stsp if (fileindex)
6367 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6368 818c7501 2019-07-11 stsp free(new_head_commit_id);
6369 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6370 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6371 818c7501 2019-07-11 stsp err = unlockerr;
6372 818c7501 2019-07-11 stsp return err;
6373 818c7501 2019-07-11 stsp }
6374 818c7501 2019-07-11 stsp
6375 818c7501 2019-07-11 stsp const struct got_error *
6376 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6377 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6378 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6379 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6380 818c7501 2019-07-11 stsp {
6381 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6382 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6383 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6384 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6385 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6386 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6387 818c7501 2019-07-11 stsp
6388 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6389 818c7501 2019-07-11 stsp if (err)
6390 818c7501 2019-07-11 stsp return err;
6391 818c7501 2019-07-11 stsp
6392 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6393 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6394 818c7501 2019-07-11 stsp if (err)
6395 818c7501 2019-07-11 stsp goto done;
6396 818c7501 2019-07-11 stsp
6397 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6398 818c7501 2019-07-11 stsp if (err)
6399 818c7501 2019-07-11 stsp goto done;
6400 818c7501 2019-07-11 stsp
6401 818c7501 2019-07-11 stsp /*
6402 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6403 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6404 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6405 818c7501 2019-07-11 stsp */
6406 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6407 818c7501 2019-07-11 stsp if (err)
6408 818c7501 2019-07-11 stsp goto done;
6409 818c7501 2019-07-11 stsp
6410 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6411 818c7501 2019-07-11 stsp if (err)
6412 818c7501 2019-07-11 stsp goto done;
6413 818c7501 2019-07-11 stsp
6414 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6415 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6416 ca355955 2019-07-12 stsp if (err)
6417 ca355955 2019-07-12 stsp goto done;
6418 ca355955 2019-07-12 stsp
6419 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6420 ca355955 2019-07-12 stsp if (err)
6421 ca355955 2019-07-12 stsp goto done;
6422 ca355955 2019-07-12 stsp
6423 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6424 818c7501 2019-07-11 stsp if (err)
6425 818c7501 2019-07-11 stsp goto done;
6426 818c7501 2019-07-11 stsp
6427 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6428 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6429 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6430 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6431 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6432 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6433 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6434 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6435 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6436 55bd499d 2019-07-12 stsp if (err)
6437 1f1abb7e 2019-08-08 stsp goto sync;
6438 55bd499d 2019-07-12 stsp
6439 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6440 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6441 ca355955 2019-07-12 stsp sync:
6442 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6443 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6444 ca355955 2019-07-12 stsp err = sync_err;
6445 818c7501 2019-07-11 stsp done:
6446 818c7501 2019-07-11 stsp got_ref_close(resolved);
6447 a3a2faf2 2019-07-12 stsp free(tree_id);
6448 818c7501 2019-07-11 stsp free(commit_id);
6449 0ebf8283 2019-07-24 stsp if (fileindex)
6450 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6451 0ebf8283 2019-07-24 stsp free(fileindex_path);
6452 0ebf8283 2019-07-24 stsp
6453 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6454 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6455 0ebf8283 2019-07-24 stsp err = unlockerr;
6456 0ebf8283 2019-07-24 stsp return err;
6457 0ebf8283 2019-07-24 stsp }
6458 0ebf8283 2019-07-24 stsp
6459 0ebf8283 2019-07-24 stsp const struct got_error *
6460 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6461 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6462 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6463 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6464 0ebf8283 2019-07-24 stsp {
6465 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6466 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6467 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6468 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6469 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6470 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6471 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6472 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6473 0ebf8283 2019-07-24 stsp
6474 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6475 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6476 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6477 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6480 0ebf8283 2019-07-24 stsp if (err)
6481 0ebf8283 2019-07-24 stsp return err;
6482 0ebf8283 2019-07-24 stsp
6483 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6484 0ebf8283 2019-07-24 stsp if (err)
6485 0ebf8283 2019-07-24 stsp goto done;
6486 0ebf8283 2019-07-24 stsp
6487 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6488 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6489 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6490 0ebf8283 2019-07-24 stsp &ok_arg);
6491 0ebf8283 2019-07-24 stsp if (err)
6492 0ebf8283 2019-07-24 stsp goto done;
6493 0ebf8283 2019-07-24 stsp
6494 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6495 0ebf8283 2019-07-24 stsp if (err)
6496 0ebf8283 2019-07-24 stsp goto done;
6497 0ebf8283 2019-07-24 stsp
6498 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6499 0ebf8283 2019-07-24 stsp if (err)
6500 0ebf8283 2019-07-24 stsp goto done;
6501 0ebf8283 2019-07-24 stsp
6502 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6503 0ebf8283 2019-07-24 stsp worktree);
6504 0ebf8283 2019-07-24 stsp if (err)
6505 0ebf8283 2019-07-24 stsp goto done;
6506 0ebf8283 2019-07-24 stsp
6507 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6508 0ebf8283 2019-07-24 stsp 0);
6509 0ebf8283 2019-07-24 stsp if (err)
6510 0ebf8283 2019-07-24 stsp goto done;
6511 0ebf8283 2019-07-24 stsp
6512 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6513 0ebf8283 2019-07-24 stsp if (err)
6514 0ebf8283 2019-07-24 stsp goto done;
6515 0ebf8283 2019-07-24 stsp
6516 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6517 0ebf8283 2019-07-24 stsp if (err)
6518 0ebf8283 2019-07-24 stsp goto done;
6519 0ebf8283 2019-07-24 stsp
6520 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6521 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6522 0ebf8283 2019-07-24 stsp if (err)
6523 0ebf8283 2019-07-24 stsp goto done;
6524 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6525 0ebf8283 2019-07-24 stsp if (err)
6526 0ebf8283 2019-07-24 stsp goto done;
6527 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6528 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6529 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6530 0ebf8283 2019-07-24 stsp goto done;
6531 0ebf8283 2019-07-24 stsp }
6532 0ebf8283 2019-07-24 stsp
6533 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6534 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6535 0ebf8283 2019-07-24 stsp if (err)
6536 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6538 0ebf8283 2019-07-24 stsp if (err)
6539 0ebf8283 2019-07-24 stsp goto done;
6540 0ebf8283 2019-07-24 stsp
6541 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6542 0ebf8283 2019-07-24 stsp if (err)
6543 0ebf8283 2019-07-24 stsp goto done;
6544 0ebf8283 2019-07-24 stsp done:
6545 0ebf8283 2019-07-24 stsp free(fileindex_path);
6546 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6547 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6548 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6549 0ebf8283 2019-07-24 stsp if (wt_branch)
6550 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6551 0ebf8283 2019-07-24 stsp if (err) {
6552 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6553 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6554 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6555 0ebf8283 2019-07-24 stsp }
6556 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6557 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6558 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6559 0ebf8283 2019-07-24 stsp }
6560 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6561 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6562 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6563 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6564 3e3a69f1 2019-07-25 stsp }
6565 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6566 0ebf8283 2019-07-24 stsp }
6567 0ebf8283 2019-07-24 stsp return err;
6568 0ebf8283 2019-07-24 stsp }
6569 0ebf8283 2019-07-24 stsp
6570 0ebf8283 2019-07-24 stsp const struct got_error *
6571 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6572 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6573 0ebf8283 2019-07-24 stsp {
6574 3e3a69f1 2019-07-25 stsp if (fileindex)
6575 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6576 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6577 0ebf8283 2019-07-24 stsp }
6578 0ebf8283 2019-07-24 stsp
6579 0ebf8283 2019-07-24 stsp const struct got_error *
6580 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6581 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6582 0ebf8283 2019-07-24 stsp {
6583 0ebf8283 2019-07-24 stsp const struct got_error *err;
6584 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6585 0ebf8283 2019-07-24 stsp
6586 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6587 0ebf8283 2019-07-24 stsp if (err)
6588 0ebf8283 2019-07-24 stsp return err;
6589 0ebf8283 2019-07-24 stsp
6590 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6591 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6592 0ebf8283 2019-07-24 stsp return NULL;
6593 0ebf8283 2019-07-24 stsp }
6594 0ebf8283 2019-07-24 stsp
6595 0ebf8283 2019-07-24 stsp const struct got_error *
6596 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6597 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6598 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6599 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6600 0ebf8283 2019-07-24 stsp {
6601 0ebf8283 2019-07-24 stsp const struct got_error *err;
6602 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6603 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6604 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6605 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6606 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6607 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6608 0ebf8283 2019-07-24 stsp
6609 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6610 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6611 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6612 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6613 0ebf8283 2019-07-24 stsp
6614 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6615 3e3a69f1 2019-07-25 stsp if (err)
6616 3e3a69f1 2019-07-25 stsp return err;
6617 3e3a69f1 2019-07-25 stsp
6618 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 3e3a69f1 2019-07-25 stsp if (err)
6620 f032f1f7 2019-08-04 stsp goto done;
6621 f032f1f7 2019-08-04 stsp
6622 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6623 f032f1f7 2019-08-04 stsp &have_staged_files);
6624 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6625 f032f1f7 2019-08-04 stsp goto done;
6626 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6627 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6628 3e3a69f1 2019-07-25 stsp goto done;
6629 f032f1f7 2019-08-04 stsp }
6630 3e3a69f1 2019-07-25 stsp
6631 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6632 0ebf8283 2019-07-24 stsp if (err)
6633 f032f1f7 2019-08-04 stsp goto done;
6634 0ebf8283 2019-07-24 stsp
6635 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6636 0ebf8283 2019-07-24 stsp if (err)
6637 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp
6639 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6640 0ebf8283 2019-07-24 stsp if (err)
6641 0ebf8283 2019-07-24 stsp goto done;
6642 0ebf8283 2019-07-24 stsp
6643 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6644 0ebf8283 2019-07-24 stsp worktree);
6645 0ebf8283 2019-07-24 stsp if (err)
6646 0ebf8283 2019-07-24 stsp goto done;
6647 0ebf8283 2019-07-24 stsp
6648 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6649 0ebf8283 2019-07-24 stsp if (err)
6650 0ebf8283 2019-07-24 stsp goto done;
6651 0ebf8283 2019-07-24 stsp
6652 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6653 0ebf8283 2019-07-24 stsp if (err)
6654 0ebf8283 2019-07-24 stsp goto done;
6655 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6656 0ebf8283 2019-07-24 stsp if (err)
6657 0ebf8283 2019-07-24 stsp goto done;
6658 0ebf8283 2019-07-24 stsp
6659 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6660 0ebf8283 2019-07-24 stsp if (err)
6661 0ebf8283 2019-07-24 stsp goto done;
6662 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6663 0ebf8283 2019-07-24 stsp if (err)
6664 0ebf8283 2019-07-24 stsp goto done;
6665 0ebf8283 2019-07-24 stsp
6666 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6667 0ebf8283 2019-07-24 stsp if (err)
6668 0ebf8283 2019-07-24 stsp goto done;
6669 0ebf8283 2019-07-24 stsp done:
6670 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6671 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6672 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6673 0ebf8283 2019-07-24 stsp if (commit_ref)
6674 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6675 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6676 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6677 0ebf8283 2019-07-24 stsp if (err) {
6678 0ebf8283 2019-07-24 stsp free(*commit_id);
6679 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6680 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6681 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6682 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6683 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6684 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6685 0ebf8283 2019-07-24 stsp }
6686 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6687 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6688 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6689 3e3a69f1 2019-07-25 stsp }
6690 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6691 0ebf8283 2019-07-24 stsp }
6692 0ebf8283 2019-07-24 stsp return err;
6693 0ebf8283 2019-07-24 stsp }
6694 0ebf8283 2019-07-24 stsp
6695 0ebf8283 2019-07-24 stsp static const struct got_error *
6696 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6697 0ebf8283 2019-07-24 stsp {
6698 0ebf8283 2019-07-24 stsp const struct got_error *err;
6699 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6700 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6701 0ebf8283 2019-07-24 stsp
6702 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6703 0ebf8283 2019-07-24 stsp if (err)
6704 0ebf8283 2019-07-24 stsp goto done;
6705 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6706 0ebf8283 2019-07-24 stsp if (err)
6707 0ebf8283 2019-07-24 stsp goto done;
6708 0ebf8283 2019-07-24 stsp
6709 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6710 0ebf8283 2019-07-24 stsp worktree);
6711 0ebf8283 2019-07-24 stsp if (err)
6712 0ebf8283 2019-07-24 stsp goto done;
6713 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6714 0ebf8283 2019-07-24 stsp if (err)
6715 0ebf8283 2019-07-24 stsp goto done;
6716 0ebf8283 2019-07-24 stsp
6717 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6718 0ebf8283 2019-07-24 stsp if (err)
6719 0ebf8283 2019-07-24 stsp goto done;
6720 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6721 0ebf8283 2019-07-24 stsp if (err)
6722 0ebf8283 2019-07-24 stsp goto done;
6723 0ebf8283 2019-07-24 stsp
6724 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6725 0ebf8283 2019-07-24 stsp if (err)
6726 0ebf8283 2019-07-24 stsp goto done;
6727 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6728 0ebf8283 2019-07-24 stsp if (err)
6729 0ebf8283 2019-07-24 stsp goto done;
6730 0ebf8283 2019-07-24 stsp done:
6731 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6732 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6733 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6734 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6735 0ebf8283 2019-07-24 stsp return err;
6736 0ebf8283 2019-07-24 stsp }
6737 0ebf8283 2019-07-24 stsp
6738 0ebf8283 2019-07-24 stsp const struct got_error *
6739 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6740 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6741 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6742 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6743 0ebf8283 2019-07-24 stsp {
6744 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6745 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6746 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6747 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6748 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6749 0ebf8283 2019-07-24 stsp
6750 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6751 0ebf8283 2019-07-24 stsp if (err)
6752 0ebf8283 2019-07-24 stsp return err;
6753 0ebf8283 2019-07-24 stsp
6754 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6755 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6756 0ebf8283 2019-07-24 stsp if (err)
6757 0ebf8283 2019-07-24 stsp goto done;
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6760 0ebf8283 2019-07-24 stsp if (err)
6761 0ebf8283 2019-07-24 stsp goto done;
6762 0ebf8283 2019-07-24 stsp
6763 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6764 0ebf8283 2019-07-24 stsp if (err)
6765 0ebf8283 2019-07-24 stsp goto done;
6766 0ebf8283 2019-07-24 stsp
6767 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6768 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6769 0ebf8283 2019-07-24 stsp if (err)
6770 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp
6772 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6773 0ebf8283 2019-07-24 stsp if (err)
6774 0ebf8283 2019-07-24 stsp goto done;
6775 0ebf8283 2019-07-24 stsp
6776 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6777 0ebf8283 2019-07-24 stsp if (err)
6778 0ebf8283 2019-07-24 stsp goto done;
6779 0ebf8283 2019-07-24 stsp
6780 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6781 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6782 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6783 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6784 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6785 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6786 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6787 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6788 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6789 0ebf8283 2019-07-24 stsp if (err)
6790 1f1abb7e 2019-08-08 stsp goto sync;
6791 0ebf8283 2019-07-24 stsp
6792 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6793 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6794 0ebf8283 2019-07-24 stsp sync:
6795 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6796 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6797 0ebf8283 2019-07-24 stsp err = sync_err;
6798 0ebf8283 2019-07-24 stsp done:
6799 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6800 0ebf8283 2019-07-24 stsp free(tree_id);
6801 818c7501 2019-07-11 stsp free(fileindex_path);
6802 818c7501 2019-07-11 stsp
6803 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6804 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6805 818c7501 2019-07-11 stsp err = unlockerr;
6806 818c7501 2019-07-11 stsp return err;
6807 818c7501 2019-07-11 stsp }
6808 0ebf8283 2019-07-24 stsp
6809 0ebf8283 2019-07-24 stsp const struct got_error *
6810 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6811 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6812 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6813 0ebf8283 2019-07-24 stsp {
6814 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6815 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6816 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6817 0ebf8283 2019-07-24 stsp
6818 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6819 0ebf8283 2019-07-24 stsp if (err)
6820 0ebf8283 2019-07-24 stsp return err;
6821 0ebf8283 2019-07-24 stsp
6822 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6823 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6824 0ebf8283 2019-07-24 stsp if (err)
6825 0ebf8283 2019-07-24 stsp goto done;
6826 0ebf8283 2019-07-24 stsp
6827 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6828 0ebf8283 2019-07-24 stsp if (err)
6829 0ebf8283 2019-07-24 stsp goto done;
6830 0ebf8283 2019-07-24 stsp
6831 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6832 0ebf8283 2019-07-24 stsp if (err)
6833 0ebf8283 2019-07-24 stsp goto done;
6834 0ebf8283 2019-07-24 stsp
6835 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6836 0ebf8283 2019-07-24 stsp if (err)
6837 0ebf8283 2019-07-24 stsp goto done;
6838 0ebf8283 2019-07-24 stsp
6839 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6840 0ebf8283 2019-07-24 stsp done:
6841 3e3a69f1 2019-07-25 stsp if (fileindex)
6842 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6843 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6844 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6845 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6846 0ebf8283 2019-07-24 stsp err = unlockerr;
6847 0ebf8283 2019-07-24 stsp return err;
6848 0ebf8283 2019-07-24 stsp }
6849 0ebf8283 2019-07-24 stsp
6850 0ebf8283 2019-07-24 stsp const struct got_error *
6851 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6852 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6853 0ebf8283 2019-07-24 stsp {
6854 0ebf8283 2019-07-24 stsp const struct got_error *err;
6855 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6856 0ebf8283 2019-07-24 stsp
6857 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6858 0ebf8283 2019-07-24 stsp if (err)
6859 0ebf8283 2019-07-24 stsp return err;
6860 0ebf8283 2019-07-24 stsp
6861 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6862 0ebf8283 2019-07-24 stsp if (err)
6863 0ebf8283 2019-07-24 stsp goto done;
6864 0ebf8283 2019-07-24 stsp
6865 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6866 0ebf8283 2019-07-24 stsp done:
6867 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6868 2822a352 2019-10-15 stsp return err;
6869 2822a352 2019-10-15 stsp }
6870 2822a352 2019-10-15 stsp
6871 2822a352 2019-10-15 stsp const struct got_error *
6872 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6873 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6874 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6875 2822a352 2019-10-15 stsp struct got_repository *repo)
6876 2822a352 2019-10-15 stsp {
6877 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6878 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6879 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6880 2822a352 2019-10-15 stsp
6881 2822a352 2019-10-15 stsp *fileindex = NULL;
6882 2822a352 2019-10-15 stsp *branch_ref = NULL;
6883 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6884 2822a352 2019-10-15 stsp
6885 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6886 2822a352 2019-10-15 stsp if (err)
6887 2822a352 2019-10-15 stsp return err;
6888 2822a352 2019-10-15 stsp
6889 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6890 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6891 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6892 2822a352 2019-10-15 stsp "update -b or different branch name required");
6893 2822a352 2019-10-15 stsp goto done;
6894 2822a352 2019-10-15 stsp }
6895 2822a352 2019-10-15 stsp
6896 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6897 2822a352 2019-10-15 stsp if (err)
6898 2822a352 2019-10-15 stsp goto done;
6899 2822a352 2019-10-15 stsp
6900 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6901 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6902 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6903 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6904 2822a352 2019-10-15 stsp &ok_arg);
6905 2822a352 2019-10-15 stsp if (err)
6906 2822a352 2019-10-15 stsp goto done;
6907 2822a352 2019-10-15 stsp
6908 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6909 2822a352 2019-10-15 stsp if (err)
6910 2822a352 2019-10-15 stsp goto done;
6911 2822a352 2019-10-15 stsp
6912 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6913 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6914 2822a352 2019-10-15 stsp done:
6915 2822a352 2019-10-15 stsp if (err) {
6916 2822a352 2019-10-15 stsp if (*branch_ref) {
6917 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6918 2822a352 2019-10-15 stsp *branch_ref = NULL;
6919 2822a352 2019-10-15 stsp }
6920 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6921 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6922 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6923 2822a352 2019-10-15 stsp }
6924 2822a352 2019-10-15 stsp if (*fileindex) {
6925 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6926 2822a352 2019-10-15 stsp *fileindex = NULL;
6927 2822a352 2019-10-15 stsp }
6928 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6929 2822a352 2019-10-15 stsp }
6930 0cb83759 2019-08-03 stsp return err;
6931 0cb83759 2019-08-03 stsp }
6932 0cb83759 2019-08-03 stsp
6933 2822a352 2019-10-15 stsp const struct got_error *
6934 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6935 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6936 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6937 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6939 2822a352 2019-10-15 stsp {
6940 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6941 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6942 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6943 2822a352 2019-10-15 stsp
6944 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6945 2822a352 2019-10-15 stsp if (err)
6946 2822a352 2019-10-15 stsp goto done;
6947 2822a352 2019-10-15 stsp
6948 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6949 2822a352 2019-10-15 stsp if (err)
6950 2822a352 2019-10-15 stsp goto done;
6951 2822a352 2019-10-15 stsp
6952 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6953 2822a352 2019-10-15 stsp worktree->path_prefix);
6954 2822a352 2019-10-15 stsp if (err)
6955 2822a352 2019-10-15 stsp goto done;
6956 2822a352 2019-10-15 stsp
6957 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6958 2822a352 2019-10-15 stsp if (err)
6959 2822a352 2019-10-15 stsp goto done;
6960 2822a352 2019-10-15 stsp
6961 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6962 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6963 2822a352 2019-10-15 stsp if (err)
6964 2822a352 2019-10-15 stsp goto sync;
6965 2822a352 2019-10-15 stsp
6966 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6967 2822a352 2019-10-15 stsp if (err)
6968 2822a352 2019-10-15 stsp goto sync;
6969 2822a352 2019-10-15 stsp
6970 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6971 2822a352 2019-10-15 stsp sync:
6972 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6973 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6974 2822a352 2019-10-15 stsp err = sync_err;
6975 2822a352 2019-10-15 stsp
6976 2822a352 2019-10-15 stsp done:
6977 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6978 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6979 2822a352 2019-10-15 stsp err = unlockerr;
6980 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6981 2822a352 2019-10-15 stsp
6982 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6983 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6984 2822a352 2019-10-15 stsp err = unlockerr;
6985 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6986 2822a352 2019-10-15 stsp
6987 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6988 2822a352 2019-10-15 stsp free(fileindex_path);
6989 2822a352 2019-10-15 stsp free(tree_id);
6990 2822a352 2019-10-15 stsp
6991 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6992 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6993 2822a352 2019-10-15 stsp err = unlockerr;
6994 2822a352 2019-10-15 stsp return err;
6995 2822a352 2019-10-15 stsp }
6996 2822a352 2019-10-15 stsp
6997 2822a352 2019-10-15 stsp const struct got_error *
6998 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6999 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7000 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7001 2822a352 2019-10-15 stsp {
7002 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7003 8b692cd0 2019-10-21 stsp
7004 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7005 8b692cd0 2019-10-21 stsp
7006 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7007 8b692cd0 2019-10-21 stsp
7008 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7009 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7010 8b692cd0 2019-10-21 stsp err = unlockerr;
7011 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7012 8b692cd0 2019-10-21 stsp
7013 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7014 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7015 8b692cd0 2019-10-21 stsp err = unlockerr;
7016 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7017 8b692cd0 2019-10-21 stsp
7018 8b692cd0 2019-10-21 stsp return err;
7019 2822a352 2019-10-15 stsp }
7020 2822a352 2019-10-15 stsp
7021 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7022 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7023 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7024 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7025 2db2652d 2019-08-07 stsp struct got_repository *repo;
7026 2db2652d 2019-08-07 stsp int have_changes;
7027 2db2652d 2019-08-07 stsp };
7028 2db2652d 2019-08-07 stsp
7029 2db2652d 2019-08-07 stsp const struct got_error *
7030 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7031 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7032 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7033 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7034 735ef5ac 2019-08-03 stsp {
7035 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7036 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7037 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7038 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7039 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7040 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7041 8b13ce36 2019-08-08 stsp
7042 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7043 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7044 8b13ce36 2019-08-08 stsp return NULL;
7045 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7046 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7047 735ef5ac 2019-08-03 stsp
7048 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7049 735ef5ac 2019-08-03 stsp if (ie == NULL)
7050 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7051 735ef5ac 2019-08-03 stsp
7052 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7053 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7054 735ef5ac 2019-08-03 stsp relpath) == -1)
7055 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7056 735ef5ac 2019-08-03 stsp
7057 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7058 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7059 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7060 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7061 735ef5ac 2019-08-03 stsp }
7062 735ef5ac 2019-08-03 stsp
7063 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7064 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7065 3aa5969e 2019-08-06 stsp goto done;
7066 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7067 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7068 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7069 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7070 735ef5ac 2019-08-03 stsp goto done;
7071 3aa5969e 2019-08-06 stsp }
7072 735ef5ac 2019-08-03 stsp
7073 2db2652d 2019-08-07 stsp a->have_changes = 1;
7074 2db2652d 2019-08-07 stsp
7075 735ef5ac 2019-08-03 stsp p = in_repo_path;
7076 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7077 735ef5ac 2019-08-03 stsp p++;
7078 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7079 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7080 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7081 735ef5ac 2019-08-03 stsp done:
7082 735ef5ac 2019-08-03 stsp free(in_repo_path);
7083 dc424a06 2019-08-07 stsp return err;
7084 dc424a06 2019-08-07 stsp }
7085 dc424a06 2019-08-07 stsp
7086 2db2652d 2019-08-07 stsp struct stage_path_arg {
7087 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7088 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7089 2db2652d 2019-08-07 stsp struct got_repository *repo;
7090 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7091 2db2652d 2019-08-07 stsp void *status_arg;
7092 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7093 2db2652d 2019-08-07 stsp void *patch_arg;
7094 7b5dc508 2019-10-28 stsp int staged_something;
7095 2db2652d 2019-08-07 stsp };
7096 2db2652d 2019-08-07 stsp
7097 2db2652d 2019-08-07 stsp static const struct got_error *
7098 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7099 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7100 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7101 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7102 0cb83759 2019-08-03 stsp {
7103 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7104 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7105 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7106 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7107 0cb83759 2019-08-03 stsp uint32_t stage;
7108 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7109 0aeb8099 2020-07-23 stsp struct stat sb;
7110 8b13ce36 2019-08-08 stsp
7111 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7112 8b13ce36 2019-08-08 stsp return NULL;
7113 0cb83759 2019-08-03 stsp
7114 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7115 d3e7c587 2019-08-03 stsp if (ie == NULL)
7116 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7117 0cb83759 2019-08-03 stsp
7118 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7119 2db2652d 2019-08-07 stsp relpath)== -1)
7120 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7121 0cb83759 2019-08-03 stsp
7122 0cb83759 2019-08-03 stsp switch (status) {
7123 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7124 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7125 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7126 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7127 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7128 0aeb8099 2020-07-23 stsp break;
7129 0aeb8099 2020-07-23 stsp }
7130 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7131 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7132 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7133 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7134 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7135 dc424a06 2019-08-07 stsp if (err)
7136 dc424a06 2019-08-07 stsp break;
7137 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7138 dc424a06 2019-08-07 stsp break;
7139 dc424a06 2019-08-07 stsp } else {
7140 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7141 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7142 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7143 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7144 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7145 dc424a06 2019-08-07 stsp break;
7146 dc424a06 2019-08-07 stsp }
7147 dc424a06 2019-08-07 stsp }
7148 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7149 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7150 0cb83759 2019-08-03 stsp if (err)
7151 dc424a06 2019-08-07 stsp break;
7152 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7153 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7154 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7155 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7156 0cb83759 2019-08-03 stsp else
7157 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7158 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7159 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7160 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7161 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7162 0aeb8099 2020-07-23 stsp } else {
7163 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7164 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7165 0aeb8099 2020-07-23 stsp }
7166 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7167 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7168 dc424a06 2019-08-07 stsp break;
7169 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7170 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7171 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7172 0cb83759 2019-08-03 stsp break;
7173 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7174 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7175 d3e7c587 2019-08-03 stsp break;
7176 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7177 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7178 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7179 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7180 dc424a06 2019-08-07 stsp if (err)
7181 dc424a06 2019-08-07 stsp break;
7182 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7183 88f33a19 2019-08-08 stsp break;
7184 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7185 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7186 dc424a06 2019-08-07 stsp break;
7187 88f33a19 2019-08-08 stsp }
7188 dc424a06 2019-08-07 stsp }
7189 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7190 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7191 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7192 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7193 dc424a06 2019-08-07 stsp break;
7194 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7195 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7196 12463d8b 2019-12-13 stsp de_name);
7197 0cb83759 2019-08-03 stsp break;
7198 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7199 d3e7c587 2019-08-03 stsp break;
7200 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7201 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7202 ebf48fd5 2019-08-03 stsp break;
7203 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7204 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7205 2a06fe5f 2019-08-24 stsp break;
7206 0cb83759 2019-08-03 stsp default:
7207 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7208 537ac44b 2019-08-03 stsp break;
7209 0cb83759 2019-08-03 stsp }
7210 dc424a06 2019-08-07 stsp
7211 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7212 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7213 dc424a06 2019-08-07 stsp free(path_content);
7214 2db2652d 2019-08-07 stsp free(ondisk_path);
7215 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7216 0ebf8283 2019-07-24 stsp return err;
7217 0ebf8283 2019-07-24 stsp }
7218 0cb83759 2019-08-03 stsp
7219 0cb83759 2019-08-03 stsp const struct got_error *
7220 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7221 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7222 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7223 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7224 1e71573e 2019-08-03 stsp struct got_repository *repo)
7225 0cb83759 2019-08-03 stsp {
7226 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7227 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7228 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7229 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7230 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7231 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7232 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7233 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7234 0cb83759 2019-08-03 stsp
7235 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7236 0cb83759 2019-08-03 stsp if (err)
7237 0cb83759 2019-08-03 stsp return err;
7238 0cb83759 2019-08-03 stsp
7239 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7240 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7241 735ef5ac 2019-08-03 stsp if (err)
7242 735ef5ac 2019-08-03 stsp goto done;
7243 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7244 735ef5ac 2019-08-03 stsp if (err)
7245 735ef5ac 2019-08-03 stsp goto done;
7246 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7247 0cb83759 2019-08-03 stsp if (err)
7248 0cb83759 2019-08-03 stsp goto done;
7249 0cb83759 2019-08-03 stsp
7250 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7251 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7252 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7253 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7254 2db2652d 2019-08-07 stsp oka.repo = repo;
7255 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7256 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7257 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7258 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7259 735ef5ac 2019-08-03 stsp if (err)
7260 735ef5ac 2019-08-03 stsp goto done;
7261 735ef5ac 2019-08-03 stsp }
7262 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7263 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7264 2db2652d 2019-08-07 stsp goto done;
7265 2db2652d 2019-08-07 stsp }
7266 735ef5ac 2019-08-03 stsp
7267 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7268 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7269 2db2652d 2019-08-07 stsp spa.repo = repo;
7270 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7271 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7272 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7273 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7274 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7275 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7276 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7277 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7278 42005733 2019-08-03 stsp if (err)
7279 2db2652d 2019-08-07 stsp goto done;
7280 7b5dc508 2019-10-28 stsp }
7281 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7282 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7283 7b5dc508 2019-10-28 stsp goto done;
7284 0cb83759 2019-08-03 stsp }
7285 0cb83759 2019-08-03 stsp
7286 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7287 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7288 0cb83759 2019-08-03 stsp err = sync_err;
7289 0cb83759 2019-08-03 stsp done:
7290 735ef5ac 2019-08-03 stsp if (head_ref)
7291 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7292 735ef5ac 2019-08-03 stsp free(head_commit_id);
7293 0cb83759 2019-08-03 stsp free(fileindex_path);
7294 0cb83759 2019-08-03 stsp if (fileindex)
7295 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7296 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7297 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7298 0cb83759 2019-08-03 stsp err = unlockerr;
7299 0cb83759 2019-08-03 stsp return err;
7300 0cb83759 2019-08-03 stsp }
7301 ad493afc 2019-08-03 stsp
7302 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7303 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7304 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7305 ad493afc 2019-08-03 stsp struct got_repository *repo;
7306 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7307 ad493afc 2019-08-03 stsp void *progress_arg;
7308 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7309 2e1f37b0 2019-08-08 stsp void *patch_arg;
7310 ad493afc 2019-08-03 stsp };
7311 2e1f37b0 2019-08-08 stsp
7312 2e1f37b0 2019-08-08 stsp static const struct got_error *
7313 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7314 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7315 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7316 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7317 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7318 2e1f37b0 2019-08-08 stsp {
7319 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7320 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7321 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7322 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7323 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7324 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7325 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7326 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7327 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7328 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7329 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7330 2e1f37b0 2019-08-08 stsp
7331 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7332 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7333 2e1f37b0 2019-08-08 stsp
7334 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7335 2e1f37b0 2019-08-08 stsp if (err)
7336 2e1f37b0 2019-08-08 stsp return err;
7337 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7338 2e1f37b0 2019-08-08 stsp if (err)
7339 2e1f37b0 2019-08-08 stsp goto done;
7340 2e1f37b0 2019-08-08 stsp
7341 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7342 2e1f37b0 2019-08-08 stsp if (err)
7343 2e1f37b0 2019-08-08 stsp goto done;
7344 2e1f37b0 2019-08-08 stsp
7345 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7346 2e1f37b0 2019-08-08 stsp if (err)
7347 2e1f37b0 2019-08-08 stsp goto done;
7348 2e1f37b0 2019-08-08 stsp
7349 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7350 2e1f37b0 2019-08-08 stsp if (err)
7351 2e1f37b0 2019-08-08 stsp goto done;
7352 2e1f37b0 2019-08-08 stsp
7353 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7354 2e1f37b0 2019-08-08 stsp if (err)
7355 2e1f37b0 2019-08-08 stsp goto done;
7356 ad493afc 2019-08-03 stsp
7357 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7358 2e1f37b0 2019-08-08 stsp if (err)
7359 2e1f37b0 2019-08-08 stsp goto done;
7360 2e1f37b0 2019-08-08 stsp
7361 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7362 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7363 2e1f37b0 2019-08-08 stsp goto done;
7364 2e1f37b0 2019-08-08 stsp }
7365 2e1f37b0 2019-08-08 stsp
7366 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7367 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7368 2e1f37b0 2019-08-08 stsp goto done;
7369 2e1f37b0 2019-08-08 stsp }
7370 2e1f37b0 2019-08-08 stsp
7371 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7372 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7373 2e1f37b0 2019-08-08 stsp if (err)
7374 2e1f37b0 2019-08-08 stsp goto done;
7375 2e1f37b0 2019-08-08 stsp
7376 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7377 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7378 2e1f37b0 2019-08-08 stsp if (err)
7379 2e1f37b0 2019-08-08 stsp goto done;
7380 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7381 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7382 2e1f37b0 2019-08-08 stsp if (err)
7383 2e1f37b0 2019-08-08 stsp goto done;
7384 2e1f37b0 2019-08-08 stsp
7385 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7386 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7387 2e1f37b0 2019-08-08 stsp goto done;
7388 2e1f37b0 2019-08-08 stsp }
7389 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7390 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7391 2e1f37b0 2019-08-08 stsp goto done;
7392 2e1f37b0 2019-08-08 stsp }
7393 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7394 2e1f37b0 2019-08-08 stsp int choice;
7395 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7396 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7397 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7398 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7399 2e1f37b0 2019-08-08 stsp if (err)
7400 2e1f37b0 2019-08-08 stsp goto done;
7401 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7402 2e1f37b0 2019-08-08 stsp have_content = 1;
7403 19e4b907 2019-08-08 stsp else
7404 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7405 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7406 2e1f37b0 2019-08-08 stsp break;
7407 2e1f37b0 2019-08-08 stsp }
7408 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7409 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7410 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7411 2e1f37b0 2019-08-08 stsp done:
7412 2e1f37b0 2019-08-08 stsp free(label1);
7413 2e1f37b0 2019-08-08 stsp if (blob)
7414 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7415 2e1f37b0 2019-08-08 stsp if (staged_blob)
7416 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7417 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7418 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7419 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7420 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7421 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7422 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7423 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7424 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7425 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7426 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7427 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7428 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7429 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7430 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7431 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7432 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7433 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7434 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7435 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7436 2e1f37b0 2019-08-08 stsp }
7437 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7438 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7439 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7440 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7441 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7442 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7443 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7444 2e1f37b0 2019-08-08 stsp }
7445 2e1f37b0 2019-08-08 stsp free(args);
7446 2e1f37b0 2019-08-08 stsp if (ds) {
7447 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7448 2e1f37b0 2019-08-08 stsp free(ds);
7449 2e1f37b0 2019-08-08 stsp }
7450 2e1f37b0 2019-08-08 stsp if (changes)
7451 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7452 2e1f37b0 2019-08-08 stsp free(path1);
7453 2e1f37b0 2019-08-08 stsp free(path2);
7454 2e1f37b0 2019-08-08 stsp return err;
7455 2e1f37b0 2019-08-08 stsp }
7456 2e1f37b0 2019-08-08 stsp
7457 ad493afc 2019-08-03 stsp static const struct got_error *
7458 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7459 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7460 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7461 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7462 ad493afc 2019-08-03 stsp {
7463 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7464 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7465 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7466 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7467 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7468 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7469 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7470 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7471 9bc94a15 2019-08-03 stsp struct stat sb;
7472 ad493afc 2019-08-03 stsp
7473 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7474 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7475 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7476 2e1f37b0 2019-08-08 stsp return NULL;
7477 2e1f37b0 2019-08-08 stsp
7478 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7479 ad493afc 2019-08-03 stsp if (ie == NULL)
7480 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7481 9bc94a15 2019-08-03 stsp
7482 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7483 9bc94a15 2019-08-03 stsp == -1)
7484 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7485 ad493afc 2019-08-03 stsp
7486 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7487 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7488 f69721c3 2019-10-21 stsp if (err)
7489 f69721c3 2019-10-21 stsp goto done;
7490 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7491 f69721c3 2019-10-21 stsp id_str) == -1) {
7492 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7493 f69721c3 2019-10-21 stsp goto done;
7494 f69721c3 2019-10-21 stsp }
7495 f69721c3 2019-10-21 stsp
7496 ad493afc 2019-08-03 stsp switch (staged_status) {
7497 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7498 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7499 ad493afc 2019-08-03 stsp blob_id, 8192);
7500 ad493afc 2019-08-03 stsp if (err)
7501 ad493afc 2019-08-03 stsp break;
7502 ad493afc 2019-08-03 stsp /* fall through */
7503 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7504 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7505 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7506 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7507 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7508 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7509 2e1f37b0 2019-08-08 stsp if (err)
7510 2e1f37b0 2019-08-08 stsp break;
7511 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7512 2e1f37b0 2019-08-08 stsp break;
7513 2e1f37b0 2019-08-08 stsp } else {
7514 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7515 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7516 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7517 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7518 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7519 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7520 2e1f37b0 2019-08-08 stsp break;
7521 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7522 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7523 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7524 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7525 2e1f37b0 2019-08-08 stsp a->repo);
7526 2e1f37b0 2019-08-08 stsp if (err)
7527 2e1f37b0 2019-08-08 stsp break;
7528 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7529 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7530 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7531 2e1f37b0 2019-08-08 stsp }
7532 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7533 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7534 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7535 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7536 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7537 f69721c3 2019-10-21 stsp a->progress_arg);
7538 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7539 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7540 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7541 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7542 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7543 2e1f37b0 2019-08-08 stsp }
7544 2e1f37b0 2019-08-08 stsp }
7545 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7546 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7547 ad493afc 2019-08-03 stsp if (err)
7548 ad493afc 2019-08-03 stsp break;
7549 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7550 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7551 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7552 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7553 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7554 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7555 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7556 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7557 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7558 ea7786be 2020-07-23 stsp break;
7559 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7560 ea7786be 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base, ondisk_path,
7561 ea7786be 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie), label_orig,
7562 ea7786be 2020-07-23 stsp blob_staged, a->worktree->base_commit_id, a->repo,
7563 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7564 ea7786be 2020-07-23 stsp break;
7565 ea7786be 2020-07-23 stsp default:
7566 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7567 ea7786be 2020-07-23 stsp break;
7568 ea7786be 2020-07-23 stsp }
7569 ad493afc 2019-08-03 stsp if (err == NULL)
7570 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7571 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7572 ad493afc 2019-08-03 stsp break;
7573 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7574 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7575 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7576 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7577 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7578 2e1f37b0 2019-08-08 stsp if (err)
7579 2e1f37b0 2019-08-08 stsp break;
7580 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7581 2e1f37b0 2019-08-08 stsp break;
7582 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7583 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7584 2e1f37b0 2019-08-08 stsp break;
7585 2e1f37b0 2019-08-08 stsp }
7586 2e1f37b0 2019-08-08 stsp }
7587 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7588 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7589 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7590 9bc94a15 2019-08-03 stsp if (err)
7591 9bc94a15 2019-08-03 stsp break;
7592 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7593 ad493afc 2019-08-03 stsp break;
7594 ad493afc 2019-08-03 stsp }
7595 f69721c3 2019-10-21 stsp done:
7596 ad493afc 2019-08-03 stsp free(ondisk_path);
7597 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7598 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7599 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7600 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7601 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7602 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7603 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7604 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7605 ad493afc 2019-08-03 stsp if (blob_base)
7606 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7607 ad493afc 2019-08-03 stsp if (blob_staged)
7608 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7609 f69721c3 2019-10-21 stsp free(id_str);
7610 f69721c3 2019-10-21 stsp free(label_orig);
7611 ad493afc 2019-08-03 stsp return err;
7612 ad493afc 2019-08-03 stsp }
7613 ad493afc 2019-08-03 stsp
7614 ad493afc 2019-08-03 stsp const struct got_error *
7615 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7616 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7617 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7618 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7619 ad493afc 2019-08-03 stsp struct got_repository *repo)
7620 ad493afc 2019-08-03 stsp {
7621 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7622 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7623 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7624 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7625 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7626 ad493afc 2019-08-03 stsp
7627 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7628 ad493afc 2019-08-03 stsp if (err)
7629 ad493afc 2019-08-03 stsp return err;
7630 ad493afc 2019-08-03 stsp
7631 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7632 ad493afc 2019-08-03 stsp if (err)
7633 ad493afc 2019-08-03 stsp goto done;
7634 ad493afc 2019-08-03 stsp
7635 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7636 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7637 ad493afc 2019-08-03 stsp upa.repo = repo;
7638 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7639 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7640 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7641 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7642 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7643 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7644 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7645 ad493afc 2019-08-03 stsp if (err)
7646 ad493afc 2019-08-03 stsp goto done;
7647 ad493afc 2019-08-03 stsp }
7648 ad493afc 2019-08-03 stsp
7649 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7650 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7651 ad493afc 2019-08-03 stsp err = sync_err;
7652 ad493afc 2019-08-03 stsp done:
7653 ad493afc 2019-08-03 stsp free(fileindex_path);
7654 ad493afc 2019-08-03 stsp if (fileindex)
7655 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7656 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7657 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7658 ad493afc 2019-08-03 stsp err = unlockerr;
7659 ad493afc 2019-08-03 stsp return err;
7660 ad493afc 2019-08-03 stsp }